IoT Lab Manual

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

RV Institute of Technology and Management

_________________________________________________________________________________
Experiment No 1
Built-in LED Blinking using Node MCU

Aim: To blink the on-board built-in LED using Node MCU


Components Required:
• NodeMCU x 1
• Micro USB cable x 1
• PC x 1
Software Required:
• Arduino IDE (version 1.8.19)
Theory:
LED_BUILTIN is the pin with the on-board LED. It's used in the blink example so that blink
will work even on boards where the on-board LED is on a different pin.
Blinking a LED is the “Connection” of embedded programming and most development board
have an integrated LED. This makes it easier to run a basic piece of code, without having to
hookup any external components.

LED_BUILTIN PIN ON BOARD.

The NodeMCU ESP8266 board has two of those LEDs. One on the NodeMCU PCB and
another on the ESP-12 module’s PCB.

In this tutorial, we are going to learn built-in command of Arduino IDE which will help to blink
LED using ESP8266 NodeMCU.

IoT Lab Manual 6|Page


RV Institute of Technology and Management
_________________________________________________________________________________
Interfacing Diagram:

Program Code:
//the setup function runs once when you press reset or power the board
void setup()
{
//initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
//the loop function runs over and over again forever
void loop ()
{
digitalWrite (LED_BUILTIN, HIGH); // turn the LED offby making the voltage level High
delay (1000); // wait for one second
digitalWrite (LED_BUILTIN, LOW); // turn the LED onby making the voltage level Low
delay (2000); //wait for two seconds
}

Note: pinMode (pin number, OUTPUT): pinMode is name of function. Pin number and
OUTPUT are arguments of the function. For example, we have connected pin 7 and 8 with two
LEDs and want to use pin 7, 8 as an output to blink LEDs.

digitalWrite (pin number, HIGH/LOW):digitalWrite is a name of function. This function


makes a specified pin number in arguments either high and low. For example, digitalWrite (9,
HIGH) will make the digital pin 9 digital high.

IoT Lab Manual 7|Page


RV Institute of Technology and Management
_________________________________________________________________________________
Procedure:
• Connect the NodeMCU to PC via micro-USB
• Right click on the tools, check the connection to Port (In this experiment COM15)
• Open the Arduino IDE
• Write the code in Arduino IDE
• Verify the codes for any syntax error using key (files)
• Upload the codes (Dump the code) using key (sketch)into NodeMCU using the
micro USB cable
• Running the results

Outcome/Result:
The onboard LED blink as per the delay

IoT Lab Manual 8|Page


RV Institute of Technology and Management
_________________________________________________________________________________

Experiment No 2
External Two LED On/Off using NodeMCU

Aim: To control external two LED lights using Node MCU


Components Required:
• Bread board
• NodeMCU x 1
• Micro USB cable x 1
• PC x 1
• LED x 2

Software Required:
• Arduino IDE (version 1.8.19)
Theory:
In this experiment, we will learn to use digital output pins of NodeMCU by blinking two LED
lights. GPIO pins (general-purpose input-output pins) of NodeMCU as digital output pins.
After conducting this experiment, we will be able to interface other output devices with
NodeMCU with two LED lights (led1 and led2).

Interfacing Diagram:

IoT Lab Manual 9|Page


RV Institute of Technology and Management
_________________________________________________________________________________

Outcome/Result:
The onboard led1 and led2 blinks as per the delay

IoT Lab Manual 11 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________
Experiment No 3
Title: Traffic Signal Control using Node MCU

Aim : Traffic Signal Control using Node MCU


Components Required:
NodeMCU, Bread board, MicroUSB, Computer/ Laptop, Red LED, Green LED, Yellow
LED, patch wires.
Software Required: Arduino
Theory:
Roads without any supervision or guidance can lead to traffic conflicts and accidents.
Traffic signals are required for an orderly flow of traffic. A traffic signal is used as an instructing
device that indicates the road user to act as per the displayed sign. Traffic lights allow everyone
to cross the intersection point one by one, reducing conflicts between vehicles entering
intersection points from different directions. It provides road safety, also helps to solve traffic in
simple manners.

There are different colours in traffic lights. Each light has a meaning, and these lights tell
drivers what to do.

• Red light ON- A driver should stop.


• Yellow light ON- A driver has to slow down and be ready to stop.
• Greenlight ON- A driver can start driving or keep driving.

Interfacing Diagram:

IoT Lab Manual 12 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________
Experiment No 4
Title: Switch & LED interfacing using Node MCU

Aim To turn ON/OFF LED with Switch using Node MCU


Components Required:

ESP8266 NodeMCU, Bread board, MicroUSB, Computer/ Laptop, LED, Switch, patch
Jumper wires.
Software Required: Arduino IDE
Theory:
In the era full of technologies, we are in search of technologies that we have full control of. So,
we require a gadget that is a semi-automatic (for example: Interface NodeMCU with Switch
which works like a remote control device),i.e., which is controlled manually as well as
automatically. One component that is used to make our device controlled manually at just one
click is a switch.
In the previous experiment, we have learned how to control the LED blink for every second,
now we will control the LED by using a button .When pressed the button, the LED was lighten
and turn off when released i.e switch button is a passive device that is used to switch between
the active high and active low states.

Interfacing Diagram:

IoT Lab Manual 15 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________
Experiment No -5
NodeMCU as a Web Server

Aim : To establish NodeMCU as a Web Server


Components Required:
NodeMCU
Micro USB

Software Required:
Arduino IDE

Theory:
NodeMCU is an open-source platform based on ESP8266 which can connect objects and
enables data transfer using the Wi-Fi protocol. It has some important features of
microcontrollers such as GPIO, PWM, ADC etc. It can also act as a Web Server and allow
client to connect to it.
A web server is software and hardware that uses HTTP (Hypertext Transfer Protocol) and other
protocols to respond to client requests made over the World Wide Web.
Interfacing Diagram:

IoT Lab Manual 17 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________

Program Code:
#include<ESP8266WiFi.h>
WiFiClient client;
WiFiServer server(80);
#define led 13
void setup() // put your setup code here, to run once
{
Serial.begin (115200);
WiFi.begin("RVITM","rvitm@123"); //user name and password of wifi connection.
while(WiFi.status()!= WL_CONNECTED) //if wifi not connected wait delay
{
delay (200);
Serial.print(" ..");

IoT Lab Manual 21 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________

Experiment No 7
Title: NodeMCU ESP8266 as a WiFI Access point ( Hot spot)

Aim : NodeMCU ESP8266 as a WiFI Access point( Hot spot)


Components Required:
NodeMCU
Bread board
USB cable
Computer/ Laptop
Software Required: Arduino IDE
Theory: In this Experiment, you will learn how to set your ESP8266 NodeMCU as an Access
Point (AP) using Arduino IDE. This allows you to connect directly to your ESP8266 via Wi-
Fi without a wireless router and here we connect the ESP8266 to a wireless router. In this
configuration, we can access the ESP8266 through the local network and the router acts as an
access point and the ESP8266 is set as a station.
Interfacing Diagram:

Figure: Schematic of ESP8266 as an access point to the client.

IoT Lab Manual 24 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________
digitalWrite(led,LOW);
}
}
}
Procedure:
1. Type the program on Arduino IDE.
2. If successful, the device should be visible as a WiFi access point with the SSID and password
you specified in the code above.
3. We used WiFi.softAP() instead of WiFi.begin() (on line 6) — this creates the NodeMCU
WiFi access point.
4. Next, get the access point IP address using the softAPIP() method and print it in the Serial
Monitor.
5. Having the ESP8266 running the sketch, in your smartphone open your Wi-Fi settings and
tap the ESP8266-Access-Point network:

