PIR Sensor-based LED Control without Internet

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

PIR Sensor-based LED Control over Wi-Fi

In this post, we’re going to show you PIR Sensor-based LED Control over WiFi without the Internet 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,

PIR-based LED Control

Parts Required

For this tutorial, you need the following parts:

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

To learn more about HTTP you can check the following links:

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 = "ESP8266-Access-Point";
const char* password = "123456789";

const char* serverNameledOn = "http://192.168.4.1/ledon";
const char* serverNameledOff = "http://192.168.4.1/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)

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();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Connected to WiFi");
}

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 = "ESP8266-Access-Point";
const char* password = "123456789";

const int ledPin = 2;

AsyncWebServer server(80);

void setup(){
  Serial.begin(115200);        // initialize serial
  pinMode(ledPin, OUTPUT);
  WiFi.softAP(ssid, password);

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);
  
  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 the RST button on the NodeMCU board and see what’s happening in real-time on the 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.


*