-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (72 loc) · 2.37 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import network
import urequests
import dht
from machine import Pin
import time
# Wi-Fi credentials
ssid = 'YOUR_WIF_SSID'
password = 'YOUR_PASSWORD'
# Set up the LED on GPIO15 as an output pin
led_pin = Pin(15, Pin.OUT)
# Set up the DHT11 sensor on GPIO16
dht_pin = Pin(16)
dht_sensor = dht.DHT11(dht_pin)
# Function to connect to Wi-Fi
def connect_to_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print("Connecting to Wi-Fi...")
wlan.connect(ssid, password)
while not wlan.isconnected():
print("Waiting for connection...")
time.sleep(1)
if wlan.isconnected():
print("Connected to Wi-Fi!")
print("Network config:", wlan.ifconfig())
# Turn on the LED to indicate successful Wi-Fi connection
led_pin.value(1)
else:
print("Failed to connect.")
# Blink the LED on failure
for _ in range(5):
led_pin.value(not led_pin.value())
time.sleep(0.5)
led_pin.value(0)
# Function to send data to the HTTP endpoint
def send_data(temperature, humidity, led_state):
url = 'YOUR_END_POINT'
headers = {'Content-Type': 'application/json'}
data = {
"device": "YOUR_DEVICE_ID",
"temperature": temperature,
"humidity": humidity,
"led_state": led_state
}
try:
response = urequests.post(url, headers=headers, json=data)
print('Data posted:', response.text)
response.close()
except Exception as e:
print('Failed to send data:', e)
# Connect to the Wi-Fi
connect_to_wifi()
# Main loop to read from DHT11 and control the LED
while True:
try:
dht_sensor.measure() # Trigger the sensor to measure
temperature = dht_sensor.temperature() # Get the temperature in Celsius
humidity = dht_sensor.humidity() # Get the humidity in percentage
led_state = 0
# Toggle LED based on temperature threshold
if temperature > 24:
led_pin.value(1)
led_state = 1
else:
led_pin.value(0)
led_state = 0
print('Temperature: {}°C Humidity: {}%'.format(temperature, humidity))
# Send data to the endpoint
send_data(temperature, humidity, led_state)
except OSError as e:
print('Failed to read sensor.')
time.sleep(10) # Wait for 10 seconds before the next reading