IoT Lab Manual
IoT Lab Manual
1
Prerequisites to the lab programs to execute
1. Install any version of Arduino software.
2. Then goto tools and Board and install ESP32 Dev Module (with wifi)
3. Then select the board as ESp32 and then select the port
4. Install the libraries necessary for each program.
Pin-Out
DHT11 sensor ESP32 pins
Data-pin GPIO27
Vcc 3.3V
GND GND
Functions:
1. dht.setup(DHTpin,DHTesp::DHT11)
The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a
capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a
digital signal on the data pin (no analog input pins needed). Its fairly simple to use, but
requires careful timing to grab data. It can measure humidity from 20% to 90% RH (relative
humidity (RH) is a measure of the water vapor content of air) and temperature from 0 to 50
degrees Celsius.
The humidity sensing capacitor has two electrodes with a moisture holding substrate as
a dielectric between them. Change in the capacitance value occurs with the change in
humidity levels. The IC measure, process this changed resistance values and change them
into digital form.
DHT11 Output: Serial data. Temperature Range: 0°C to 50°C. Humidity Range: 20% to
90%.
2
Program:
#include "DHTesp.h"
#define DHTpin 27
DHTesp dht;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
Serial.println("Status\tHumidity(%)\tTemperature(C)");
dht.setup(DHTpin,DHTesp::DHT11);
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
float h = dht.getHumidity();
float t = dht.getTemperature();
Serial.print(dht.getStatusString());
Serial.print("\t");
Serial.print(h);
Serial.print("\t");
Serial.println(t);
}
3
OUTPUT:
4
Task-2: Write a program for interfacing the Ultrasonic Sensor with Generic Sensor
Board
Program:
// defines pins numbers
const int trigPin = 25;
const int echoPin = 26;
// defines variables
5
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");// in centimeter
Serial.println(distance);
delay(1000);// delay is in miliseconds
}
6
OUTPUT:
7
Task-3: Using Generic Sensor Board write a program to interface the Soil Moisture
Sensor
PIN-OUT
Soil moisture Sensor signal ESP32 pins
AO GPIO34
DO NC
Vcc 3.3V
GND GND
Program:
// Soil Moisture sensor pin : GPIO34
8
#define smPin 34
int smValue;
void setup() {
Serial.begin(115200);
}
void loop() {
smValue = 4095 - analogRead(smPin);
Serial.print("Soil Moisture = ");
Serial.println(smValue);
delay(1000);
}
OUTPUT:
9
Task-4: Programming a Generic Sensor Board to interface the Real Time Clock
module.
PIN-OUT
Real Time Clock – RTC ESP32 pins
SCL GPIO22
SDA GPIO21
Vcc 3.3V
GND GND
10
Program:
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if(!rtc.begin()){
Serial.print("No RTC module found");
while(1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
void loop() {
// put your main code here, to run repeatedly:
char buffer[10];
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
sprintf(buffer,"%02d:%02d:%02d",now.hour(),now.minute(),now.second());
Serial.println(buffer);
delay(1000);
OUTPUT:
11
Task-5: Design and interface the Magnetic Sensor with Generic Sensor Board for
door/window open detection system.
PIN-OUT
Magnetic Sensor ESP32 pins
Sensor o/p contact1 GPIO16
Sensor o/p contac2 GND
12
Vcc 3.3V
Magnetic sensors detect moving ferrous metal. The simplest magnetic sensor consists of a
wire coiled around a permanent magnet. A ferrous object approaching the sensor changes
magnetic flux through the coil, generating a voltage at the coil terminals. ... Magnetic sensors
measure speeds up to 600,000 rpm.
The INPUT_PULLUP also does the same function as INPUT however only differing in the
aspect of base voltage, where the INPUT argument pulls down the voltage of that port to 0V
every time there is no voltage is detected across the port while the INPUT_PULLUP
argument pulls up the voltage across the port to the maximum .
Program:
#define magSW 16
void setup()
{
pinMode(magSW,INPUT_PULLUP);
Serial.begin(115200);
13
}
void loop()
{
int sw_status;
sw_status = digitalRead(magSW);
if (sw_status)
Serial.println("Switch open");
else
Serial.println("Switch closed");
delay(1000);
}
OUTPUT:
Task-6: With the help of Generic Sensor Board, built an interface for switching
applications using relay.
14
Vcc 3.3V
Relay Sensor:
Relay works on the principle of electromagnetic induction. When the electromagnet is
applied with some current it induces a magnetic field around it. ... In the relay Copper coil
and the iron core acts as electromagnet. When the coil is applied with DC current it starts
attracting the contact as shown.
When an electric current crosses the coil a magnetic field is formed over the armature that
attracts it and activates the contacts. A control current applied to the relay coil makes possible
to open, close or commute the contacts and controls the currents that circulate through
external loads.
Relays are switches that open and close circuits electromechanically or electronically.
Relays control one electrical circuit by opening and closing contacts in another circuit.
Note: Here we will use ultrasonic sensor and relay and buzzer. When any object comes closer
to than the distance specified the relay will be HIGH and on the buzzer.
Program:
// defines pins numbers
const int trigPin = 25;
const int echoPin = 26;
// defines variables
long duration;
int distance;
//#define relay 13;
//#define buzzer 16;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
15
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
pinMode(13, OUTPUT);
pinMode(16, OUTPUT);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
}
else{
Serial.println("Buzzer turned OFF");
digitalWrite(13,LOW);
digitalWrite(16,LOW);
//delay(2000);
}
delay(1000);
}
Output:
16
17
Task-7: Develop a Mobile app for simple User Interface.
There are several steps to develop a mobile app. These are the following Steps:
Step 1: go to Kodular Creator website by clicking below link you can go the website.
https://www.kodular.io/creator
Step 2 : Click on get Started button
Step 3: then you will get a scree like this
18
Step 5: Then click on next and finish. Then your project will be created and window will be
opened like this
Step 6: then go to the left side pallet and click on User Interface then you will find all GUI
items like buttons, lables, etc..as shown in the above figure.
Step 7: drag and drop the controls which you need and those controls will be visible in your
app.
Here as a sample mobile app, drag and drop one label and add the text as “Welcome to IOT
LAB” and you can apply colour,font etc…
Step 8: And take 2 button one as GRIET and the other is IT Dept and took two labels one will
be printing the text when GRIET button is clicked in the same screen and other will print the
text when IT Dept. button is clicked in another screen.
19
Step 9: click on Add Screen and click on create button then a new screen will be created.
Step 10: go to screen1 and go to Blocks tab on the right to write the code to get the text when
you click the buttons accordingly.
20
Step 11: go to screen2 and put a label and write the text that should be displayed when you
click on IT Dept. button in screen.
Step12: now test the mobile app, buy clicking test and connect to companion. Before this
testing open google play store in your mobile and install the app named “Kodular
Companion”.
Step 13: then open that app and scan the QR code which we get when we click on connect to
companion in Kodular Creator.
21
Step 14: If you scan the QR code in your mobile the app will be opened in your mobile. Now
test the app whether it is working correctly or not.
22
Step15: then to create permanent mobile app , go to Export option and click android app.
23
24
Step16: Now scan the QR code and also Click on download APK. Then download the app in
mobile. Make sure that mobile and laptop should be connect to same wifi. Then you will get
app in mobile.
Follow the screen shots..
25
26
27
Task-8: Design a Mobile app to work with data of a cloud.
Program:
Follow the below steps to create a firebase(cloud) project.
Step1:
Step3: here type your project name and tick I accept and the click on continue.
28
Step4:click on continue
Step5:select the location as INDIA and tick the check box of I accept the terms and finally
click on Create Project button.
29
30
Step 6: now project is created then click on continue.
31
Step 7: Now move down the scrollbar and select cloud firestore
32
Now scroll down and select see all build features.
33
Now click on create database
34
Now select start in test mode radio button and then click on Enable button.
35
Now copy the firebase url into a notepad
Step8: go to left side pallet and click on settings icon and select project settings.
36
Now select service accounts
Click on database secrets which is in the left panel and there we find the secrete key copy that
key into the notepad (whenever we need it we will use it).
37
Now we are done with creation of a database in cloud (firebase) to store the data which
comes from the board.
For example, we will take an Arduino program to sense temperature and humidity using
DHT11 sensor on the board.
#include "DHTesp.h"
#define DHTpin 27
DHTesp dht;
int delayTime = 2000;
#include "FirebaseESP32.h"
#define FIREBASE_HOST "xxxxxxxxxxxxxxxxxxxxxxxxxxx" //Do not include https:// in
FIREBASE_HOST
#define FIREBASE_AUTH "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define WIFI_SSID "xxxxxxxxx"
#define WIFI_PASSWORD "xxxxxxxxxx"
//Define Firebase Data object
FirebaseData firebaseData;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
Serial.println("Status\tHumidity(%)\tTemperature(C)");
dht.setup(DHTpin,DHTesp::DHT11);
//Connecting to Wi-Fi network
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
38
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
}
void loop() {
// put your main code here, to run repeatedly:
delay(delayTime);
float h = dht.getHumidity();
float t = dht.getTemperature();
Serial.print(dht.getStatusString());
Serial.print("\t");
Serial.print(h);
Firebase.setFloat(firebaseData,"IOTLAB/Ambient_Parameters/Humidity",h);
Serial.print("\t");
Serial.println(t);
Firebase.setFloat(firebaseData,"IOTLAB/Ambient_Parameters/Temperature",t);
}
Compile and dump the program on to the board and then you will see the output in serial
monitor and the same updated values you will see in realtime database also. The output is as
shown in the below picture.
39
Step1: Open kodular creator by typing in google or go to this link
https://www.kodular.io/creator
Here click on create project and give the name of the project and click on next
40
Then we will get this following window then just click on finish.
41
We get this following page to create your mobile app
Here on the left side, we will see the Palette where we can find all the GUI widgets, we
should just drag and drop the necessary widgets on to the mobile screen visible beside the
palette.
Drag and drop the labels and name the text of the labels as Temperature and Humidity and
the takemore labels to display the values of temperature and humidity by getting the values
from the firebase (database). And also we can use alignments also like vertical or horizontal
accordingly to your requirements, you should also drag and drop the firebase and clock from
the Palette.
Now design part of the mobile app is done. Now move to Blocks to write the code by
clicking Blocks on the right-side top of the window.
42
Now your mobile app is ready which is connected to firebase to get the ambient parameters
that are updated for every clock second from the board.
43
Task-9: Build the necessary code for deploying App on a mobile phone.
Program:
The following steps illustrates the procedure of deploying App on a mobile phone.
Step 1: open Kodular Creator and go to the mobile app project that you have already created
in the previous task.
44
If we click on connect to companion the we will get a QR code. Now open your kodular
companion app in mobile and scan the QR code.
Now your mobile app will be loaded into your mobile test it once to check whether it is
working or not.
45
Now go to Export and click on Android app (.apk) , then you will be seen the following
windows.
46
you will get a QR code again scan the QR code by using kodular companion app in your
mobile then you can download the app into your mobiles and install it .
47
You can also download the APK file if you are unable to scan the QR code and we can
download the mobile app and now install the app in your mobile.
Open the app after installation is done.
You can see the Temperature and Humidity values that are been sensed by the mico
controller.
The following are the images that shows you how an app is installed in our mobiles using
Kodular companion app.
48
49
Note: When we click on open you can see the ambient parameter values, but make sure that
board, laptop in which you are executing the Arduino program and the mobile in which you
have installed the app is connected to same wifi.
50
Task-10: Design an application to create a Remote Motor Control System.
Program:
#define motorPin 13
#include "FirebaseESP32.h"
#define FIREBASE_HOST "xxxxxxxxxxxxxxxxxxxxxxx" //Do not include https:// in
FIREBASE_HOST
#define FIREBASE_AUTH "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define WIFI_SSID "xxxxxxxxxxxxxxxxxxx"
#define WIFI_PASSWORD "xxxxxxxxxxxxxxxxxxxxxxx"
//Define Firebase Data object
FirebaseData firebaseData;
int delayTime = 2000;
void setup() {
// put your setup code here, to run once:
pinMode(motorPin,OUTPUT);
Serial.begin(115200);
//Connecting to Wi-Fi network
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
}
void loop() {
// put your main code here, to run repeatedly:
String motorCmdFull;
String motorCmd;
int finalCmd;
Firebase.getString(firebaseData,"IOTLAB/Motor_Control/Motor_Cmd",motorCmdFull);
motorCmd = motorCmdFull.substring(2,motorCmdFull.length()-2);
finalCmd = motorCmd.toInt();
Serial.println(finalCmd);
if(finalCmd == 1)
{
digitalWrite(motorPin,HIGH);
Serial.println("Motor ON");
}
else
{
digitalWrite(motorPin,LOW);
Serial.println("Motor OFF");
51
}
delay(2000);
}
OUTPUT:
If click on start motor will start and if you click stop them motor should off.
52
53
Task-11: Develop a Smart Garden using Generic Sensor Board.
PIN-OUT
Soil Moisture sensor ESP32 pins
AO GPIO34
DO NC
Vcc 3.3V
GND GND
Program:
/* Soil Moisture sensor pin : GPIO34
* Relay pin : GPIO13
*
*/
#include "FirebaseESP32.h"
#define FIREBASE_HOST "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" //Do not include https:// in
FIREBASE_HOST
#define FIREBASE_AUTH "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define WIFI_SSID "Saadhvi"
#define WIFI_PASSWORD "saadhvi123"
//Define Firebase Data object
FirebaseData firebaseData;
#define smPin 34
#define relayPin 13
int smValue;
int smLimit = 3000;
int delayTime = 2000;
String smValueCloud;
void setup() {
// put your setup code here, to run once:
pinMode(relayPin,OUTPUT);
Serial.begin(115200);
54
void loop() {
// put your main code here, to run repeatedly:
String smCloudFull;
String smCloud;
if(Firebase.getString(firebaseData,"xxx/IOTLAB/Smart_Garden/SM_Threshold",smCloudFu
ll)){
smCloud = smCloudFull.substring(2,smCloudFull.length()-2);
smLimit = smCloud.toInt();
Serial.println(smLimit);
}
smValue = 4095 - analogRead(smPin);
Serial.print("Soil Moisture = ");
Serial.println(smValue);
Firebase.setInt(firebaseData,"IOTLAB/Smart_Garden/Soil_Moisture",smValue);
Firebase.setString(firebaseData,"IOTLAB/Smart_Garden/Motor_Status","OFF");
}
delay(delayTime);
}
OUTPUT:
55
56
Here using a mobile app we can set the Soil Moisture limit, depending upon the seasons we
can change the limit of the Soil Moisture.
57
Task- 12: Integrate different blocks to do the IOT project on Real Time-based
Appliance Control.
Note here we are using RTC and on the LED when the no. of minutes greater than 7 and less
the 10. Similarly, we can on the led at evening 6PM and off it at 6AM also.
LED on bread bord connections: connect LED log leg to resister and from resister to VCC,
and LED short leg to GND i.e cut that wire and put to relay.
PIN-OUT
Real Time Clock – RTC ESP32 pins
SCL GPIO22
SDA GPIO21
Vcc 3.3V
GND GND
Program:
#include <Wire.h>
#include "RTClib.h"
#define relayPin 13
RTC_DS3231 rtc;
pinMode(relayPin,OUTPUT);
Serial.begin(115200);
if(!rtc.begin()){
Serial.print("No RTC module found");
while(1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
void loop() {
// put your main code here, to run repeatedly:
char buffer[10];
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
58
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
sprintf(buffer,"%02d:%02d:%02d",now.hour(),now.minute(),now.second());
int i=now.minute();
Serial.println(i);
/*digitalWrite(relayPin,HIGH);
delay(500);
digitalWrite(relayPin,LOW);
delay(500);*/
if(i>7 && i<10)
{
digitalWrite(relayPin,HIGH);
Serial.println("LED ON");
}
else {
digitalWrite(relayPin,LOW);
Serial.println("LED OFF");
}
Serial.println(buffer);
delay(1000);
OUTPUT:
59
60