Skip to content

Commit 6b3a0d4

Browse files
author
brentru
committed
add fona sms sensor code
1 parent e6f01ec commit 6b3a0d4

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

FONA_SMS_Sensor/code.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import time
2+
import board
3+
import busio
4+
import digitalio
5+
from adafruit_fona.adafruit_fona import FONA
6+
import adafruit_bme280
7+
8+
print("FONA SMS Sensor")
9+
10+
# Create a serial connection for the FONA connection
11+
uart = busio.UART(board.TX, board.RX)
12+
rst = digitalio.DigitalInOut(board.D5)
13+
14+
# Initialize FONA module (this may take a few seconds)
15+
fona = FONA(uart, rst, debug=True)
16+
17+
# Initialize BME280 Sensor
18+
i2c = busio.I2C(board.SCL, board.SDA)
19+
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
20+
21+
# Initialize Network
22+
while fona.network_status != 1:
23+
print("Connecting to network...")
24+
time.sleep(1)
25+
print("Connected to network!")
26+
print("RSSI: %ddB" % fona.rssi)
27+
28+
# Enable FONA SMS notification
29+
fona.enable_sms_notification = True
30+
31+
# store incoming notification info
32+
notification_buf = bytearray(64)
33+
34+
print("FONA Ready!")
35+
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+
sms_slot = notification_buf.split(",")[1]
41+
42+
print("NEW SMS!\n\t Slot: ", sms_slot)
43+
44+
# Get SMS message and address
45+
sender, message = fona.read_sms(sms_slot)
46+
print("FROM: ", sender)
47+
print("MSG: ", message)
48+
49+
# Read BME280 sensor values
50+
temp = bme280.temperature
51+
humid = bme280.humidity
52+
pres = bme280.pressure
53+
54+
message = message.lower()
55+
print('MSG: ', message)
56+
57+
if message in ['temp', 'temperature']:
58+
response = "Temperature: %0.1f C" % temp
59+
elif message in ['humid', 'humidity']:
60+
response = "Humidity: %0.1f %%" % humid
61+
elif message in ['pres', 'pressure']:
62+
response = "Pressure: %0.1f hPa" % pres
63+
elif message in ['state', 'status']:
64+
response = "Temperature: {0:.2f}C\nHumidity: {1:.1f}% \
65+
Pressure: {2:.1f}hPa".format(temp, humid, pres)
66+
else:
67+
response = "Incorrect message format received"
68+
69+
print("Sending response: ", response)
70+
71+
# Send a response back to the sender
72+
print("Sending response...")
73+
if not fona.send_sms(int(sender), response):
74+
print("SMS Send Failed")
75+
print("SMS Sent!")
76+
77+
# Delete the original message
78+
if not fona.delete_sms(sms_slot):
79+
print("Could not delete SMS in slot", sms_slot)
80+
print("OK!")

0 commit comments

Comments
 (0)