-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscd40ThingSpeakLEDDHT11.py
83 lines (69 loc) · 2.36 KB
/
scd40ThingSpeakLEDDHT11.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
import time
import board
import adafruit_scd4x
import requests
import RPi.GPIO as GPIO
import dht11
# Initialising GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Assigning and Initialising LED pin
led_pin = 27
GPIO.setup(led_pin, GPIO.OUT)
# Assigning and Initialising DHT 11
myDHT = dht11.DHT11(pin=17)
# Initialising the I2C communication with Raspberry Pi
i2c = board.I2C()
scd4x = adafruit_scd4x.SCD4X(i2c)
print("SCD40 Sensor Serial no:", [hex(i) for i in scd4x.serial_number])
# Starting Low periodic measurement.
scd4x.start_low_periodic_measurement()
print("Measuring CO2, Temperature and Humidity Values....")
# ThingSpeak endpoint and API KEY
API_ENDPOINT = "https://api.thingspeak.com/update"
API_KEY = "9MB5PUUHD6AKTA8Z"
def sendDataToCloud(CO2, temperature, humidity, current_time):
print(CO2)
print(temperature)
print(humidity)
print(current_time)
print()
# Creating payload with data and API key
payload = {
"api_key": API_KEY,
"field1": current_time,
"field2": temperature,
"field3": humidity,
"field4": CO2
}
# Sending HTTP POST type request to ThingSpeak
response = requests.post(API_ENDPOINT, params=payload)
# Printing response status code
print("Data sent to ThingSpeak Cloud. Status Code:", response.status_code)
def blink_led(num_times, delay):
print("LED blink intruction arrived")
for _ in range(num_times):
GPIO.output(led_pin, GPIO.HIGH)
time.sleep(delay)
GPIO.output(led_pin, GPIO.LOW)
time.sleep(delay)
while True:
result = myDHT.read()
if result.is_valid():
print("Temp: %d °C" % result.temperature +
' ' + "Humid: %d %%" % result.humidity)
if scd4x.data_ready:
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("current time %s " % current_time)
print(f"CO2: {scd4x.CO2} ppm")
print(f"Temperature: {scd4x.temperature:.1f} °C")
print(f"Humidity: {scd4x.relative_humidity:.1f} %")
print()
sendDataToCloud(scd4x.CO2, scd4x.temperature,
scd4x.relative_humidity, current_time)
if scd4x.temperature > 23 or scd4x.relative_humidity > 70 or scd4x.CO2 > 900:
blink_led(num_times=2, delay=0.7)
print("LED blink sent")
print("sleeping...")
time.sleep(5)