print("MINI PROJECT")
print('AMINUDDIN / NAFIS')


#Import all related Libraries/Modules
from machine import Pin,SoftI2C
from utime import sleep
import Oled_display
import dht



#Pin declaration
blue_led = Pin(2 ,Pin.OUT)

#Dht pin
Dht = dht.DHT22(Pin(14))


#Define Oled
pin_oled = SoftI2C(scl = Pin(22), sda = Pin(21))
oled_width = 128
oled_height = 64
oled = Oled_display.SSD1306_I2C(oled_width, oled_height, i2c_oled)


#Main program
while True:
    Dht.measure()
    temperature = sensor.temperature()
    humidity = sensor.humidity()
    print('Temperature: %3.1f C' %temperature)
    print('Humidity: %3.1f %%' %humidity)
    #Let`s send text to our OLED!
    #calour black value =0,white value =1
    #y is row -->line 1 - 0, line 2 -10, line 3 - 20
    #By default,the screen is in black color
    oled.fill(0)
    oled.text("Temperature: %3.1f C".format(temperature), 0, 0)
    oled.text("Humidity:%3.1f %%".format(humidity), 0, 10)

    oled.show()
    
    blue_led.on()
    sleep(1)
    blue_led.off()
    sleep(2)

    '''
    There are 3 steps to send data to Favoiot using HTTP prototaip
    1.Organize data to be send in JSON Format
    2.Declare the http header
    3.Submit!!
    4.Introduce delay to submit new data
    '''
    #Step 1 :
    Our_data = {
         "channel_id":2397061,
         "field1":temperature,
         "field2":humidity
                }

    #Step 2 :
    http_header = { 'content-type': "application/json"}

    #Step 3 :
    data_submission = requests.request("POST", url='https://api.thingspeak.com/update?api_key=1186P06C7OIPG7UK&', headers=http_header, json=Our_data)
    data_submission.close() #to avoid your board from timeout

    sleep(15)