IoT File

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

20EC413

Lab Manual
of
Embedded System Design with IoT Laboratory
Subject Code: 4EC06
Semester: 7th
Academic Year: 2023-24
Name of Student: Shalini Kotecha
Id No.: 20EC413
BACHELOR OF TECHNOLOGY
IN ELECTRONICS AND COMMUNICATION

BIRLA VISVAKARMA MAHAVIDHYALAYA ENGINEERING


COLLEGE
(An Autonomous Institution)
Vallabh Vidyanagar – 388120

1|Page
20EC413

BIRLA VISHVAKARMA MAHAVIDYALAYA


ELECTRONICS AND COMMUNICATION DEPARTMENT

Certificate
This is to certify that Miss Shalini Kotecha with ID. No.
20EC413 of Electronics & Communication has satisfactorily
completed the entire practical in the subject of 4EC06 –
Embedded System Design with IoT Laboratory.

Subject In-Charge Head of Department


(Prof. Hiren Patel) (Dr. Bhargav C. Goradiya)

2|Page
20EC413

INDEX
SR. PAGE
DATE AIM SIGN
NO. NO.
Write a program to control an LED with a switch
01 05/07/2023 04
using Arduino on TinkerCAD.
Write a program to control LED intensity based on
02 12/07/2023 a PIR sensor and LDR using Arduino on 07
TinkerCAD.
Write a program to implement a light - following
03 19/07/2023 robot based on LDR and DC motors using Arduino 10
on TinkerCAD.
Write a program to create a web server based on
04 26/07/2023 14
ESP32 to host a web page.
Write a program to update data on a web page
05 26/07/2023 17
hosted on ESP32.
Write a program to auto - update data on a web
06 12/09/2023 24
page using AJAX.
Write a program to control an LED from a web
07 12/09/2023 29
page using ESP32.
Write a program to read data from a switch on the
08 12/09/2023 Blynk dashboard and update the status of the 33
hardware LED.
Write a program to read data from hardware and
09 26/09/2023 36
update the status on the Blynk dashboard.
Write a program to control a servo motor from the
10 26/09/2023 39
Blynk dashboard.

3|Page
20EC413

PRACTICAL NO.: 01
AIM:

Write a program to control an LED with a switch using Arduino on TinkerCAD.

SOFTWARE USED:

TinkerCAD

HARDWARE COMPONENT USED:

Red LED, Arduino Uno R3, 220 Ω Resistor, Pushbutton

CIRCUIT:

PROGRAM:
int toggle = 0;
void setup()
{
pinMode(13, OUTPUT); //LED

4|Page
20EC413

pinMode (7, INPUT) ; //Switch


digitalWrite (7, HIGH) ;
}

void loop()
{
if (!(digitalRead(7))){
if (toggle){
toggle = 0;
}
else{
toggle = 1;
}
digitalWrite (13, toggle);
while (!(digitalRead(7)));
}
}

OUTPUT:

5|Page
20EC413

CONCLUSION:
In this practical, an LED was successfully controlled using a switch through Arduino on
TinkerCAD.

6|Page
20EC413

PRACTICAL NO.: 02
AIM:

Write a program to control LED intensity based on a PIR sensor and LDR using Arduino on
TinkerCAD.

SOFTWARE USED:

TinkerCAD

HARDWARE COMPONENT USED:

Arduino Uno R3, Photoresistor (LDR), Voltage Multimeter, 10 kΩ Resistor, Red LED, PIR
Sensor, 220 Ω Resistor

CIRCUIT:

7|Page
20EC413

PROGRAM:
void setup()
{
pinMode (2, INPUT); //PIR Signal Pin
pinMode (9, OUTPUT); //LED
Serial.begin(9600);
}

void loop()
{
float N = analogRead(A0); //LDR
float PIR = digitalRead(2);
float V = N * 5000 / 1024; //10 bit ADC

float pert = map (V, 241, 4729, 255, 0);


if (PIR && (pert < 50)){
digitalWrite(9, HIGH);
}
else{
digitalWrite(9, LOW);
}
Serial.print ("N = ");
Serial.print (N);
Serial.print (";");
Serial.print ("V =");
Serial.println (V);
}

8|Page
20EC413

OUTPUT:

CONCLUSION:
Thus, we have demonstrated the program to control LED intensity based on the input from a PIR
sensor and LDR using Arduino on TinkerCAD.

9|Page
20EC413

