5
5
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
#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
//WIFI CLIENT
WiFiClient client;
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;
}
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)