Code Usage

Install CircuitPython Code

In the embedded code element below, click on the Download: Project Zip link, and save the .zip archive file to your computer.

Then, uncompress the .zip file, it will unpack to a folder named FONA_SMS_Sensor.

# 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")

# Create a serial connection for the FONA connection
uart = busio.UART(board.TX, board.RX)
rst = digitalio.DigitalInOut(board.D4)

# 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)

# Initialize Network
while fona.network_status != 1:
    print("Connecting to network...")
    time.sleep(1)
print("Connected to network!")
print("RSSI: %ddB" % fona.rssi)

# Enable FONA SMS notification
fona.enable_sms_notification = True

print("Listening for messages...")
while True:
    sender, message = fona.receive_sms()
    if message:
        print("New Message!")
        print("FROM: ", sender)
        print("MSG: ", message)

        # Read BME280 sensor values
        temp = bme280.temperature
        humid = bme280.humidity
        pres = bme280.pressure

        # Sanitize message
        message = message.lower()
        message = message.strip()

        if message in ["temp", "temperature", "t"]:
            response = "Temperature: %0.1f C" % temp
        elif message in ["humid", "humidity", "h"]:
            response = "Humidity: %0.1f %%" % humid
        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"]:
            response = "I'm a SMS Sensor - txt me with a command:\
                        TEMP - Read temperature\
                        HUMID - Read humidity\
                        PRES - Read pressure\
                        STATUS - Read all sensors.\
                        HELP - List commands"
        else:
            response = "Incorrect message format received. \
                        Text HELP to this number for a list of commands."

        # Send a response back to the sender
        print("Sending response...")
        if not fona.send_sms(int(sender), response):
            print("SMS Send Failed")
        print("SMS Sent!")

Copy the contents of the FONA_SMS_Sensor directory to your PyPortal's CIRCUITPY drive.

This is what the final contents of the CIRCUITPY drive will look like:

Obtain FONA's Number

Next, you'll need your SIM card's phone number. This should be available from your carrier's website. Here's an example of the Ting devices page displaying the FONA's number

Code Usage

After saving the code.py to your board, open the REPL. After the FONA registers with the network, it will output its received signal strength indicator (RSSI) level and display that the FONA is ready to receive messages from your phone. 

Text HELP to your device's phone number.

The FONA should reply with a SMS listing all possible commands.

Let's try texting one of the commands. You can read the temperature by texting TEMP to the FONA.

Try this by texting TEMP to the FONA. The FONA will reply with the BME280's temperature reading.

Text HUMID to the FONA. The FONA will reply with the the current relative humidity reading.

Text PRES to the FONA. The FONA will reply with the current pressure. 

Want to read all the sensors on the BME280 breakout at once? Text STATUS to the FONA module.

The FONA will reply back with readings from all the sensors on the BME280.

This guide was first published on May 20, 2020. It was last updated on May 20, 2020.
This page (Code Usage) was last updated on Jun 16, 2020.