PRACTICAL NO.: 03

AIM:

Write a program to implement a light - following robot based on LDR and DC motors using
Arduino on TinkerCAD.

SOFTWARE USED:

TinkerCAD

HARDWARE COMPONENT USED:

Arduino Uno R3, DC Motors, L293D H-bridge Motor Driver, 1 kΩ Resistor, Photoresistor,
Voltage Multimeter

CIRCUIT:

PROGRAM:
int Rldr = A0;
int Lldr = A1;
int in1 = 2;
int in2 = 3;
int in3 = 4;
int in4 = 5;

10 | P a g e
20EC413

void setup()
{
pinMode(in1, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in1, OUTPUT);

Serial.begin(9600);
}

void loop()
{
float leftVal = analogRead (Lldr);
float rightVal = analogRead (Rldr);
float ILeft = leftVal * 5000 / 1024;
float IRight = rightVal * 5000 / 1024;
int LLight = map (ILeft, 241, 4740, 0, 100);
int RLight = map (IRight, 241, 4740, 0, 100);
if (LLight > RLight)
{
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
digitalWrite (in3, LOW);
digitalWrite (in4, HIGH);
Serial.println ("Rotate Right");
}
else if (LLight < RLight)
{
digitalWrite (in1, LOW);
digitalWrite (in2, HIGH);
digitalWrite (in3, HIGH);
digitalWrite (in4, LOW);
Serial.println ("Rotate Left");
}
else
{
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
digitalWrite (in3, HIGH);
digitalWrite (in4, LOW);
Serial.println ("Move Forward");

11 | P a g e
20EC413

}
}

OUTPUT:

12 | P a g e
20EC413

CONCLUSION:

We successfully implemented a light-following robot using LDR and DC motors on TinkerCAD.


The robot uses the input from the LDRs to adjust its movement, following the direction of light
intensity.

13 | P a g e
20EC413

PRACTICAL NO.: 04
AIM:

Write a program to create a web server based on ESP32 to host a web page.

SOFTWARE USED:

Arduino IDE 1.8.19

HARDWARE COMPONENT USED:

NodeMCU

CIRCUIT:

PROGRAM:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "index.h" //Our HTML webpage

const char* ssid = "V2029";


const char* password = "shalu_71";
ESP8266WebServer server(80); //Server on port 80

void handleRoot() {
String s = MAIN_page; //Read HTML contents
server.send(200, "text/html", s); //Send web page
}

14 | P a g e
20EC413

void setup(void){
Serial.begin(115200);

WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.on("/", handleRoot);

server.begin(); //Start server


Serial.println("HTTP server started");
}

void loop(void){
server.handleClient(); //Handle client requests
}

index.h

const char MAIN_page[] PROGMEM = R"=====(


<HTML>
<HEAD>
<TITLE>My first web page</TITLE>
</HEAD>
<BODY>
<CENTER>
<B>Hello</B>
<p>
<B>20EC413 shalini </B>
<br>
<B>Web server based on ESP32 to host a web page </B>
</p>
</CENTER>

15 | P a g e
20EC413

</BODY>
</HTML>
)=====";

OUTPUT:

shalini

20EC413 shalini
kotecha

CONCLUSION:
Thus, we have successfully created a web server using a Wi-Fi-enabled board, allowing the
board to host a simple webpage. This basic program sets up a server, connects to Wi-Fi, and
responds to incoming client requests by sending a basic HTML page.

16 | P a g e
20EC413

PRACTICAL NO.: 05
AIM:

Write a program to update data on a web page hosted on ESP32.

SOFTWARE USED:

Arduino IDE 1.8.19

HARDWARE COMPONENT USED:

NodeMCU

CIRCUIT:

PROGRAM:
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#include <SPIFFS.h>
#else
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <Hash.h>
#include <FS.h>
#endif

#include <ESPAsyncWebServer.h>

17 | P a g e
20EC413

AsyncWebServer server(80);

const char* ssid = "V2039";


const char* password = "shalu_71";

const char* PARAM_STRING = "inputString";


const char* PARAM_INT = "inputInt";
const char* PARAM_FLOAT = "inputFloat";

// HTML web page to handle 3 input fields (inputString,


