Skip to content

Commit 373dd68

Browse files
author
brentru
committed
close sockets properly, add cheerlights example
1 parent a52fcf0 commit 373dd68

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

adafruit_fona/adafruit_fona.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -686,14 +686,12 @@ def socket_close(self, sock_num, quick_close=1):
686686
sock_num < FONA_MAX_SOCKETS
687687
), "Provided socket exceeds the maximum number of \
688688
sockets for the FONA module."
689-
# eat prv. response
690689
self._read_line()
691-
self._uart.write(b"AT+CIPCLOSE=" + str(sock_num).encode())
692-
self._uart.write(b"," + str(quick_close).encode() + b"\r\n")
693690

691+
self._uart.write(b"AT+CIPCLOSE="+ str(sock_num).encode() + b",")
692+
self._uart.write(str(sock_num).encode() + b"\r\n")
694693
self._read_line()
695-
self._parse_reply(b"", idx=1)
696-
if not "CLOSE OK" in self._buf:
694+
if not self._parse_reply(b"CLOSE OK", idx=0):
697695
return False
698696
return True
699697

examples/fona_cheerlights.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
import neopixel
10+
import adafruit_fancyled.adafruit_fancyled as fancy
11+
12+
# Get GPRS details and more from a secrets.py file
13+
try:
14+
from secrets import secrets
15+
except ImportError:
16+
print("GPRS secrets are kept in secrets.py, please add them there!")
17+
raise
18+
19+
# Create a serial connection for the FONA connection using 4800 baud.
20+
# These are the defaults you should use for the FONA Shield.
21+
# For other boards set RX = GPS module TX, and TX = GPS module RX pins.
22+
uart = busio.UART(board.TX, board.RX, baudrate=4800)
23+
rst = digitalio.DigitalInOut(board.D4)
24+
25+
# Initialize FONA module (this may take a few seconds)
26+
print("Initializing FONA")
27+
fona = FONA(uart, rst)
28+
29+
# Enable GPS
30+
fona.gps = True
31+
32+
# Bring up cellular connection
33+
fona.configure_gprs((secrets["apn"], secrets["apn_username"], secrets["apn_password"]))
34+
35+
# Bring up GPRS
36+
fona.gprs = True
37+
print("FONA initialized")
38+
39+
# Initialize a requests object with a socket and cellular interface
40+
requests.set_socket(cellular_socket, fona)
41+
42+
DATA_SOURCE = "http://api.thingspeak.com/channels/1417/feeds.json?results=1"
43+
DATA_LOCATION = ["feeds", 0, "field2"]
44+
45+
# neopixels
46+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.3)
47+
pixels.fill(0)
48+
49+
attempts = 3 # Number of attempts to retry each request
50+
failure_count = 0
51+
response = None
52+
53+
# we'll save the value in question
54+
last_value = value = None
55+
56+
while True:
57+
print("Fetching json from", DATA_SOURCE)
58+
response = requests.get(DATA_SOURCE)
59+
print(response.json())
60+
value = response.json()
61+
for key in DATA_LOCATION:
62+
value = value[key]
63+
print(value)
64+
response.close()
65+
66+
if not value:
67+
continue
68+
if last_value != value:
69+
color = int(value[1:], 16)
70+
red = color >> 16 & 0xFF
71+
green = color >> 8 & 0xFF
72+
blue = color & 0xFF
73+
gamma_corrected = fancy.gamma_adjust(fancy.CRGB(red, green, blue)).pack()
74+
75+
pixels.fill(gamma_corrected)
76+
last_value = value
77+
response = None
78+
time.sleep(60)

examples/fona_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"IP lookup adafruit.com: %s" % fona.get_host_by_name("adafruit.com")
4545
)
4646

47-
fona._debug = True
47+
# fona._debug = True
4848
print("Fetching text from", TEXT_URL)
4949
r = requests.get(TEXT_URL)
5050
print("-" * 40)

0 commit comments

Comments
 (0)