Skip to content

Commit e8b0d14

Browse files
committed
fix request json - add aio post example
1 parent a984f77 commit e8b0d14

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

adafruit_espatcontrol/adafruit_espatcontrol_requests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def request(method, url, data=None, json=None, headers=None, stream=None):
122122
sock.write(b"Content-Length: %d\r\n" % len(data))
123123
sock.write(b"\r\n")
124124
if data:
125-
sock.write(data)
125+
sock.write(bytes(data,'utf-8'))
126126

127127
line = sock.readline()
128128
#print(line)

examples/espatcontrol_aio_post.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import time
2+
import board
3+
import busio
4+
from digitalio import DigitalInOut
5+
from adafruit_espatcontrol import adafruit_espatcontrol
6+
from adafruit_espatcontrol import adafruit_espatcontrol_requests as requests
7+
8+
9+
# Get wifi details and more from a settings.py file
10+
try:
11+
from settings import settings
12+
except ImportError:
13+
print("WiFi settings are kept in settings.py, please add them there!")
14+
raise
15+
16+
17+
# With a Metro or Feather M4
18+
resetpin = DigitalInOut(board.D5)
19+
rtspin = DigitalInOut(board.D9)
20+
uart = busio.UART(board.TX, board.RX, timeout=0.1)
21+
22+
# With a Particle Argon
23+
"""
24+
RX = board.ESP_TX
25+
TX = board.ESP_RX
26+
resetpin = DigitalInOut(board.ESP_WIFI_EN)
27+
rtspin = DigitalInOut(board.ESP_CTS)
28+
uart = busio.UART(TX, RX, timeout=0.1)
29+
esp_boot = DigitalInOut(board.ESP_BOOT_MODE)
30+
from digitalio import Direction
31+
esp_boot.direction = Direction.OUTPUT
32+
esp_boot.value = True
33+
"""
34+
35+
36+
esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, reset_pin=resetpin,
37+
run_baudrate = 460800, rts_pin=rtspin, debug=True)
38+
print("Resetting ESP module")
39+
esp.hard_reset()
40+
requests.set_interface(esp)
41+
42+
print("Connected to AT software version", esp.get_version())
43+
44+
counter = 0
45+
while True:
46+
try:
47+
# Connect to WiFi if not already
48+
while not esp.is_connected:
49+
print("Connecting...")
50+
esp.connect(settings)
51+
print("Connected to", esp.remote_AP)
52+
# great, lets get the data
53+
print("Posting data...", end='')
54+
data=counter
55+
feed='test'
56+
payload={'value':data}
57+
response=requests.post("https://io.adafruit.com/api/v2/"+settings['aio_username']+"/feeds/"+feed+"/data",json=payload,headers={bytes("X-AIO-KEY","utf-8"):bytes(settings['aio_key'],"utf-8")})
58+
print(response.json())
59+
response.close()
60+
counter = counter + 1
61+
print("OK")
62+
except (RuntimeError, adafruit_espatcontrol.OKError) as e:
63+
print("Failed to get data, retrying\n", e)
64+
continue
65+
response = None

examples/espatcontrol_settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
'password' : 'my password',
77
'timezone' : -5, # this is offset from UTC
88
'github_token' : 'abcdefghij0123456789',
9-
'aio_feed_webhook' : 'abcdefghij0123456789',
9+
'aio_username' : 'myusername',
10+
'aio_key' : 'abcdefghij0123456789',
1011
}

0 commit comments

Comments
 (0)