|
| 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.D4) |
| 13 | + |
| 14 | +# Initialize FONA module (this may take a few seconds) |
| 15 | +fona = FONA(uart, rst) |
| 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 | + 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) |
| 48 | + print("FROM: ", sender) |
| 49 | + print("MSG: ", message) |
| 50 | + |
| 51 | + # Read BME280 sensor values |
| 52 | + temp = bme280.temperature |
| 53 | + humid = bme280.humidity |
| 54 | + pres = bme280.pressure |
| 55 | + |
| 56 | + # Sanitize message |
| 57 | + message = message.lower() |
| 58 | + message = message.strip() |
| 59 | + |
| 60 | + if message in ['temp', 'temperature', 't']: |
| 61 | + response = "Temperature: %0.1f C" % temp |
| 62 | + elif message in ['humid', 'humidity', 'h']: |
| 63 | + response = "Humidity: %0.1f %%" % humid |
| 64 | + elif message in ['pres', 'pressure', 'p']: |
| 65 | + 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']: |
| 70 | + response = "I'm a SMS Sensor - txt me with a command:\ |
| 71 | + TEMP - Read temperature\ |
| 72 | + HUMID - Read humidity\ |
| 73 | + PRES - Read pressure\ |
| 74 | + STATUS - Read all sensors.\ |
| 75 | + HELP - List commands" |
| 76 | + else: |
| 77 | + response = "Incorrect message format received. \ |
| 78 | + Text HELP to this number for a list of commands." |
| 79 | + |
| 80 | + # Send a response back to the sender |
| 81 | + print("Sending response...") |
| 82 | + if not fona.send_sms(int(sender), response): |
| 83 | + print("SMS Send Failed") |
| 84 | + 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