cstram
Published © GPL3+

Cool Car Garage Detection System

This new car garage detection system using Arduino and LoRa. A smart solution for monitoring the presence of your Car in the garage.

BeginnerFull instructions provided5 hours327
Cool Car Garage Detection System

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Arduino Nano R3
Arduino Nano R3
×1
LED (generic)
LED (generic)
×2
RYLR998
×2
ESP32 Development Board
×1
OLED 128 x 32
×1
Resistor 10k ohm
Resistor 10k ohm
×2
Resistor 4.75k ohm
Resistor 4.75k ohm
×2

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Drill / Driver, Cordless
Drill / Driver, Cordless

Story

Read more

Schematics

CAR PARK Transmitter

CAR PARK Receiver

Code

CAR Detector RYLR998 Receiver

Arduino
Receiver Module
/*
 * RYLR998 Check your CAR RECEIVER (In or out of the garage?) 
 * February 5th 2023 
 * Carlo Stramaglia
 * https://www.youtube.com/c/CarloStramaglia
 * To change the Password, change the 8 digits in the below function LoRa.print("AT+CPIN=11223344\r\n");
 * You need to have the same password on both Receiver and Transmitter
 */

#include <NTPClient.h>
#include <SoftwareSerial.h>
#include <U8g2lib.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include "arduino_secrets.h"

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

U8G2_SSD1306_128X32_UNIVISION_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // Adafruit Feather ESP8266/32u4 Boards + FeatherWing OLED
SoftwareSerial LoRa(19, 23); // RX, TX

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key index number (needed only for WEP)

int status = WL_IDLE_STATUS;

String msg = "", received_msg = "";
int waiting = 200;


void setup() {
  Serial.begin(57600);
  LoRa.begin(57600);

  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  u8g2.begin();
  set_AT_commands ("50","18");
  msg="";
  u8g2.clearBuffer();          // clear the internal memory
  u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
  u8g2.drawStr(0,8,"Ready to Receive");  // write something to the internal memory
  u8g2.sendBuffer();          // transfer internal memory to the display 

  timeClient.begin();
  delay (3000);
  u8g2.clearBuffer();          // clear the internal memory
}

void loop() {
  delay (50);
  get_serial_data();
  if(msg != ""){ 
      Serial.print("Data Arrived: ");
      Serial.println(msg);
      int delimiter_1, delimiter_2;
      delimiter_1 = msg.indexOf("@");
      delimiter_2 = msg.indexOf("$", delimiter_1 + 1);

      received_msg = msg.substring(delimiter_1 + 1, delimiter_2);
      int str_len = received_msg.length() + 1;
      char messageLocal[str_len];

      received_msg.toCharArray(messageLocal, str_len);
            
      u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
      
      if (messageLocal[0] == 0x31) {
        u8g2.clearBuffer();          // clear the internal memory
        u8g2.drawStr(0,8,"The CAR is IN");  // write something to the internal memory
        timeClient.update();
        u8g2.drawStr(0,20,"Last time IN: ");  // write something to the internal memory
        Serial.println(timeClient.getFormattedTime());
        Serial.println(timeClient.getDay());
        display();       
      }        
      else
        u8g2.drawStr(0,8,"The CAR is OUT");  // write something to the internal memory      
      u8g2.sendBuffer();          // transfer internal memory to the display
      
  
    }
}


void get_serial_data(){
  if (LoRa.available()){
    msg = LoRa.readString();
  }
}

void set_AT_commands(String address, String network_ID){
    LoRa.print("AT\r\n");                           // Just a check if the module is well connected
    delay(waiting);
    LoRa.print("AT+ADDRESS=" + address + "\r\n");   // Device Address will be stored in Flash
    delay(waiting);
    LoRa.print("AT+NETWORKID="+network_ID+"\r\n");  // Network ID will be stored in flash
    delay(waiting);        
    LoRa.print("AT+PARAMETER=9,7,1,12\r\n");        // Default data. Can be deleted
    delay(waiting);   
    LoRa.print("AT+IPR=57600\r\n");                 // Lower serial speed. My Arduino was not working properly at 115200
    delay(waiting);
    LoRa.print("AT+RESET\r\n");                     // Reset is required otherwise seems not to work properly at startup
    delay(waiting);
    LoRa.print("AT+BAND=868100000\r\n");            // Eurpean Bandwdth 
    delay(waiting);
    LoRa.print("AT+CPIN=11223344\r\n");             // Encrypted password should be the same as the receiver
    delay(waiting);
}

// Display Ora a data
void display()
{
  char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  char buf[40];
  char dayWord[40];

  u8g2.setFont(u8g2_font_ncenB08_tr);

  sprintf(dayWord, daysOfTheWeek[timeClient.getDay()]);
    
  Serial.print ("Day: ");
  Serial.println(dayWord);
  u8g2.drawStr(80,20, dayWord);  
   
  sprintf(buf, "%.2d:%.2d:%.2d GMT", timeClient.getHours(), timeClient.getMinutes(), timeClient.getSeconds());
  Serial.print ("Time: ");
  Serial.println(buf);
  u8g2.drawStr(0,30, buf);   
    
  u8g2.sendBuffer();          
 
  delay(1000);

}

arduino_secrets.h

Arduino
Secrets File for Receiver module
#define SECRET_SSID "SSID Name"
#define SECRET_PASS "Password"

CAR Detector RYLR998 Transmitter