inputInt, inputFloat)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<title>ESP Input Form</title>
<meta name="viewport" content="width=device-width, initial-
scale=1">
<script>
function submitMessage() {
alert("Saved value to ESP SPIFFS");
setTimeout(function(){ document.location.reload(false); },
500);
}
</script></head><body>
<form action="/get" target="hidden-form">
inputString (current value %inputString%): <input
type="text" name="inputString">
<input type="submit" value="Submit" onclick =
"submitMessage()">
</form><br>
<form action="/get" target="hidden-form">
inputInt (current value %inputInt%): <input type="number "
name = "inputInt">
<input type="submit" value="Submit" onclick =
"submitMessage()">
</form><br>
<form action="/get" target="hidden-form">
inputFloat (current value %inputFloat%): <input type="number
" name="inputFloat">
<input type="submit" value="Submit"
onclick="submitMessage()">
</form>

18 | P a g e
20EC413

<iframe style="display:none" name="hidden-form"></iframe>


</body></html>)rawliteral";

void notFound(AsyncWebServerRequest *request) {


request->send(404, "text/plain", "Not found");
}

String readFile(fs::FS &fs, const char * path){


Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path, "r");
if(!file || file.isDirectory()){
Serial.println("- empty file or failed to open file");
return String();
}
Serial.println("- read from file:");
String fileContent;
while(file.available()){
fileContent+=String((char)file.read());
}
file.close();
Serial.println(fileContent);
return fileContent;
}

void writeFile(fs::FS &fs, const char * path, const char *


message){
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, "w");
if(!file){
Serial.println("- failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- write failed");
}
file.close();
}

// Replaces placeholder with stored values

19 | P a g e
20EC413

String processor(const String& var){


//Serial.println(var);
if(var == "inputString"){
return readFile(SPIFFS, "/inputString.txt");
}
else if(var == "inputInt"){
return readFile(SPIFFS, "/inputInt.txt");
}
else if(var == "inputFloat"){
return readFile(SPIFFS, "/inputFloat.txt");
}
return String();
}

void setup() {
Serial.begin(115200);
// Initialize SPIFFS
#ifdef ESP32
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting
SPIFFS");
return;
}
#else
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting
SPIFFS");
return;
}
#endif

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed!");
return;
}
Serial.println();
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());

20 | P a g e
20EC413

// Send web page with input fields to client


server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});

// Send a GET request to


<ESP_IP>/get?inputString=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest
*request) {
String inputMessage;
// GET inputString value on
<ESP_IP>/get?inputString=<inputMessage>
if (request->hasParam(PARAM_STRING)) {
inputMessage = request->getParam(PARAM_STRING)->value();
writeFile(SPIFFS, "/inputString.txt",
inputMessage.c_str());
}
// GET inputInt value on
<ESP_IP>/get?inputInt=<inputMessage>
else if (request->hasParam(PARAM_INT)) {
inputMessage = request->getParam(PARAM_INT)->value();
writeFile(SPIFFS, "/inputInt.txt", inputMessage.c_str());
}
// GET inputFloat value on
<ESP_IP>/get?inputFloat=<inputMessage>
else if (request->hasParam(PARAM_FLOAT)) {
inputMessage = request->getParam(PARAM_FLOAT)->value();
writeFile(SPIFFS, "/inputFloat.txt",
inputMessage.c_str());
}
else {
inputMessage = "No message sent";
}
Serial.println(inputMessage);
request->send(200, "text/text", inputMessage);
});
server.onNotFound(notFound);
server.begin();
}

21 | P a g e
20EC413

void loop() {
// To access your stored values on inputString, inputInt,
inputFloat
String yourInputString = readFile(SPIFFS, "/inputString.txt");
Serial.print("*** Your inputString: ");
Serial.println(yourInputString);

int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt();


Serial.print("*** Your inputInt: ");
Serial.println(yourInputInt);

float yourInputFloat = readFile(SPIFFS,


"/inputFloat.txt").toFloat();
Serial.print("*** Your inputFloat: ");
Serial.println(yourInputFloat);
delay(5000);
}

OUTPUT:

22 | P a g e
20EC413

CONCLUSION:

We have successfully created a program to update data on a web page hosted by a Wi-Fi-enabled
board.

23 | P a g e
20EC413

PRACTICAL NO.: 06
AIM:

Write a program to auto - update data on a web page using AJAX.

SOFTWARE USED:

Arduino IDE 1.8.19

HARDWARE COMPONENT USED:

NodeMCU

CIRCUIT:

PROGRAM:
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n
\r\n";

String html_1 = R"=====(


