LED Control over Wi-Fi with Static/Fixed IP

PIR-Sensor-based-LED-Control
Spread the love

In this post we’re going to show you PIR Sensor based LED Control over Wi-Fi with Static/Fixed IP using NodeMCU.

The communication between the two NodeMCU with the help of HTTP communication protocol.

Project Overview

The following figure shows an overview of what we’re going to do in this project,

Parts Required

For this tutorial, you need the following parts:

  • Two NodeMCU Development boards
  • PIR Motion Sensor
  • Jumper Wires

Also Read: What is HTTP protocol?

Installing Libraries

Installing the ESPAsyncWebServer Library

  1. Click here to download the ESPAsyncWebServer Library. You should have a .zip folder in your Downloads folder.
  2. You can go to Sketch > Include Library > Add. ZIP library and select the library you’ve just downloaded.

Installing the ESP8266HTTPClient Library

  1. Click here to download the ESP8266HTTPClient Library. You should have a .zip folder in your Downloads folder.
  2. You can go to Sketch > Include Library > Add. ZIP library and select the library you’ve just downloaded.

Arduino Sketch for NodeMCU Client

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "WAREHOUSE";
const char* password = "india@12345";

const char* serverNameledOn = "http://192.168.0.230/ledon";
const char* serverNameledOff = "http://192.168.0.230/ledoff";

int led = 12;                // the pin that the LED is atteched to
int sensor = 2;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)                

// Set your Static IP address
IPAddress local_IP(192, 168, 0, 230);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);

IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8);   //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional

WiFiClient client; 

String httpGETRequestledOn(const char* serverNameledOn) {
  WiFiClient client;
  HTTPClient http;
  http.begin(client, serverNameledOn);
  int httpResponseCode = http.GET();  
  String payload = "-"; 
  
  if (httpResponseCode>0) {
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
  }
  http.end();
  return payload;
}

String httpGETRequestledOff(const char* serverNameledOff) {
  WiFiClient client;
  HTTPClient http;
  http.begin(client, serverNameledOff);
  int httpResponseCode = http.GET();  
  String payload = "-"; 
  
  if (httpResponseCode>0) {
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
  }
  http.end();
  return payload;
}

void setup() {
  Serial.begin(115200);
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  Serial.println();
  
  // Configures static IP address
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
  }

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {    

  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(10);                 // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!");     
      httpGETRequestledOn(serverNameledOn);
      state = HIGH;       // update variable state to HIGH
    }
  } 
  else {
    digitalWrite(led, LOW); // turn LED OFF
    delay(10);              // delay 200 milliseconds 
      
    if (state == HIGH){
      Serial.println("Motion stopped!");
      httpGETRequestledOff(serverNameledOff);
      state = LOW;       // update variable state to LOW
    }
  }                
}

Arduino Sketch for NodeMCU Server

#include <ESP8266WiFi.h>
#include "ESPAsyncWebServer.h"

const char* ssid = "WAREHOUSE";
const char* password = "india@12345";

const int ledPin = 2;

// Set your Static IP address
IPAddress local_IP(192, 168, 0, 230);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);

IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional

AsyncWebServer server(80);

void setup(){
  Serial.begin(115200);        // initialize serial
  pinMode(ledPin, OUTPUT);
  Serial.println();
  
  // Configures static IP address
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
  }

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  server.on("/ledon", HTTP_GET, [](AsyncWebServerRequest *request){
   request->send_P(100, "text/plain", "ON responce!");
   Serial.println("Send ON!"); 
   digitalWrite(ledPin, HIGH);       
  });

  server.on("/ledoff", HTTP_GET, [](AsyncWebServerRequest *request){
   request->send_P(100, "text/plain", "OFF responce!");
   Serial.println("Send OFF!"); 
   digitalWrite(ledPin, LOW);
  });
  
  server.begin();  
}

void loop(){   
  
}

Uploading code

When the code is successfully uploaded, open the serial monitor at a baud rate of 115200. Press RST button on ESP8266 board and see what’s happening in real time on figure below.

Github Link: download the project files


Spread the love

Be the first to comment

Leave a Reply

Your email address will not be published.


*