Library and examples on GitHubgithub.com / TexhFexLabs / HydroNode-Library

Get the Sensor ID and Secret

The library signs every request with your sensor's Sensor ID and Secret. Create the sensor and copy both from Settings → Connection:

Sensor and credentialsCreate a sensor, copy Sensor ID and Secret, keep them safe

Install the library

The library is in the official Arduino Library Manager. Open Tools → Manage Libraries, search for HydroNode-Library and install it. The IDE offers to install the dependencies along with it: say yes. For reference, these are:

ArduinoJson
JSON parsing, version 7
ArduinoJson
ArduinoHttpClient
HTTP client for Arduino
ArduinoHttpClient
WiFiManager
Only for the captive portal example
WiFiManager
TinyGSM
Only for LTE-M, NB-IoT and other modems
TinyGSM
ESP_SSLClient
Only for TLS over a W5500 or a modem
ESP_SSLClient

In PlatformIO, pin the Git tag. The registry does not always carry the newest version right away:

platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
  https://github.com/TexhFexLabs/HydroNode-Library.git#1.9.0

Or clone the repository into your libraries/ folder and install the dependencies above by hand:

shell
git clone https://github.com/TexhFexLabs/HydroNode-Library.git ~/Arduino/libraries/HydroNode

A complete .ino sketch

Fill in YOUR_WIFI_SSID, YOUR_WIFI_PASSWORD, your Sensor ID and your Secret, then upload. sendValues() sends every value of one round in a single signed request, stamped with the time it was measured. That saves one connection per value, which is several seconds on an ESP8266. The library fetches the time itself and refreshes it every 30 minutes.

arduino / C++
#include <HydroNode.h>

const char* WIFI_SSID     = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";

// Settings > Connection on the sensor page
const char* SENSOR_ID  = "YOUR_SENSOR_ID";
const char* SECRET_KEY = "YOUR_SECRET";

HydroNode hydro(SENSOR_ID, SECRET_KEY);

// Command "pump" with type UINT32 from the HydroNode app
void pumpCallback(uint32_t ms) {
  if (ms > 10000) ms = 10000;  // never trust a network value blindly
  Serial.printf("Pump for %u ms\n", ms);
  // run the pump for ms milliseconds
}

void setup() {
  Serial.begin(115200);
  hydro.setDebug(Serial);  // optional: prints every request and answer

  while (!hydro.connectWiFi(WIFI_SSID, WIFI_PASSWORD)) {
    Serial.println("WiFi failed, retrying");
  }
  hydro.begin();  // fetches the time, needed for every signature

  hydro.onUInt32("pump", pumpCallback);
}

void loop() {
  // Replace with real readings from your hardware
  HydroNodeValue reading[] = {
    {"TEMPERATURE", 22.4},  // °C
    {"HUMIDITY",    65.2},  // %
  };
  int status = hydro.sendValues(reading, 2);
  Serial.printf("HydroNode answered %d\n", status);

  delay(60000);  // one round per minute
}

HydroNode accepts one value per measurement type every 10 seconds, and up to 64 values in one request. sendValue("TEMPERATURE", 22.4) still works for a single value.

Check that the values arrive

