Professional Documents
Culture Documents
Task B: Measure the light level in the room and store it on the cloud. 8
Task C: Detect Human Presence using PIR Sensor and store it on the cloud 8
Components Required 8
Diagram: 9
LOGICAL CONNECTIONS: 9
PHYSICAL CONNECTIONS: 10
On Arduino: 10
Connecting to THINGSPEAK FROM NODEMCU 11
Serial Output : 13
ThingSpeak Access Details: 13
VIDEO Recording : 14
Task D : Based on the light level and human presence recorded on the cloud, turn ON / OFF the
light 14
Details : Assignment No. 2
Project Description: Develop IoT based Smart School System. In smart school, attendance is taken
automatically through RFID tags and electrical bulbs are automatically controlled. The developed
system should be able to do the following.
a. Record attendance using RFID tags and store it on the cloud (student 1, Student2).
b. Measure the light level in the room and store it on the cloud (Student 3)
c. Detect Human Presence using PIR Sensor and store it on the cloud (Student 4, student 5)
d. Based on the light level and human presence recorded on the cloud, turn ON / OFF the light
(Student 6)
SMS notification needs to be sent to students when the RFID tag is detected. Also the system should
generate an email notification regarding the attendance to principal.
All the data collected should be stored on the cloud (Blynk / ThingSpeak cloud) using single account.
Components Required
Hardware:
1. Arduino
2. NodeMCU
3. RFID Sensor & Tag
4. Jump wires
Software:
1. Library MFRC522 - https://github.com/miguelbalboa/rfid
2. Library “DSC Keybus Interface” - https://github.com/miguelbalboa/rfid
Diagram:
Logical Connections:
GND GND
3.3V 3.3V
9 RST
10 SDA
11 MOSI
12 MISO
13 SDA
GND G
6 (Tx) D5 (Rx)
7 (Rx) D6 (Tx)
Physical Connections:
Flow Summary:
1. Read card ID by RFID sensor.
2. Receive the ID on Arduino sent by RFID sensor.
3. Send the ID to NodeMCU.
4. NodeMCU establishes connection to thinkspeak portal; and sends the ID using http
POST method.
5. NodeMCU reads the code returned by POST method to identify whether data is successfully
saved on thinkspeak or not.
6. If it was successful, NodeMCU performs following tasks:
a. Establishes connection to Twilio portal, and sends SMS to student’s mobile phone
number confirming attendance record.
b. Establishes connection to google SMTP server, and sends email to principal
confirming attendance record.
Code - Arduino:
// Arduino UNO code
#include <SPI.h>
#include <MFRC522.h>
#include <SoftwareSerial.h>
ard.begin (9600);
}
//
**********************************************************************************
*******//
void loop()
{
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF;
}
//-------------------------------------------
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when
idle.
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
//Serial.println(F("**Card Detected:**"));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
if (ard.available() > 0)
{
for (byte i = 0; i < mfrc522.uid.size; i++) {
ard.write(card_ID[i]);}
}
}
Code - NodeMCU:
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "base64.h"
HTTPClient http;
int httpCode = 0;
const char* AccountSID = "xxxxx"; // Set the account SID from the Twilio Account Dashboard
const char* AuthToken = "xxxxx"; // Set the auth token from the Twilio Account Dashboard
const char* From = "xxxxx"; // From phone number, starting with the country code without the +
sign: 18005551234
const char* To = "xxxxx"; // To phone number, starting with the country code without the +
sign: 18005551234
const char* messagePrefix = "[Attendance system] "; // Set a prefix for all messages
WiFiClient client;
// HTTPS root certificate for api.twilio.com: DigiCert Global Root CA, expires 2031.11.10
const char twilioCertificateRoot[] = R"=EOF=(
-----BEGIN CERTIFICATE-----
MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT
MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j
b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB
CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97
nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt
43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P
T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4
gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO
BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR
TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw
DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr
hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg
06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF
PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls
YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk
CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=
-----END CERTIFICATE-----
)=EOF=";
// Initialize components
X509List twilioCert(twilioCertificateRoot);
WiFiClientSecure ipSMSClient, ipMailClient;
char twilioAuth[128];
size_t twilioAuthLength = 128;
char encodedTwilioAuth[128], encodedMessagePrefix[128], encodedMessageContent[480];
void setup()
{
esp.begin (9600);
//Serial.begin (9600);
Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
ipSMSClient.setTrustAnchors(&twilioCert);
Serial.print(F("NTP time...."));
configTime(0, 0, "pool.ntp.org");
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(2000);
now = time(nullptr);
}
Serial.println(F("synchronized."));
void loop(){
if (esp.available() > 0){
data = esp.read();
//Serial.println ("Receiving data...");
//Serial.println (data, HEX);
if (strdata.length() != 0)
{
strdata += " ";
}
char tempstr[2];
sprintf(tempstr, "%02X", data);
strdata += tempstr;
}
if (strdata.length() == 11)
{
String postStr = "api_key=";
postStr += apiKey;
postStr +="&field1=";
postStr += strdata;
int n = newstr.length();
char msgContent[n+1];
strcpy(msgContent, newstr.c_str());
Serial.printf("messageContent: %s\n", msgContent);
//Send SMS
sendMessage(msgContent);
//Send Mail
Serial.print(F("Email...."));
if (sendMailMessage(msgContent))
Serial.println(F("connected and sent."));
else
Serial.println(F("connection error."));
}
}
else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
// sendMessage() takes the SPI content as parameter, prepares and sends the message.
bool sendMessage(const char* messageContent) {
encodeURL(messageContent, encodedMessageContent); // Encodes message content in URL
encoding
if (!ipSMSClient.connect("api.twilio.com", 443)) return false;
ipSMSClient.print(F("POST https://api.twilio.com/2010-04-01/Accounts/"));
ipSMSClient.print(AccountSID);
ipSMSClient.println(F("/Messages.json HTTP/1.1"));
ipSMSClient.print(F("Authorization: Basic "));
ipSMSClient.println(encodedTwilioAuth);
ipSMSClient.println(F("Host: api.twilio.com"));
ipSMSClient.println(F("User-Agent: ESP8266"));
ipSMSClient.println(F("Accept: */*"));
ipSMSClient.println(F("Content-Type: application/x-www-form-urlencoded"));
ipSMSClient.print(F("Content-Length: "));
ipSMSClient.println(strlen(To) + strlen(From) + strlen(encodedMessagePrefix) +
strlen(encodedMessageContent) + 21);
ipSMSClient.println("Connection: Close");
ipSMSClient.println();
ipSMSClient.print(F("To=%2B"));
ipSMSClient.print(To);
ipSMSClient.print(F("&From=%2B"));
ipSMSClient.print(From);
ipSMSClient.print(F("&Body="));
ipSMSClient.print(encodedMessagePrefix);
ipSMSClient.print(encodedMessageContent);
// Reads the response until the first space - the next characters will be the HTTP status code
while (ipSMSClient.available()) {
if (ipSMSClient.read() == ' ') break;
}
// Checks the first character of the HTTP status code - the message was sent successfully if the
status code
// begins with "2"
char statusCode = ipSMSClient.read();
// Encodes a char array to URL encoded using '+' for spaces as required for application/x-www-form-
urlencoded
char *encodeURL(const char *src, char *dst) {
char c, *d = dst;
while (c = *src++) {
if (c == ' ') {
*d++ = '+';
continue;
}
else if (!('a' <= c && c <= 'z')
&& !('A' <= c && c <= 'Z')
&& !('0' <= c && c <= '9')) {
*d++ = '%';
*d++ = encodeHex(c >> 4);
c = encodeHex(c);
}
*d++ = c;
}
*d = '\0';
return dst;
}
// sendMailMessage() takes the email subject and body as separate parameters. Configure the
settings for your SMTP
// server - the login and password must be base64 encoded. For example, on the macOS/Linux
terminal:
// $ echo -n 'mylogin@example.com' | base64 -w 0
bool sendMailMessage(const char* messageContent) {
if (!ipMailClient.connect("smtp.gmail.com", 465)) return false; // Set the SMTP server
address - for example: smtp.gmail.com
if(!smtpValidResponse()) return false;
ipMailClient.println(F("HELO ESP8266"));
if(!smtpValidResponse()) return false;
ipMailClient.println(F("AUTH LOGIN"));
if(!smtpValidResponse()) return false;
ipMailClient.println(F("xxxxx")); // Set the SMTP server login in base64
if(!smtpValidResponse()) return false;
ipMailClient.println(F("xxxxx")); // Set the SMTP server password in base64; google
app-password may be applicable
if(!smtpValidResponse()) return false;
ipMailClient.println(F("MAIL FROM:<xxxxx@gmail.com>")); // Set the sender address
if(!smtpValidResponse()) return false;
ipMailClient.println(F("RCPT TO:<xxxxx@gmail.com>")); // Set the recipient address - repeat
to add multiple recipients
if(!smtpValidResponse()) return false;
//ipMailClient.println(F("RCPT TO:<recipient2@example.com>")); // An optional additional
recipient
//if(!smtpValidResponse()) return false;
ipMailClient.println(F("DATA"));
if(!smtpValidResponse()) return false;
ipMailClient.println(F("From: Security System xxxxx@gmail.com")); // Set the sender displayed in
the email header
ipMailClient.println(F("To: Recipient xxxxx@gmail.com")); // Set the recipient displayed in the
email header
ipMailClient.print(F("Subject: "));
ipMailClient.print(messagePrefix);
ipMailClient.println(messageContent);
ipMailClient.println(); // Required blank line between the header and body
ipMailClient.print(messagePrefix);
ipMailClient.println(messageContent);
ipMailClient.println(F("."));
if(!smtpValidResponse()) return false;
ipMailClient.println(F("QUIT"));
if(!smtpValidResponse()) return false;
ipMailClient.stop();
return true;
}
bool smtpValidResponse() {
// Checks the first character of the SMTP reply code - the command was successful if the reply code
begins
// with "2" or "3"
char replyCode = ipMailClient.read();
// Successful, reads the remainder of the response to clear the client buffer
if (replyCode == '2' || replyCode == '3') {
while (ipMailClient.available()) ipMailClient.read();
return true;
}
Student Attendance
This portal stores records of attendance entries, which can be downloaded using “Export recent
data” option.
Email Notification:
Gmail SMTP server has been used here to send email to admin mail id. Following screenshot was ca
SMS Notification:
Twilio online messaging service has been used here to send text message to student’s mobile phone
number. Following screenshot was captured as part of this project execution:
Video Recording :
Google drive link: https://drive.google.com/file/d/1Nf3xU2S3dvbLxpMHRtq_rc6rdxAsEBh7/view?
usp=sharing
Task B: Measure the light level in the room and store it on the cloud.
Task C: Detect Human Presence using PIR Sensor and store it on the
cloud
Components Required
1. Arduino
2. NodeMCU
3. PIR Sensor
4. Jump wires
5. LED for Testing locally
Diagram:
LOGICAL CONNECTIONS:
PIRSENSOR🡨🡪Arduino PINS 🡨🡪 Node MCU
VCC 5V
GND GND
INPUT PIN2
13-LED
TX PIND5
RX PIND6
PHYSICAL CONNECTIONS:
On Arduino:
1. Programming :-
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(Sensorinput, INPUT); // declare sensor as input
Serial.begin(9600);
void loop(){
val = digitalRead(Sensorinput); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
delay(500);
if (SensorState == LOW) {
// we have just turned on
//Serial.println("Motion detected!");
Serial.println(val);
// We only want to print on the output change, not state
SensorState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
delay(200);
if (SensorState == HIGH){
// we have just turned of
//Serial.println("Motion ended!");
Serial.println(val);
// We only want to print on the output change, not state
SensorState = LOW;
}
}
PS: Value 1 means Sensor is Activated; human detection is activated and 0 Mean No Human
Detection Happened.
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
SoftwareSerial esp (D5,D6); // Pin D5 RX and Pin D6 TX
int alldata;
int value;
String apiKey = "NG5WA11MCFTIFORF"; // Enter your Write API key from
ThingSpeak
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
esp.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
Serial.print("motion : ");
Serial.print(value);
Serial.println(" , Send to Thingspeak.");
}
client.stop();
Graph is presented for the same reflecting the details. Along with that a LIGHT is triggered,
turns in to GREEN when Sensor is activated.
VIDEO Recording :
Components Required
1 Arduino
2 Jump wires
3 LED for Testing locally
Diagram:
LOGICAL CONNECTIONS:
Node MCU🡨🡪LED
GND GND
INPUT D0
D1
PHYSICAL CONNECTIONS:
On NodeMcu:
2. Programming :-
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
WiFiClient client;
//---------Channel Details---------//
unsigned long counterChannelNumber = 1913397; // Channel ID
//-------------------------------//
void setup()
pinMode(D0, OUTPUT);
pinMode(D1, OUTPUT);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
void loop()
if (WiFi.status() != WL_CONNECTED)
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println(" ....");
Serial.println(WiFi.status());
Serial.println(WL_CONNECTED);
WiFi.begin(ssid, pass);
delay(5000);
if (statusCode == 200)
Serial.println(Motion);
if(Motion==-)
digitalWrite(D0, HIGH);
digitalWrite(D1, LOW);
delay(10000);
else
statusCode = ThingSpeak.getLastReadStatus();
if (statusCode == 200)
{
Serial.print("Light sensor value: ");
Serial.println(light );
if(light>=32)
digitalWrite(D0, LOW);
digitalWrite(D1, HIGH);
delay(10000);
else
Serial Output :
ThingSpeak Access Details:
Graph is presented for the same reflecting the details. Along with that a LIGHT is triggered,
turns in to GREEN when Sensor is activated.
VIDEO Recording :
https://drive.google.com/file/d/1FlIxK5LJ6Mvx1Bw4yCGrmqyfqqAFHEKf/view?
usp=share_link