0% found this document useful (0 votes)
13 views

5

Uploaded by

crce.9502.ecs
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)
13 views

5

Uploaded by

crce.9502.ecs
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/ 13

Fr.

CONCEICAO RODRIGUES COLLEGE OF ENGINEERING


( FrCRCE)
Department of Electronics and Computer Science (ECS)

Marking scheme (Rubrics)

Timeline (3) Depth of Completeness Total Signature


Understanding (4) (3)

Academic year 2024 – 2025 Estimated Time 02 Hours


Course B.E. (ECS) Subject Name Internet of Things
Semester VII Chapter Title Interfacing to Flex sensor
Experiment Type Coding Subject Code ECL 702

ESP8266 Voice Control with Google Assistant and Adafruit IO


Theory:

Adafruit IO is an IOT platform built around the Message Queue Telemetry Transport (MQTT)
Protocol. MQTT is a lightweight protocol that allows multiple devices to connect to a shared
server, called the MQTT Broker, and subscribe or write to user-defined topics. When a device is
subscribed to a topic, the broker will send it a notification whenever that topic changes. MQTT is
best suited for applications with low data rates, strict power constraints, or slow Internet
connections.
In addition to providing the MQTT Broker service, Adafruit IO also allows you to set up
dashboards that let you directly manipulate or view the current value of each topic. Since it can
be accessed from a web browser, it makes it the ideal hub for monitoring and controlling all of
your various IOT projects.
Connecting the ESP8266 to the Adafruit IO system is relatively straightforward. Before you get
started with the code, you'll need to install the Adafruit MQTT Client library, which can be found
Fr. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
( FrCRCE)
Department of Electronics and Computer Science (ECS)

under the Arduino Library Manager (Sketch > Include Library > Library Manager...). Even
though this library is produced and maintained by Adafruit, it can be used to connect to any
MQTT server.

Further, we connect our Google Assistant to the Adafruit IO MQTT Broker to allow us to control
the lights with voice commands. To do this, we'll use the IFTTT (If This Then That) platform,
which allows hundreds of different services to trigger actions in a variety of other services.

Post-Lab questions:

1. Explain the three libraries and their functions that are included at the beginning of the
program.
2. Explain the MQTT connect function.
Fr. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
( FrCRCE)
Department of Electronics and Computer Science (ECS)

IoT Lab 5:
Name: Saaqib Shaikh Class: BE ECS Roll number: 9502

Code used in Arduino IDE:

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define led1 D1
#define led2 D2
#define WLAN_SSID "Hotspot-Shady" // Your SSID
#define WLAN_PASS "shady1357" // Your password

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER "io.adafruit.com" //Adafruit Server


#define AIO_SERVERPORT 1883
#define AIO_USERNAME "Saaqib " // Username
#define AIO_KEY “aio_nmce07GOIOhXY4vBa8cHPW6QDFLX" //
Auth Key
Fr. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
( FrCRCE)
Department of Electronics and Computer Science (ECS)

//WIFI CLIENT
WiFiClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT,


AIO_USERNAME, AIO_KEY);

Adafruit_MQTT_Subscribe Light1 = Adafruit_MQTT_Subscribe(&mqtt,


AIO_USERNAME"/feeds/greenled"); // Feeds name should be same everywhere
Adafruit_MQTT_Subscribe Light2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME
"/feeds/redled");

void MQTT_connect();

void setup() {
Serial.begin(115200);

pinMode(led1, OUTPUT);
// pinMode(led2, OUTPUT);
// 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.println("IP address: ");
Serial.println(WiFi.localIP());

mqtt.subscribe(&Light1);
// mqtt.subscribe(&Light2);

}
Fr. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
( FrCRCE)
Department of Electronics and Computer Science (ECS)
void loop() {

MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(20000))) {
if (subscription == &Light1) {

Serial.print(F("Got: "));
Serial.println((char *)Light1.lastread);
int Light1_State = atoi((char *)Light1.lastread);
digitalWrite(led1, Light1_State);

}
/* if (subscription == &Light2) {
Serial.print(F("Got: "));
Serial.println((char *)Light2.lastread);
int Light2_State = atoi((char *)Light2.lastread);
digitalWrite(led2, Light2_State);
}*/
}
}
void MQTT_connect() {
int8_t ret;

if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");

uint8_t retries = 3;

while ((ret = mqtt.connect()) != 0) {


Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000);
retries--;
if (retries == 0) {
while (1);
}
Fr. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
( FrCRCE)
Department of Electronics and Computer Science (ECS)

}
Serial.println("MQTT Connected!");

Output:

Dashboard
Fr. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
( FrCRCE)
Department of Electronics and Computer Science (ECS)
Circuit Board Created
Fr. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
( FrCRCE)
Department of Electronics and Computer Science (ECS)

Led Glows on Voice Command: Activate Led On


Fr. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
( FrCRCE)
Department of Electronics and Computer Science (ECS)

Led Glows off Voice Command: Activate Led Off


POSTLAB

You might also like