Skip to content

Commit 112ee9c

Browse files
author
brentru
committed
add fona code
1 parent 6b3a0d4 commit 112ee9c

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

code.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
# sanitize message
55+
message = message.lower()
56+
message = message.strip()
57+
58+
if message in ['temp', 'temperature']:
59+
response = "Temperature: %0.1f C" % temp
60+
elif message in ['humid', 'humidity']:
61+
response = "Humidity: %0.1f %%" % humid
62+
elif message in ['pres', 'pressure']:
63+
response = "Pressure: %0.1f hPa" % pres
64+
elif message in ['state', 'status']:
65+
response = "Temperature: {0:.2f}C\nHumidity: {1:.1f}% \
66+
Pressure: {2:.1f}hPa".format(temp, humid, pres)
67+
else:
68+
response = "Incorrect message format received"
69+
70+
71+
print("Sending response: ", response)
72+
73+
# Send a response back to the sender
74+
print("Sending response...")
75+
if not fona.send_sms(int(sender), response):
76+
print("SMS Send Failed")
77+
print("SMS Sent!")
78+
79+
# Delete the original message
80+
if not fona.delete_sms(sms_slot):
81+
print("Could not delete SMS in slot", sms_slot)
82+
print("OK!")

0 commit comments

Comments
 (0)