IoT Project for Energy Monitoring: Setup and Example
An IoT project for energy monitoring uses
energy sensors connected to a microcontroller that sends data via MQTT protocol to a server or cloud. This data can be visualized on a dashboard to track energy consumption in real time.Syntax
This project uses the following components:
- Energy Sensor: Measures voltage and current.
- Microcontroller: Reads sensor data and sends it.
- MQTT Protocol: Lightweight messaging to send data.
- Broker: Receives and forwards messages.
- Dashboard: Displays energy usage data.
Basic MQTT publish syntax on microcontroller:
client.publish(topic, message)
Where topic is the channel name and message is the sensor data in JSON format.
plaintext
client.publish('home/energy', '{"voltage":230,"current":5}')
Example
This example shows an ESP32 microcontroller reading a simulated energy sensor and sending data to an MQTT broker.
cpp
#include <WiFi.h> #include <PubSubClient.h> const char* ssid = "yourSSID"; const char* password = "yourPassword"; const char* mqtt_server = "broker.hivemq.com"; WiFiClient espClient; PubSubClient client(espClient); void setup_wifi() { delay(10); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } } void reconnect() { while (!client.connected()) { if (client.connect("ESP32Client")) { // Connected } else { delay(5000); } } } void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); float voltage = 230.0; // Simulated voltage float current = 5.0; // Simulated current char msg[50]; snprintf(msg, 50, "{\"voltage\":%.1f,\"current\":%.1f}", voltage, current); client.publish("home/energy", msg); delay(5000); }
Output
Data published every 5 seconds to topic 'home/energy' with JSON payload {"voltage":230.0,"current":5.0}
Common Pitfalls
Common mistakes include:
- Not connecting to WiFi before MQTT connection.
- Using incorrect MQTT broker address or port.
- Not formatting JSON data properly, causing parsing errors.
- Not handling MQTT reconnects, leading to lost data.
Always check network and broker status before publishing.
cpp
/* Wrong: Publishing before WiFi connected */ client.publish("home/energy", "data"); /* Right: Ensure WiFi and MQTT connected before publishing */ if (WiFi.status() == WL_CONNECTED && client.connected()) { client.publish("home/energy", "data"); }
Quick Reference
- MQTT Broker: Use public brokers like broker.hivemq.com or set up your own.
- Sensor Data: Format as JSON for easy parsing.
- Reconnect Logic: Always implement reconnect loops for WiFi and MQTT.
- Dashboard: Use tools like Node-RED or Grafana to visualize data.
Key Takeaways
Use MQTT protocol to send energy sensor data from microcontroller to server.
Format sensor readings as JSON for easy processing and visualization.
Ensure stable WiFi and MQTT connections with reconnect logic.
Visualize energy data on dashboards like Node-RED or Grafana for real-time monitoring.