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

K. J.

Somaiya College of Engineering, Mumbai-77


(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

Course Name: WSN & IoT Semester: VIII


Date of
17/01/2024 Batch No: 1
Performance:
Faculty Name: Prof. Bharati Khedkar Roll No: 16010220062
Faculty Sign &
Grade/Marks: / 25
Date:

Experiment No:3
Title: Controlling LED through an HTTP page Using Node MCU

Aim and Objective of the Experiment:


AIM: To Understand Web page controlled LED using NodeMCU connected on a local network.

COs to be achieved: Students will be able to design application of IoT using appropriate platform
CO3: Describe fundamentals of IoT
CO5: Apply concept of WSN and IoT for domain specific applications

Theory:

How to program NodeMCU and ESP32 using Arduino


IDE
In order to use Arduino IDE to program the NodeMCU, you have to introduce it to the
software at first.
To do this copy the following code and follow the steps below:
http://arduino.esp8266.com/stable/package_esp8266com_index.json

For ESP32:
https://espressif.github.io/arduino-esp32/package_esp32_index.json
step1. Choose Preferences in the File menu and enter the copied code in Additional Board
Manager URLs part. Then press OK.

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

Step2. Search the word ESP8266(Node MCU) in Boards>boards manager from Tools
menu. Then install ESP8266 boards. After complete installation, you will see the
INSTALLED label on ESP8266 boards.

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

After these two steps, you can see ESP8266 based boards such as NodeMCU in your
Arduino IDE boards list, and you can choose your desired board to upload the code.

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

You can see the NodeMCU pinouts below

In order to use digital pins, you should select GPIO numbers.


For example, the D7 pin is defined as GPIO13. So you should set up the pin number 13
whenever you want to use D7 in your program. Also, you can use pin D2(GPIO4) as SDA
and pin D1(GPIO5) as SCL

Selection of Port :

2) Go to Tools > Port and select a COM port available. If the COM port is grayed out,
this means you don’t have the required USB drivers. Check the section Installing USB
Drivers before proceeding.

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

Upload Code to the ESP8266 NodeMCU


using Arduino IDE
First, make sure you have an ESP8266 selected in Tools > Board. If you’re using the
ESP8266-12E NodeMCU Kit as shown in previous pictures, select the NodeMCU 1.0
(ESP-12E Module) option.

Then, go to File > Examples > ESP8266WiFi > WiFiScan.


This will load a sketch that scans Wi-Fi networks within the range of your ESP8266
board.

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

Connect your ESP8266 development board to your computer using a USB cable.
Now, follow the next steps to upload the code.

1) Go to Tools > Board, scroll down to the ESP8266 section and select your ESP8266
board.
2) Go to Tools > Port and select a COM port available. If the COM port is grayed out,
this means you don’t have the required USB drivers. Check the section Installing USB
Drivers before proceeding.
3) Press the upload button.

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

The code should be successfully uploaded to the board after a few seconds.

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

For ESP32 :
1. Open the Boards Manager. Go to Tools > Board >Boards Manager…
Search for ESP32 and install the “ESP32 by Espressif Systems“:
That’s it. It will be installed after a few seconds.

2. After this, restart your Arduino IDE.

Then, go to Tools > Board and check that you have ESP32 boards available.

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

Overview:

Stepwise-Procedure:

1) Write the code in arduino IDE


2) Connect the led to nodemcu
3) Upload the code onnode mcu
4) Get the IP address of web page from serial monitor
5) Control led through the page

Observation Table:
#include <ESP8266WiFi.h>

#include <WiFiClient.h>

#include <ESP8266WebServer.h>

/* Set these to your


desired credentials. */

const char *ssid = "Megh ka hotspot"; //Enter your WIFI ssid

const char *password = "01000101"; //Enter your WIFI password

ESP8266WebServer
server(80);

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

void handleRoot() {
String html = "<form action=\"/LED_BUILTIN_on\" method=\"get\"><button
type=\"submit\" style=\"font-size:200px;
padding:200px;\">Off</button></form>";
html += "<form action=\"/LED_BUILTIN_off\" method=\"get\"><button
type=\"submit\" style=\"font-size:200px;
padding:200px;\">On</button></form>";
server.send(200, "text/html", html);
}

