|
| 1 | +# pylint: disable=unused-import |
1 | 2 | import time |
2 | 3 | import board |
3 | 4 | import busio |
4 | 5 | import digitalio |
5 | 6 | from adafruit_fona.adafruit_fona import FONA |
| 7 | +from adafruit_fona.fona_3g import FONA3G |
6 | 8 | import adafruit_bme280 |
7 | 9 |
|
8 | 10 | print("FONA SMS Sensor") |
|
11 | 13 | uart = busio.UART(board.TX, board.RX) |
12 | 14 | rst = digitalio.DigitalInOut(board.D4) |
13 | 15 |
|
14 | | -# Initialize FONA module (this may take a few seconds) |
| 16 | +# Use this for FONA800 and FONA808 |
15 | 17 | fona = FONA(uart, rst) |
16 | 18 |
|
| 19 | +# Use this for FONA3G |
| 20 | +# fona = FONA3G(uart, rst) |
| 21 | + |
17 | 22 | # Initialize BME280 Sensor |
18 | 23 | i2c = busio.I2C(board.SCL, board.SDA) |
19 | 24 | bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c) |
|
28 | 33 | # Enable FONA SMS notification |
29 | 34 | fona.enable_sms_notification = True |
30 | 35 |
|
31 | | -# store incoming notification info |
32 | | -notification_buf = bytearray(64) |
33 | | - |
34 | | -print("FONA Ready!") |
| 36 | +print("Listening for messages...") |
35 | 37 | 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!") |
48 | 41 | print("FROM: ", sender) |
49 | 42 | print("MSG: ", message) |
50 | 43 |
|
|
57 | 50 | message = message.lower() |
58 | 51 | message = message.strip() |
59 | 52 |
|
60 | | - if message in ['temp', 'temperature', 't']: |
| 53 | + if message in ["temp", "temperature", "t"]: |
61 | 54 | response = "Temperature: %0.1f C" % temp |
62 | | - elif message in ['humid', 'humidity', 'h']: |
| 55 | + elif message in ["humid", "humidity", "h"]: |
63 | 56 | response = "Humidity: %0.1f %%" % humid |
64 | | - elif message in ['pres', 'pressure', 'p']: |
| 57 | + elif message in ["pres", "pressure", "p"]: |
65 | 58 | 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"]: |
70 | 64 | response = "I'm a SMS Sensor - txt me with a command:\ |
71 | 65 | TEMP - Read temperature\ |
72 | 66 | HUMID - Read humidity\ |
|
82 | 76 | if not fona.send_sms(int(sender), response): |
83 | 77 | print("SMS Send Failed") |
84 | 78 | 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