Amir Pournasserian
Published © MIT

Laser Tripwire with ESP32 and Arduino IDE

Create your own laser tripwire like in spy movies using two ESP32s, Arduino IDE, a laser and a photoresistor!

IntermediateFull instructions provided2 hours499

Things used in this project

Hardware components

FireBeetle ESP32 IOT Microcontroller (Supports Wi-Fi & Bluetooth)
DFRobot FireBeetle ESP32 IOT Microcontroller (Supports Wi-Fi & Bluetooth)
At least 1, but 2 is preferable
×2
Sunfounder Laser Diode Module
You can buy all of the sensors cheaper in a sensor kit.
×1
Sunfounder Photoresistor Sensor
You can buy all of the sensors cheaper in a sensor kit.
×1

Software apps and online services

Free Subscription
uBeac IoT Platform Free Subscription
Arduino IDE
Arduino IDE

Story

Read more

Schematics

ESP32 and module setup

Code

Laser Code

Arduino
int laser = 1;

void setup() {
  pinMode(laser, OUTPUT);
  digitalWrite(laser, HIGH);
}

void loop() {
}

Photoresistor Code

Arduino
#include <WiFi.h>
#include <HTTPClient.h>

int pin = 32;
int sensorValue;
int crossed = 0;
int last = 0;

const char* ssid     = "MAIN WIFI SSID";              //Main Router      
const char* password = "MAIN WIFI PASSWORD";          //Main Router Password

const char* url = "UBEAC GATEWAY URL";
String payload_pattern = "[{\"id\": \"MyESP\", \"sensors\": [{\"id\": \"Laser Crossed\", \"value\": $laser$}]}]";

void setup(){
  Serial.begin(115200);  

  delay(4000);   //Delay needed before calling the WiFi.begin
 
  WiFi.begin(ssid, password); 
 
  while (WiFi.status() != WL_CONNECTED) { //Check for the connection
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println("Connected to the WiFi network");
}

void wifi(String payload){
  if(WiFi.status()== WL_CONNECTED){ 

  HTTPClient http;   
 
  http.begin(url);  
  int httpResponseCode = http.POST(payload); 
 
   if(httpResponseCode>0){
     String response = http.getString(); 
     Serial.println(httpResponseCode);
     }
     http.end();
 
  }else{
    Serial.println("Error in WiFi connection");    
  }
  delay(1000);
}

void loop(){
  sensorValue = analogRead(pin); 
  Serial.print(sensorValue, DEC);
  Serial.print(" \n"); 

  last = crossed;
      
  if(sensorValue > 0){
    crossed = 0;      
  }else{
    crossed = 1;
  }
    
  String payload = payload_pattern;
  payload.replace("$laser$", String(crossed));
  
  if(crossed == 0 || (crossed == 1 && last == 0)){
    wifi(payload);
  }

}

Credits

Amir Pournasserian

Amir Pournasserian

7 projects • 3 followers
Data scientist, machine learning specialist, IoT nerd. CEO of Momentaj and Founder of uBeac, IoT thinktanks.

Comments