Oluşturulma Tarihi: 2021-07-18 16:01:04
microPython ile sensor degerlerini web sunucuya gönderme
DHT11
microPython
microPython ile sensör verilerini web sunucuya göndermek isteyen kullanıcılar için rehber.
"""+str(temp)+"""*
"""+str(hum)+"""%
#boot.py import deneyap try: import usocket as socket except: import socket import network from machine import Pin import dht import esp esp.osdebug(None) import gc gc.collect() ssid = 'ağ adi' password = 'sifre' station = network.WLAN(network.STA_IF) station.active(True) station.connect(ssid, password) while station.isconnected() == False: pass print('Connection successful') print(station.ifconfig()) #sensor = dht.DHT22(Pin(14)) sensor = dht.DHT11(Pin(deneyap.D0)) #main.py def read_sensor(): global temp, temp_percentage, hum temp = temp_percentage = hum = 0 try: sensor.measure() temp = sensor.temperature() hum = sensor.humidity() 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) # uncomment for Fahrenheit #temp = temp * (9/5) + 32.0 #temp_percentage = (temp-21)/(104-21)*(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 = """ DHT Sensor """+str(temp)+"""* """+str(hum)+"""% """ return html 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()