You will want to use a chip with more memory with FONA CircuitPython such as the M4, STM32, or nRF52

FONA808 or FONA800 Wiring

If you're using a FONA808 or FONA800 breakout, use the following wiring so that the hardware UART pins are used. Here's an example with the Feather M4:

  • Board 5V (or 3.3v) to FONA808 VIO
  • Board GND to FONA808 GND
  • Board GND to FONA808 KEY
  • Board Serial TX to FONA808 RX
  • Board Serial RX to FONA808 TX
  • Board Digital Pin 4 to FONA808 RST

FONA 3G Wiring

If you're using a FONA3G breakout, use the following wiring so that the hardware UART pins are used. Here's an example with the Feather M4:

  • Board 5V (or 3.3V) to Fona3G Vio
  • Board GND to FONA3G GND
  • Board GND to FONA3G Key
  • Board RX to FONA3G TX
  • Board TX to FONA3G RX
  • Board Digital 4 Pin to Fona3G RST

Assembly

Before using the FONA with CircuitPython, make sure you've attached a LiPoly battery, GPS antenna and GSM antenna to the FONA.

You must also insert a SIM card to use the FONA with cellular data.

Make sure the battery you're using is charged before running the code below.

All of the above are REQUIRED! Flaky behavior is often a low battery, no SIM, no GSM antenna, etc!

Code Usage

Copy the following code to the code.py file on your microcontroller. 

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# pylint: disable=unused-import
import time
import board
import busio
import digitalio
import adafruit_requests as requests
from adafruit_fona.adafruit_fona import FONA
from adafruit_fona.fona_3g import FONA3G
import adafruit_fona.adafruit_fona_network as network
import adafruit_fona.adafruit_fona_socket as cellular_socket

print("FONA Webclient Test")

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"

# Get GPRS details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("GPRS secrets are kept in secrets.py, please add them there!")
    raise

# 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 cellular data network
network = network.CELLULAR(
    fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])
)

while not network.is_attached:
    print("Attaching to network...")
    time.sleep(0.5)
print("Attached!")

while not network.is_connected:
    print("Connecting to network...")
    network.connect()
    time.sleep(0.5)
print("Network Connected!")

print("My IP address is:", fona.local_ip)
print("IP lookup adafruit.com: %s" % fona.get_host_by_name("adafruit.com"))

# Initialize a requests object with a socket and cellular interface
requests.set_socket(cellular_socket, fona)

# fona._debug = True
print("Fetching text from", TEXT_URL)
r = requests.get(TEXT_URL)
print("-" * 40)
print(r.text)
print("-" * 40)
r.close()

print()
print("Fetching json from", JSON_URL)
r = requests.get(JSON_URL)
print("-" * 40)
print(r.json())
print("-" * 40)
r.close()

print("Done!")

If you're using a FONA808, you do not need to modify the code below.

If you're using a FONA3G, you'll need to comment out the FONA800/FONA808 initialization and un-comment the FONA3G initialization in the code: 

# Use this for FONA800 and FONA808
# fona = FONA(uart, rst)

# Use this for FONA3G
fona = FONA3G(uart, rst)

Save the code.py file and open the REPL

In order, the example code:

Imports APN details from the secrets.py file.

# Get GPRS details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("GPRS secrets are kept in secrets.py, please add them there!")
    raise

Creates a serial UART connection for the FONA and sets up the RST pin.

NOTE: You may need to change these pins if you're using a FONA breakout instead of a FONA shield.

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

Initializes the FONA module. This step may take a few seconds since the module attempts to communicate with and bring up the module.

# Use this for FONA800 and FONA808
fona = FONA(uart, rst)

# Use this for FONA3G
# fona = FONA3G(uart, rst)

We use a module called fona_network to configure the modem. This module automatically determines if the modem connected is a 2G/GSM or 3G/CDMA modem and initializes the modem appropriately. 

# Initialize cellular data network
network = network.CELLULAR(
    fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])
)

The code waits for the FONA to attach to the cellular network using the carrier settings.

while not network.is_attached:
    print("Attaching to network...")
    time.sleep(0.5)
print("Attached!")

Once attached, it attempts to connect to the network and bring up the modem. This step may take a while to connect, depending on your cellular reception.

while not network.is_connected:
    print("Connecting to network...")
    network.connect()
    time.sleep(0.5)
print("Network Connected!")

Prints out the local IP and attempts to perform an IP address lookup for adafruit.com.

print("My IP address is:", fona.local_ip)
print("IP lookup adafruit.com: %s" % fona.get_host_by_name("adafruit.com"))

OK now we're getting to the really interesting part. With a SAMD51 or other large-RAM (well, over 32 KB) device, we can do a lot of neat tricks. Like, for example, we can implement an interface a lot like requests - which makes getting data really really easy.

Calling requests.set_socket passes requests our fona interface and a socket-like implementation for the FONA.

# Initialize a requests object with a socket and cellular interface
requests.set_socket(cellular_socket, fona)

To read in all the text from a web URL, call requests.get 

print("Fetching text from", TEXT_URL)
r = requests.get(TEXT_URL)
print("-" * 40)
print(r.text)
print("-" * 40)
r.close()

Or, if the data is in structured JSON, you can get the json pre-parsed into a Python dictionary that can be easily queried or traversed. (Again, only for nRF52840, M4 and other high-RAM boards)

print("Fetching json from", JSON_URL)
r = requests.get(JSON_URL)
print("-" * 40)
print(r.json())
print("-" * 40)
r.close()

This guide was first published on May 13, 2020. It was last updated on May 13, 2020.

This page (Usage) was last updated on May 05, 2021.

Text editor powered by tinymce.