Introduction
The world is moving towards the automation. Development of the Internet and its wide availability has made day-to-day products getting connected to the Internet.
Similarly, we can transform the classroom into the smart
classroom not only by making it as a safer place to study but also controlling the facilities inside the
classroom by smart phone. The motivation for this project is to make something for betterment for the University, which could be managed implemented easily. Therefore, we finalised to make the smart classroom. This concept can be
easily implemented in all the classrooms in the university.
Objective
The main objective of this project is to design a prototype of an IoT based Smart Classroom. Here, we will simulate the control of classroom lights, Projector, Temperature of central heating system and the door lock system using Google assistant. In this project, commands given to the Google Assistant will be analysed by IFTTT platform. IFTTT platform sends appropriated message to the Adafruit IO cloud.
Prior to having controls of all the classroom appliances, we need to ensure that the teachers/students, who wants to get access, should be in the vicinity of the building. The door locking system is based on the GPS location of the smart phone. This system will be highly secured because it will unlock the door only for teachers/students, who have access to classroom. Once the phone is near the classroom, it will send the command to controller to unlock the door lock. The IFTTT is used to integrated google maps to detect the location and send data to Adafruit IO cloud. If this system fails, then we can unlock the door from the command given from the secured App. All the appliances in the classroom will be connected to ESP-12E. ESP-12E will obtain command messages from Adafruit IO using MQTT protocol.
The voice commands can be used for the automated classroom lights and the automated projector. The phone will use the google voice assistance (Google Assistant) to listen the commands and those will be forwarded to controller to take the necessary actions. These commands could be enabled only if the phone is near the classroom. Moreover, the centralised temperature control will also be controlled through voice commands. The fire sensor is also mounted in the classroom to notify the user through not only buzzer but also through MQTT mobile notification.
Prior to having controls of all the classroom appliances, we need to ensure that the teachers/students, who wants to get access, should be in the vicinity of the building. The door locking system is based on the GPS location of the smart phone. This system will be highly secured because it will unlock the door only for teachers/students, who have access to classroom. Once the phone is near the classroom, it will send the command to controller to unlock the door lock. The IFTTT is used to integrated google maps to detect the location and send data to Adafruit IO cloud. If this system fails, then we can unlock the door from the command given from the secured App. All the appliances in the classroom will be connected to ESP-12E. ESP-12E will obtain command messages from Adafruit IO using MQTT protocol.
The voice commands can be used for the automated classroom lights and the automated projector. The phone will use the google voice assistance (Google Assistant) to listen the commands and those will be forwarded to controller to take the necessary actions. These commands could be enabled only if the phone is near the classroom. Moreover, the centralised temperature control will also be controlled through voice commands. The fire sensor is also mounted in the classroom to notify the user through not only buzzer but also through MQTT mobile notification.
Design Steps
#1 System Architecture
Above figure shows the system level architecture of the project.
#2 Hardware design
We will require following components:
1. ESP8266(ESP-12E)
2. OLED Display
3. LEDS
4. Micro Servo
5. MQ-2 Gas/Smoke Sensor
6. Buzzer
7. Connecting Wires
Components are connected to ESP8266 board in the following configurations.
LED(Classroom Light) -> D7
LED (Projector) ->D3
Servo :
Data(Yellow) -> Do
Vcc(Red)-> 5V(VU pin of the ESP8266)
Ground(Gray)-> Gnd
MQ-2 Sensor:
Vcc -> 5V(VU pin of the ESP8266)
Do -> D6
Gnd -> Gnd
OLED Display:
Vcc -> 5V(VU pin of ESP8266)
SDA -> D1
SCL -> D2
Gnd -> Gnd
Buzzer -> D5
#3 Software and Platforms
Following platforms will be used.
1. IFTT
2. Google Assistant
3. Adafruit IO
4. MQTT Protocol
5. Arduino Programming
IFTT(If This Then That) is a free web based service to create chains of simple conditional statements called Applets. An applet can be triggered by the changes that occur within other widely available web services(in our case Google Assistant) and Applet can perform the specified action(Sending command data to the cloud).
Adafruit IO is a cloud platform developed by Adafruit Inc. It cab be used to visualize data obtained from different web services and sources and retrieve data as needed using a suitable protocol as needed. Here, we will be using MQTT protocol to read data from Adafruit IO and send it to ESP8266.
MQTT protocol is a publish/Subscribe based Messaging protocol. It used message topics to deliver the message to the specified clients. An MQTT server called a Broker will filter the message on the basis of topic. Here, Adafruit IO is the MQTT broker which receive command messages from IFTTT platform and sends messages to ESP8266 based on the subscribed topics.
Creating data feeds on Adafruit IO
In order to get access to Adafruit IO, we need to create an account. Click here to sign up to Adafruit IO.
After signing into Adafruit IO, next step is to create feeds for each appliance. Click on Feeds tab in the top left corner to open feed window. Under Actions tab, click Create a New Feed to create feeds for each appliances. Name of each feed will act as Topic for sending command data to ESP8266 using MQTT protocol.
Creating IFTTT Applets
Firstly, we need to install IFTTT mobile application in our smart phone. After installing IFTTT application,we are ready to create our applets.
Click on My Applets bottom right corner which shows all the set up applets. Click on '+' tab at top right corner to open a New Applet window.
Now, click on "this" to create a trigger. From the list of trigger services, choose Google Assistant and input the command phrases .Also, input the phrase that you want google assistant to say in return. Click on "Create trigger" to complete the trigger.
Now, in order to create a action, click on "that" and select Adafruit from the list of action services.
After selection Adafruit as action service, select feed to send the data to, and enter the command data that is intended to be sent to the Adafruit IO cloud. Click on "Create Action" button to complete the action.
Repeat the steps to create applets for each components/appliances. Similarly, create an applet for location detection using "Location" trigger service and send data to Adafruit IO cloud.
The command from the Google assistant could be as follows:
After creating applets on IFTTT and setting up Adafruit IO, we need to write code for ESP8266 to obtain command data from Adafruit IO feeds using MQTT protocol. We will be writing code on Arduino IDE using Arduino programming.
In order to program ESP8266 using Arduino IDE, we need to install driver for ESP8266 on Arduino IDE. Follow the instructions below to setup ESP8266 on Arduino IDE.
- Download Arduino IDE.
- Open IDE and click on "File -> Preferences".
- In "Aditional Boards Manager URLs" add this line and click on "OK":
- Go to "Tools -> Board -> Boards Manager", type "ESP8266" and install it.
- Go again to "Tools -> Board" and select "NodeMCU 1.0 (ESP-12E Module)".
After setting up ESP8266 for Arduino IDE, we are ready to write our program. Click here to get complete code of the project.
In our code we can see that, we have included two external libraries, Adafruit MQTT libraray and OLED library. To install MQTT and OLED library follow the steps as follows:
- Open Arduino IDE and click on "Sketch -> Include Library -> Manage Libraries.."
- Search for "Adafruit MQTT Library" and install the latest version.
- Search for " ESP8266 and ESP32 Oled Driver for SSD1306 display" and install the latest version.
Program Flow Chart
First step is to check whether the user is in the vicinity of Building. If person is detected to be in the building, "entered" message is sent from IFTTT to Adafruit cloud. In the program, a variable "locationFlag" is set to 1 when "entered" message is received. When locationFlag=1, user can now access the door. User can give command to Google Assistant to unlock the door. It is possible to control lights, projector, and Central temperature of the classroom only when the door is unlocked; Hence, in the program we define a variable "doorFlag" which will be set to 1 when door is unlocked. Commands for controlling other appliances in the classroom will be executed only when doorFlag is set to 1. Now, users can give command to Google Assistant to control the Appliances in the classroom.
Problem encountered during execution:
The ESP 8266 was crashing randomly: The cause for this problem was hardware reset due to stack overflow of the controller. To solve this problem, use latest version of the Arduino and libraries. However, if this problem still exists, then refer here for the further support.
Existing Weakness:
- Accuracy issue of the google map: If the google maps gives false values, then the unlocking mechanism will not work properly. In this case, you need to change the 'Location' feed in the Adafruit IO manually, so that your rest of the project works fine.
- The google assistance do not get any feedback from the micro controller that the specific action has been taken.
Future Development Ideas
The project will be further used to automate the attendance system & analysing the not only attendance but also power management of the entire building. The students needs to be physically present in the classroom to get the attendance. Thus, Bluetooth could be the best option to implement the automated attendance system because of its shorter operation range. The exact duration for which the lights, projectors, centralised temperature controls for individual classrooms could be stored. Thus, the data could be analysed to take necessary actions if required to conserve the power.
The Code and Prototype Testing
Have a look at testing video of prototype here.
The Program Code:
Your Code is here
#include "Wire.h"
#include "SSD1306Wire.h"
#include "ESP8266WiFi.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Servo.h"
#define WLAN_SSID "OnePlus 6"
#define WLAN_PASS "1234567890"
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "poudelsurya"
#define AIO_KEY "cbf746c3283e4eee90ed221bb1ca40a5"
#define roomLed D7
#define projectorLed D3
#define doorServo D0
#define smokeDetector D6
#define buzzer D5
int doorFlag=0;
int locationFlag=0;
SSD1306Wire display(0x3c, D1, D2);
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish smokedetect = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/smokedetect");
Adafruit_MQTT_Subscribe classroomlight = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/classroomlight");
Adafruit_MQTT_Subscribe Projector = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Projector");
Adafruit_MQTT_Subscribe Temperature= Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Temperature");
Adafruit_MQTT_Subscribe doorswitch= Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/doorswitch");
Adafruit_MQTT_Subscribe location= Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/location");
void MQTT_connect();
Servo myServo;
void setup()
{
pinMode(roomLed, OUTPUT);
pinMode(projectorLed,OUTPUT);
pinMode(smokeDetector, INPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(115200);
display.init();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.flipScreenVertically();
display.setFont(ArialMT_Plain_16);
display.drawString(0,0,"Temperature:");
display.setFont(ArialMT_Plain_24);
display.drawString(0,32,"19 °C");
display.display();
myServo.attach(doorServo);
delay(10);
Serial.println(F("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.println("IP address: ");
Serial.println(WiFi.localIP());
// Setup MQTT subscription for classroomlightfeed
mqtt.subscribe(&doorswitch);
mqtt.subscribe(&classroomlight);
mqtt.subscribe(&Projector);
mqtt.subscribe(&Temperature);
mqtt.subscribe(&location);
}
int32_t x=0;
void loop()
{
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000)))
{
if (subscription == &location)
{
Serial.print(F("Got:"));
Serial.println((char*)location.lastread);
if(!strcmp((char*)location.lastread, "entered"))
locationFlag=1;
if (!strcmp((char*)location.lastread, "exit"))
locationFlag=0;
}
if(locationFlag==1)
{
if (subscription == &doorswitch)
{
Serial.print(F("Got: "));
Serial.println((char*)doorswitch.lastread);
if(!strcmp((char*) doorswitch.lastread, "doorunlock"))
{
myServo.write(180);
doorFlag=1;
display.setFont(ArialMT_Plain_16);
display.drawString(0,0,"Temperature:");
display.setFont(ArialMT_Plain_24);
display.drawString(0,32,"19 °C");
display.display();
}
else if(!strcmp((char*)doorswitch.lastread, "doorlock"))
{
myServo.write(90);
doorFlag=0;
digitalWrite(projectorLed,LOW);
digitalWrite(roomLed, LOW);
display.clear();
display.display();
}
}
}
if (subscription == &classroomlight)
{
Serial.print(F("Got: "));
if(doorFlag==1)
{
Serial.println((char*)classroomlight.lastread);
if(!strcmp((char*) classroomlight.lastread, "roomledon"))
digitalWrite(roomLed, HIGH);
if(!strcmp((char*) classroomlight.lastread, "roomledoff"))
digitalWrite(roomLed, LOW);
}
}
if (subscription == &Projector)
{
Serial.print(F("Got: "));
if(doorFlag==1)
{
Serial.println((char*)Projector.lastread);
if(!strcmp((char*) Projector.lastread, "projectoron"))
digitalWrite(projectorLed, HIGH);
if(!strcmp((char*) Projector.lastread, "projectoroff"))
digitalWrite(projectorLed, LOW);
}
}
if (subscription == &Temperature)
{
if(doorFlag==1)
{
Serial.print(F("Got: "));
String temp= String((char*)Temperature.lastread);
Serial.println((char*)Temperature.lastread);
int tem= temp.toInt();
if(tem<30)
{
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawString(0,0, "Temperature:");
display.setFont(ArialMT_Plain_24);
display.drawString(0,32,(char*)Temperature.lastread);
display.drawString(64,32,"°C");
display.display();
}
else
{
display.clear();
display.setFont(ArialMT_Plain_24);
display.drawString(0,0,"Too Hot!!");
display.display();
}
}
}
}
if( digitalRead(smokeDetector)== LOW)
{
smokedetect.publish("Smoke Detected!!! RUN RUN");
digitalWrite(buzzer, HIGH);
delay(5000);
}
else
digitalWrite(buzzer, LOW);
}
void MQTT_connect()
{
int8_t ret;
// Stop if already connected.
if (mqtt.connected())
{
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0)
{
// connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0)
{
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
#include "Wire.h"
#include "SSD1306Wire.h"
#include "ESP8266WiFi.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Servo.h"
#define WLAN_SSID "OnePlus 6"
#define WLAN_PASS "1234567890"
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "poudelsurya"
#define AIO_KEY "cbf746c3283e4eee90ed221bb1ca40a5"
#define roomLed D7
#define projectorLed D3
#define doorServo D0
#define smokeDetector D6
#define buzzer D5
int doorFlag=0;
int locationFlag=0;
SSD1306Wire display(0x3c, D1, D2);
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish smokedetect = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/smokedetect");
Adafruit_MQTT_Subscribe classroomlight = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/classroomlight");
Adafruit_MQTT_Subscribe Projector = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Projector");
Adafruit_MQTT_Subscribe Temperature= Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Temperature");
Adafruit_MQTT_Subscribe doorswitch= Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/doorswitch");
Adafruit_MQTT_Subscribe location= Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/location");
void MQTT_connect();
Servo myServo;
void setup()
{
pinMode(roomLed, OUTPUT);
pinMode(projectorLed,OUTPUT);
pinMode(smokeDetector, INPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(115200);
display.init();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.flipScreenVertically();
display.setFont(ArialMT_Plain_16);
display.drawString(0,0,"Temperature:");
display.setFont(ArialMT_Plain_24);
display.drawString(0,32,"19 °C");
display.display();
myServo.attach(doorServo);
delay(10);
Serial.println(F("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.println("IP address: ");
Serial.println(WiFi.localIP());
// Setup MQTT subscription for classroomlightfeed
mqtt.subscribe(&doorswitch);
mqtt.subscribe(&classroomlight);
mqtt.subscribe(&Projector);
mqtt.subscribe(&Temperature);
mqtt.subscribe(&location);
}
int32_t x=0;
void loop()
{
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000)))
{
if (subscription == &location)
{
Serial.print(F("Got:"));
Serial.println((char*)location.lastread);
if(!strcmp((char*)location.lastread, "entered"))
locationFlag=1;
if (!strcmp((char*)location.lastread, "exit"))
locationFlag=0;
}
if(locationFlag==1)
{
if (subscription == &doorswitch)
{
Serial.print(F("Got: "));
Serial.println((char*)doorswitch.lastread);
if(!strcmp((char*) doorswitch.lastread, "doorunlock"))
{
myServo.write(180);
doorFlag=1;
display.setFont(ArialMT_Plain_16);
display.drawString(0,0,"Temperature:");
display.setFont(ArialMT_Plain_24);
display.drawString(0,32,"19 °C"); display.display();
}
else if(!strcmp((char*)doorswitch.lastread, "doorlock"))
{
myServo.write(90);
doorFlag=0;
digitalWrite(projectorLed,LOW);
digitalWrite(roomLed, LOW);
display.clear();
display.display();
}
}
}
if (subscription == &classroomlight)
{
Serial.print(F("Got: "));
if(doorFlag==1)
{
Serial.println((char*)classroomlight.lastread);
if(!strcmp((char*) classroomlight.lastread, "roomledon"))
digitalWrite(roomLed, HIGH);
if(!strcmp((char*) classroomlight.lastread, "roomledoff"))
digitalWrite(roomLed, LOW);
}
}
if (subscription == &Projector)
{
Serial.print(F("Got: "));
if(doorFlag==1)
{
Serial.println((char*)Projector.lastread);
if(!strcmp((char*) Projector.lastread, "projectoron"))
digitalWrite(projectorLed, HIGH);
if(!strcmp((char*) Projector.lastread, "projectoroff"))
digitalWrite(projectorLed, LOW);
}
}
if (subscription == &Temperature)
{
if(doorFlag==1)
{
Serial.print(F("Got: "));
String temp= String((char*)Temperature.lastread);
Serial.println((char*)Temperature.lastread);
int tem= temp.toInt();
if(tem<30)
{
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawString(0,0, "Temperature:");
display.setFont(ArialMT_Plain_24);
display.drawString(0,32,(char*)Temperature.lastread);
display.drawString(64,32,"°C");
display.display();
}
else
{
display.clear();
display.setFont(ArialMT_Plain_24);
display.drawString(0,0,"Too Hot!!");
display.display();
}
}
}
}
if( digitalRead(smokeDetector)== LOW)
{
smokedetect.publish("Smoke Detected!!! RUN RUN");
digitalWrite(buzzer, HIGH);
delay(5000);
}
else
digitalWrite(buzzer, LOW);
}
void MQTT_connect()
{
int8_t ret;
// Stop if already connected.
if (mqtt.connected())
{
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0)
{
// connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0)
{
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

Comments
Post a Comment