0% found this document useful (0 votes)
17 views4 pages

MQTT Led Control

This document is an Arduino sketch for an ESP8266 microcontroller that connects to WiFi and communicates with an MQTT broker. It subscribes to a topic to receive messages to control a built-in LED, turning it ON or OFF based on the received commands. The code includes functions for setting up WiFi, handling MQTT messages, and maintaining the connection to the broker.

Uploaded by

nitusp156789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

MQTT Led Control

This document is an Arduino sketch for an ESP8266 microcontroller that connects to WiFi and communicates with an MQTT broker. It subscribes to a topic to receive messages to control a built-in LED, turning it ON or OFF based on the received commands. The code includes functions for setting up WiFi, handling MQTT messages, and maintaining the connection to the broker.

Uploaded by

nitusp156789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <ESP8266WiFi.

h>

#include <PubSubClient.h>

// WiFi credentials

const char* ssid = "YOUR WIFI ID";

const char* password = "YOUR WIFI PASSWORD";

// MQTT Broker details

const char* mqtt_server = "test.mosquitto.org"; // public broker

const int mqtt_port = 1883;

const char* mqtt_topic_sub = "nttf/led"; // topic to subscribe

const char* mqtt_topic_pub = "nttf/status"; // topic to publish

WiFiClient espClient;

PubSubClient client(espClient);

// Built-in LED on NodeMCU (D0 is not real LED, use LED_BUILTIN which is GPIO2)

const int ledPin = LED_BUILTIN;

void setup_wifi() {

delay(10);

Serial.println();

Serial.print("Connecting to ");

Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.print(".");

}
Serial.println("");

Serial.println("WiFi connected");

Serial.print("IP address: ");

Serial.println(WiFi.localIP());

void callback(char* topic, byte* payload, unsigned int length) {

Serial.print("Message arrived [");

Serial.print(topic);

Serial.print("] ");

String message;

for (unsigned int i = 0; i < length; i++) {

message += (char)payload[i];

Serial.println(message);

// Control LED based on message

if (message == "ON") {

digitalWrite(ledPin, LOW); // Active LOW LED

client.publish(mqtt_topic_pub, "LED is ON");

} else if (message == "OFF") {

digitalWrite(ledPin, HIGH); // Active LOW LED

client.publish(mqtt_topic_pub, "LED is OFF");

void reconnect() {

// Loop until reconnected

while (!client.connected()) {

Serial.print("Attempting MQTT connection...");


// Create a random client ID

String clientId = "NodeMCUClient-";

clientId += String(random(0xffff), HEX);

// Attempt to connect

if (client.connect(clientId.c_str())) {

Serial.println("connected");

client.subscribe(mqtt_topic_sub); // subscribe to topic

} else {

Serial.print("failed, rc=");

Serial.print(client.state());

Serial.println(" try again in 5 seconds");

delay(5000);

void setup() {

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, HIGH); // OFF at start (because built-in LED is active LOW)

Serial.begin(115200);

setup_wifi();

client.setServer(mqtt_server, mqtt_port);

client.setCallback(callback);

void loop() {

if (!client.connected()) {

reconnect();

}
client.loop();

You might also like