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.
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.
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.