0% found this document useful (0 votes)
38 views6 pages

Outputs - Iot - Exp10

This program connects an ESP8266 device to a WiFi network and MQTT broker. It subscribes to the "device/led" topic and will turn the built-in LED on or off depending on the message payload. The ESP8266 connects to WiFi on startup and reconnects to the MQTT broker if the connection is lost. It then enters a loop to receive and process messages on the subscribed topic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Outputs - Iot - Exp10

This program connects an ESP8266 device to a WiFi network and MQTT broker. It subscribes to the "device/led" topic and will turn the built-in LED on or off depending on the message payload. The ESP8266 connects to WiFi on startup and reconnects to the MQTT broker if the connection is lost. It then enters a loop to receive and process messages on the subscribed topic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAM:

#include <ESP8266WiFi.h>

#include <PubSubClient.h>

const char* ssid = "OnePlus Nord2 5G";

const char* password = "vi3iq4y8";

const char* mqtt_server = "91.121.93.94";

WiFiClient espClient;

PubSubClient client(espClient);

unsigned long lastMsg = 0;

#define MSG_BUFFER_SIZE (50)

char msg[MSG_BUFFER_SIZE];

int value = 0;

void setup_wifi() {

delay(10);

Serial.println();

Serial.print("Connecting to ");

Serial.println(ssid);

WiFi.mode(WIFI_STA);

WiFi.begin(ssid, password);

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

delay(500);

Serial.print("."); }

randomSeed(micros());

Serial.println("");

Serial.println("WiFi connected");

Serial.println("IP address: ");

Serial.println(WiFi.localIP()); }

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

Serial.print("Message arrived [");

Serial.print(topic);

Serial.print("] ");
for (int i = 0; i < length; i++) {

Serial.print((char)payload[i]); }

Serial.println();

if ((char)payload[0] == '1') {

digitalWrite(BUILTIN_LED, LOW); }

else {

digitalWrite(BUILTIN_LED, HIGH); } }

void reconnect() {

while (!client.connected()) {

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

String clientId = "ESP8266Client-";

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

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

Serial.println("connected");

client.subscribe("device/led"); }

else {

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

Serial.print(client.state());

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

delay(5000); } } }

void setup() {

pinMode(BUILTIN_LED, OUTPUT);

Serial.begin(115200);

setup_wifi();

client.setServer(mqtt_server, 1883);

client.setCallback(callback);

Serial.println("Abishekh - 21xh01");

Serial.println("Nilesh - 21xh07");

Serial.println("sriram - 21xh09"); }

void loop() {

if (!client.connected()) {
reconnect(); }

client.loop(); }
OUTPUT:

You might also like