Laser Tripwire using Arduino Nano

Hello all,

So I'm building my own laser tripwire using a Nano. The components are as follows;

Arduino Nano
SIM800L GSM module
Light Dependent Resistor
Buzzer
10K Resistor
External 5v Power Supply

After doing some research, I found some code and tried to implement it into my code which was a complete and total disaster. Needless to say I broke the code and had to start from scratch. But I was able to get it working again and functioning as it should. But the code I posted (not my code) allows the user to arm and disarm the system using an SMS message. THAT is what I'm trying to accomplish but I don't understand the code that the original youtuber posted. I'm not great at coding but I'm chugging along, however, I just don't understand what's taking place here. If someone could explain it to me I would greatly appreciate it.

Once I get the system working I'll be happy to post the updated code so others can use it. Side note, the reason I can't get this particular code to work is because the youtuber first built a laser tripwire with no Arduino but rather he used some chips that I've never seen before, and then implemented the Arduino and SIM800L module after the fact. I've posted everything below and thanks in advance.

YOUTUBE VIDEO

https://www.youtube.com/watch?v=qlvs8C-wY0E&t=411s

Schematic_Laser-Tripwire_2024-02-16

#include <SoftwareSerial.h>
#define Ard1 7
#define Ard2 A0
SoftwareSerial sim(10, 11);//RX & TX pinout between parenthesis. If it doesn't work change the pins to: RX #10 & TX #11
int _timeout, state=0;
char order;
short OK=-1;
String _buffer;
String number = "+19999999999"; // change with your number
String activation_password = "Arm";//change with your activation password
String deactivation_password = "Drm";//change with your deactivation password
void setup() {
  delay(7000); //delay for 7 seconds to make sure the modules get the signal
  Serial.begin(115200);
  pinMode(Ard1, OUTPUT);
  pinMode(Ard2, INPUT);
  digitalWrite(Ard1,1);
  _buffer.reserve(50);
  Serial.println("System Started...");
  sim.begin(9600);
  delay(1000);
  Serial.println("System Ready");//Wait for this message to start testing
SendMessage("Hey! It's the Tripwire. Type On to activate");
  RecieveMessage();
}
void loop() {
  String RSMS;
  switch(state){
    case 0:
while(sim.available()> 0){
  order=sim.read();
  RSMS.concat(order);
OK=RSMS.indexOf("On");
  
}
  if(OK != -1){
    SendMessage("Type the activation password below:");
    state=1;
     OK=-1;
 RecieveMessage();
 
  }
  break;

  case 1:

       while(sim.available()> 0){
  order=sim.read();
  RSMS.concat(order);
OK=RSMS.indexOf(activation_password);
}
  if(OK != -1){
    SendMessage("Turned On!. To deactivate Type Off");
    state=2;
    digitalWrite(Ard1,0);
     OK=-1;
      RecieveMessage();
  }
  break;

  case 2:
  if(digitalRead(Ard2)== 0 &&
  digitalRead(Ard1) == 0){
    Serial.println("Detected someone");
    delay(500);
    callNumber();
     RecieveMessage();
  }
       while(sim.available()> 0){
  order=sim.read();
  RSMS.concat(order);
OK=RSMS.indexOf("Off");
}
  if(OK != -1){
    SendMessage("Type the deactivation code below:");
    state=3;
     OK=-1;
      RecieveMessage();
  }
  break;

  case 3:
    if(digitalRead(Ard2)== 0 &&
    digitalRead(Ard1) == 0){
      Serial.println("Detected someone");
      delay(500);
    callNumber();
     RecieveMessage();
  }
       while(sim.available()> 0){
  order=sim.read();
  RSMS.concat(order);
OK=RSMS.indexOf(deactivation_password);
}
  if(OK != -1){
    SendMessage("Turned Off!");
    digitalWrite(Ard1,1);
    state=0;
     OK=-1;
      RecieveMessage();
  }
  break;
  }
  if (sim.available() > 0)
    Serial.write(sim.read());
}
void SendMessage(String SMS)
{
  //Serial.println ("Sending Message");
  sim.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);
  //Serial.println ("Set SMS Number");
  sim.println("AT+CMGS=\"" + number + "\"\r"); //Mobile phone number to send message
  delay(1000);
  sim.println(SMS);
  delay(100);
  sim.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
  _buffer = _readSerial();
}
void RecieveMessage()
{
  Serial.println ("SIM800L Read an SMS");
  delay (1000);
  sim.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(1000);
  Serial.write ("Unread Message done");
}
String _readSerial() {
  _timeout = 0;
  while  (!sim.available() && _timeout < 12000  )
  {
    delay(13);
    _timeout++;
  }
  if (sim.available()) {
    return sim.readString();
  }
}

