Skip to content

ESP32 Setup

Connect your ESP32 microcontroller to SiliconWit.IO using MQTT.

  • ESP32 development board
  • Arduino IDE or PlatformIO
  • WiFi network
  • SiliconWit.IO account with a registered device
  1. Open Arduino IDE
  2. Go to File → Preferences
  3. Add to “Additional Board Manager URLs”:
    https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
  4. Go to Tools → Board → Boards Manager
  5. Search for “esp32” and install

Install these libraries via Sketch → Include Library → Manage Libraries:

  • PubSubClient - MQTT client library
  • ArduinoJson - JSON parsing (optional but recommended)
#include <WiFi.h>
#include <PubSubClient.h>
// WiFi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// SiliconWit.IO MQTT settings
const char* mqtt_server = "mqtt.siliconwit.io";
const int mqtt_port = 1883;
const char* device_id = "YOUR_DEVICE_ID";
const char* access_token = "YOUR_ACCESS_TOKEN";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect(device_id, device_id, access_token)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" retrying in 5 seconds");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Send data every 10 seconds
static unsigned long lastMsg = 0;
if (millis() - lastMsg > 10000) {
lastMsg = millis();
// Create JSON payload
String payload = "{\"temperature\":25.5,\"humidity\":60}";
// Publish to telemetry topic
String topic = "devices/" + String(device_id) + "/telemetry";
client.publish(topic.c_str(), payload.c_str());
Serial.println("Data sent: " + payload);
}
}

For production, use TLS encryption:

#include <WiFiClientSecure.h>
WiFiClientSecure espClient;
PubSubClient client(espClient);
void setup() {
// ... WiFi setup ...
espClient.setInsecure(); // For testing only
// For production, use: espClient.setCACert(root_ca);
client.setServer(mqtt_server, 8883); // TLS port
}
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// ... WiFi and MQTT setup ...
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
static unsigned long lastMsg = 0;
if (millis() - lastMsg > 30000) {
lastMsg = millis();
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if (!isnan(temp) && !isnan(hum)) {
String payload = "{\"temperature\":" + String(temp) +
",\"humidity\":" + String(hum) + "}";
String topic = "devices/" + String(device_id) + "/telemetry";
client.publish(topic.c_str(), payload.c_str());
}
}
}
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 300 // 5 minutes
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
if (client.connect(device_id, device_id, access_token)) {
// Read and send data
String payload = "{\"temperature\":25.5}";
String topic = "devices/" + String(device_id) + "/telemetry";
client.publish(topic.c_str(), payload.c_str());
delay(100); // Allow message to send
client.disconnect();
}
// Go to sleep
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
void loop() {
// Never reached
}
ProblemSolution
WiFi won’t connectCheck SSID/password, ensure 2.4GHz network
MQTT connection failsVerify device ID and access token
Messages not appearingCheck topic format matches dashboard

Enable verbose logging:

Serial.setDebugOutput(true);