DavidFlory

Untitled

Jun 5th, 2020
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.41 KB | None | 0 0
  1. # Boot.py
  2. try:
  3.   import usocket as socket
  4. except:
  5.   import socket
  6. import network
  7. from machine import Pin
  8. import dht
  9. import esp
  10. esp.osdebug(None)
  11. import gc
  12. gc.collect()
  13. from utime import sleep
  14. from utime import sleep_ms
  15. try:
  16.   import urequests as requests
  17. except:
  18.   import requests
  19.  
  20. try:
  21.   import ujson as json
  22. except:
  23.   import json
  24. ssid = ‘xxxxxxx’
  25. password = ‘xxxxxxxx’
  26.  
  27. station = network.WLAN(network.STA_IF)
  28. station.active(True)
  29. station.connect(ssid, password)
  30. while station.isconnected() == False:
  31.  pass
  32.  
  33. print(‘Connection successful’)
  34. print(station.ifconfig())
  35.  
  36. # code for Main.py
  37. city = ‘Clare’
  38. country_code = ‘GB’
  39. clare=str(2652941)
  40. bury=str(2654186)
  41. sudbury=str(2636564)
  42. hh=str(2647310)
  43. open_weather_map_api_key = ‘xxxxxxxxxxxxxxxxxxxxxxxxxx’
  44.  
  45. def process_data(url):
  46.   weather_data = requests.get(url)
  47.   #print(weather_data.json())
  48.   location = ‘Location: ‘ + weather_data.json().get(‘name’) + ‘-‘ + weather_data.json().get(sys).get(‘country’)
  49.   #if location has multiple words and spaces, spaces need to be replaced with %20
  50.   #create string for PHP request
  51.   x = weather_data.json().get(‘name’)
  52.   loc = x.replace(” “,”%20)
  53.   msg=’?loc=’ + loc + ‘-‘ + weather_data.json().get(sys).get(‘country’)
  54.   description = ‘Description: ‘ + weather_data.json().get(‘weather’)[0].get(‘main’)
  55.   x = weather_data.json().get(‘weather’)[0].get(‘description’)
  56.   des = weather_data.json().get(‘weather’)[0].get(‘main’) +’%20′ + x.replace(” “,”%20)
  57.   msg = msg +’&des=’ + des
  58.   #description = ‘Description: ‘ + weather_data.json().get(‘weather’)[0].get(‘description’)
  59.   #msg = msg +’&des=’ + weather_data.json().get(‘weather’)[0].get(‘description’)
  60.   raw_temperature = weather_data.json().get(‘main’).get(‘temp’)-273.15
  61.   temperature = ‘Temperature: ‘ + str(raw_temperature) + ‘*C’
  62.   msg = msg +’&temp=’ + str(raw_temperature)
  63.   pressure = ‘Pressure: ‘ + str(weather_data.json().get(‘main’).get(‘pressure’)) + ‘hPa’
  64.   msg = msg +’&pres=’ + str(weather_data.json().get(‘main’).get(‘pressure’)) + ‘hPa’
  65.   humidity = ‘Humidity: ‘ + str(weather_data.json().get(‘main’).get(‘humidity’)) + ‘%’
  66.   msg = msg +’&hum=’ + str(weather_data.json().get(‘main’).get(‘humidity’)) + ‘%’
  67.   wind = ‘Wind: ‘ + str(weather_data.json().get(‘wind’).get(‘speed’)) + ‘mps ‘ + str(weather_data.json().get(‘wind’).get(‘deg’)) + ‘*’
  68.   msg = msg +’&wind=’ + str(weather_data.json().get(‘wind’).get(‘speed’))
  69.   msg = msg + ‘&dir=’ + str(weather_data.json().get(‘wind’).get(‘deg’))
  70.   return (msg)
  71.  
  72. def read_sensor():
  73.   #get data from DHT22
  74.   global temp, hum, count
  75.   temp = hum = count = 0
  76.   try:
  77.     sensor.measure()
  78.     temp = sensor.temperature()
  79.     hum = sensor.humidity()
  80.  
  81.     if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)):
  82.       temp=round(temp,1)
  83.       hum=round(hum,1)
  84.       msg = (‘/insertdata.php?temp=’+str(temp)+’&hum=’+str(hum))
  85.       return(msg)
  86.     else:
  87.       while count < 3:
  88.         sleep(2)
  89.         count = count + 1
  90.         sensor.measure()
  91.         if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)):
  92.           temp=round(temp,1)
  93.           hum=round(hum,1)
  94.           msg = (‘/insertdata.php?temp=’+str(temp)+’&hum=’+str(hum))
  95.           return(msg)
  96.      msg = (‘/insertdata.php?temp=’+str(temp)+’&hum=’+str(hum))
  97.     return(msg)
  98.  except OSError as e:
  99.    temp=999
  100.    hum=999
  101.    msg = (‘/insertdata.php?temp=’+str(temp)+’&hum=’+str(hum))
  102.    return(msg)
  103.  
  104. while True:
  105.   snooze = 1790
  106.   url = ‘http://api.openweathermap.org/data/2.5/weather?id=&#8217; + hh + ‘&APPID=’ + open_weather_map_api_key
  107.   msg = process_data(url)
  108.   print(msg)
  109.   url = ‘http://daveflory.co.uk/inserthh.php&#8217; + msg
  110.   print(url)
  111.   response = requests.get(url)
  112.   print(response.text)
  113.   url = ‘http://api.openweathermap.org/data/2.5/weather?id=&#8217; + sudbury + ‘&APPID=’ + open_weather_map_api_key
  114.   msg = process_data(url)
  115.   print(msg)
  116.   url = ‘http://daveflory.co.uk/insertsudbury.php&#8217; + msg
  117.   print(url)
  118.   response = requests.get(url)
  119.   print(response.text)
  120.   url = ‘http://api.openweathermap.org/data/2.5/weather?id=&#8217; + bury + ‘&APPID=’ + open_weather_map_api_key
  121.   msg = process_data(url)
  122.   print(msg)
  123.   url = ‘http://daveflory.co.uk/insertbury.php&#8217; + msg
  124.   print(url)
  125.   response = requests.get(url)
  126.   print(response.text)
  127.   url = ‘http://api.openweathermap.org/data/2.5/weather?id=&#8217; + clare + ‘&APPID=’ + open_weather_map_api_key
  128.   msg = process_data(url)
  129.   print(msg)
  130.   url = ‘http://daveflory.co.uk/insertclare.php&#8217; + msg
  131.   print(url)
  132.   response = requests.get(url)
  133.   print(response.text)
  134.   sensor_readings = read_sensor()
  135.   print(sensor_readings)
  136.   #response = web_page1(sensor_readings)
  137.   #print(response)
  138.   url = ‘http://daveflory.co.uk&#8217; + sensor_readings
  139.   print(url)
  140.   response = requests.get(url)
  141.   #print(response.text) this prints 1790, which it gets from the database
  142.   print(int(response.text))
  143.   #if int(response.text) > 0: snooze = int(response.text) commented out to debug why sleep not working on battery
  144.   sleep(snooze)
Add Comment
Please, Sign In to add comment