void callNumber() {
  sim.print (F("ATD"));
  sim.print (number);
  sim.print (F(";\r\n"));
  _buffer = _readSerial();
  Serial.println(_buffer);
}

//Base code's source: https://breakrow.com/miliohm/sim800l-with-arduino-tutorial/

What's the question?

1 Like

While you are formulating your question get a copy of the Arduino Cookbook and skim it cover to cover, then read sections that appear to be part of your project.

1 Like

I want to implement a way to arm and disarm my system through SMS. How do I do this?

Make a test program that receives SMS. Make it send the SMS to ArduinoIDE serial terminal. Make your program recognize two different commands. Make it send that over serial.

Did you read the code... and see this link... and follow the tutorial?

I got all that working. When I boot the Arduino it sends me a message and it's armed. If I break the laser the buzzer sounds and I get a message. As far as that goes everything works. In the youtube video, the young man was able to arm and disarm the system by using text messages sent to and from the Arduino. This is what I'm trying to add to my code but I'm falling short.

I looked up SWITCH CASE on the Arduino website and gained a little knowledge but how to use it in conjunction with my system is the issue. I made this post long after I did some research. Again, I'm not very good at this which is why I'm here.

I'm to the point where the system sends me a message telling me to type "On" to arm, then sends a follow up message asking for a password after I send the initial "On" message. Once I send the "Arm" password nothing else happens.

Here's my code:

#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
#define LASER_RECEIVER_PIN A0
#define LASER_TRANSMIT_PIN 7
#define BUZZER_PIN 8
SoftwareSerial sim800L(rxPin, txPin);
short OK = -1;
char order;
String _buffer;
int _timeout, state = 0;
const String PHONE = "9999999999";
String activation_password = "Arm";    //change with your activation password
String deactivation_password = "Drm";  //change with your deactivation password
int LDR_Value = 0;
boolean laser_flag = 0;  //fire_flag = 0 means tripwire detected

void SendMessage(String SMS) {
  //Serial.println ("Sending Message");
  sim800L.println("AT+CMGF=1");  //Sets the GSM Module in Text Mode
  delay(1000);
  //Serial.println ("Set SMS Number");
  sim800L.println("AT+CMGS=\"" + PHONE + "\"\r");  //Mobile phone number to send message
  delay(1000);
  sim800L.println(SMS);
  delay(100);
  sim800L.println((char)26);  // ASCII code of CTRL+Z
  delay(1000);
  _buffer = _readSerial();
}

void RecieveMessage() {
  Serial.println("SIM800L Read an SMS");
  delay(1000);
  sim800L.println("AT+CNMI=2,2,0,0,0");  // AT Command to receive a live SMS
  delay(1000);
  Serial.write("Unread Message done");
}
String _readSerial() {
  _timeout = 0;
  while (!sim800L.available() && _timeout < 12000) {
    delay(13);
    _timeout++;
  }
  if (sim800L.available()) {
    return sim800L.readString();
  }
}

void callNumber() {
  sim800L.print (F("ATD"));
  sim800L.print (PHONE);
  sim800L.print (F(";\r\n"));
  _buffer = _readSerial();
  Serial.println(_buffer);
}

void setup() {

  Serial.begin(115200);
  sim800L.begin(9600);

  pinMode(LASER_RECEIVER_PIN, INPUT);
  pinMode(LASER_TRANSMIT_PIN, OUTPUT);
  //by default the LASER Transmitter is ON
  digitalWrite(LASER_TRANSMIT_PIN, HIGH);

  pinMode(BUZZER_PIN, OUTPUT);
  //by default the BUZZER is OFF
  digitalWrite(BUZZER_PIN, LOW);

  Serial.println("Initializing...");
  //Once the handshake test is successful, it will back to OK
  sim800L.println("AT");
  delay(1000);
  sim800L.println("AT+CMGF=1");
  delay(1000);
  sim800L.println("AT+GSMBUSY = 1");
  _buffer.reserve(50);
  Serial.println("System Ready");  //Wait for this message to start testing
  SendMessage("Hey! It's the Tripwire. Type On to activate");
  RecieveMessage();
}

