Sunday, 13 July 2025

IOT LAB EXP 4.2 PROGRAM

 #include <ESP8266WiFi.h>

 #include "Adafruit_MQTT.h"
 #include "Adafruit_MQTT_Client.h"
 #define WLAN_SSID "ECE_STAFF_ROOM"
  #define WLAN_PASS "specnet@23"
 #define AIO_SERVER "io.adafruit.com"
 #define AIO_SERVERPORT 1883
 #define AIO_USERNAME "ChanduPrasanth"
 #define AIO_KEY "aio_WIaH72ooBeG8RlOfFJxF3a1DRGXV"
 // Create an ESP8266 WiFiClient class to connect to the MQTT server.
 WiFiClient client;
 // Setup the MQTT client class by passing in the WiFi client and
 // MQTT server and login details.
 Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
 // Setup a feed called iot-lab-exp04-attendance for publishing.
 Adafruit_MQTT_Publish attendanceFeed = Adafruit_MQTT_Publish(&mqtt,
 "ChanduPrasanth/feeds/iot attendance");
 void connectMQTT() {
 Serial.print("Connecting to Adafruit IO...");
 while (mqtt.connected() == false) {
 if (mqtt.connect()) {
 Serial.println("Connected to Adafruit IO !");
 } else {
 Serial.println("Failed to connect to Adafruit IO, retrying in 5s...");
 delay(5000);
 }
 }
 }
 void setup() {
 Serial.begin(115200);
 delay(10);
 Serial.println("Adafruit MQTT demo");
 // Connect to WiFi access point.
 Serial.println(); Serial.println();
 Serial.print("Connecting to ");
 Serial.println(WLAN_SSID);
 WiFi.begin(WLAN_SSID, WLAN_PASS);
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println();
 Serial.println("WiFi connected");
 Serial.print("IP address: "); Serial.println(WiFi.localIP());
 connectMQTT();
 }
 
 void loop() {
 // Now we can publish stuff!
 Serial.print("Sending Attendance ");
 Serial.print("...");
 if (mqtt.connected()){
 // Replace your 10-Digit roll number here
 if (! attendanceFeed.publish("22261A0400")) {
 Serial.println("Failed");
 } else {
 Serial.println("OK!");
 }
 }
 delay(5000);
 }

No comments:

Post a Comment