Skip to content

Arduino library ​

The Sensora library is for ESP32 firmware that you write yourself. You declare the values your device measures. The library joins Wi-Fi, sends the readings to your instance, and tries again when sending fails.

If you do not want to write code, flash the prebuilt firmware instead. It is built with this library.

What it supports ​

  • Chips: ESP32, ESP32-S3 and ESP32-C3, with the Arduino core for ESP32 in version 3.x. The ESP8266 is not supported.
  • Transport: HTTP or HTTPS to /ingest on your instance. The library does not use MQTT.
  • Direction: from the device to Sensora only. Sensora cannot send values or commands to a device.
  • Source: github.com/sensoraiot/sensora-library, Apache 2.0.

Install ​

PlatformIO ​

Add the library to platformio.ini. The platform line selects a build of the ESP32 platform that has Arduino core 3.x.

ini
[env:esp32dev]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.311/platform-espressif32.zip
framework = arduino
board = esp32dev
monitor_speed = 115200
lib_deps =
  https://github.com/sensoraiot/sensora-library

PlatformIO also installs the library's one dependency, OneWire.

Arduino IDE ​

  1. In Boards Manager, install esp32 by Espressif Systems, version 3.x.
  2. In Library Manager, install OneWire.
  3. Download the library as a ZIP file from its GitHub page and add it with Sketch, Include Library, Add .ZIP Library….

A minimal sketch ​

Create the device in the dashboard first to get its device_uid and secret. See Connect your first device.

cpp
#include <Arduino.h>
#include <Sensora.h>

Property temperature("temperature");
Property humidity("humidity");

void setup() {
  Serial.begin(115200);
  temperature.setDataType(DataType::Float).setUnit("°C");
  humidity.setDataType(DataType::Float).setUnit("%");

  // Remove these two lines to set Wi-Fi and the link over USB instead.
  Sensora.setWiFi("my-network", "my-password");
  Sensora.setCredentials("http://192.168.1.20:8090", "dev_xxxxxxxx", "sec_...");

  Sensora.setFirmware("room-sensor", "1.0.0");
  Sensora.setInterval(60);
  Sensora.setup();
}

unsigned long lastRead = 0;

void loop() {
  Sensora.loop();

  if (millis() - lastRead > 5000) {
    lastRead = millis();
    temperature.setValue(21.7);  // replace with your sensor
    humidity.setValue(45.0);
  }
}

Three rules keep a sketch working:

  • Declare each Property as a global variable. The name you give it is the metric name in Sensora.
  • Call setDataType for every property. The default type is String, and Sensora does not store text.
  • Call Sensora.loop() on every pass of loop() and do not block for long.

What the library sends ​

  • Readings. Once per interval, 60 seconds by default, the device sends one request to /ingest with all properties that have a value, plus rssi (Wi-Fi signal in dBm), uptime_s and heap_free.
  • Changes. A property sends early when its value changes, at most once every 5 seconds.
  • Announcement. Before its first reading after each start, the device tells Sensora its firmware, its sensors and its properties with their units. Sensora uses this to create a template and a starter dashboard. See Announce what the device is.
  • Retries. When sending fails, the library waits 15 seconds, then 30, and so on up to 10 minutes. When the server answers 401, because the device was deleted or created again, it tries once every 5 minutes and logs the reason on the serial port.

For an https:// address, the library checks the server's certificate against the certificate authorities that are built into the ESP32 core. A self-signed certificate does not pass this check. On a local network, use the http:// address.

Main API ​

Property ​

CallWhat it does
Property name("id")Declares a metric. The id has up to 32 characters. uid, secret and recorded_at are reserved. A sketch can have up to 16 properties.
setDataType(DataType::Float)Sets the type. Use Float, Integer or Boolean for values that Sensora stores.
setUnit("°C")Sets the unit that the announcement gives to the template.
clear()Removes the value. The next request leaves the property out.
setSyncStrategy(SyncStrategy::Periodic)Sends the property only on the interval. The default is SyncStrategy::OnChange.

Sensora ​

CallWhat it does
Sensora.setup()Starts the library. Call it once, at the end of setup(), after the other calls in this table.
Sensora.setInterval(seconds)Seconds between readings, from 10 to 86400. The default is 60.
Sensora.setFirmware(name, version)The firmware name and version that the device reports.
Sensora.addSensor(model, bus, address)Declares a sensor for the announcement, for example addSensor("BME280", "i2c", "0x76"). setSensor("BME280") on a property names the sensor that produces it.
Sensora.onBeforeUplink(callback)Runs your function right before each request. Read your sensors there.
Sensora.enableWatchdog(loopSeconds, noAnswerSeconds)Restarts the board when loop() is not reached for loopSeconds (default 60), or when the device is online and gets no answer for noAnswerSeconds (default 1800, 0 for never). Off unless you call it.
Sensora.enableOta(publicKey, seconds)Turns on updates over the air. See Updates over the air.
Sensora.onConsole(handler, help)Adds your own commands to the serial console.

Sensor autodetection ​

SensoraSensors finds the same sensors as the prebuilt firmware and creates a property for each reading. The list is in Sensors the firmware detects.

cpp
void setup() {
  Serial.begin(115200);
  SensoraSensors.begin();                                  // before Sensora.setup()
  Sensora.onBeforeUplink([] { SensoraSensors.read(); });   // fresh values in every request
  Sensora.setup();
}

Provisioning ​

The board needs its Wi-Fi details, the address of your instance and its credentials. There are two ways to give them.

In code. Call Sensora.setWiFi() and Sensora.setCredentials() before Sensora.setup(), as in the minimal sketch. Values that you set in code win over the values stored on the board.

Over USB, from the dashboard. Leave both calls out. The board then waits for its settings on the serial port.

  1. Upload your sketch to the board.
  2. In the dashboard, open Devices, select Connect device, then Plug in a board by USB.
  3. Select Board already runs Sensora: link only.
  4. Pick the serial port, then pick the Wi-Fi network and enter its password.

Sensora creates the device and writes its address, uid and secret onto the board. The board keeps these settings when you upload a new version of your sketch. See Flash a board over USB for the browser and address requirements.

The serial port must run at 115200 baud. On ESP32-S3 and ESP32-C3 builds where Serial is the chip's own USB port, add the UART Serial0 as a second port so that either one works:

cpp
Serial.begin(115200);
Serial0.begin(115200);
Sensora.addSerial(Serial).addSerial(Serial0);

The same port takes console commands as lines of plain text. See The USB tab. The README of the library describes the serial protocol.

Updates over the air ​

A device built with the library can download a new firmware from your instance and install it. This is off unless your sketch turns it on:

cpp
Sensora.setFirmware("room-sensor", "1.0.0");
Sensora.enableOta("<public key>", 3600);   // check every hour; 0 means every 6 hours

The public key is the one that bun run firmware keygen prints. The device installs only firmware that is signed with the matching private key. The version of a release that you upload must be exactly the version that the new build passes to setFirmware().

Firmware updates explains how to make a key, sign an image, upload it and roll it out.

Sketches for library version 0.0.x ​

#include <EspWifi.h>, subscribe() and AccessMode::Write still compile, so old sketches build. Sensora does not send values to devices, and the old MQTT cloud is gone. Link the board to your instance again, over USB or with setCredentials().

Examples in the repository ​

The folder examples/esp has two sketches: room-sensors, with two properties and simulated values, and ota-updates, which takes updates over the air. examples/agent is the Sensora Agent, the prebuilt firmware of the browser flasher.

Sensora is open source under the Apache 2.0 license.