forked from donskytech/micropython-raspberry-pi-pico
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDHT_remote.py
52 lines (47 loc) · 1.35 KB
/
DHT_remote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from machine import Pin
from dht import DHT11
import network
import socket
import time
# Create DHT object
pin = Pin(17, Pin.OUT, Pin.PULL_DOWN)
sensor = DHT11(pin)
def connect():
good_connection= False
while good_connection == False:
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('your network','your password')
for ct in range(5):
if wlan.isconnected() == True:
good_connection= True
else:
print('Waiting for connection...', ct, end='\r')
time.sleep(1)
ip = wlan.ifconfig()[0]
print(f'WiFi Connected on {ip}')
return ip
def open_socket(ip):
# Open a socket
address = (ip, 80)
connection = socket.socket()
connection.bind(address)
connection.listen(1)
return connection
# Start program
ip = connect()
connection = open_socket(ip)
while True:
try:
cl, addr = connection.accept()
request = cl.recv(1024)
print(request)
# No need to unpack request in this example
sensor.measure()
sensor_data = f'{sensor.temperature()}:{sensor.humidity()}'
cl.send(sensor_data)
print("Sent: " + sensor_data)
cl.close()
except OSError as e:
cl.close()
print('connection closed')