Arduino library for connecting ESP32 devices to the Devspace IoT platform over MQTT.
This repository is the working copy for the public devspace-arduino library.
It is intentionally small for v0.1: connect WiFi, connect MQTT with Devspace
device credentials, publish device status, and send sensor readings.
- ESP32 with the Arduino framework
Devspace itself is MQTT-based, so other boards can connect through the raw MQTT contract. This official Arduino library v0.1 is currently tested with ESP32. ESP8266 support is intentionally outside the v0.1 library scope.
Install the Arduino board package that matches your hardware.
For the current ESP32_DHT11 example, install:
esp32by Espressif Systems
Install these libraries from Arduino Library Manager:
PubSubClientby Nick O'LearyArduinoJsonby Benoit Blanchon
The ESP32_DHT11 example also needs:
DHT sensor libraryby AdafruitAdafruit Unified Sensor
When installing this library from a GitHub ZIP, Arduino IDE may not install dependencies automatically. Install the dependencies above manually before compiling the examples.
During development, copy or symlink this folder into your Arduino libraries directory:
~/Documents/Arduino/libraries/DevspaceThe folder should contain library.properties, src/, and examples/ at the
top level.
Create a device and sensors in Devspace first, then copy the generated MQTT credentials and sensor IDs into an example sketch.
#include <Devspace.h>
Devspace devspace;
const char* WIFI_SSID = "your-wifi";
const char* WIFI_PASSWORD = "your-password";
const char* MQTT_BROKER = "mqtt.devspace.id";
const uint16_t MQTT_PORT = 1883;
const char* MQTT_CLIENT_ID = "devspace_xxxxxxxxxxxx";
const char* MQTT_USERNAME = "mqtt_xxxxxxxxxxxx";
const char* MQTT_PASSWORD = "generated-password";
const char* SENSOR_ID = "sensor-uuid-from-dashboard";
unsigned long lastSend = 0;
void setup() {
Serial.begin(115200);
devspace.begin(
WIFI_SSID,
WIFI_PASSWORD,
MQTT_BROKER,
MQTT_PORT,
MQTT_CLIENT_ID,
MQTT_USERNAME,
MQTT_PASSWORD
);
}
void loop() {
devspace.loop();
if (devspace.connected() && millis() - lastSend >= 5000) {
lastSend = millis();
devspace.sendData(SENSOR_ID, 25.5);
}
}For production Devspace, use the same MQTT host shown in the dashboard:
const char* MQTT_BROKER = "mqtt.devspace.id";
const uint16_t MQTT_PORT = 1883;For local Devspace, replace MQTT_BROKER with your laptop/server LAN IP, for
example:
const char* MQTT_BROKER = "192.168.1.100";
const uint16_t MQTT_PORT = 1883;Do not use localhost from ESP32 firmware. On the ESP32, localhost means the
ESP32 itself.
bool begin(
const char* wifiSSID,
const char* wifiPassword,
const char* mqttBroker,
uint16_t mqttPort,
const char* mqttClientId,
const char* mqttUsername,
const char* mqttPassword
);
void loop();
bool connected();
bool sendData(const char* sensorId, double value);
bool sendData(const char* sensorId, double value, const char* timestamp);
bool publishStatus(const char* status);
void setDebug(bool enabled);
void setReconnectInterval(unsigned long intervalMs);
void setWiFiConnectTimeout(unsigned long timeoutMs);
int mqttState();The library builds Devspace topics from mqttClientId:
- Data:
devspace/{mqttClientId}/data - Status:
devspace/{mqttClientId}/status
Sensor data is published with retained = false. Status messages are published
with retained = true, and the MQTT Last Will message is set to offline.
See docs/mqtt-contract.md for the raw MQTT contract.
BasicPublish: sends a dummy value every 5 seconds.ESP32_DHT11: reads DHT11 temperature and humidity, then sends each reading as a separate Devspace sensor data message.
This library uses SemVer. Public API changes should be reflected in
CHANGELOG.md.