Iot Labrecord 541
Iot Labrecord 541
R ENGINEERING COLLEGE(A)
( Affiliated to JNTU, KAKINADA )
DEPARTMENT OF COMPUTER SCIENCE
ENGINEERING CHINAAMIRAM::BHIMAVARAM-534204
(2020-2021)
INTERNET OF THINGS
LABORATORY RECORD
.
INDEX
S. NO TITLE PG. NO
1 Design an IOT System for LED blinking. 2
1
Experiment -1: Design an IOT System for LED blinking.
Aim: To study the applications and usage of ESP32 board for designing Internet of Things (IOT)
applications.
Objectives: Student should get the knowledge and usage of IOT devices such as ESP32 Board.
Outcomes: Student will be aware of the device connections and usage of IOT devices such as
ESP32 Board.
CODE:
const int ledpin1 =2;
Aim: To study the applications and usage of ESP32 Board and DHT11 Sensor.
Objectives: Student should get the knowledge and usage of ESP32 Board and DHT11 Sensor.
Outcomes: Student will be aware of the device connections and usage of IOT devices such as
ESP32 and DHT11 Sensor.
Block Diagram:
Code :
#include "DHT.h"
#define DHTPIN 4 // Digital pin connected to the DHT
sensor #define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
}
Experiment -3: Read Distance in cm or meter using Ultrasonic Sensor and display
it on Serial Monitor using ESP32.
Aim: To study the applications and usage of ESP32 Board and UltraSonic Sensor.
Objectives: Student should get the knowledge and usage of ESP32 Board and UltraSonic Sensor.
Outcomes: Student will be aware of the device connections and usage of IOT devices such as
ESP32 and UltraSonic Sensor.
Hardware Recommended:
s. no Devices/Sensors Qty, No
1 ESP32 Board 1
2 Basic Shield 1
3 Ultrasonic Sensor 1
4 Connecting Wires 10
Block Diagram:
Code:
#include "NewPing.h" // include NewPing library
// for ESP32 microcontroller
int trigPin = 4; // trigger pin
int echoPin = 0; // echo pin
NewPing sonar(trigPin, echoPin);
void setup()
{
Serial.begin(9600);
}
void loop()
{
float distance = sonar.ping_median(5);
if(distance>400 || distance<2)
Serial.println("Out of range");
else{ Serial.print("Distanc
e: ");
Serial.print(distance, 1);
Serial.println(" cm");
}
delay(50);
}
Experiment -4: Design an IOT System and an App to ON/OFF lights using
Bluetooth technology.
Hardware Recommended:
Block Diagram:
Bluetooth App design: (MIT APP INVENTOR TOOL)
Code:
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
String state;
void setup()
{ pinMode(2,
OUTPUT); pinMode(4,
OUTPUT);
Serial.begin(115200);
SerialBT.begin("chandu"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
if (Serial.available())
{
SerialBT.write(Serial.read());
Serial.println("hello");
}
if (SerialBT.available()) {
state=SerialBT.read();
Serial.print("State :");
Serial.println(state);
if (state.equals("55"))
{ digitalWrite(2, HIGH);
Serial.println("Light On");
}
// if the state is 'LED1OFF' the led1 will turn off
else if (state.equals("49")){
digitalWrite(2, LOW);
Serial.println("Light Off");
}
// if the state is 'LED2ON' the Led2 will turn on
else if (state.equals("57")){
digitalWrite(4, HIGH);
Serial.println("AC On");
}
}
state="";
delay(200);
}
Experiment -5: Design an IOT System and an App to ON/OFF lights using WIFI
technology.
Aim: To study the various applications of Wireless Fidelity (Wi-Fi) in Internet of Things (IoT)
by fetching the data from sensors and relay kit where such in a case by using HTTP protocol the
information is being transferred within the devices and the devices are operated automatically.
Objectives: Student should get the knowledge of Wi-Fi communication devices by Designing
Mobile App to ON or OFF light.
Outcomes: Student will be aware of communication using Wi-Fi background devices buy using
Mobile App.
Hardware Recommended:
Block Diagram:
WiFi App design(using MIT App inventor Tool):
Code:
#include <ThingSpeak.h>
#include <WiFi.h>
WiFiClient client;
const char* ssid = "ONLYCSE";//Enter the ssid of your router
const char* password = "keepsmile";//Enter the password of your router
const char* host = "api.thingspeak.com";
const char* privateKey = "UOZ3XFFHY6J345IO";//read key
const char* privateKey1 = "9HO22G0EY0D8XYD4";//write key
void setup() {
Serial.begin(115200);
ThingSpeak.begin(client);
delay(10);
Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){ delay(500);
Serial.print(".");
Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
float h= ThingSpeak.readFloatField( 626040,2); int d= ThingSpeak.readIntField( 626082,1);
Serial.print(d);
if(d==1)
{ digitalWrite(4,HIGH);
Serial.print("LED ON");
Serial.println("");
}
if(d==0)
{ digitalWrite(4,LOW);
Serial.print("LED OFF");
Serial.println("");
}
delay(1000);
}
Experiment -6: Design IOT System and an App to retrieve data (Temperature and
Humidity) from cloud (ThinkSpeak).
Outcomes: Student will be aware of communication using Wi-Fi background devices buy
using Mobile App
Hardware Recommended:
Block Diagram:
App designing (using MIT inventor Tool): To display temperature and humidity
Code:
#include <WiFi.h>
#include "DHT.h"
#define DHTPIN 4 // what pin we're connected to
#define DHTTYPE DHT11 // define type of sensor DHT 11
DHT dht (DHTPIN, DHTTYPE);
const char* ssid = "JioFi4_134EAC";//Enter the ssid of your router
const char* password = "cp19qexyh5";//Enter the password of your router
const char* host = "api.thingspeak.com";
const char* privateKey = "SYANE5W2LLUBYMGT";//read key
const char* privateKey1 = "TUEKQN0T47U4KA15";//write key
String line,line1;
float h,t;
void setup()
{ Serial.begin(115200);
pinMode(light, OUTPUT);//setting led as output
pinMode(fan, OUTPUT);//setting led as output
dht.begin();
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{ delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(){
h = dht.readHumidity();
t = dht.readTemperature();
Serial.print("temperature:");
Serial.println(t);
Serial.print("Humidity:");
Serial.println(h);
delay(10000);
retrieve_from_Cloud();
delay(10000);
}
void retrieve_from_Cloud()
{ Serial.print("connecting to ");
Serial.println(host); // Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}// We now create a URI for the request
String url1 = "/channels/539141";
url1 += "/fields/3/last?";
url1 += "api_key=";
url1 += privateKey;
Serial.print("Requesting URL: ");
Serial.println(url1); // This will send the request to the server
client.print(String("GET ") + url1 + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(1000);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
if(client.find("@")){
line = client.readString();
Serial.print(line);
}
}
Serial.println();
Serial.println("closing connection");
}
Experiment -7: Design IOT System and an App to upload data (Temperature and
Humidity) from cloud (ThinkSpeak).
Outcomes: Student will be aware of communication using Wi-Fi background devices buy
using Mobile App
Hardware Recommended:
Block Diagram:
App designing (using MIT inventor Tool): To display temperature and humidity
Code:
#include <WiFi.h>
#include "DHT.h"
#define DHTPIN 4 // what pin we're connected to
#define DHTTYPE DHT11 // define type of sensor DHT 11
DHT dht (DHTPIN, DHTTYPE);
const char* ssid = "JioFi4_134EAC";//Enter the ssid of your router
const char* password = "cp19qexyh5";//Enter the password of your router
const char* host = "api.thingspeak.com";
const char* privateKey = "SYANE5W2LLUBYMGT";//read key
const char* privateKey1 = "TUEKQN0T47U4KA15";//write key
String line,line1;
float h,t;
void setup()
{ Serial.begin(115200);
pinMode(light, OUTPUT);//setting led as output
pinMode(fan, OUTPUT);//setting led as output
dht.begin();
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{ delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(){
h = dht.readHumidity();
t = dht.readTemperature();
Serial.print("temperature:");
Serial.println(t);
Serial.print("Humidity:");
Serial.println(h);
delay(10000);
retrieve_from_Cloud();
delay(10000);
}
void upload()
{ Serial.print("connecting to ");
Serial.println(host); // Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort))
{ Serial.println("connection failed");
return;
}// We now create a URI for the request
String url = "/update";
url += "?api_key=";
url += privateKey1;
url += "&field1=";
url += t;
url += "&field2=";
url += h;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(1000);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line1 = client.readStringUntil('\r');
Serial.print(line1);
}
Serial.println("closing connection");
}