#main.py import network import webrepl import dht import time from machine import Pin,I2C import ssd1306 try: import usocket as socket except: import socket AUTH_OPEN = 0 AUTH_WEP = 1 AUTH_WPA_PSK = 2 AUTH_WPA2_PSK = 3 AUTH_WPA_WPA2_PSK = 4 SSID = "Makerfabs" PASSWORD = "20160704" sensor = dht.DHT11(Pin(14)) i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000) #Init i2c olcd=ssd1306.SSD1306_I2C(128,64,i2c) #create LCD object,Specify col and row def read_sensor(): global temp, temp_percentage, hum temp = temp_percentage = hum = 0 try: sensor.measure() temp = sensor.temperature() hum = sensor.humidity() olcd.fill(0) olcd.text("Temperature:",0,20) olcd.text(str(temp),95,20) olcd.text("C",115,20) olcd.text("Humidity:",0,40) olcd.text(str(hum),75,40) olcd.text("%",95,40) olcd.show() print(temp) print(hum) if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)): msg = (b'{0:3.1f},{1:3.1f}'.format(temp, hum)) temp_percentage = (temp+6)/(40+6)*(100) hum = round(hum, 2) return(msg) else: return('Invalid sensor readings.') except OSError as e: return('Failed to read sensor.') def web_page(): html = """

Makerfabs DHT Sensor

"""+str(temp)+"""C

"""+str(hum)+"""%

""" return html def do_connect(ssid, psw): import network import time wlan = network.WLAN(network.STA_IF) wlan.active(True) s = wlan.config("mac") mac = ('%02x:%02x:%02x:%02x:%02x:%02x').upper() %(s[0],s[1],s[2],s[3],s[4],s[5]) print(" Local MAC:"+mac) #get mac wlan.connect(ssid, psw) if not wlan.isconnected(): print('connecting to network...' + ssid) wlan.connect(ssid, psw) start = time.ticks_ms() # get millisecond counter while not wlan.isconnected(): time.sleep(1) # sleep for 1 second if time.ticks_ms()-start > 20000: print("connect timeout!") break if wlan.isconnected(): print('network config:', wlan.ifconfig()) return wlan def connect(): do_connect(SSID,PASSWORD) def app(): connect() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 80)) s.listen(5) while True: conn, addr = s.accept() print('Got a connection from %s' % str(addr)) request = conn.recv(1024) print('Content = %s' % str(request)) sensor_readings = read_sensor() print(sensor_readings) response = web_page() conn.send('HTTP/1.1 200 OK\n') conn.send('Content-Type: text/html\n') conn.send('Connection: close\n\n') conn.sendall(response) conn.close() app()