Skip to content

Commit 152c41f

Browse files
authored
Merge pull request #1132 from brentru/update-sms
Update FONA SMS Sensor for FONA Library Update
2 parents 6ce637a + 6f9b711 commit 152c41f

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

FONA_SMS_Sensor/code.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
# pylint: disable=unused-import
12
import time
23
import board
34
import busio
45
import digitalio
56
from adafruit_fona.adafruit_fona import FONA
7+
from adafruit_fona.fona_3g import FONA3G
68
import adafruit_bme280
79

810
print("FONA SMS Sensor")
@@ -11,9 +13,12 @@
1113
uart = busio.UART(board.TX, board.RX)
1214
rst = digitalio.DigitalInOut(board.D4)
1315

14-
# Initialize FONA module (this may take a few seconds)
16+
# Use this for FONA800 and FONA808
1517
fona = FONA(uart, rst)
1618

19+
# Use this for FONA3G
20+
# fona = FONA3G(uart, rst)
21+
1722
# Initialize BME280 Sensor
1823
i2c = busio.I2C(board.SCL, board.SDA)
1924
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
@@ -28,23 +33,11 @@
2833
# Enable FONA SMS notification
2934
fona.enable_sms_notification = True
3035

31-
# store incoming notification info
32-
notification_buf = bytearray(64)
33-
34-
print("FONA Ready!")
36+
print("Listening for messages...")
3537
while True:
36-
if fona.in_waiting: # data is available from FONA
37-
notification_buf = fona.read_line()[1]
38-
# Split out the sms notification slot num.
39-
notification_buf = notification_buf.decode()
40-
if "+CMTI:" not in notification_buf:
41-
continue
42-
sms_slot = notification_buf.split(",")[1]
43-
44-
print("NEW SMS!\n\t Slot: ", sms_slot)
45-
46-
# Get SMS message and address
47-
sender, message = fona.read_sms(sms_slot)
38+
sender, message = fona.receive_sms()
39+
if message:
40+
print("New Message!")
4841
print("FROM: ", sender)
4942
print("MSG: ", message)
5043

@@ -57,16 +50,17 @@
5750
message = message.lower()
5851
message = message.strip()
5952

60-
if message in ['temp', 'temperature', 't']:
53+
if message in ["temp", "temperature", "t"]:
6154
response = "Temperature: %0.1f C" % temp
62-
elif message in ['humid', 'humidity', 'h']:
55+
elif message in ["humid", "humidity", "h"]:
6356
response = "Humidity: %0.1f %%" % humid
64-
elif message in ['pres', 'pressure', 'p']:
57+
elif message in ["pres", "pressure", "p"]:
6558
response = "Pressure: %0.1f hPa" % pres
66-
elif message in ['status', 's']:
67-
response = "Temperature: {0:.2f}C\nHumidity: {1:.1f}% \
68-
Pressure: {2:.1f}hPa".format(temp, humid, pres)
69-
elif message in ['help']:
59+
elif message in ["status", "s"]:
60+
response = "Temperature: {0:.2f}C\nHumidity: {1:.1f}%Pressure: {2:.1f}hPa".format(
61+
temp, humid, pres
62+
)
63+
elif message in ["help"]:
7064
response = "I'm a SMS Sensor - txt me with a command:\
7165
TEMP - Read temperature\
7266
HUMID - Read humidity\
@@ -82,8 +76,3 @@
8276
if not fona.send_sms(int(sender), response):
8377
print("SMS Send Failed")
8478
print("SMS Sent!")
85-
86-
# Delete the original message
87-
if not fona.delete_sms(sms_slot):
88-
print("Could not delete SMS in slot", sms_slot)
89-
print("OK!")

0 commit comments

Comments
 (0)