void loop() {
  String RSMS;
  switch (state) {
    case 0:
      while (sim800L.available() > 0) {
        order = sim800L.read();
        RSMS.concat(order);
        OK = RSMS.indexOf("On");
      }
      if (OK != -1) {
        SendMessage("Type the activation password below:");
        state = 1;
        OK = -1;
        RecieveMessage();
      }
      break;

    case 1:

      while (sim800L.available() > 0) {
        order = sim800L.read();
        RSMS.concat(order);
        OK = RSMS.indexOf(activation_password);
      }

      if (OK != -1) {
        SendMessage("Turned On!. To deactivate Type Off");
        state = 2;
        digitalWrite(LASER_TRANSMIT_PIN, 1);
        OK = -1;
        RecieveMessage();
      }
      break;

      case 2:
  if(analogRead(LASER_RECEIVER_PIN) <= 800 &&
  digitalRead(LASER_TRANSMIT_PIN) == 0){
    Serial.println("Detected someone");
    delay(500);
    callNumber();
     RecieveMessage();
  }
       while(sim800L.available()> 0){
  order=sim800L.read();
  RSMS.concat(order);
OK=RSMS.indexOf("Off");
}
  if(OK != -1){
    SendMessage("Type the deactivation code below:");
    state=3;
     OK=-1;
      RecieveMessage();
  }
  break;

  case 3:
    if(analogRead(LASER_RECEIVER_PIN) <= 800 &&
    digitalRead(LASER_TRANSMIT_PIN) == 0){
      Serial.println("Detected someone");
      delay(500);
    callNumber();
     RecieveMessage();
  }
       while(sim800L.available()> 0){
  order=sim800L.read();
  RSMS.concat(order);
OK=RSMS.indexOf(deactivation_password);
}
  if(OK != -1){
    SendMessage("Turned Off!");
    digitalWrite(LASER_TRANSMIT_PIN,1);
    state=0;
     OK=-1;
      RecieveMessage();
  }
  break;

      while (sim800L.available()) {
        Serial.println(sim800L.readString());
      }

      int laser_value = analogRead(LASER_RECEIVER_PIN);
      //Serial.println(laser_value);

      //The laser trip wire is detected, trigger Alarm and send sms
      if (laser_value <= 800) {
        digitalWrite(BUZZER_PIN, HIGH);
      }
  }
}

I don't see where you evaluate the received SMS.

How and why do i do that?

if (CONDITION) {
   do_this();
} else {
  do_that();
}

Because that's what writing a program is about, maybe ...

Be aware you may need to modulate/demodulate the signal to avoid the influence from outside system lighting.

I've got most of the code written, and functioning but I'm running into some timing problems and the fact that when the tripwire is tripped, the system calls me rather than texting me. I'll post the code that works so far shortly. Thanks for sticking with me. Im using the "case(switch)" feature, bot so much "if" statements.

I thought of that bit so far nothing. How would i go about modulating/demodulating?

Well here it is. Seems simple but not for me, spent a few days on this but it works. You can arm/disarm through text message, and the system will text you if the tripwire is tripped. There's an option for the system to call you if you wish, just uncomment the callNumber line in case 2 and case 3. Hope this helps someone.

#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
#define LASER_RECEIVER_PIN A0
#define LASER_TRANSMIT_PIN 7
#define BUZZER_PIN 8
SoftwareSerial sim800L(rxPin, txPin);
short OK = -1;
char order;
String _buffer;
int _timeout, state = 0;
const String PHONE = "9999999999";
String activation_password = "Arm";    //change with your activation password
String deactivation_password = "Drm";  //change with your deactivation password
int LDR_Value = 0;
boolean laser_flag = 0;  //fire_flag = 0 means tripwire detected

void SendMessage(String SMS) {
  //Serial.println ("Sending Message");
  sim800L.println("AT+CMGF=1");  //Sets the GSM Module in Text Mode
  delay(1000);
  //Serial.println ("Set SMS Number");
  sim800L.println("AT+CMGS=\"" + PHONE + "\"\r");  //Mobile phone number to send message
  delay(1000);
  sim800L.println(SMS);
  delay(100);
  sim800L.println((char)26);  // ASCII code of CTRL+Z
  delay(1000);
  _buffer = _readSerial();
}

void RecieveMessage() {
  Serial.println("SIM800L Read an SMS");
  delay(1000);
  sim800L.println("AT+CNMI=2,2,0,0,0");  // AT Command to receive a live SMS
  delay(1000);
  Serial.write("Unread Message done");
}
String _readSerial() {
  _timeout = 0;
  while (!sim800L.available() && _timeout < 12000) {
    delay(13);
    _timeout++;
  }
  if (sim800L.available()) {
    return sim800L.readString();
  }
}

