This blog posts explains how to use the nRF9160 connectivity statistics to monitor data usage.
One of the biggest cost factors when operating a cellular IoT product are data transfers. Not only are prices for IoT connectivity multiple magnitudes more expensive to what we are used from smartphone contracts, but transmitting data also requires a lot of energy. The longer the devices needs to transmit a payload the more likely it is also that the connection deteriorates (especially when the device is moving) and re-transmits need to happen. Therefore it is important to pay close attention to the amount of data your product is sending from the beginning. Having knowledge about the data usage profile of your application at hand also becomes important when picking the right connectivity partner.
While it is possible to infer a device's data consumption on the terminating endpoint, this information is not accurate, because it can observe successfully incoming messages. It can also become challenging to cover all endpoints, for example Firmware over the Air updates are typically downloaded via HTTPs from a web server and not through MQTT.
Therefore it is very valuable to use the Connectivity statistics the nRF9160 modem provides: it is a set of AT commands which configure the modem to start collecting statistics and to retrieve these statistics:
- use AT%XCONNSTAT=1 to tell the modem to start collecting connectivity statistics
- use AT%XCONNSTAT? to read the current connectivity statistics
In an application this can be implemented as follows:
1. After starting the application send the AT%XCONNSTAT=1 AT command once:
2. Implement a worker that regularly queries the connectivity statistics:
Note: You can see a full diff of how I added this to one of my applications here.
This is how the output will look like:
Connection stats: %XCONNSTAT: 0,0,14,16,748,134 | Uptime: 5041 seconds
Now you have access to the connectivity statistics, and can for example publish this to the cloud every hour so you can collect precise data consumption usage from your devices. Together with the uptime information collected on the device you will be able to develop a very good understand of what the typical data usage per day, week and month will be for your devices.
Caution: do not use the connectivity statistics in applications which use the LwM2M carrier library. This library manages the collection of connectivity statistics and will turn them on and off on its behalf. If your application interferes with this statistics collection it will result in incorrect measurements in the carrier's device management solution.