Skip to content

Commit e261580

Browse files
author
brentru
committed
fix hostname parsing, add aio posting
1 parent ab36ca2 commit e261580

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

adafruit_fona/adafruit_fona.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -506,29 +506,24 @@ def get_host_by_name(self, hostname):
506506
:param str hostname: Destination server.
507507
508508
"""
509+
self._read_line()
509510
if self._debug:
510511
print("*** get_host_by_name: ", hostname)
511512
if isinstance(hostname, str):
512513
hostname = bytes(hostname, "utf-8")
513514

514-
self._read_line()
515-
self._uart.reset_input_buffer()
516-
517515
if self._debug:
518516
print("\t---> AT+CDNSGIP=", hostname)
519-
520517
if not self._send_check_reply(b'AT+CDNSGIP="' + hostname + b'"\r\n', reply=REPLY_OK):
521518
return False
522519

523-
# self._uart.write(b'AT+CDNSGIP="' + hostname + b'"\r\n')
524-
# if not self._expect_reply(REPLY_OK):
525-
# return False
526-
527-
# parse the second response
520+
# attempt to parse a response
528521
self._read_line()
522+
while not self._parse_reply(b"+CDNSGIP:", idx=2):
523+
self._read_line()
524+
529525
if self._debug:
530526
print("\t<--- ", self._buf)
531-
self._parse_reply(b"+CDNSGIP:", idx=2)
532527
return self._buf
533528

534529
### Socket API (TCP, UDP) ###

examples/fona_aio_post.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import time
2+
import board
3+
import busio
4+
import digitalio
5+
from adafruit_fona.adafruit_fona import FONA
6+
import adafruit_fona.adafruit_fona_socket as cellular_socket
7+
import adafruit_requests as requests
8+
9+
# Get GPRS details and more from a secrets.py file
10+
try:
11+
from secrets import secrets
12+
except ImportError:
13+
print("GPRS secrets are kept in secrets.py, please add them there!")
14+
raise
15+
16+
# Create a serial connection for the FONA connection using 4800 baud.
17+
# These are the defaults you should use for the FONA Shield.
18+
# For other boards set RX = GPS module TX, and TX = GPS module RX pins.
19+
uart = busio.UART(board.TX, board.RX, baudrate=4800)
20+
rst = digitalio.DigitalInOut(board.D4)
21+
22+
# Initialize FONA module (this may take a few seconds)
23+
print("Initializing FONA")
24+
fona = FONA(uart, rst)
25+
26+
# Enable GPS
27+
fona.gps = True
28+
29+
# Bring up cellular connection
30+
fona.configure_gprs((secrets["apn"], secrets["apn_username"], secrets["apn_password"]))
31+
32+
# Bring up GPRS
33+
fona.gprs = True
34+
print("FONA initialized")
35+
36+
# Initialize a requests object with a socket and cellular interface
37+
requests.set_socket(cellular_socket, fona)
38+
39+
counter = 0
40+
41+
while True:
42+
print("Posting data...", end="")
43+
data = counter
44+
feed = "test"
45+
payload = {"value": data}
46+
response = requests.post(
47+
"http://io.adafruit.com/api/v2/"
48+
+ secrets["aio_username"]
49+
+ "/feeds/"
50+
+ feed
51+
+ "/data",
52+
json=payload,
53+
headers={"X-AIO-KEY": secrets["aio_key"]},
54+
)
55+
print(response.json())
56+
response.close()
57+
counter = counter + 1
58+
print("OK")
59+
response = None
60+
time.sleep(15)

0 commit comments

Comments
 (0)