void callNumber() {
  sim800L.print(F("ATD"));
  sim800L.print(PHONE);
  sim800L.print(F(";\r\n"));
  _buffer = _readSerial();
  Serial.println(_buffer);
}

void setup() {

  Serial.begin(115200);
  sim800L.begin(9600);
  pinMode(LASER_RECEIVER_PIN, INPUT);
  pinMode(LASER_TRANSMIT_PIN, OUTPUT);
  digitalWrite(LASER_TRANSMIT_PIN, LOW);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);
  Serial.println("Initializing...");
  sim800L.println("AT");
  delay(1000);
  sim800L.println("AT+CMGF=1");
  delay(1000);
  sim800L.println("AT+GSMBUSY = 1");
  _buffer.reserve(50);
  Serial.println("System Ready");  //Wait for this message to start testing
  SendMessage("Laser Tripwire Ready! Type On to activate");
  RecieveMessage();
}

void loop() {
  int laser_value = analogRead(LASER_RECEIVER_PIN);
  // Serial.println(laser_value);

  String RSMS;
  switch (state) {
    case 0:
      while (sim800L.available() > 0) {
        order = sim800L.read();
        RSMS.concat(order);
        OK = RSMS.indexOf("On");
      }
      if (OK != -1) {
        SendMessage("Type the activation password below:");
        state = 1;
        OK = -1;
        RecieveMessage();
      }
      break;

    case 1:
      while (sim800L.available() > 0) {
        order = sim800L.read();
        RSMS.concat(order);
        OK = RSMS.indexOf(activation_password);
        digitalWrite(LASER_TRANSMIT_PIN, 1);
      }

      if (OK != -1) {
        SendMessage("Tripwire ACTIVE! To Deactivate Type Off");
        state = 2;
        digitalWrite(LASER_TRANSMIT_PIN, 1);
        digitalWrite(BUZZER_PIN, 1);
        delay(30);
        digitalWrite(BUZZER_PIN, 0);
        OK = -1;
        RecieveMessage();
      }
      break;

    case 2:
      if (analogRead(LASER_RECEIVER_PIN) <= 800 && digitalRead(LASER_TRANSMIT_PIN) == 1) {
        digitalWrite(BUZZER_PIN, HIGH);
        Serial.println("Detected someone");
        delay(500);
        //callNumber();
        SendMessage("Laser Tripped!");
        RecieveMessage();
      }
      while (sim800L.available() > 0) {
        order = sim800L.read();
        RSMS.concat(order);
        OK = RSMS.indexOf("Off");
      }
      if (OK != -1) {
        SendMessage("Type the deactivation code below:");
        state = 3;
        OK = -1;
        RecieveMessage();
      }
      break;

    case 3:
      if (analogRead(LASER_RECEIVER_PIN) <= 800 && digitalRead(LASER_TRANSMIT_PIN) == 1) {
        Serial.println("Detected someone");
        delay(500);
        //callNumber();
        SendMessage("Laser Tripped!");
        RecieveMessage();
      }
      while (sim800L.available() > 0) {
        order = sim800L.read();
        RSMS.concat(order);
        OK = RSMS.indexOf(deactivation_password);
      }
      if (OK != -1) {
        SendMessage("Turned Off!");
        digitalWrite(LASER_TRANSMIT_PIN, 0);
        digitalWrite(BUZZER_PIN, LOW);
        state = 0;
        OK = -1;
        RecieveMessage();
      }
      break;

      while (sim800L.available()) {
        Serial.println(sim800L.readString());
      }

    }
  }

Either hardware or software.

Please deconvolute your code. Why do you have ReceiveMessage() scattered around the place? Why delay(500)? Why RMS in loop and what for? Why while() inside a case? ...

So is that why it takes so long for the system to arm/disarm? I have to send the password several times before it will work. So you're saying that I can simply get rid of all that stuff and it will still work? Better? Gonna try it now.

I guess I don't know how to deconvolute my code. I tried changing the "while" statements to "if" statements but it just hosed the code. I got rid of the delay, seems to have to effect. So I went back to the original code, if you're willing to give me some pointers I'd sure appreciate it. Thanks.

The best thing would be to get a basic book on programming and read that first. Then come back to the code. But that suggestion was already made ...