Arduino
Receiver code for CAR Garage Detector
/*
 * RYLR998 Check your CAR TRANSMITTER (In or out of the garage?) 
 * February 5th 2023 
 * Carlo Stramaglia
 * https://www.youtube.com/c/CarloStramaglia
 * To change the Password, change the 8 digits in the below function LoRa.print("AT+CPIN=11223344\r\n");
 * You need to have the same password on both Receiver and Transmitter
 */

#include <SoftwareSerial.h>
#include <HCSR04.h>

SoftwareSerial LoRa(6, 5); // First (6) connected to TX of RYLR998, Second (5) to RX of RYLR998

#define PIN_LED_RED   7
//#define PIN_LED_RED   13
#define PIN_LED_GREEN 8
byte triggerPin = 9;
byte echoPin = 10;

int ledState = LOW;

String msg = "";
int waiting = 200;
int j=0;
char messageNumber[10] = "";


void setup() { 
  Serial.begin(57600);
  HCSR04.begin(triggerPin, echoPin);
  pinMode(PIN_LED_RED, OUTPUT);
  pinMode(PIN_LED_GREEN, OUTPUT);
  digitalWrite(PIN_LED_GREEN, ledState);
  digitalWrite(PIN_LED_RED, ledState);
  LoRa.begin(57600);
  set_AT_commands ("51","18");
  delay(2000);
  LoRa.print("AT+BAND=868100000\r\n"); // Placed this command because sometimes the board was going back to US standard
  delay(2000);
}

void loop() {
  double* distances = HCSR04.measureDistanceCm();
  Serial.print("1: ");
  Serial.print(distances[0]);
  Serial.println(" cm");
  
  Serial.println("---");  
  if (distances[0] > 100 or distances[0] == -1) {
    j=0;
    sprintf (messageNumber, "@%i$",j);
    send_AT_message("50", messageNumber);
    digitalWrite(PIN_LED_RED, HIGH);
    digitalWrite(PIN_LED_GREEN, ledState);
  }
  else
  {
    j=1;
    sprintf (messageNumber, "@%i$",j);
    send_AT_message("50", messageNumber);
    digitalWrite(PIN_LED_RED, ledState);
    digitalWrite(PIN_LED_GREEN, HIGH);
  }
  delay(3000);

}


void get_serial_data(){
  if (LoRa.available()){
    msg = LoRa.readString();
  }
}

void set_AT_commands(String address, String network_ID){
    LoRa.print("AT\r\n");                           // Just a check if the module is well connected
    delay(waiting);   
    LoRa.print("AT+ADDRESS=" + address + "\r\n");   // Device Address will be stored in Flash
    delay(waiting);
    LoRa.print("AT+NETWORKID="+network_ID+"\r\n");  // Network ID will be stored in flash
    delay(waiting);   
    LoRa.print("AT+PARAMETER=9,7,1,12\r\n");        // Default data. Can be deleted
    delay(waiting);
    LoRa.print("AT+IPR=57600\r\n");                 // Lower serial speed. My Arduino was not working properly at 115200
    delay(waiting);
    LoRa.print("AT+RESET\r\n");                     // Reset is required otherwise seems not to work properly at startup
    delay(waiting);
    LoRa.print("AT+BAND=868100000\r\n");            // Eurpean Bandwdth
    delay(waiting);
    LoRa.print("AT+CPIN=11223344\r\n");             // Encrypted password should be the same as the receiver
    delay(waiting);
    
}


void send_AT_message(String address, char* textmessage){
    int Lenght=0;
    Lenght = strlen (textmessage);
    LoRa.print("AT+SEND=" + address + "," + Lenght + "," + textmessage + "\r\n");
    delay(waiting);
}

RYLR998 Setup for changing the Baud Rate from 115200 to 57600

Arduino
Load this first to both Receiver and transmitter before anything else and RUN IT ONCE.
/*
 * DO THIS ONLY ONCE ON BOTH RECEIVER AND TRANSMITTER
 * RYLR998 Setup code for 57600 baud communication 
 * December 31st 2022 
 * Carlo Stramaglia
 * https://www.youtube.com/c/CarloStramaglia
 *
 */

#include <SoftwareSerial.h>

SoftwareSerial LoRa(6, 5); // First (6) connected to TX of RYLR998, Second (5) to RX of RYLR998


String msg = "";
int waiting = 200;
int i=0, j=0;
char messageNumber[10] = "";


void setup() { 
  LoRa.begin(115200);
  set_AT_commands ("100","18");
  delay(5000);
}

void loop() {  
  
}


void get_serial_data(){
  if (LoRa.available()){
    msg = LoRa.readString();
  }
}

void set_AT_commands(String address, String network_ID){
    LoRa.print("AT\r\n");                           // Just a check if the module is well connected
    delay(waiting);   
    LoRa.print("AT+ADDRESS=" + address + "\r\n");   // Device Address will be stored in Flash
    delay(waiting);
    LoRa.print("AT+NETWORKID="+network_ID+"\r\n");  // Network ID will be stored in flash
    delay(waiting);   
    LoRa.print("AT+PARAMETER=9,7,1,12\r\n");        // Default data. Can be deleted
    delay(waiting);
    LoRa.print("AT+IPR=57600\r\n");                 // Lower serial speed. My Arduino was not working properly at 115200
    delay(waiting);
    LoRa.print("AT+RESET\r\n");                     // Reset is required otherwise seems not to work properly at startup
    delay(waiting);
    LoRa.print("AT+BAND=868100000\r\n");            // Eurpean Bandwdth
    delay(waiting);
    
}

Credits

cstram
16 projects • 21 followers
Passionate about IT, Electronics and DIY. Strong believer in Raspberry and Arduino devices. Experience in digital television and security.
Contact

Comments

Please log in or sign up to comment.