Components and supplies
2
SparkFun Breadboard Power Supply 5V/3.3V
2
SMA To IPEX Pigtail
2
Antenna, 2.4 GHz
1
Ultrasonic Sensor - HC-SR04 (Generic)
2
Arduino Nano R3
1
LoRa-02
1
Buzzer
2
Breadboard (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Code for Ardunio Nano (Sender)
c_cpp
1 2/* 3 * code for ultrasonic sensor hc-sr05 4 * 5 * description: playground.arduino.cc/Code/NewPing 6 * 7 ************ hc-sr05 module 8*/ 9// hcsr05 //////////////////////////////// 10#include <NewPing.h> // library for newping methods 11#include <dmtimer.h> // library for dmtimer methods 12 // 13 // vcc: power 5V 14#define trigPin 3 // trig: pwm 3 15#define echoPin 4 // echoPin: pwm 4 16 // gnd: black 17#define DistanceMax 10000 // 1000cm = 1m 18 // 19NewPing sonar1(trigPin, echoPin, DistanceMax); 20DMTimer myTimer1(50000); // set mytimer1 to 50ms (50000 = 50ms) 21////////////////////////////////////////// 22/* 23 * interfacing sx1278 (ra-02) lora module with arduino nano 24 * 25 * copyright (c) sandeep mistry. all rights reserved. licensed under the mit license. 26 * 27 * description: circuitdigest.com/microcontroller-projects/arduino-lora-sx1278-interfacing-tutorial 28 * 29 ************ lora module 30*/ 31// LoRa-02//////////////////////////////// 32#include <SPI.h> // library for spi methods 33#include <LoRa.h> // library for lora methods 34 // 35int counter = 0; // 36////////////////////////////////////////// 37#define DEBUG 1 // read debug information (1 = send data to serial port) 38int Distance = 0; // distance current (init=0) 39int DistHarmonic = 0; // distance harmonic (dynamischer mittelwert pro sekunde, init=0) 40int z = 0; // use 20 cicles for harmanic distance 41int DistRealCur = 0; // current real distance 42int DistRealOld = 0; // old real distance (bevor a second) 43int HarmonicLoop = 2; // how many harmonic loop 44int DelayBetween = 4000; // delay between measurements (perhaps 5 minutes) 45bool alert = false; // if something happen then alert is true 46////////////////////////////////////////// 47 48void setup(){ 49 Serial.begin(9600); 50 while (!Serial); 51 52 Serial.println("LoRa Sender"); 53 54 if (!LoRa.begin(433E6)) { // frequency 433 MHz for switzerland and eu 55 Serial.println("Starting LoRa failed!"); 56 while (1); 57 } 58} 59 60void hcsr05(){ 61 if (myTimer1.isTimeReached()){ // wait 50ms between pings (about 20 pings/sec) 62 // 29ms should be the shortest "delay" between pings 63 Distance = sonar1.ping_cm(); 64 65 if (Distance != 0) { 66 67 #if DEBUG == 1 68 Serial.print("Distance: "); 69 Serial.print(Distance); 70 Serial.println(" cm"); 71 #endif 72 73 z++; 74 if (z <= HarmonicLoop) { 75 DistHarmonic = DistHarmonic + Distance; 76 77 #if DEBUG == 1 78 //Serial.print("DistHarmonic: "); 79 //Serial.print(DistHarmonic); 80 //Serial.println(" cm"); 81 #endif 82 83 84 } else { 85 DistRealCur = DistHarmonic / HarmonicLoop; 86 87 if (DistRealOld - DistRealCur > 20){ 88 alert = true; // alarm = true if old and cur distance is different 89 ota(); // call ota function and send data 90 } 91 92 DistRealOld = DistRealCur; 93 94 DistHarmonic = 0; // start new harmonic cicle loop 95 z = 0; 96 DistRealCur = 0; 97 } 98 } 99 } 100} 101 102void ota(){ 103 Serial.print("Sending alert packet: "); 104 Serial.print(counter); 105 106 // send alert packet 107 #if DEBUG == 1 108 Serial.print(". Sending alert signal!! Distance = "); 109 Serial.print(DistRealCur); 110 Serial.println("cm"); 111 #endif 112 113 LoRa.beginPacket(); 114 LoRa.print("Alarm ausgeloest"); 115 // LoRa.print(counter); 116 LoRa.endPacket(); // transmit over the air (ota) 117 118 counter++; 119 120 delay(DelayBetween); // set to 10 minutes in productive areas 121 alert = false; 122} 123 124void loop(){ 125 hcsr05(); // call sonar function 126} 127
Code for Ardunio Nano (Sender)
c_cpp
1 2/* 3 * code for ultrasonic sensor hc-sr05 4 * 5 * description: playground.arduino.cc/Code/NewPing 6 * 7 ************ hc-sr05 module 8*/ 9// hcsr05 //////////////////////////////// 10#include <NewPing.h> // library for newping methods 11#include <dmtimer.h> // library for dmtimer methods 12 // 13 // vcc: power 5V 14#define trigPin 3 // trig: pwm 3 15#define echoPin 4 // echoPin: pwm 4 16 // gnd: black 17#define DistanceMax 10000 // 1000cm = 1m 18 // 19NewPing sonar1(trigPin, echoPin, DistanceMax); 20DMTimer myTimer1(50000); // set mytimer1 to 50ms (50000 = 50ms) 21////////////////////////////////////////// 22/* 23 * interfacing sx1278 (ra-02) lora module with arduino nano 24 * 25 * copyright (c) sandeep mistry. all rights reserved. licensed under the mit license. 26 * 27 * description: circuitdigest.com/microcontroller-projects/arduino-lora-sx1278-interfacing-tutorial 28 * 29 ************ lora module 30*/ 31// LoRa-02//////////////////////////////// 32#include <SPI.h> // library for spi methods 33#include <LoRa.h> // library for lora methods 34 // 35int counter = 0; // 36////////////////////////////////////////// 37#define DEBUG 1 // read debug information (1 = send data to serial port) 38int Distance = 0; // distance current (init=0) 39int DistHarmonic = 0; // distance harmonic (dynamischer mittelwert pro sekunde, init=0) 40int z = 0; // use 20 cicles for harmanic distance 41int DistRealCur = 0; // current real distance 42int DistRealOld = 0; // old real distance (bevor a second) 43int HarmonicLoop = 2; // how many harmonic loop 44int DelayBetween = 4000; // delay between measurements (perhaps 5 minutes) 45bool alert = false; // if something happen then alert is true 46////////////////////////////////////////// 47 48void setup(){ 49 Serial.begin(9600); 50 while (!Serial); 51 52 Serial.println("LoRa Sender"); 53 54 if (!LoRa.begin(433E6)) { // frequency 433 MHz for switzerland and eu 55 Serial.println("Starting LoRa failed!"); 56 while (1); 57 } 58} 59 60void hcsr05(){ 61 if (myTimer1.isTimeReached()){ // wait 50ms between pings (about 20 pings/sec) 62 // 29ms should be the shortest "delay" between pings 63 Distance = sonar1.ping_cm(); 64 65 if (Distance != 0) { 66 67 #if DEBUG == 1 68 Serial.print("Distance: "); 69 Serial.print(Distance); 70 Serial.println(" cm"); 71 #endif 72 73 z++; 74 if (z <= HarmonicLoop) { 75 DistHarmonic = DistHarmonic + Distance; 76 77 #if DEBUG == 1 78 //Serial.print("DistHarmonic: "); 79 //Serial.print(DistHarmonic); 80 //Serial.println(" cm"); 81 #endif 82 83 84 } else { 85 DistRealCur = DistHarmonic / HarmonicLoop; 86 87 if (DistRealOld - DistRealCur > 20){ 88 alert = true; // alarm = true if old and cur distance is different 89 ota(); // call ota function and send data 90 } 91 92 DistRealOld = DistRealCur; 93 94 DistHarmonic = 0; // start new harmonic cicle loop 95 z = 0; 96 DistRealCur = 0; 97 } 98 } 99 } 100} 101 102void ota(){ 103 Serial.print("Sending alert packet: "); 104 Serial.print(counter); 105 106 // send alert packet 107 #if DEBUG == 1 108 Serial.print(". Sending alert signal!! Distance = "); 109 Serial.print(DistRealCur); 110 Serial.println("cm"); 111 #endif 112 113 LoRa.beginPacket(); 114 LoRa.print("Alarm ausgeloest"); 115 // LoRa.print(counter); 116 LoRa.endPacket(); // transmit over the air (ota) 117 118 counter++; 119 120 delay(DelayBetween); // set to 10 minutes in productive areas 121 alert = false; 122} 123 124void loop(){ 125 hcsr05(); // call sonar function 126} 127
Code for Ardunio Nano (Receiver)
c_cpp
1/* 2 * interfacing sx1278 (ra-02) lora module with arduino nano 3 4 * 5 * copyright (c) sandeep mistry. all rights reserved. licensed under the mit 6 license. 7 * 8 * description: circuitdigest.com/microcontroller-projects/arduino-lora-sx1278-interfacing-tutorial 9 10 * 11 ************ lora module 12*/ 13// lora-02 /////////////////////////////// 14#include 15 <SPI.h> // library for spi methods 16#include <LoRa.h> 17 // library for lora methods 18 // 19////////////////////////////////////////// 20const 21 String SenderString = ""; // input is "Alarm ausgeloest" when something 22 happen 23 24/* 25 * piezo alarm 26 * 27 * specify digital pin on the arduino 28 nano that 29 * the positive lead of piezo buzzer is attached 30 ************ buzzer 31 module 32*/ 33int piezoPin = 3; // buzzer to arduino nano 34 pin 3 35int AlertCount = 0; // 36int AlertMax = 7; // 37 how many times (default = 7) 38////////////////////////////////////////// 39#define 40 DEBUG 1 // read debug information (1 = send data to serial 41 port) 42////////////////////////////////////////// 43 44void setup() { 45 46 47 Serial.begin(9600); 48 while (!Serial); 49 50 Serial.println("LoRa 51 Receiver"); 52 53 if (!LoRa.begin(433E6)) { // frequency 433 MHz 54 for switzerland and eu 55 Serial.println("Starting LoRa failed!"); 56 57 while (1); 58 } 59} 60 61void piezoalert() { 62 63 while 64 (AlertCount < AlertMax){ 65 /*tone needs 2 arguments, but can take three 66 67 1) pin# 68 2) frequency - this is in hertz (cycles per second) which 69 determines the pitch of the noise made 70 3) duration - how long teh tone 71 plays 72 */ 73 tone(piezoPin, 900, 300); 74 delay(500); 75 76 77 AlertCount++; 78 } 79} 80 81void loop() { 82 83 84 // try to parse packet 85 int packetSize = LoRa.parsePacket(); 86 if 87 (packetSize) { 88 // received a packet 89 Serial.print("Received 90 packet '"); 91 92 // read packet 93 while (LoRa.available()) { 94 95 // Serial.print((char)LoRa.read()); 96 SenderString = (char)LoRa.read(); 97 98 Serial.print(SenderString); 99 } 100 101 // print rssi 102 of packet 103 Serial.print("', signal strength to sender is "); 104 Serial.println(LoRa.packetRssi()); 105 106 107 if (SenderString == "t") { 108 piezoalert(); // 109 call alarm function 110 AlertCount = 0; // reset 111 } 112 113 } 114} 115
Code for Ardunio Nano (Receiver)
c_cpp
1/* 2 * interfacing sx1278 (ra-02) lora module with arduino nano 3 * 4 * copyright (c) sandeep mistry. all rights reserved. licensed under the mit license. 5 * 6 * description: circuitdigest.com/microcontroller-projects/arduino-lora-sx1278-interfacing-tutorial 7 * 8 ************ lora module 9*/ 10// lora-02 /////////////////////////////// 11#include <SPI.h> // library for spi methods 12#include <LoRa.h> // library for lora methods 13 // 14////////////////////////////////////////// 15const String SenderString = ""; // input is "Alarm ausgeloest" when something happen 16 17/* 18 * piezo alarm 19 * 20 * specify digital pin on the arduino nano that 21 * the positive lead of piezo buzzer is attached 22 ************ buzzer module 23*/ 24int piezoPin = 3; // buzzer to arduino nano pin 3 25int AlertCount = 0; // 26int AlertMax = 7; // how many times (default = 7) 27////////////////////////////////////////// 28#define DEBUG 1 // read debug information (1 = send data to serial port) 29////////////////////////////////////////// 30 31void setup() { 32 33 Serial.begin(9600); 34 while (!Serial); 35 36 Serial.println("LoRa Receiver"); 37 38 if (!LoRa.begin(433E6)) { // frequency 433 MHz for switzerland and eu 39 Serial.println("Starting LoRa failed!"); 40 while (1); 41 } 42} 43 44void piezoalert() { 45 46 while (AlertCount < AlertMax){ 47 /*tone needs 2 arguments, but can take three 48 1) pin# 49 2) frequency - this is in hertz (cycles per second) which determines the pitch of the noise made 50 3) duration - how long teh tone plays 51 */ 52 tone(piezoPin, 900, 300); 53 delay(500); 54 55 AlertCount++; 56 } 57} 58 59void loop() { 60 61 // try to parse packet 62 int packetSize = LoRa.parsePacket(); 63 if (packetSize) { 64 // received a packet 65 Serial.print("Received packet '"); 66 67 // read packet 68 while (LoRa.available()) { 69 // Serial.print((char)LoRa.read()); 70 SenderString = (char)LoRa.read(); 71 Serial.print(SenderString); 72 } 73 74 // print rssi of packet 75 Serial.print("', signal strength to sender is "); 76 Serial.println(LoRa.packetRssi()); 77 78 if (SenderString == "t") { 79 piezoalert(); // call alarm function 80 AlertCount = 0; // reset 81 } 82 } 83} 84
Downloadable files
Sender
Sender

Receiver
Receiver

Sender
Sender

Receiver
Receiver

Documentation
Schema
Schema
Comments
Only logged in users can leave comments