Albertus Zerk
Published © CC BY-NC-SA

Sports hall masks alarm | Turnhallen Masken Alarm, v1.4

If you want to play without a mask in a sports hall, then you need an arduino based area surveillance.

BeginnerFull instructions provided397
Sports hall masks alarm | Turnhallen Masken Alarm, v1.4

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×2
Buzzer
Buzzer
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
LoRa-02
×1
Breadboard (generic)
Breadboard (generic)
×2
SparkFun Breadboard Power Supply 5V/3.3V
SparkFun Breadboard Power Supply 5V/3.3V
×2
SMA To IPEX Pigtail
×2
Antenna, 2.4 GHz
Antenna, 2.4 GHz
×2

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Schema

Schematics

Sender

Receiver

Code

Code for Ardunio Nano (Sender)

C/C++
/*
 * code for ultrasonic sensor hc-sr05
 *
 * description:  playground.arduino.cc/Code/NewPing
 *
 ************ hc-sr05 module
*/
// hcsr05 ////////////////////////////////
#include <NewPing.h>                    // library for newping methods 
#include <dmtimer.h>                    // library for dmtimer methods
                                        //
                                        // vcc: power 5V
#define trigPin  3                      // trig: pwm 3
#define echoPin  4                      // echoPin: pwm 4
                                        // gnd: black
#define DistanceMax 10000               // 1000cm = 1m
                                        //
NewPing sonar1(trigPin, echoPin, DistanceMax);
DMTimer myTimer1(50000);                // set mytimer1 to 50ms (50000 = 50ms)
//////////////////////////////////////////
/*
 * interfacing sx1278 (ra-02) lora module with arduino nano
 *
 * copyright (c) sandeep mistry. all rights reserved. licensed under the mit license.
 *
 * description: circuitdigest.com/microcontroller-projects/arduino-lora-sx1278-interfacing-tutorial
 *
 ************ lora module
*/
// LoRa-02////////////////////////////////
#include <SPI.h>                        // library for spi methods 
#include <LoRa.h>                       // library for lora methods
                                        //
int counter       = 0;                  //
//////////////////////////////////////////
#define DEBUG 1                         // read debug information (1 = send data to serial port)
int      Distance = 0;                  // distance current (init=0)
int  DistHarmonic = 0;                  // distance harmonic (dynamischer mittelwert pro sekunde, init=0)
int             z = 0;                  // use 20 cicles for harmanic distance
int   DistRealCur = 0;                  // current real distance
int   DistRealOld = 0;                  // old real distance (bevor a second)
int  HarmonicLoop = 2;                  // how many harmonic loop
int  DelayBetween = 4000;               // delay between measurements (perhaps 5 minutes)
bool        alert = false;              // if something happen then alert is true
//////////////////////////////////////////

void setup(){
    Serial.begin(9600);
    while (!Serial);

    Serial.println("LoRa Sender");

    if (!LoRa.begin(433E6)) {           // frequency 433 MHz for switzerland and eu
        Serial.println("Starting LoRa failed!");
        while (1);
    }
}

void hcsr05(){
    if (myTimer1.isTimeReached()){      // wait 50ms between pings (about 20 pings/sec)
                                        // 29ms should be the shortest "delay" between pings
        Distance = sonar1.ping_cm();
        
        if (Distance != 0) {

            #if DEBUG == 1            
            Serial.print("Distance: ");
            Serial.print(Distance);
            Serial.println(" cm");           
            #endif  

            z++;
            if (z <= HarmonicLoop) {
                DistHarmonic = DistHarmonic + Distance;
                
                #if DEBUG == 1            
                //Serial.print("DistHarmonic: ");
                //Serial.print(DistHarmonic);
                //Serial.println(" cm");           
                #endif           
                
                
            } else {
                DistRealCur = DistHarmonic / HarmonicLoop;
              
                if (DistRealOld - DistRealCur > 20){
                    alert = true;           // alarm = true if old and cur distance is different
                    ota();                  // call ota function and send data
                }
                
                DistRealOld = DistRealCur;
                           
                DistHarmonic = 0;           // start new harmonic cicle loop
                z = 0;
                DistRealCur = 0;
            }
        }
    }
}

void ota(){
    Serial.print("Sending alert packet: ");
    Serial.print(counter);

    // send alert packet
    #if DEBUG == 1
    Serial.print(". Sending alert signal!! Distance = ");
    Serial.print(DistRealCur);
    Serial.println("cm");
    #endif
    
    LoRa.beginPacket();
    LoRa.print("Alarm ausgeloest");
    // LoRa.print(counter);
    LoRa.endPacket();                    // transmit over the air (ota)

    counter++;

    delay(DelayBetween);                 // set to 10 minutes in productive areas  
    alert = false; 
}

void loop(){
    hcsr05();                            // call sonar function   
}

Code for Ardunio Nano (Receiver)

C/C++
/*
 * interfacing sx1278 (ra-02) lora module with arduino nano
 *
 * copyright (c) sandeep mistry. all rights reserved. licensed under the mit license.
 *
 * description: circuitdigest.com/microcontroller-projects/arduino-lora-sx1278-interfacing-tutorial
 *
 ************ lora module
*/
// lora-02 ///////////////////////////////
#include <SPI.h>                        // library for spi methods 
#include <LoRa.h>                       // library for lora methods
                                        //
//////////////////////////////////////////
const String SenderString = "";         // input is "Alarm ausgeloest" when something happen 

/*
 * piezo alarm
 *
 * specify digital pin on the arduino nano that
 * the positive lead of piezo buzzer is attached
 ************ buzzer module
*/
int      piezoPin = 3;                  // buzzer to arduino nano pin 3
int    AlertCount = 0;                  // 
int      AlertMax = 7;                  // how many times (default = 7)
//////////////////////////////////////////
#define DEBUG 1                         // read debug information (1 = send data to serial port)
//////////////////////////////////////////

void setup() {
       
    Serial.begin(9600);
    while (!Serial);

    Serial.println("LoRa Receiver");

    if (!LoRa.begin(433E6)) {           // frequency 433 MHz for switzerland and eu
        Serial.println("Starting LoRa failed!");
        while (1);
    }
}

void piezoalert() {
    
    while (AlertCount < AlertMax){
        /*tone needs 2 arguments, but can take three
        1) pin#
        2) frequency - this is in hertz (cycles per second) which determines the pitch of the noise made
        3) duration - how long teh tone plays
        */
        tone(piezoPin, 900, 300);
        delay(500);
        
        AlertCount++;
    }    
}

void loop() {
    
    // try to parse packet
    int packetSize = LoRa.parsePacket();
    if (packetSize) {
        // received a packet
        Serial.print("Received packet '");

        // read packet
        while (LoRa.available()) {
            // Serial.print((char)LoRa.read());
            SenderString = (char)LoRa.read();
            Serial.print(SenderString);
        }

        // print rssi of packet
        Serial.print("', signal strength to sender is ");
        Serial.println(LoRa.packetRssi());

        if (SenderString == "t") {
            piezoalert();               // call alarm function
            AlertCount = 0;             // reset
        }
    }
}

Credits

Albertus Zerk
0 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.