Spresense 6-core microcontroller board with ultra-low power consumption

High-performance microcontroller board with hi-res audio, camera input, internal GPS and Edge AI support.

Buy Spresense

How to connect Spresense to a cellular network

In this example we will equip Spresense with an Adafruit FONA MiniGSM module, hook it up to a hall effect sensor and then transmit registered sensor values over the cellular network. For simplicity we will publish the gathered data on the dweet.io web site where it will be displayed as a graph.

We will count the number of times that the sensor is triggered and upload the accumulated value once every minute. A hall effect sensor detects the proximity of a magnetic field and will signal whenever it is close to some kind of magnet. A real-life application of this could be to gather information about when or how often a gate is opened or, if mounted near the wheel, calculate how far a rental bike has travelled to know when it is time for maintenance. Using a hall effect sensor is of course just done as an example and can be replaced with any other data gathering method.

Materials

  • Adafruit FONA MiniGSM module
  • SIM card that supports GPRS
  • An antenna
  • Li-Poly or LiIon Battery, 1200 mAh or larger
  • Hall sensor component, e.g. TLV49645
  • A magnet (a regular whiteboard magnet or similar will do fine)

The FONA module is available in two versions with different antenna connectors (SMA or uFL) so make sure to get an antenna that matches it. An external battery also needs to be connected to the modem since USB alone won’t be able to drive it during current spikes in its operation.

Step 1: Connect the hardware

  1. Insert a SIM card into the FONA module.
  2. Connect the antenna to the FONA module.
  3. Connect the FONA module and the hall sensor to the Spresense board according to the drawing.
  4. Connect the battery to the JP1 connector of the FONA module.

It is common to attach a pull-up resistor to the data pin of a hall effect sensor, but in this tutorial we configure the input pin of the Spresense board to use its internal pull-up instead.

Step 2: Get the source code

Download the Spresense FONA MiniGSM demo sketch from here:

developer.sony.com/file/download/spresense-fona-minigsm-demo-sketch/

Step 3: Configure the application

There are a couple of parameters that need to be configured before running the application. Load the sketch in the Arduino IDE and find and update the following defines:

DWEET_THING_NAME

The name used to publish your data on the dweet.io site. Try to pick something unique which is not very likely that someone else might be using.

FONA_APN

This is the access point name provided by your mobile operator and is needed to access the internet using GPRS.

FONA_PIN

This string is commented out by default but should contain the PIN code of your SIM card if it needs to be unlocked by the application. Leave it commented out if not needed.

Step 4: Run the application

1. Make sure you have the battery connected to the FONA module.

2. Upload the sketch to the Spresense board using the Arduino IDE and open the serial monitor.

3. Construct a web address by adding the name you choose for DWEET_THING_NAME to the dweet.io URL, e.g. “https://dweet.io/follow/gsm_test_123” if the name is "gsm_test_123", and open it in a web browser.

4. Once the board is running, touch the hall sensor with the magnet and you should see the updated sensor change count value in log output in the serial monitor of the Arduino IDE, e.g. “SENSOR COUNT: 1”. Repeat this a number of times to gather some data to be transferred with the modem.

5. After the set sleep period has passed you will see printouts in the log in the Arduino serial monitor from when the FONA module is started and eventually the data will be sent.

6. If the data transmission was successful you should see the message “SENT UPDATE: ” in the serial monitor log and the dweet.io web page should automatically be updated with the new value.

If you play around with the code and the FONA ends up in a bad state so it cannot connect anymore, just disconnect the battery and the USB cable and reconnect them again to reset.

Note that the FONA module has a built-in charger circuit, so if the battery runs out (you will see warnings about it in the log in the serial monitor of the Arduino IDE), just disconnect the USB cable from Spresense and connect it to the FONA USB connector instead. A green light on the module indicates that the battery is fully charged.

Step 5: Brief code overview

GSM module communication

This example application uses the FONA support library provided by Adafruit. Since the module is controlled using AT commands over UART we simply need to setup a software serial instance using our selected pins (FONA_PIN_TX and FONA_PIN_RX) for the TX transmit and the RX receive line. Then, when initializing the library using its begin function, we just provide our software serial instance to it.

We also need to connect the module’s reset pin to Spresense to be able to initialize it. The Spresense pin name (defined as FONA_PIN_RST) is provided to the constructor when instantiating the support library in our application.

Data update handling

In this rather minimal example sending data to the server is done by executing the following steps:

  • Enable GPRS
  • Generate a HTTP json request using our dweet.io URL and updated value
  • Send the request to the server and retrieve the status code from it
  • Write the full server response to the serial log for reference

All underlying AT commands and data handling is handled by the Fona library, so we don’t have to worry about it.

Update frequency

The application is set to send an updated value to dweet.io once every minute by default. The number of seconds between updates is defined by the value UPDATE_SLEEP_S. Note that it takes about 20 seconds for the modem to connect to the server, send data and receive a response. Therefore, keep the sleep value well above this to avoid any conflicts when trying to initiate a connection while a previous one is still ongoing.

Sensor handling

The task of connecting to the network and sending an update takes a bit of time, approximately 20 seconds, and will block the caller while being executed. Therefore, an interrupt handler (“sensor_interrupt”) is attached to the sensor input pin. This will capture all sensor state changes so they can be recorded in a global hit count variable regardless of the overall application state.

When it is time to transmit a new sensor hit count value, we must first make sure that we don’t run into troubles if we happen to try to read and write the variable at the same. To avoid this we first disable interrupts, read the value and then enable interrupts again. Note that the hit counter is also reset right after it is read so any sensor events triggered while the modem is doing some transfer will be included in the next update.

Step 5: More information

For more information about the FONA module please see:

learn.adafruit.com/adafruit-fona-mini-gsm-gprs-cellular-phone-module

The Spresense main board pin names can be found in the introduction page at:

developer.sony.com/develop/spresense/docs/introduction_en.html#_main_board

The Spresense Arduino documentation can be found at:

developer.sony.com/develop/spresense/developer-tools/api-reference/api-references-arduino/