The code first creates a serial UART connection with the FONA module and a DigitalInOut object to digitally control IO Pin D4. Then, it initializes the FONA module. 

Download: file
# 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)

Then an I2C connection is established with the BME280 sensor. 

Download: file
# Initialize BME280 Sensor
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)

The FONA takes some time to register itself with the cellular network. Depending on your location relative to cellular towers, this step may take some time.

Once connected, the received signal strength indicator (RSSI) will be printed to the REPL.

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

By default, the FONA module does not raise any notifications when it receives a text message. Setting enable_sms_notification tells the FONA module to send a message to the microcontroller whenever it receives a new message. 

Download: file
# Enable FONA SMS notification
fona.enable_sms_notification = True

On each iteration of the while True loop, the code checks if data is available to be read from the FONA module using the receive_sms() method.

Download: file
while True:
    sender, message = fona.receive_sms()

The code reads the sender and message from the SIM card's memory slot. 

Download: file
if message:
        print("New Message!")
        print("FROM: ", sender)
        print("MSG: ", message)

The BME280 sensor's values are read and stored in temp, humid, and pres.

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

The code selects a response based on the contents of the SMS message. 

Download: file
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."

The response is sent back to the sender's phone number.

Download: file
# 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!")

That's it! The code will wait to receive the next text message.

This guide was first published on May 20, 2020. It was last updated on May 20, 2020.
This page (Code Walkthrough) was last updated on Sep 19, 2020.