<!DOCTYPE html>
<html>
<head>
<meta name='viewport' content='width=device-width, initial-
scale=1.0'/>
<meta charset='utf-8'>
<style>
body {font-size:100%;}

24 | P a g e
20EC413

#main {display: table; margin: auto; padding: 0 10px 0


10px; }
h2 {text-align:center; }
p { text-align:center; }
</style>

<script>
function updateCount()
{
ajaxLoad('getCount');
}

var ajaxRequest = null;


if (window.XMLHttpRequest)
{
ajaxRequest =new XMLHttpRequest();
}
else
{
ajaxRequest =new ActiveXObject("Microsoft.XMLHTTP");
}

function ajaxLoad(ajaxURL)
{
if(!ajaxRequest){ alert('AJAX is not supported.'); return;
}

ajaxRequest.open('GET',ajaxURL,true);
ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 4 && ajaxRequest.status ==
200)
{
var ajaxResult = ajaxRequest.responseText;
document.getElementById('count_P').innerHTML =
ajaxResult;
}
}
ajaxRequest.send();
}

25 | P a g e
20EC413

setInterval(updateCount, 5000);

</script>
<title>Auto Update Example Using Javascript 2</title>
</head>

<body>
<div id='main'>
<h2> Auto Update Using AJAX </h2>
<div id='count_DIV'>
<p id='count_P'>Count = 0</p>
</div>
</div>
</body>
</html>
)=====";

#include <ESP8266WiFi.h>

char ssid[] = "V2029";


char pass[] = "shalu_71";

WiFiServer server(80);

String request = "";


unsigned int count = 0;

void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("Serial started at 115200");
Serial.println();
// Connect to a WiFi network
Serial.print(F("Connecting to ")); Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}

26 | P a g e
20EC413

Serial.println("");
Serial.println(F("[CONNECTED]"));
Serial.print("[IP ");
Serial.print(WiFi.localIP());
Serial.println("]");
// start a server
server.begin();
Serial.println("Server started");
}

void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) { return; }
// Read the first line of the request
request = client.readStringUntil('\r');
if ( request.indexOf("getCount") > 0 )
{
count ++;
client.print( header );
client.print( "Count = " );
client.print( count );

Serial.print("Count=");
Serial.println(count);
}
else
{
client.flush();
client.print( header );
client.print( html_1 );
count = 0;
}
delay(5);
}

27 | P a g e
20EC413

OUTPUT:

CONCLUSION:

We have successfully implemented a program that uses AJAX to auto-update data on a web
page.

28 | P a g e
20EC413

PRACTICAL NO.: 07
AIM:

Write a program to control an LED from a web page using ESP32.

SOFTWARE USED:

Arduino IDE 1.8.19

HARDWARE COMPONENT USED:

Red LED, NodeMCU, 220 Ω Resistor

CIRCUIT:

PROGRAM:
#include <ESP8266WiFi.h>

const char* ssid = "V2029";


const char* password = "shalu_71";

int LED = 2; // led connected to GPIO2 (D4)

WiFiServer server(80);

29 | P a g e
20EC413

void setup()
{
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Serial.print("Connecting to the Newtork");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
server.begin(); // Starts the Server
Serial.println("Server started");
Serial.print("IP Address of network: ");
Serial.println(WiFi.localIP());
}