6. Enter the password you’ve defined earlier.

IoT Lab Manual 26 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________

Fig: NodeMCU as HTTP Server using Wi-Fi AP mode

We can set SSID and Password for AP mode which will be used to authenticate other devices
while connecting to it.

NodeMCU as HTTP Server using Wi-Fi STA mode

NodeMCU has Station (STA) mode using which it can connect to the existing wi-fi network
and can act as an HTTP server with an IP address assigned by that network.

Fig: NodeMCU as HTTP Server using Wi-Fi STA mode

IoT Lab Manual 29 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________
NodeMCU gets IP from the Wi-Fi router to which it is connected. With this IP address, it can
act as an HTTP server to which any wi-fi device can connect.

Example

Let’s write Arduino Sketch to enable NodeMCU as an HTTP server with Wi-Fi STA/AP mode
and control an LED connected at the server-side from the client-side.

Here we have connected LED to the pin no. 2 i.e. D2 pin on the NodeMCU board as shown in
the below figure.

HTML page for client

As we are making an HTTP server for LED on/off functionality, we are going to make a simple
HTML page that will be visible at the client-side and able to take user input for LED on/off. It
is a user-friendly representation of button input that takes input from the user on click.

We need to write two HTML pages for LED ON and LED OFF state i.e. when a client clicks
the LED ON button, then in the next action, we need to provide options for LED OFF. Below
are the two HTML code snippets for LED ON and LED OFF state presentation. Following
figure gives the outline of the webpage design.

