Devspace Documentation

Complete guide to connecting your ESP32/ESP8266 devices to the Devspace IoT platform.

1
Getting Started

Follow these steps to start using Devspace:

1. Register Account

Create a free account to access the dashboard.

2. Create Device

Login to the dashboard and add a new device. Note down the MQTT Credentials that appear.

2
Install Libraries

Install the following libraries via Arduino Library Manager or PlatformIO:

  • PubSubClient by Nick O'Leary - For MQTT connection
  • ArduinoJson by Benoit Blanchon - For JSON parsing

PlatformIO: Add to platformio.ini

1lib_deps =
2 knolleary/PubSubClient@^2.8
3 bblanchon/ArduinoJson@^6.21.3

3
ESP32 Code Template

Copy the following template code to your Arduino IDE. This template includes WiFi and MQTT reconnection logic.

Important: Don't forget to update the CONFIGURATION section with credentials you got from the dashboard.
main.ino
1#include <WiFi.h>
2#include <PubSubClient.h>
3#include <ArduinoJson.h>
4
5// ============================================================
6// 🔧 CONFIGURATION - CUSTOMIZE WITH YOUR DATA!
7// ============================================================
8
9// WiFi Credentials
10const char* WIFI_SSID = "your-wifi-ssid";
11const char* WIFI_PASSWORD = "your-wifi-password";
12
13// MQTT Broker (Devspace)
14const char* MQTT_BROKER = "mqtt.devspace.id";
15const int MQTT_PORT = 1883;
16
17// MQTT Credentials (from Device Detail Dashboard)
18const char* MQTT_CLIENT_ID = "dev_your-client-id";
19const char* MQTT_USERNAME = "mqtt_your-username";
20const char* MQTT_PASSWORD = "your-mqtt-password";
21
22// Sensor ID (from Sensor List Dashboard)
23const char* SENSOR_ID = "your-sensor-uuid";
24
25// ============================================================
26// 🔌 OBJECTS & VARIABLES
27// ============================================================
28
29WiFiClient espClient;
30PubSubClient mqttClient(espClient);
31
32const int SENSOR_PIN = 34; // Sensor Pin (adjust as needed)
33const long SEND_INTERVAL = 5000; // Send data every 5 seconds
34unsigned long lastSendTime = 0;
35
36// Topics
37String DATA_TOPIC;
38String STATUS_TOPIC;
39
40void setup() {
41 Serial.begin(115200);
42
43 // Setup WiFi
44 WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
45 while (WiFi.status() != WL_CONNECTED) {
46 delay(500);
47 Serial.print(".");
48 }
49 Serial.println("\nWiFi Connected!");
50
51 // Setup MQTT
52 DATA_TOPIC = "devspace/" + String(MQTT_CLIENT_ID) + "/data";
53 STATUS_TOPIC = "devspace/" + String(MQTT_CLIENT_ID) + "/status";
54
55 mqttClient.setServer(MQTT_BROKER, MQTT_PORT);
56}
57
58void reconnect() {
59 while (!mqttClient.connected()) {
60 Serial.print("Connecting to MQTT...");
61 if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD)) {
62 Serial.println("Connected!");
63 mqttClient.publish(STATUS_TOPIC.c_str(), "online");
64 } else {
65 Serial.print("failed, rc=");
66 Serial.print(mqttClient.state());
67 delay(5000);
68 }
69 }
70}
71
72void loop() {
73 if (!mqttClient.connected()) {
74 reconnect();
75 }
76 mqttClient.loop();
77
78 // Send Data Periodically
79 unsigned long now = millis();
80 if (now - lastSendTime > SEND_INTERVAL) {
81 lastSendTime = now;
82
83 // Read Sensor (Example: Random value)
84 float value = random(200, 300) / 10.0;
85
86 // Create JSON Payload
87 StaticJsonDocument<200> doc;
88 doc["sensorId"] = SENSOR_ID;
89 doc["value"] = value;
90
91 char jsonBuffer[200];
92 serializeJson(doc, jsonBuffer);
93
94 // Publish
95 mqttClient.publish(DATA_TOPIC.c_str(), jsonBuffer);
96 Serial.println(jsonBuffer);
97 }
98}

Monitor Serial Output

After uploading, open Serial Monitor (Baud Rate: 115200). You should see output like this if successful:

1Connecting to WiFi...
2WiFi Connected!
3Connecting to MQTT...Connected!
4{"sensorId":"your-uuid","value":25.5}
5{"sensorId":"your-uuid","value":25.6}

MQTT Reference

TopicDirectionDescription
devspace/{clientId}/dataDevice → ServerPublish sensor data
devspace/{clientId}/statusDevice → ServerOnline/Offline status

Payload Format

payload.json
1{
2 "sensorId": "YOUR-SENSOR-UUID",
3 "value": 25.5,
4 "unit": "celsius" // Optional
5}

Troubleshooting

Error: rc=-2 (Connect Failed)

Usually caused by incorrect credentials.

  • Double check MQTT Username & Password
  • Ensure Client ID is correct

Device Online but No Data

  • Ensure sensorId in code matches exactly with the dashboard (UUID)
  • Check if JSON format is valid

Need More Help?

Our support team is ready to help you integrate your IoT devices.