Open the serial monitor at 115200 baud. HydroNode answered 202 means accepted. Then open the sensor page in HydroNode and press the ` key. The device console shows every request the moment it arrives, with the signature check and how far the device clock is off.

The Live tab of the device console with accepted HTTP requestsThe Live tab of the device console with accepted HTTP requests
One line per request: time, status, values, signature check and clock offset.
AnswerMeaningWhat to do
202AcceptedNothing, all good.
401Signature or time wrongCheck the Sensor ID and Secret. The console names the exact reason.
429Too fastSend at most one value per measurement type every 10 seconds.
-1No networkCheck the WiFi name and password, the cable or the modem.
-2No timeAllow UDP port 123 (NTP) on your network.
-3Connection failedTLS or network error. The library tries again with the next round.
-4Invalid type nameUse upper case: TEMPERATURE, not temperature.
-5Invalid valueThe value is NaN or infinite. Check the sensor reading.

A rejected request shows up in red in the console, with the exact reason. The Rejected filter shows only those.

Send commands to the device

A command has a name, a value type and a value. On the device you register one callback per name and type. One name can have several types, for example relay1 true and relay1 1400.

BOOL
true or false
hydro.onBool(name, fn)
INT32
-2,147,483,648 to 2,147,483,647
hydro.onInt32(name, fn)
UINT32
0 to 4,294,967,295, hex like 0x20124 works
hydro.onUInt32(name, fn)
INT64
about ±9.2 × 10¹⁸
hydro.onInt64(name, fn)
UINT64
0 to about 1.8 × 10¹⁹, hex works
hydro.onUInt64(name, fn)
STRING
Any text up to 500 characters
hydro.onString(name, fn)
arduino / C++
// App: name "lamp", type BOOL, value true
hydro.onBool("lamp", [](bool on) {
  digitalWrite(LAMP_PIN, on ? HIGH : LOW);
});

// Same name, type UINT32: "lamp 1400" lights it for 1.4 s
hydro.onUInt32("lamp", [](uint32_t ms) {
  ms = min(ms, (uint32_t)10000);  // local safety limit
  digitalWrite(LAMP_PIN, HIGH);
  delay(ms);
  digitalWrite(LAMP_PIN, LOW);
});

// App: name "display", type STRING, value "Hello"
hydro.onString("display", [](const String& text) {
  lcd.print(text);
});
  1. 1
    Send it
    On the sensor page press Send command and fill in name, type and value. Or type lamp:bool true in the Commands tab of the console.
  2. 2
    Wait for the next round
    Commands travel with the answer to the next value the device sends. With one round per minute, that takes up to a minute.
  3. 3
    Read the status
    Confirmed means the device has a matching callback and started it. Declined names the reason and the callback never runs: No handler on the device when the name is unknown, Type does not match when the callback expects another type, Value not accepted when the value does not fit, for example -1 for UINT32.
The Commands tab of the device console with a prompt and sent commandsThe Commands tab of the device console with a prompt and sent commands
The Commands tab: type name, type and value, and follow each command's status.

Hex like 0x20124 is accepted for the integer types and arrives as the number 131364. There is no float type: send a whole number in a fixed unit, for example tenths of a degree. Commands are delivered at most once. If a response is lost, send the command again.

Confirmed means the device received the command, not that the pump actually ran. Values come over the network, so clamp them on the device to what your hardware can safely do, like the 10 second cap in the sketch above.

Switches and choices with a real state

For a relay or a mode in HydroNode's Controls, let the device report the state it really took as a value (0/1, or the index of a mode) and listen to a command for it. In HydroNode, open that data point, set Read as to On or off or Choice and turn on Send a command when it is set with the command's name and type (BOOL or UINT32). Controls then show the change as waiting until the reported value matches. The library example EnumState is a heater with three modes built this way.

Protect a battery device

From library 1.8.0, HydroNodeBatteryGuard looks after the battery the same way HydroNode firmware does: it sends less often below Save, turns WiFi and outputs off below Radio off, sleeps an hour at a time below Standby and runs normally again from Resume. Before it goes quiet it sends one last round, so HydroNode shows Low battery standby instead of Offline.

With onSettings() the sketch also takes a new send interval and new thresholds that you change in HydroNode under Settings → Battery → On the device. The example PowerThresholds in the library shows the whole round, including deep sleep. Every threshold and rule is in Battery and Power.

Ethernet, LTE-M and NB-IoT

The signature does not care how the bytes travel. From library 1.9.0 the network is a separate object, the transport. Pick the one for your hardware and keep the rest of the sketch: values, commands and battery settings work the same everywhere. Only what you include is compiled, so a WiFi sketch carries no modem code.

NetworkHardwareExample
WiFiEvery ESP32 and the ESP8266QuickStart
EthernetESP32 with built-in port (WT32-ETH01, Olimex ESP32-POE)Ethernet_ESP32
EthernetW5500 module on SPI, any boardEthernet_W5500
LTE-M, NB-IoTSIM7080G, SIM7000, BG95, SARA-R4Cellular_SendNow
LTE Cat-1A7670, SIM7600, TLS in the modemCellular_ModemTLS
WiFi, else LTE-MESP32 with a modemWiFi_Fallback_LTE
Any clientUNO R4 WiFi, MKR, Nano 33 IoT, Pico WBoard_MKR_WiFi1010

A cellular board in short. TinyGSM drives the modem, ESP_SSLClient runs TLS on the board, and the transport takes the time from the modem:

arduino / C++
#define TINY_GSM_MODEM_SIM7080
#include <TinyGsmClient.h>
#include <ESP_SSLClient.h>
#include <HydroNodeClient.h>
#include <HydroNodeCellular.h>
#include <HydroNodeCerts.h>

TinyGsm modem(Serial1);
TinyGsmClient tcp(modem);
ESP_SSLClient tls;
HydroNodeCellular<TinyGsm> lte(modem, tls, HydroNodeNet::LTE_M);
HydroNodeClient hydro(lte, SENSOR_ID, SECRET_KEY);

void setup() {
  tls.setClient(&tcp);
  tls.setCACert(HYDRONODE_CA_BUNDLE);
  lte.onBeforeConnect([](uint32_t now) { tls.setX509Time(now); });
  modem.init();
  modem.waitForNetwork();
  modem.gprsConnect("iot.1nce.net");  // the APN of your SIM
}

void loop() {
  HydroNodeValue reading[] = {{"TEMPERATURE", 14.2}};
  hydro.sendValues(reading, 1);
  delay(300000);
}

The fleet view shows the network of the last request: WiFi, Ethernet, LTE-M, NB-IoT or LTE, with the signal from the modem.

Keep readings and send them later

From library 1.9.0 a HydroNodeBuffer keeps readings until they reach HydroNode. Every reading keeps the time it was measured, so the chart looks the same as if it had been sent right away. It covers two cases:

  1. 1
    Never lose a reading
    When the network is gone, sendValues() stores the round and sends it with the next successful request.
  2. 2
    Send less often
    record() stores without radio, flush() sends many rounds in one request. A cellular board measures every 6 minutes and sends once an hour.
arduino / C++
// Memory that survives deep sleep on the ESP32 (about 100 rounds)
RTC_DATA_ATTR uint8_t mem[4096];
HydroNodeBuffer buffer(mem, sizeof(mem));

void setup() {
  buffer.begin();             // keeps what was stored before the sleep
  hydro.attachBuffer(buffer);

  HydroNodeValue reading[] = {{"TEMPERATURE", 12.3}};
  hydro.record(reading, 1);   // store, no radio

  // Ten readings stored, or the oldest is an hour old: one request for all
  if (hydro.flushDue(10, 3600) && connect()) hydro.flush();

  esp_deep_sleep(6 * 60 * 1000000ULL);
}

Rounds can be up to 7 days old. A round sent twice is stored once. HydroNode learns the sending rhythm from the deliveries, so a board that sends once an hour is not shown as delayed in between. In the device console a late round shows when it was measured. Full examples: Cellular_CollectHourly, Buffer_OfflineSafe and Buffer_DeepSleep. From SIM card to the first stored reading, and why a battery board sends once an hour: ESP32 over LTE-M, NB-IoT and Ethernet.

Captive portal setup via WiFiManager

The example WiFiManagerSetup uses WiFiManager instead of a hardcoded network. On first boot the device opens an access point named HydroNode-Setup-XXXX (the last four characters of your Sensor ID, from hydro.getApName()). Connect to it from a phone or laptop, and a captive portal asks for your network. The device saves it to flash and restarts into normal operation. No password is compiled into the firmware.

All examples are in the library's examples/ folder on GitHub.

Make an outdoor sensor a station

If this board is part of an outdoor weather or environmental installation, it can become a station without changing the sketch or switching to LoRaWAN. A station has a location and can appear on the public map.

  1. 1
    Save the sensor as Outdoor
    On the sensor page open Settings → General, set Environment to Outdoor and save. Indoor sensors cannot be stations.
  2. 2
    Turn on Use as a station
    The switch sits in the Station group of the same section. The Sensor ID, the Secret and every reading stay as they are.
  3. 3
    Set the location
    Location and public map opens the station settings. Enter the location, and turn on Show on the public map only if you want others to see it.
Switching Use as a station off removes the location and the place on the map. The sensor and every reading stay.
The type name, such as TEMPERATURE, is case-sensitive. Always use the exact uppercase key. The library refuses names that are not upper case letters, digits and underscores (-4), and a typo like TEMPERATUR creates a new measurement instead of an error. See every type on the Sensor Types reference.