Components and supplies
Two 4.7K Ohm 1/4 watt resistors
Garmin LIDAR-Lite v3HP
Arduino MKR WiFi 1010
680 uF capacitor
Tools and machines
Breadboard, 170 Pin
10 Pc. Jumper Wire Kit, 5 cm Long
Apps and platforms
Pushsafer
Project description
Code
LIDAR intrusion sensor sketch
arduino
Sends LIDAR infrared laser pulses continuously and monitors the reflected beam for interruptions
1/*This IoT sketch is for a LIDAR Lite v3HP sensor instrusion alarm with an Arduino MKR WIFI 1010 microcontroller. The LIDAR 2 * sensor emits a narrow beam of infrared light with a range of up to 40 meters each time the loop is executed. The returned 3 * reflection is converted to the distance in centimeters when a new reading is available over I2C interface.The sketch 4 * compares the new distance to the previous distance and generates a notification through www.pushsafer.com to an iPhone XS 5 * with the Pushsafer iOS app installed and signed into the Pushsafer website. The microcontroller must be registered on the 6 * website account and the access key must be stored in the arduino_secrets.h include file along with the wifi network SSID 7 * and password. 8*/ 9#include <stdint.h> 10#include <Wire.h> 11#include <LIDARLite_v3HP.h> 12#include <WiFiNINA.h> 13#include <WiFiClient.h> 14#include <RTCZero.h> 15#include <Pushsafer.h> 16#include "arduino_secrets.h" 17 18// Wifi network login credentials 19char ssid[] = SECRET_SSID; // wifi network SSID (name) from arduino_secrets.h 20char password[] = SECRET_PASS; // wifi network key from arduino_secrets.h 21 22// Pushsafer private or alias key 23char PushsaferKey[] = SECRET_KEY; // pushsafer private key from arduin_secrets.h 24 25//Object declarations 26WiFiClient client; 27Pushsafer pushsafer(PushsaferKey, client); // Pushsafer object to send event 28RTCZero rtc; // Real time clock object on Arduino MKR board 29LIDARLite_v3HP myLidarLite; // LIDAR sensor object 30 31#define FAST_I2C // Operate the LIDAR Lite v3HP in fast serial communications mode 32 33//Variables 34float cmtofeet = 0.0328084; // Conversion factor from cm to feet 35uint16_t threshld = 20; // Threshold value for change in LIDAR distance reading to activate alarm 36String dtstg = String(__DATE__); // Compiler date for setting RTC 37String tmstg = String(__TIME__); // Compiler time for setting RTC 38uint16_t distance,prevdistance; // LIDAR sensor distance and previous distance 39bool firstpass; // Switch to suppress alarm on first loop iteration 40long t; // Time variable to prevent multiple alarm reporting on each beam interruption 41 42bool push = false; // Switch to prevent Pushsafer notification during testing 43 44// Executes once on startup 45void setup() 46{ 47 Serial.begin(115200); // Start serial communications 48 Wire.begin(); // Wait for serial port to initialize 49 50 // Attempt to connect to Wifi network: 51 Serial.print("Connecting Wifi: "); 52 Serial.println(ssid); 53 WiFi.begin(ssid, password); 54 55 // Wait until wifi connects 56 while (WiFi.status() != WL_CONNECTED) { 57 Serial.print("."); 58 delay(500); 59 } 60 61 // Wifi is connected. Display IP address 62 Serial.println(""); 63 Serial.println("WiFi connected"); 64 Serial.print("IP address: "); 65 Serial.println(WiFi.localIP()); 66 67 // Real time clock is used to timestamp alarms 68 rtc.begin(); // Start the real time clock 69 rtcSet(); // Set the RTC with the compile date and time 70 Serial.print("RTC started at: "); 71 if (rtc.getMonth() < 10) Serial.print("0"); // Display leading month zero if < 10 72 Serial.print(rtc.getMonth()); 73 Serial.print("-"); 74 if (rtc.getDay() < 10) Serial.print("0"); // Display leading day zero if < 10 75 Serial.print(rtc.getDay()); 76 Serial.print("-"); 77 Serial.print(rtc.getYear()); 78 Serial.print(" "); 79 if (rtc.getHours() < 10) Serial.print("0"); // Display leading hours zero if < 10 80 Serial.print(rtc.getHours()); 81 Serial.print(":"); 82 if (rtc.getMinutes() < 10) Serial.print("0"); // Display leading minutes zero if < 10 83 Serial.print(rtc.getMinutes()); 84 Serial.print(":"); 85 if (rtc.getSeconds() < 10) Serial.print("0"); // Display leading seconds zero if < 10 86 Serial.println(rtc.getSeconds()); 87 88 myLidarLite.configure(0); // Configure the LIDAR sensor performance and sensitivity 89 prevdistance = distance; // Set the previous distance for the first loop iteration 90 firstpass = true; // Turn on the switch to signal the first loop iteration completed 91 t = millis(); // Take a milliseconds since startup timestamp 92} 93 94// Main loop executes continuously 95void loop() 96{ 97 uint8_t newDistance = 0; // Flag set by distanceSingle function to signal new data 98 newDistance = distanceSingle(&distance); // Take a single LIDAR reading 99 // When there is new distance data, print it to the serial port 100 if (newDistance) 101 { 102 // If the difference between the new distance reading and the previous distance reading 103 // is greater than the threshold variable, send an alarm to Pushsafer and display the 104 // data on the serial monitor. Absolute value needed for negative or positive differences. 105 if (abs(distance - prevdistance) > threshld) 106 { 107 // LIDAR returns multiple distances per pulse. This code prevents more than one 108 // alarm every 2 seconds 109 if ((millis() - t) > 2000) 110 { 111 PrintValues(distance*cmtofeet); // Print the distance in feet 112 t = millis(); // Take another timestamp to prevent multiple alarms per pulse 113 } 114 } 115 prevdistance = distance; // Copy the new distance into the previous distance for the next loop 116 } 117 // Turn off the first poss flag which prevented an alarm on the first pass 118 firstpass = false; 119} 120 121// This function sends a single LIDAR pulse and returns a flag to indicate if there 122// is new data to the calling instruction in the main loop. The distance is read as a 123// pointer to the distance variable. This function comes from the LIDARLite_v3HP library 124// v3HP_I2C example sketch. 125uint8_t distanceSingle(uint16_t * distance) 126{ 127 // 1. Wait for busyFlag to indicate device is idle. This must be 128 // done before triggering a range measurement. 129 myLidarLite.waitForBusy(); 130 131 // 2. Trigger range measurement. 132 myLidarLite.takeRange(); 133 134 // 3. Wait for busyFlag to indicate device is idle. This should be 135 // done before reading the distance data that was triggered above. 136 myLidarLite.waitForBusy(); 137 138 // 4. Read new distance data from device registers 139 *distance = myLidarLite.readDistance(); 140 141 return 1; // Return a boolean value that indicates there is new data to be read 142} 143 144// Display a timestamp from the RTC and distance on the serial monitor and generate a 145// Pushsafer notification when an alarm is triggered 146void PrintValues(float d) 147{ 148 String yy,mm,dd,hr,mn,se; // String variables to hold the elements of the RTC timestamp 149 yy = String(rtc.getYear()); // Get the timestamp year from the RTC 150 if (rtc.getMonth() < 10) // Add a leading month zero if < 10 151 { 152 mm = String("0")+String(rtc.getMonth()); 153 } else { 154 mm = String(rtc.getMonth()); 155 } 156 if (rtc.getDay() < 10) // Add a leading day zero if < 10 157 { 158 dd = String("0")+String(rtc.getDay()); 159 } else { 160 dd = String(rtc.getDay()); 161 } 162 if (rtc.getHours() < 10) // Add a leading hour zero if < 10 163 { 164 hr = String("0")+String(rtc.getHours()); 165 } else { 166 hr = String(rtc.getHours()); 167 } 168 if (rtc.getMinutes() < 10) // Add a leading minute zero if < 10 169 { 170 mn = String("0")+String(rtc.getMinutes()); 171 } else { 172 mn = String(rtc.getMinutes()); 173 } 174 if (rtc.getSeconds() < 10) // Add a leading second zero if < 10 175 { 176 se = String("0")+String(rtc.getSeconds()); 177 } else { 178 se = String(rtc.getSeconds()); 179 } 180 // Combine the RTC timestamp elements into a single string 181 String timestr = mm+"-"+dd+"-"+yy+" "+hr + ":" + mn + ":" + se; 182 183 // This is the structure of the pushsafer input object 184 // Change the values for different notifications 185 struct PushSaferInput input; 186 input.message = "LiDAR sensed movement at " + String(timestr); 187 input.title = "LiDAR sensor tripped!"; 188 input.sound = "8"; 189 input.vibration = "1"; 190 input.icon = "1"; 191 input.iconcolor = "#FFCCCC"; 192 input.priority = "1"; 193 input.device = "a"; 194 input.url = "https://www.pushsafer.com"; 195 input.urlTitle = "Open Pushsafer.com"; 196 input.picture = ""; 197 input.picture2 = ""; 198 input.picture3 = ""; 199 input.time2live = ""; 200 input.retry = ""; 201 input.expire = ""; 202 input.answer = ""; 203 204 // Sends the push notification to the phone if the push switch is true 205 if (push) Serial.print(pushsafer.sendEvent(input)); 206 207 // Displays the alarm timestamp and distance in feet 208 Serial.print("MOVEMENT at "); 209 Serial.print(timestr); 210 Serial.print(", distance: "); 211 Serial.print(d); 212 Serial.println(" feet"); 213} 214 215// Sets the MKR WIFI 1010 RTC to the sketch compile time 216void rtcSet() 217{ 218 String months = "JanFebMarAprMayJunJulAugSepOctNovDec"; // Month identifier array 219 220 // Parses the compiler DATE string into separate substrings needed to set the RTC 221 String mm = dtstg.substring(0,3); 222 String dd = dtstg.substring(4,7); 223 String yy = dtstg.substring(8,11); 224 225 // Convert the date strings to integers needed to set the RTC 226 int ddnbr = dd.toInt(); 227 int mmnbr = int((months.indexOf(mm)/3)+1); 228 int yynbr = yy.toInt(); 229 230 // Parses the compiler TIME string into separate substrings needed to set the RTC 231 String hh = tmstg.substring(0,2); 232 String mn = tmstg.substring(3,5); 233 String ss = tmstg.substring(6,8); 234 235 // Convert the time strings to integers needed to set the RTC 236 int hhnbr = hh.toInt(); 237 int mnnbr = mn.toInt(); 238 int ssnbr = mn.toInt(); 239 240 // Set the RTC date and time 241 rtc.setDate(ddnbr,mmnbr,yynbr); 242 rtc.setTime(hhnbr,mnnbr,ssnbr); 243}
LIDAR intrusion sensor sketch
arduino
Sends LIDAR infrared laser pulses continuously and monitors the reflected beam for interruptions
1/*This IoT sketch is for a LIDAR Lite v3HP sensor instrusion alarm with an Arduino MKR WIFI 1010 microcontroller. The LIDAR 2 * sensor emits a narrow beam of infrared light with a range of up to 40 meters each time the loop is executed. The returned 3 * reflection is converted to the distance in centimeters when a new reading is available over I2C interface.The sketch 4 * compares the new distance to the previous distance and generates a notification through www.pushsafer.com to an iPhone XS 5 * with the Pushsafer iOS app installed and signed into the Pushsafer website. The microcontroller must be registered on the 6 * website account and the access key must be stored in the arduino_secrets.h include file along with the wifi network SSID 7 * and password. 8*/ 9#include <stdint.h> 10#include <Wire.h> 11#include <LIDARLite_v3HP.h> 12#include <WiFiNINA.h> 13#include <WiFiClient.h> 14#include <RTCZero.h> 15#include <Pushsafer.h> 16#include "arduino_secrets.h" 17 18// Wifi network login credentials 19char ssid[] = SECRET_SSID; // wifi network SSID (name) from arduino_secrets.h 20char password[] = SECRET_PASS; // wifi network key from arduino_secrets.h 21 22// Pushsafer private or alias key 23char PushsaferKey[] = SECRET_KEY; // pushsafer private key from arduin_secrets.h 24 25//Object declarations 26WiFiClient client; 27Pushsafer pushsafer(PushsaferKey, client); // Pushsafer object to send event 28RTCZero rtc; // Real time clock object on Arduino MKR board 29LIDARLite_v3HP myLidarLite; // LIDAR sensor object 30 31#define FAST_I2C // Operate the LIDAR Lite v3HP in fast serial communications mode 32 33//Variables 34float cmtofeet = 0.0328084; // Conversion factor from cm to feet 35uint16_t threshld = 20; // Threshold value for change in LIDAR distance reading to activate alarm 36String dtstg = String(__DATE__); // Compiler date for setting RTC 37String tmstg = String(__TIME__); // Compiler time for setting RTC 38uint16_t distance,prevdistance; // LIDAR sensor distance and previous distance 39bool firstpass; // Switch to suppress alarm on first loop iteration 40long t; // Time variable to prevent multiple alarm reporting on each beam interruption 41 42bool push = false; // Switch to prevent Pushsafer notification during testing 43 44// Executes once on startup 45void setup() 46{ 47 Serial.begin(115200); // Start serial communications 48 Wire.begin(); // Wait for serial port to initialize 49 50 // Attempt to connect to Wifi network: 51 Serial.print("Connecting Wifi: "); 52 Serial.println(ssid); 53 WiFi.begin(ssid, password); 54 55 // Wait until wifi connects 56 while (WiFi.status() != WL_CONNECTED) { 57 Serial.print("."); 58 delay(500); 59 } 60 61 // Wifi is connected. Display IP address 62 Serial.println(""); 63 Serial.println("WiFi connected"); 64 Serial.print("IP address: "); 65 Serial.println(WiFi.localIP()); 66 67 // Real time clock is used to timestamp alarms 68 rtc.begin(); // Start the real time clock 69 rtcSet(); // Set the RTC with the compile date and time 70 Serial.print("RTC started at: "); 71 if (rtc.getMonth() < 10) Serial.print("0"); // Display leading month zero if < 10 72 Serial.print(rtc.getMonth()); 73 Serial.print("-"); 74 if (rtc.getDay() < 10) Serial.print("0"); // Display leading day zero if < 10 75 Serial.print(rtc.getDay()); 76 Serial.print("-"); 77 Serial.print(rtc.getYear()); 78 Serial.print(" "); 79 if (rtc.getHours() < 10) Serial.print("0"); // Display leading hours zero if < 10 80 Serial.print(rtc.getHours()); 81 Serial.print(":"); 82 if (rtc.getMinutes() < 10) Serial.print("0"); // Display leading minutes zero if < 10 83 Serial.print(rtc.getMinutes()); 84 Serial.print(":"); 85 if (rtc.getSeconds() < 10) Serial.print("0"); // Display leading seconds zero if < 10 86 Serial.println(rtc.getSeconds()); 87 88 myLidarLite.configure(0); // Configure the LIDAR sensor performance and sensitivity 89 prevdistance = distance; // Set the previous distance for the first loop iteration 90 firstpass = true; // Turn on the switch to signal the first loop iteration completed 91 t = millis(); // Take a milliseconds since startup timestamp 92} 93 94// Main loop executes continuously 95void loop() 96{ 97 uint8_t newDistance = 0; // Flag set by distanceSingle function to signal new data 98 newDistance = distanceSingle(&distance); // Take a single LIDAR reading 99 // When there is new distance data, print it to the serial port 100 if (newDistance) 101 { 102 // If the difference between the new distance reading and the previous distance reading 103 // is greater than the threshold variable, send an alarm to Pushsafer and display the 104 // data on the serial monitor. Absolute value needed for negative or positive differences. 105 if (abs(distance - prevdistance) > threshld) 106 { 107 // LIDAR returns multiple distances per pulse. This code prevents more than one 108 // alarm every 2 seconds 109 if ((millis() - t) > 2000) 110 { 111 PrintValues(distance*cmtofeet); // Print the distance in feet 112 t = millis(); // Take another timestamp to prevent multiple alarms per pulse 113 } 114 } 115 prevdistance = distance; // Copy the new distance into the previous distance for the next loop 116 } 117 // Turn off the first poss flag which prevented an alarm on the first pass 118 firstpass = false; 119} 120 121// This function sends a single LIDAR pulse and returns a flag to indicate if there 122// is new data to the calling instruction in the main loop. The distance is read as a 123// pointer to the distance variable. This function comes from the LIDARLite_v3HP library 124// v3HP_I2C example sketch. 125uint8_t distanceSingle(uint16_t * distance) 126{ 127 // 1. Wait for busyFlag to indicate device is idle. This must be 128 // done before triggering a range measurement. 129 myLidarLite.waitForBusy(); 130 131 // 2. Trigger range measurement. 132 myLidarLite.takeRange(); 133 134 // 3. Wait for busyFlag to indicate device is idle. This should be 135 // done before reading the distance data that was triggered above. 136 myLidarLite.waitForBusy(); 137 138 // 4. Read new distance data from device registers 139 *distance = myLidarLite.readDistance(); 140 141 return 1; // Return a boolean value that indicates there is new data to be read 142} 143 144// Display a timestamp from the RTC and distance on the serial monitor and generate a 145// Pushsafer notification when an alarm is triggered 146void PrintValues(float d) 147{ 148 String yy,mm,dd,hr,mn,se; // String variables to hold the elements of the RTC timestamp 149 yy = String(rtc.getYear()); // Get the timestamp year from the RTC 150 if (rtc.getMonth() < 10) // Add a leading month zero if < 10 151 { 152 mm = String("0")+String(rtc.getMonth()); 153 } else { 154 mm = String(rtc.getMonth()); 155 } 156 if (rtc.getDay() < 10) // Add a leading day zero if < 10 157 { 158 dd = String("0")+String(rtc.getDay()); 159 } else { 160 dd = String(rtc.getDay()); 161 } 162 if (rtc.getHours() < 10) // Add a leading hour zero if < 10 163 { 164 hr = String("0")+String(rtc.getHours()); 165 } else { 166 hr = String(rtc.getHours()); 167 } 168 if (rtc.getMinutes() < 10) // Add a leading minute zero if < 10 169 { 170 mn = String("0")+String(rtc.getMinutes()); 171 } else { 172 mn = String(rtc.getMinutes()); 173 } 174 if (rtc.getSeconds() < 10) // Add a leading second zero if < 10 175 { 176 se = String("0")+String(rtc.getSeconds()); 177 } else { 178 se = String(rtc.getSeconds()); 179 } 180 // Combine the RTC timestamp elements into a single string 181 String timestr = mm+"-"+dd+"-"+yy+" "+hr + ":" + mn + ":" + se; 182 183 // This is the structure of the pushsafer input object 184 // Change the values for different notifications 185 struct PushSaferInput input; 186 input.message = "LiDAR sensed movement at " + String(timestr); 187 input.title = "LiDAR sensor tripped!"; 188 input.sound = "8"; 189 input.vibration = "1"; 190 input.icon = "1"; 191 input.iconcolor = "#FFCCCC"; 192 input.priority = "1"; 193 input.device = "a"; 194 input.url = "https://www.pushsafer.com"; 195 input.urlTitle = "Open Pushsafer.com"; 196 input.picture = ""; 197 input.picture2 = ""; 198 input.picture3 = ""; 199 input.time2live = ""; 200 input.retry = ""; 201 input.expire = ""; 202 input.answer = ""; 203 204 // Sends the push notification to the phone if the push switch is true 205 if (push) Serial.print(pushsafer.sendEvent(input)); 206 207 // Displays the alarm timestamp and distance in feet 208 Serial.print("MOVEMENT at "); 209 Serial.print(timestr); 210 Serial.print(", distance: "); 211 Serial.print(d); 212 Serial.println(" feet"); 213} 214 215// Sets the MKR WIFI 1010 RTC to the sketch compile time 216void rtcSet() 217{ 218 String months = "JanFebMarAprMayJunJulAugSepOctNovDec"; // Month identifier array 219 220 // Parses the compiler DATE string into separate substrings needed to set the RTC 221 String mm = dtstg.substring(0,3); 222 String dd = dtstg.substring(4,7); 223 String yy = dtstg.substring(8,11); 224 225 // Convert the date strings to integers needed to set the RTC 226 int ddnbr = dd.toInt(); 227 int mmnbr = int((months.indexOf(mm)/3)+1); 228 int yynbr = yy.toInt(); 229 230 // Parses the compiler TIME string into separate substrings needed to set the RTC 231 String hh = tmstg.substring(0,2); 232 String mn = tmstg.substring(3,5); 233 String ss = tmstg.substring(6,8); 234 235 // Convert the time strings to integers needed to set the RTC 236 int hhnbr = hh.toInt(); 237 int mnnbr = mn.toInt(); 238 int ssnbr = mn.toInt(); 239 240 // Set the RTC date and time 241 rtc.setDate(ddnbr,mmnbr,yynbr); 242 rtc.setTime(hhnbr,mnnbr,ssnbr); 243}
Downloadable files
Wiring Diagram
Wiring Diagram
Comments
Only logged in users can leave comments