IoT Lab Manual 30 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________

Interfacing Diagram:

Fig: Interfacing Diagram

Program Code:
#include<ESP8266WiFi.h>
#define greenLED 13
#define redLED 2

WiFiClient client;
WiFiServer server(80);

void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin("RVITM", "rvitm@123");
while(WiFi.status()!=WL_CONNECTED)
{
Serial.print("..");
delay(200);
}
Serial.println();
Serial.println("NodeMCU is connected to WiFi");
Serial.println(WiFi.localIP());

IoT Lab Manual 31 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________
2. Compose a code using an Arduino IDE.
3. Dump the code to NodeMCU hardware.
4. Open the browser and place the NodeMCU IP on the HTTP window.
5. Then select the ON OFF button on the browser to on and off the LED’s connected to
the Node MCU.
6. Same result you can observe in the serial monitor.

Outcome/Result:
1. Observe the LED ON/OFF in the hardware setup
2. Following output, you can see in the serial monitor:

IoT Lab Manual 33 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________

Interfacing Diagram:

Humidity and Temperature Sensor

Program Code:

IoT Lab Manual 35 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________
To get the graph, go to humidity sensor link in thingsspeak.com and click on private view

Graph:

IoT Lab Manual 38 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________

Fig: Connection between NodeMCU and Cloud

Example

writing a C/C++ based Arduino sketch for NodeMCU as an HTTP Client and GET/POST the
data from to the Thingspeak server.

In this experiment, we are using the Thingspeak server for HTTP Client demo purposes.

Thingspeak is an open IOT platform where anyone can visualize and analyze live data from
their sensor devices. Also, we can perform data analysis on data posted by remote devices with
Matlab code in Thingspeak. To learn more about Thingspeak refer link:
https://thingspeak.com/pages/learn_more.

Just sign up and create a channel. We have below the example channel ID and write key on
Thingspeak for data send and receive.

• Channel ID is = 309236
• Write Key is = 1EYZIS5OCRJSKZHG

Note: Do not forget to tick the Make Public field in the channel setting option on your
Thingspeak channel.

IoT Lab Manual 40 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________

Interfacing Diagram:

Fig: Interfacing Diagram

Interfacing Program:

IoT Lab Manual 41 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________

4. Click “Save Channel”


2. Modify Channel Sharing Settings
1. After creating your channel, go the “Sharing” tab and enable “Share Channel
With Everyone”

3. Copy Channel ID
1. Head to the “Channel Settings” tab and copy the “Channel ID” (you will need
to input this ID in your code)

IoT Lab Manual 43 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________

4. Place Write URL on the browser to control the LED

Open new tab in browser and paste above


URL

IoT Lab Manual 44 | P a g e


RV Institute of Technology and Management
_________________________________________________________________________________
Outcome/Result:

3. Observe the LED ON/OFF in the hardware setup


4. Following output, you can see in the serial monitor:

IoT Lab Manual 45 | P a g e

You might also like