This project demonstrates how to blink an LED on an ESP32 microcontroller. The built-in LED toggles on and off at a regular interval, providing a simple yet effective way to verify that the board is running and responding to code.
The guide includes a complete parts list, wiring diagram, and step-by-step assembly instructions. Readers will also receive the Arduino firmware needed to program the ESP32, along with explanations of how the code controls the LED timing using a timer interrupt.
Wiring diagram

Assemble it in 1 step
1. Connect the board
Plug the NodeMCU ESP8266 into your computer with a USB data cable. No external parts or wires are needed.
Deploy the firmware
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecureBearSSL.h>
#include <PubSubClient.h>
#include <time.h>
#include <Ticker.h>
// Fill these in locally. Do not share your password or token in chat.
constexpr char wifiName[] = "Miji";
constexpr char wifiPassword[] = "12345678";
constexpr char deviceToken[] = "4apv0VpUQdIALhglWuYF7CzU9NcpgqdG";
constexpr char deviceId[] = "talhah_device@mijimy81"; // Favoriot Device Developer ID
#include "favoriot_root_ca.h"
BearSSL::WiFiClientSecure transport;
PubSubClient mqtt(transport);
String topic;
char clientId[32];
bool configured;
uint32_t lastRetry, lastSend;
Ticker ledTicker;
void toggleLed() {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
ledTicker.attach_ms(500, toggleLed);
Serial.begin(115200);
configured = wifiName[0] && deviceToken[0] && deviceId[0] && brokerRootCA[0];
if (!configured) {
Serial.println("Fill in Wi-Fi and Favoriot device details in src/main.cpp; root CA is in src/favoriot_root_ca.h.");
return;
}
topic = String(deviceToken) + "/v2/streams";
snprintf(clientId, sizeof(clientId), "nodemcu-%06lx", (unsigned long)ESP.getChipId());
static BearSSL::X509List trustAnchor(brokerRootCA);
transport.setTrustAnchors(&trustAnchor);
randomSeed(ESP.getCycleCount());
mqtt.setServer("mqtt.favoriot.com", 8883);
mqtt.setSocketTimeout(5);
if (!mqtt.setBufferSize(2048)) {
configured = false;
Serial.println("Could not allocate MQTT buffer.");
return;
}
WiFi.mode(WIFI_STA);
WiFi.setAutoReconnect(true);
WiFi.begin(wifiName, wifiPassword);
configTime(0, 0, "pool.ntp.org", "time.google.com");
}
void loop() {
const uint32_t now = millis();
delay(5);
if (!configured || WiFi.status() != WL_CONNECTED || time(nullptr) < 1700000000) return;
if (!mqtt.connected()) {
if (now - lastRetry < 10000) return;
if (mqtt.connect(clientId, deviceToken, deviceToken)) {
lastSend = millis() - 1000;
Serial.println("Connected to Favoriot MQTT.");
} else {
Serial.printf("MQTT connection failed (%d); will retry.\n", mqtt.state());
}
lastRetry = millis();
return;
}
if (!mqtt.loop() || now - lastSend < 1000) return;
lastSend = now;
// Simulated values; no electrical measurements are taken.
const double maximumDemand = random(10, 101) / 10.0;
const double powerConsumption = random(1, 501) / 100.0;
const double voltage = random(2200, 2411) / 10.0;
const double temperature = random(200, 351) / 10.0;
const double humidity = random(400, 811) / 10.0;
const double current = random(0, 451) / 10.0;
char payload[512];
const int length = snprintf(payload, sizeof(payload),
"{\"device_developer_id\":\"%s\",\"data\":{\"maximum_demand_kwt\":\"%.1f\",\"power_consumption_kwh\":\"%.2f\",\"voltage_v\":\"%.2f\",\"temperature_c\":\"%.1f\",\"humidity_pct\":\"%.1f\",\"current_a\":\"%.1f\"}}",
deviceId, maximumDemand, powerConsumption, voltage, temperature, humidity, current);
if (length > 0 && length < (int)sizeof(payload) && mqtt.publish(topic.c_str(), payload)) {
Serial.printf("Sent sample: %.1f kWT, %.2f kWh, %.2f V, %.1f C, %.1f%% RH, %.1f A\n", maximumDemand, powerConsumption, voltage, temperature, humidity, current);
} else {
Serial.println("MQTT publish failed.");
}
}Remix this project
Make it yours in one click
Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.




