Skip to content

Update FONA SMS Sensor for FONA Library Update #1132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 18 additions & 29 deletions FONA_SMS_Sensor/code.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# pylint: disable=unused-import
import time
import board
import busio
import digitalio
from adafruit_fona.adafruit_fona import FONA
from adafruit_fona.fona_3g import FONA3G
import adafruit_bme280

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

# Initialize FONA module (this may take a few seconds)
# Use this for FONA800 and FONA808
fona = FONA(uart, rst)

# Use this for FONA3G
# fona = FONA3G(uart, rst)

# Initialize BME280 Sensor
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
Expand All @@ -28,23 +33,11 @@
# Enable FONA SMS notification
fona.enable_sms_notification = True

# store incoming notification info
notification_buf = bytearray(64)

print("FONA Ready!")
print("Listening for messages...")
while True:
if fona.in_waiting: # data is available from FONA
notification_buf = fona.read_line()[1]
# Split out the sms notification slot num.
notification_buf = notification_buf.decode()
if "+CMTI:" not in notification_buf:
continue
sms_slot = notification_buf.split(",")[1]

print("NEW SMS!\n\t Slot: ", sms_slot)

# Get SMS message and address
sender, message = fona.read_sms(sms_slot)
sender, message = fona.receive_sms()
if message:
print("New Message!")
print("FROM: ", sender)
print("MSG: ", message)

Expand All @@ -57,16 +50,17 @@
message = message.lower()
message = message.strip()

if message in ['temp', 'temperature', 't']:
if message in ["temp", "temperature", "t"]:
response = "Temperature: %0.1f C" % temp
elif message in ['humid', 'humidity', 'h']:
elif message in ["humid", "humidity", "h"]:
response = "Humidity: %0.1f %%" % humid
elif message in ['pres', 'pressure', 'p']:
elif message in ["pres", "pressure", "p"]:
response = "Pressure: %0.1f hPa" % pres
elif message in ['status', 's']:
response = "Temperature: {0:.2f}C\nHumidity: {1:.1f}% \
Pressure: {2:.1f}hPa".format(temp, humid, pres)
elif message in ['help']:
elif message in ["status", "s"]:
response = "Temperature: {0:.2f}C\nHumidity: {1:.1f}%Pressure: {2:.1f}hPa".format(
temp, humid, pres
)
elif message in ["help"]:
response = "I'm a SMS Sensor - txt me with a command:\
TEMP - Read temperature\
HUMID - Read humidity\
Expand All @@ -82,8 +76,3 @@
if not fona.send_sms(int(sender), response):
print("SMS Send Failed")
print("SMS Sent!")

# Delete the original message
if not fona.delete_sms(sms_slot):
print("Could not delete SMS in slot", sms_slot)
print("OK!")