void handleSave() {

if (server.arg("pass") != "") {

Serial.println(server.arg("pass"));

void setup() {

pinMode(LED_BUILTIN, OUTPUT);

delay(3000);

Serial.begin(115200);

Serial.println();

Serial.print("Configuring access point...");

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

delay(500);

Serial.print(".");

Serial.println("");

Serial.println("WiFi connected");

Serial.println("IP address:");

Serial.println(WiFi.localIP());

server.on ( "/",
handleRoot );

server.on ("/save", handleSave);

server.begin();

Serial.println ( "HTTP server started" );

server.on("/LED_BUILTIN_on", []() {

digitalWrite(LED_BUILTIN, 1);

Serial.println("on");

handleRoot();

});

server.on("/LED_BUILTIN_off", []() {

digitalWrite(LED_BUILTIN, 0);

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

Serial.println("off");

handleRoot();

});

void loop() {

server.handleClient();

###LED BLINK CODE:


int LED = D4;

void setup()
{
pinMode(LED, OUTPUT);
}

void loop(){

digitalWrite(LED , HIGH);
delay(1000);
digitalWrite(LED , LOW);
delay(1000);

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

Post Lab Subjective/Objective type Questions:

Q1. What is the difference between microprocessor and microcontroller?

Microprocessors and microcontrollers are both essential components in embedded systems, but
they serve different purposes and have distinct characteristics. Here are the key differences
between a microprocessor and a microcontroller:

1. **Functionality:**

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

- **Microprocessor:** Primarily designed to execute general-purpose tasks. It is the central


processing unit (CPU) of a computer system and is responsible for processing instructions,
performing arithmetic and logic operations, and managing data flow.
- **Microcontroller:** Specifically designed for embedded systems and is a compact
integrated circuit that contains a processor core, memory, and peripherals. Microcontrollers are
intended to control a specific task or function within a system.

2. **Components:**
- **Microprocessor:** Typically consists of just the CPU. It relies on external components
like memory, input/output devices, and other peripherals to form a complete computer system.
- **Microcontroller:** Integrates the CPU, memory (RAM and ROM), input/output ports,
timers, and sometimes other peripherals like analog-to-digital converters on a single chip.

3. **Applications:**
- **Microprocessor:** Used in general-purpose computing applications such as personal
computers, servers, and workstations.
- **Microcontroller:** Employed in dedicated control applications, such as in embedded
systems found in appliances, automotive systems, industrial automation, robotics, and more.

4. **Complexity and Cost:**


- **Microprocessor:** Generally more powerful and complex, often requiring external
components to function. As a result, they tend to be more expensive.
- **Microcontroller:** Designed for specific applications, which makes them simpler and
more cost-effective. They are optimized for the tasks they are meant to perform.

5. **Power Consumption:**
- **Microprocessor:** Typically designed for higher performance and may consume more
power.
- **Microcontroller:** Designed for efficiency, with lower power consumption, making them
suitable for battery-powered and low-power applications.

6. **Programming:**
- **Microprocessor:** Usually programmed in high-level languages and used for a variety of
applications.
- **Microcontroller:** Often programmed in low-level languages or assembly language and
tailored for specific tasks.

In summary, while microprocessors are suitable for general-purpose computing,


microcontrollers are specialized for embedded systems and designed to control specific
functions efficiently.

Q 2 . What is Node MCUused for?

The NodeMCU is a low-cost open-source IoT (Internet of Things) platform based on the
ESP8266 Wi-Fi module. It provides an easy and inexpensive way to connect embedded systems

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

or devices to the internet and enable them to communicate with each other. Here are some
common uses and applications of NodeMCU:

1. **IoT Prototyping:** NodeMCU is popular for prototyping IoT projects due to its simplicity
and ease of use. It allows developers to quickly create and test connected devices.

2. **Home Automation:** NodeMCU can be used in home automation projects to control and
monitor devices such as lights, thermostats, door locks, and sensors over Wi-Fi.

3. **Sensor Networks:** It is often employed to create sensor networks where various sensors
(temperature, humidity, motion, etc.) are connected to NodeMCU modules, and the data is sent
to a central server or cloud platform.

4. **Smart Agriculture:** NodeMCU can be used in agricultural applications to monitor and


control parameters like soil moisture, temperature, and humidity. This information can then be
used to automate irrigation systems.

5. **Industrial Monitoring:** In industrial settings, NodeMCU can be utilized for monitoring


equipment status, collecting data from sensors, and transmitting this data for analysis or control
purposes.

6. **Wireless Communication:** It enables wireless communication between devices, making it


suitable for projects where wired connections are not practical. Devices equipped with
NodeMCU can communicate with each other over a Wi-Fi network.

7. **Educational Projects:** Due to its affordability and ease of programming, NodeMCU is


often used in educational settings for teaching and learning about IoT, embedded systems, and
programming.

8. **Smart Cities:** In the context of smart city initiatives, NodeMCU can be applied to create
intelligent solutions for monitoring and managing urban infrastructure, such as smart streetlights
or waste management systems.

9. **Remote Monitoring and Control:** NodeMCU allows remote monitoring and control of
devices. For example, you can use it to check the status of a device, receive notifications, or
control it remotely over the internet.

NodeMCU is compatible with the Arduino IDE, making it accessible to a wide community of
developers who are familiar with Arduino programming. It simplifies the process of integrating
Wi-Fi capabilities into projects, making it a popular choice for IoT enthusiasts and developers.

WSN & IoT Semester: VIII Academic Year: 2023-24


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics Engineering

Conclusion:
Implemented and Understood Web page controlled LED using NodeMCU connected on a local
network.

Signature of faculty in-charge with Date:

WSN & IoT Semester: VIII Academic Year: 2023-24

You might also like