5. OTA-Enabled Field Device
Publish business data together with firmware version, Wi-Fi signal, uptime, and free memory. Telemetry runs on its own schedule while PxServ OTA checks remain available.
Level: Advanced · Time: 60 minutes
Requirements
- ESP32 and DHT22: VCC→3V3, GND→GND, DATA→GPIO4
- PxServ Arduino
1.3.0+ - DHT sensor library and Adafruit Unified Sensor
- An OTA-capable partition layout with enough application space
Complete sketch
#include <PxServ.h>
#include <DHT.h>
#include <WiFi.h>
const char* WIFI_SSID = "your_wifi_ssid";
const char* WIFI_PASSWORD = "your_wifi_password";
const char* PXSERV_API_KEY = "your_pxserv_api_key";
const char* FIRMWARE_VERSION = "1.0.0";
const int DHT_PIN = 4;
const unsigned long TELEMETRY_INTERVAL_MS = 30000;
PxServ client(PXSERV_API_KEY);
DHT dht(DHT_PIN, DHT22);
unsigned long lastTelemetryAt = 0;
void publishHealth() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
bool sensorValid = !isnan(temperature) && !isnan(humidity);
if (sensorValid) {
client.setData("field/temperature", String(temperature, 1));
client.setData("field/humidity", String(humidity, 1));
}
client.setData("field/sensor-status", sensorValid ? "normal" : "error");
client.setData("field/firmware", client.getDeviceFirmwareVersion());
client.setData("field/uptime-seconds", String(millis() / 1000));
client.setData("field/free-heap-bytes", String(ESP.getFreeHeap()));
client.setData("field/wifi-rssi-dbm", String(WiFi.RSSI()));
client.setData("field/device-status", "online");
}
void setup() {
Serial.begin(115200);
dht.begin();
PxServ::connectWifi(WIFI_SSID, WIFI_PASSWORD);
client.setDeviceFirmwareVersion(FIRMWARE_VERSION);
publishHealth();
}
void loop() {
unsigned long now = millis();
if (now - lastTelemetryAt >= TELEMETRY_INTERVAL_MS) {
lastTelemetryAt = now;
publishHealth();
}
client.checkOtaFirmware();
delay(20);
}Update workflow
- Upload OTA-capable version
1.0.0by USB and verify health keys. - Make a visible change, set
FIRMWARE_VERSIONto1.0.1, and retain all OTA calls. - Export the main compiled
.binthrough Arduino IDE. - Test the PxServ OTA flow first on a backup device with stable power and Wi-Fi.
- Confirm the new firmware value and reset uptime after reboot.
See OTA Feature for the complete panel workflow.
Security note
PxServ Arduino 1.3.0 source uses setInsecure() for its HTTPS clients, and the example flow does not show firmware-signature verification. For security-sensitive or commercial deployments, assess the threat model and add signed firmware, integrity verification, staged rollout, rollback, and a physical recovery path.
You have completed all 25 projects. Return to the Arduino Library reference for API details.
Last updated