Skip to content

Commit b1a1c29

Browse files
authored
Merge pull request #11 from jerryneedell/jerryn_addpost
add POST to url_request
2 parents 6776e9d + d3db360 commit b1a1c29

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

adafruit_espatcontrol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def begin(self):
147147
except OKError:
148148
pass #retry
149149

150-
def request_url(self, url, ssl=False):
150+
def request_url(self, url, ssl=False, request_type="GET"):
151151
"""Send an HTTP request to the URL. If the URL starts with https://
152152
we will force SSL and use port 443. Otherwise, you can select whether
153153
you want ssl by passing in a flag."""
@@ -165,7 +165,7 @@ def request_url(self, url, ssl=False):
165165
port = 443
166166
if not self.socket_connect(conntype, domain, port, keepalive=10, retries=3):
167167
raise RuntimeError("Failed to connect to host")
168-
request = "GET "+path+" HTTP/1.1\r\n"
168+
request = request_type+" "+path+" HTTP/1.1\r\n"
169169
request += "Host: "+domain+"\r\n"
170170
request += "User-Agent: "+self.USER_AGENT+"\r\n"
171171
request += "\r\n"

examples/espatcontrol_post.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import time
2+
import board
3+
import busio
4+
from digitalio import DigitalInOut
5+
import adafruit_espatcontrol
6+
7+
8+
# Get wifi details and more from a settings.py file
9+
try:
10+
from settings import settings
11+
except ImportError:
12+
print("WiFi settings are kept in settings.py, please add them there!")
13+
raise
14+
15+
16+
17+
URL = "https://io.adafruit.com/api/v2/webhooks/feed/"+settings['aio_feed_webhook']+"?value="
18+
19+
resetpin = DigitalInOut(board.D5)
20+
rtspin = DigitalInOut(board.D9)
21+
uart = busio.UART(board.TX, board.RX, timeout=0.1)
22+
23+
24+
25+
print("Post to a URL", URL)
26+
27+
esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, reset_pin=resetpin,
28+
run_baudrate = 115200, rts_pin=rtspin, debug=True)
29+
print("Resetting ESP module")
30+
esp.hard_reset()
31+
print("Connected to AT software version", esp.get_version())
32+
33+
counter = 0
34+
while True:
35+
try:
36+
# Connect to WiFi if not already
37+
while not esp.is_connected:
38+
print("Connecting...")
39+
esp.connect(settings)
40+
print("Connected to", esp.remote_AP)
41+
# great, lets get the data
42+
print("Posting request URL...", end='')
43+
header, body = esp.request_url(URL+str(counter), request_type = "POST")
44+
counter = counter + 1
45+
print("OK")
46+
except (RuntimeError, adafruit_espatcontrol.OKError) as e:
47+
print("Failed to get data, retrying\n", e)
48+
continue
49+
header = body = None
50+
time.sleep(15)

examples/espatcontrol_settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
'password' : 'my password',
77
'timezone' : -5, # this is offset from UTC
88
'github_token' : 'abcdefghij0123456789',
9+
'aio_feed_webhook' : 'abcdefghij0123456789',
910
}

0 commit comments

Comments
 (0)