-
Notifications
You must be signed in to change notification settings - Fork 10
/
WebJSONQuery_Template.py
64 lines (47 loc) · 1.69 KB
/
WebJSONQuery_Template.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
# ESP8266 Web JSON Query Template
import network, requests, time, sys
# user info
ssid = '' # change this to your WiFi AP name
pw = '' # change this to your WiFi AP password
# API url
api_url = 'http://api.open-notify.org/iss-now.json'
# wifi error descriptions
wifi_error = {
network.STAT_WRONG_PASSWORD: 'wrong password',
network.STAT_NO_AP_FOUND: 'wifi AP not found',
-1: 'due to other problems',
}
# connecting to WiFi
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, pw)
print('ESP8266 connecting to', ssid, '...')
while wifi.status() == network.STAT_CONNECTING:
pass
if wifi.status() != network.STAT_GOT_IP:
print('Failed to connect:', wifi_error.get(wifi.status(), wifi_error[-1]))
sys.exit()
print('Connected.')
print('IP:', wifi.ifconfig()[0], '\n')
time.sleep(1)
while True:
try:
# send HTTP GET request to the API
print('Querying API:', api_url)
response = requests.get(api_url)
if response.status_code == 200:
print('Query successful. JSON response:')
# return a dict object containing the JSON data
parsed = response.json()
print(parsed) # you can access data by using parsed[key]
else:
print('Query failed. ' + \
'Status code:', response.status_code)
response.close()
except Exception as e:
print('Error occurred:', e)
print('If you see a SSL error above, ' + \
'either you have unstable Wifi or ' + \
'the API is not supported by MicroPython.')
print('')
time.sleep(30)