Examples of Datalogging Using Microcontrollers
tigoe.github.io
WiFi Datalogging
Node.js Datalogging Server
Google Sheets Datalogging Server
Tracking Time
MKR Series Datalogging
Home
This project is maintained by tigoe
The examples described here log data to a server via WiFi using a network-enabled microcontroller as a datalogging client. These examples should all work on the network-enabled MKR boards, along with the Nano 33 IoT and other SAMD-based boards which have network capability. They can likely work on Espressif-based boards (ESP32, ESP8266) with some modification.
This collection includes an ArduinoHttpClient example along with a node.js server script. There are also instructions for how to log data to a Google Sheets spreadsheet using Google Apps script, thanks to Arnab Chakravarty.
The scripts:
To make this work, you’ll need:
Background material you might want to review beforehand:
For other WiFi and ArduinoHttpClient examples, see this repository. For other Node examples, see this repository.
The Arduino client is set up to log the data by default to a Google Apps Script, as explained here. If you want to change it to send to a node.js script like the one shown here, then change the server
and route
variables to match the address of your server and the API route for posting. If you’re running the node server via HTTP rather than via HTTPS, then change the WiFiSSLClient
declaration to WiFiClient
and change the port
variable to the port number on which your node script is listening.
When you’re setting up a microcontroller to be a sensor datalogging client, there are a few things to consider:
The first question, what is being gathered, will depend on your particular case, of course. The examples given here were written to gather lighting data: illuminance and color temperature. Your characteristics will vary.
It’s useful to attach a timestamp to each set of sensor readings, and it’s useful to use some time standards. The simplest solution is to let the server time stamp each reading, ad the servers in this collection do. However, there may be reasons to time stamp locally by the microcontroller. If that is the case, then you want to attach a real-time clock to the microcontroller, or use a controller with one built-in, like the Nano 33 IoT or the MKR boards. The RTCZero library lets you access that realtime clock, and the WiFi libraries let you set the clock by making a network time server request, using the command WiFi.getTime()
. For more on time in connected systems, see this page.
Unless you’re using A GPS reader or some other machine-based way to determine your location, the simplest way is just to name the location in your data. Adding a parameter to your data with the location name is a good way to start.
It’s a good idea to always include a unique ID in your sensor readings so you can identify the device that took the reading, independent of the location or the time. The servers described below filter requests by checking a unique ID (uid) in the request. If the ID that the client sends doesn’t match one of the ones in a list called knownClients
, the server responds with a 403 error and the data is not saved.
You’ll need to fill in an uid for your microcontroller for this to work. From any networked microcontroller, you can use the MAC address of the WiFi radio. You can see this in the Arduino example.