Commit 58e1facb authored by sam.pople's avatar sam.pople

Add new file

parents
#!/usr/bin/env python3
#Run this file to enact the device
import requests
import random
import time
from seeed_dht import DHT
from grove.display.jhd1802 import JHD1802
from grove.grove_ryb_led_button import GroveLedButton
# Connects to the ThingSpeak channel
WRITE_API_KEY = 'I7OWLPEYX17YJFVH' # API key
CHANNEL_ID = '2767951' # Channel ID
THINGSPEAK_URL = 'https://api.thingspeak.com/update?api_key=I7OWLPEYX17YJFVH&field1=0'# URL
def send_data_to_thingspeak(temperature, humidity):
# Sends temperature data to ThingSpeak
# Create the payload with the field data
payload = {
'field1': temperature, # Field1 corresponds to temperature
'field2': humidity # Field2 corresponds to humidity
}
# Send the HTTP GET request to ThingSpeak
response = requests.get(THINGSPEAK_URL, params=payload)
# Check the response code
# Announces if successfully sent
if response.status_code == 200:
print('Data sent successfully')
else:
print('Failed to send data. Response code: {response.status_code}')
def main():
# Grove - 16x2 LCD(White on Blue) connected to I2C port
lcd = JHD1802()
# This connects the LED button to the D22 port
ledbtn = GroveLedButton(22)
# Grove - Temperature&Humidity Sensor connected to port D5
sensor = DHT('11', 5)
while True:
ledbtn.led.light(True)
# This detects the temperature and humidity
humi, temp = sensor.read()
# This converts the temperature (which is Celsius) to Farenheit
faren = round(temp* 1.8 + 32,2)
# This displays the Temperature in Celsius and in Farenheit on the terminal
print('Temperature = {}C ({}F)'.format(temp, faren))
# This displays the Temperature in Celsius on the LCD's top row
lcd.setCursor(0, 0)
lcd.write('Temperature: {0:2}C'.format(temp))
# This displays the Temperature in Farenheit on the LCD's second row
lcd.setCursor(1, 0)
lcd.write('({0:2}F)'.format(faren))
# This waits 10 seconds to show the temperature before moving on
time.sleep(10)
lcd.clear()
# This displays the recommended clothes for a temperature equal to or higher than 20C
if temp >= 20:
print('Recommended Clothes: T-Shirt, shorts, sunscreen, short socks')
lcd.setCursor(0, 0)
# This displays the appropriate clothes and information on the LCD while waiting a few seconds to display one screen after another
lcd.write(' Recommended')
lcd.setCursor(1, 0)
lcd.write('Clothes')
time.sleep(5)
lcd.clear()
lcd.setCursor(0, 0)
lcd.write(' T-Shirt, Shorts')
lcd.setCursor(1, 0)
lcd.write(' Sunscreen')
time.sleep(10)
lcd.clear()
lcd.setCursor(0, 0)
lcd.write(' Short Socks')
# This displays the recommended clothes for a temperature between 10 and 20
elif temp > 10 and temp < 20:
print('Recommended Clothes: Long shirt, trousers, jacket, regular socks')
lcd.setCursor(0, 0)
# This displays the appropriate clothes and information on the LCD while waiting time to display it one screen after another
lcd.write(' Recommended')
lcd.setCursor(1, 0)
lcd.write(' Clothes')
time.sleep(5)
lcd.clear()
lcd.setCursor(0, 0)
lcd.write(' Long shirt')
lcd.setCursor(1, 0)
lcd.write(' Trousers')
time.sleep(10)
lcd.clear()
lcd.setCursor(0, 0)
lcd.write(' Jacket')
lcd.setCursor(1, 0)
lcd.write(' Regular Socks')
# This displays the recommended clothes for a temperature below 11
else:
print('Recommended Clothes: Coat, trousers, gloves, jumper, long shirt, thick socks')
lcd.setCursor(0, 0)
# This displays the appropriate clothes and information on the LCD while waiting a few seconds to display one screen after another
lcd.write(' Recommended')
lcd.setCursor(1, 0)
lcd.write('Clothes')
time.sleep(5)
lcd.clear()
lcd.setCursor(0, 0)
lcd.write(' Coat, Trousers')
lcd.setCursor(1, 0)
lcd.write('Jumper, Gloves')
time.sleep(10)
lcd.clear()
lcd.setCursor(0, 0)
lcd.write(' Long shirt')
lcd.setCursor(1, 0)
lcd.write('Thick socks')
#This send the temperature and humidity to Thingspeak
send_data_to_thingspeak(temp, humi)
time.sleep(10)
# This waits another 10 seconds to show the advice before clearing and detecting and displaying the next current temperature
lcd.clear()
if __name__ == '__main__':
main()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment