Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Boot.py
- try:
- import usocket as socket
- except:
- import socket
- import network
- from machine import Pin
- import dht
- import esp
- esp.osdebug(None)
- import gc
- gc.collect()
- from utime import sleep
- from utime import sleep_ms
- try:
- import urequests as requests
- except:
- import requests
- try:
- import ujson as json
- except:
- import json
- ssid = ‘xxxxxxx’
- password = ‘xxxxxxxx’
- station = network.WLAN(network.STA_IF)
- station.active(True)
- station.connect(ssid, password)
- while station.isconnected() == False:
- pass
- print(‘Connection successful’)
- print(station.ifconfig())
- # code for Main.py
- city = ‘Clare’
- country_code = ‘GB’
- clare=str(2652941)
- bury=str(2654186)
- sudbury=str(2636564)
- hh=str(2647310)
- open_weather_map_api_key = ‘xxxxxxxxxxxxxxxxxxxxxxxxxx’
- def process_data(url):
- weather_data = requests.get(url)
- #print(weather_data.json())
- location = ‘Location: ‘ + weather_data.json().get(‘name’) + ‘-‘ + weather_data.json().get(‘sys’).get(‘country’)
- #if location has multiple words and spaces, spaces need to be replaced with %20
- #create string for PHP request
- x = weather_data.json().get(‘name’)
- loc = x.replace(” “,”%20”)
- msg=’?loc=’ + loc + ‘-‘ + weather_data.json().get(‘sys’).get(‘country’)
- description = ‘Description: ‘ + weather_data.json().get(‘weather’)[0].get(‘main’)
- x = weather_data.json().get(‘weather’)[0].get(‘description’)
- des = weather_data.json().get(‘weather’)[0].get(‘main’) +’%20′ + x.replace(” “,”%20”)
- msg = msg +’&des=’ + des
- #description = ‘Description: ‘ + weather_data.json().get(‘weather’)[0].get(‘description’)
- #msg = msg +’&des=’ + weather_data.json().get(‘weather’)[0].get(‘description’)
- raw_temperature = weather_data.json().get(‘main’).get(‘temp’)-273.15
- temperature = ‘Temperature: ‘ + str(raw_temperature) + ‘*C’
- msg = msg +’&temp=’ + str(raw_temperature)
- pressure = ‘Pressure: ‘ + str(weather_data.json().get(‘main’).get(‘pressure’)) + ‘hPa’
- msg = msg +’&pres=’ + str(weather_data.json().get(‘main’).get(‘pressure’)) + ‘hPa’
- humidity = ‘Humidity: ‘ + str(weather_data.json().get(‘main’).get(‘humidity’)) + ‘%’
- msg = msg +’&hum=’ + str(weather_data.json().get(‘main’).get(‘humidity’)) + ‘%’
- wind = ‘Wind: ‘ + str(weather_data.json().get(‘wind’).get(‘speed’)) + ‘mps ‘ + str(weather_data.json().get(‘wind’).get(‘deg’)) + ‘*’
- msg = msg +’&wind=’ + str(weather_data.json().get(‘wind’).get(‘speed’))
- msg = msg + ‘&dir=’ + str(weather_data.json().get(‘wind’).get(‘deg’))
- return (msg)
- def read_sensor():
- #get data from DHT22
- global temp, hum, count
- temp = hum = count = 0
- try:
- sensor.measure()
- temp = sensor.temperature()
- hum = sensor.humidity()
- if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)):
- temp=round(temp,1)
- hum=round(hum,1)
- msg = (‘/insertdata.php?temp=’+str(temp)+’&hum=’+str(hum))
- return(msg)
- else:
- while count < 3:
- sleep(2)
- count = count + 1
- sensor.measure()
- if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)):
- temp=round(temp,1)
- hum=round(hum,1)
- msg = (‘/insertdata.php?temp=’+str(temp)+’&hum=’+str(hum))
- return(msg)
- msg = (‘/insertdata.php?temp=’+str(temp)+’&hum=’+str(hum))
- return(msg)
- except OSError as e:
- temp=999
- hum=999
- msg = (‘/insertdata.php?temp=’+str(temp)+’&hum=’+str(hum))
- return(msg)
- while True:
- snooze = 1790
- url = ‘http://api.openweathermap.org/data/2.5/weather?id=’ + hh + ‘&APPID=’ + open_weather_map_api_key
- msg = process_data(url)
- print(msg)
- url = ‘http://daveflory.co.uk/inserthh.php’ + msg
- print(url)
- response = requests.get(url)
- print(response.text)
- url = ‘http://api.openweathermap.org/data/2.5/weather?id=’ + sudbury + ‘&APPID=’ + open_weather_map_api_key
- msg = process_data(url)
- print(msg)
- url = ‘http://daveflory.co.uk/insertsudbury.php’ + msg
- print(url)
- response = requests.get(url)
- print(response.text)
- url = ‘http://api.openweathermap.org/data/2.5/weather?id=’ + bury + ‘&APPID=’ + open_weather_map_api_key
- msg = process_data(url)
- print(msg)
- url = ‘http://daveflory.co.uk/insertbury.php’ + msg
- print(url)
- response = requests.get(url)
- print(response.text)
- url = ‘http://api.openweathermap.org/data/2.5/weather?id=’ + clare + ‘&APPID=’ + open_weather_map_api_key
- msg = process_data(url)
- print(msg)
- url = ‘http://daveflory.co.uk/insertclare.php’ + msg
- print(url)
- response = requests.get(url)
- print(response.text)
- sensor_readings = read_sensor()
- print(sensor_readings)
- #response = web_page1(sensor_readings)
- #print(response)
- url = ‘http://daveflory.co.uk’ + sensor_readings
- print(url)
- response = requests.get(url)
- #print(response.text) this prints 1790, which it gets from the database
- print(int(response.text))
- #if int(response.text) > 0: snooze = int(response.text) commented out to debug why sleep not working on battery
- sleep(snooze)
Add Comment
Please, Sign In to add comment