0% found this document useful (0 votes)
23 views8 pages

36 IOTExp4

Uploaded by

xidadom330
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

36 IOTExp4

Uploaded by

xidadom330
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment No: 4

Node MCU Wireless Communication

Node MCU as a web server for Traffic Signal Control.

Name of the Student: _Deepak More__ Div. _CSAI-A__ Roll No.___36_

Aim: Node MCU wireless communication. Node MCU as a web server for Traffic Signal
Control.

Components Required:

1) Node MCU – 1
2) Micro USB Cable – 1
3) PC/Laptop – 1
4) Connecting Wires
5) Bread Board – 1
6) Red, Yellow Green LEDs – (each 1)
7) Resistor 200 Ohm – 3

Software Required:

Arduino IDE

Theory:

We are familiar with blinking LED using Arduino boards as this is the fundamental step towards
using a new development board. In this experiment, first we will see how to connect an external
LED with NodeMCU and how to blink it using GPIO pins of ESP8266 NodeMCU. After that
we will see how to connect buzzer with Node MCU.

Procedure:

Step 1: Connect Node MCU to PC / Laptop with the help of micro USB cable.

HOW TO CONNECT LED:

Make the circuit diagram on bread board according to connection diagram shown below. Positive
terminal of LED (RED) long leg of the buzzer] is connected to the one point of the Resistor 200
1
Ohm and another point is connected to the D5 pin of the NodeMCU, negative terminal of LED
(RED) [short leg of the buzzer] is connected to the ground pin.

Positive terminal of LED (YELLOW) long leg of the buzzer] is connected to the one point of the
Resistor 200 Ohm and another point is connected to the D6 pin of the NodeMCU, negative
terminal of LED (YELLOW) [short leg of the buzzer] is connected to the ground pin.

Positive terminal of LED (GREEN) long leg of the buzzer] is connected to the one point of the
Resistor 200 Ohm and another point is connected to the D7 pin of the NodeMCU, negative
terminal of LED (GREEN) [short leg of the buzzer] is connected to the ground pin.

Step 2: Open new Sketch, Go to file ----> New

Step 3: Write following code in new sketch

#include <ESP8266WiFi.h>

WiFiClient client;
WiFiServerserver(80);
# define LED_Red D5
# define LED_Yellow D6
# define LED_Green D7
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
WiFi.begin("wi-fi login ID","wi-fi Password");
while(WiFi.status() !=WL_CONNECTED)
{delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("NodeMCU is Connected");
Serial.println(WiFi.localIP());
server.begin();
pinMode(LED_Red, OUTPUT);
pinMode(LED_Yellow, OUTPUT);
pinMode(LED_Green, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
client=server.available();
if(client==1)
{
String request =client.readStringUntil('\n');
Serial.println(request);
request.trim();
if(request =="GET /LED_Red HTTP/1.1")
{
2
digitalWrite(LED_Red, HIGH);
digitalWrite(LED_Yellow, LOW);
digitalWrite(LED_Green, LOW);
}
if(request =="GET /LED_Yellow HTTP/1.1")
{
digitalWrite(LED_Red, LOW);
digitalWrite(LED_Yellow, HIGH);
digitalWrite(LED_Green, LOW);
}
if(request =="GET /LED_Green HTTP/1.1")
{
digitalWrite(LED_Red, LOW);
digitalWrite(LED_Yellow, LOW);
digitalWrite(LED_Green, HIGH);
}
}
}

Step 5: Save the new sketch by appropriate name in a folder on your PC / Laptop

Step 6: Upload the sketch on Node MCU. Go to Sketch ----> Upload

Ensure everything is connected as described under the schematics section. After uploading the
code, you should see the IP address of your web server displayed in the serial monitor as shown
below.

3
Copy the IP address and paste it in a web browser on any device (Mobile or PC) connected to the
same network as the NodeMCU. You should see the web page and be able to toggle the
connected appliances by clicking the buttons. Copy the IP which occur on serial monitor copy
that and next to that write down _ Red then RED led becomes glow. If you are writing Yellow
than YELLOW LED becomes glow similarly for Green.

Step 7: Observe the outputs.

Screenshots:

A.

1. Serial monitor with display of wi-fi connected and LED switched on

2. LED ON photo- Hardware

4
B. Traffic Signal Generator

1. LED On- Photos

3. Modified code

#include <ESP8266WiFi.h>

const char* ssid = "Nikita";

const char* password = "nikita23";

WiFiServer server(80);

void setup() {

pinMode(D1, OUTPUT);

digitalWrite(D1, LOW);

pinMode(D2, OUTPUT);
5
digitalWrite(D2, LOW);

pinMode(D3, OUTPUT);

digitalWrite(D3, LOW);

Serial.begin(9600);

Serial.print("Connecting to ");

Serial.println(ssid);

WiFi.mode(WIFI_STA);

WiFi.begin(ssid, password);

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

delay(500);

Serial.print(".");

Serial.println("");

Serial.println("NodeMCU is connected to WiFi");

Serial.print("IP address: ");

Serial.println(WiFi.localIP());

server.begin();

delay(3000);

6
void loop() {

WiFiClient client;

client = server.available();

if (client == 1) {

String request = client.readStringUntil('\n');

client.flush();

Serial.println(request);

if (request.indexOf("ledon") != -1)

digitalWrite(D1, HIGH);

delay(6000);

digitalWrite(D2, HIGH);

delay(3000);

digitalWrite(D1, LOW);

digitalWrite(D2, LOW);

delay(1000);

digitalWrite(D3, HIGH);

delay(5000);

digitalWrite(D3, LOW);

delay(1000);

7
digitalWrite(D2, HIGH);

delay(5000);

digitalWrite(D2, LOW);

delay(1000);

if (request.indexOf("ledoff") != -1)

digitalWrite(D1, LOW);

digitalWrite(D2, LOW);

digitalWrite(D3, LOW);

Conclusion:

Successful upload of code into NodeMCU and entering our hotspot name and password in the
code to which we want to connect NodeMCU gives us IP Address of nodeMCU Which is now
connected to the wifi, after entering this IP Address to the new tab in browser we can remotely
control the traffic signal just by entering led on to start the traffic light signal and led off to stop
the traffic light signal after the IP address.

You might also like