void loop()
{
WiFiClient client = server.available();
if (!client)
{
return;
}
while(!client.available())
{
delay(1);
}
String request = client.readStringUntil('\r');
client.flush();
int value = LOW;
if(request.indexOf("/LED=ON") != -1)
{
digitalWrite(LED, HIGH); // Turn LED ON
value = HIGH;
}
if(request.indexOf("/LED=OFF") != -1)
{
digitalWrite(LED, LOW); // Turn LED OFF

30 | P a g e
20EC413

value = LOW;
}

//*-------HTML Page Code -------*//

client.println("HTTP/1.1 200 OK"); //


client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print(" CONTROL LED: ");
if(value == HIGH)
{
client.print("ON");
}
else
{
client.print("OFF");
}
client.println("<br> <br>");
client.println("<a href=\"/LED=ON\"\"> <button> ON </button>
</a>");
client.println("<a href=\"/LED=OFF\"\"> <button> OFF </button>
</a> <br />");
client.println("</html>");
delay(1);
}

OUTPUT:

31 | P a g e
20EC413

CONCLUSION:

We have successfully created a program to control an LED from a web page using a Wi-Fi-
enabled board.

32 | P a g e
20EC413

PRACTICAL NO.: 08
AIM:

Write a program to read data from a switch on the Blynk dashboard and update the status of the
hardware LED on Wi-Fi enabled board.

SOFTWARE USED:

Arduino IDE 1.8.19, Blynk Dashboard

HARDWARE COMPONENT USED:

Yellow LED, NodeMCU, 220 Ω Resistor

CIRCUIT:

PROGRAM:
#define BLYNK_TEMPLATE_ID "TMPL3KAxnsbrP"
#define BLYNK_TEMPLATE_NAME "LED"
#define BLYNK_AUTH_TOKEN "DtGrkeuUR4lr8Ly1t-WmTsyWpSfQHTr9"

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

33 | P a g e
20EC413

char auth[] = BLYNK_AUTH_TOKEN;


char ssid[] = "V2029";
char pass[] = "shalu_71";
int ledpin = 2; //D4 pin
BLYNK_WRITE(V0)
{
int Switch_state = param.asInt();
digitalWrite(ledpin, Switch_state);
}

void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(ledpin,OUTPUT);
}

void loop()
{
Blynk.run();
}

OUTPUT:

34 | P a g e
20EC413

CONCLUSION:

We successfully implemented a program that reads data from a switch on the Blynk dashboard
and updates the status of a hardware LED. The Blynk library facilitates seamless communication
between the hardware and the Blynk app, allowing for remote control of the LED through the
Blynk dashboard switch.

35 | P a g e
20EC413

PRACTICAL NO.: 09
AIM:

Write a program to read data from hardware and update the status on the Blynk dashboard.

SOFTWARE USED:

Arduino IDE 1.8.19, Blynk Dashboard

HARDWARE COMPONENT USED:

Yellow LED, NodeMCU, 220 Ω Resistor

CIRCUIT:

PROGRAM:
#define BLYNK_TEMPLATE_ID "TMPL3KAxnsbrP"
#define BLYNK_TEMPLATE_NAME "LED"
#define BLYNK_AUTH_TOKEN "DtGrkeuUR4lr8Ly1t-WmTsyWpSfQHTr9"

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

36 | P a g e
20EC413

BlynkTimer timer;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "V2029";
char pass[] = "shalu_71";

int ledpin = 2; //D4 pin

BLYNK_WRITE(V0)
{
int Switch_state = param.asInt();
digitalWrite(ledpin, Switch_state);
}

void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(ledpin,OUTPUT);
timer.setInterval(2000, readState);
}

void readState()
{
Blynk.virtualWrite(V1, "20EC402");
int LED_state = digitalRead(ledpin);
if (LED_state == HIGH){
Blynk.virtualWrite(V2, "LED ON");
}
else
{
Blynk.virtualWrite(V2, "LED OFF");
}
}

void loop()
{
Blynk.run();
timer.run();
}

37 | P a g e
20EC413

OUTPUT:

CONCLUSION:

We have successfully implemented a program that reads data from hardware and updates the
status on the Blynk dashboard.

38 | P a g e
20EC413

PRACTICAL NO.: 10
AIM:

Write a program to control a servo motor from the Blynk dashboard.

SOFTWARE USED:

Arduino IDE 1.8.19, Blynk Dashboard

HARDWARE COMPONENT USED:

NodeMCU, Micro Servo

CIRCUIT:

PROGRAM:
#define BLYNK_TEMPLATE_ID "TMPL3KAxnsbrP"
#define BLYNK_TEMPLATE_NAME "LED"
#define BLYNK_AUTH_TOKEN "DtGrkeuUR4lr8Ly1t-WmTsyWpSfQHTr9"

39 | P a g e
20EC413

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

char auth[] = BLYNK_AUTH_TOKEN;


char ssid[] = "V2029";
char pass[] = "shalu_71";

Servo myservo;
BlynkTimer timer;
int pos = 0;

BLYNK_WRITE(V2)
{
int pos = param.asInt();
myservo.write(pos);
}

void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
myservo.attach(13); //D7 pin
}

void loop()
{
Blynk.run();
}

40 | P a g e
20EC413

OUTPUT:

CONCLUSION:

By establishing a connection to the Blynk server and utilizing the Blynk library, the servo
motor's position is updated in real-time based on the slider value on the Blynk app.

41 | P a g e

You might also like