Components and supplies
Buzzer, Piezo
Tactile Switch, Top Actuated
LED (generic)
Male/Male Jumper Wires
Arduino UNO
Resistor 1k ohm
Wemos D1 Mini
Apps and platforms
SMS Messaging API
Project description
Code
panicButton.ino
arduino
Upload this code to the Arduino Uno.
1#include "LowPower.h" 2 3// set the corresponding snooze/off button pin number 4const int buttonPin = 8; 5 6// set the four corresponding LED pin numbers 7const int ledPin1 = 4; 8const int ledPin2 = 3; 9const int ledPin3 = 5; 10const int ledPin4 = 6; 11const int alarmPin = 7; 12 13// initialize the snooze/off button state 14int buttonState = 0; 15 16void setup() { 17 // initialize the LED pins as outputs: 18 Serial.begin(9600); 19 pinMode(ledPin1, OUTPUT); 20 pinMode(ledPin2, OUTPUT); 21 pinMode(ledPin3, OUTPUT); 22 pinMode(ledPin4, OUTPUT); 23 24 // initialize the snooze button pin as an input: 25 pinMode(buttonPin, INPUT); 26 27} 28 29void loop() { 30 // after time is up, reads the state of the snooze button: 31 buttonState = digitalRead(buttonPin); 32 33 // checks to see if the snooze/off button is pressed: 34 if (buttonState == LOW) { 35 // alarm rings 36 // LEDs flash 37 tone(alarmPin, 3000); 38 digitalWrite(ledPin1, HIGH); 39 digitalWrite(ledPin2, HIGH); 40 digitalWrite(ledPin3, HIGH); 41 digitalWrite(ledPin4, HIGH); 42 delay (500); 43 noTone(alarmPin); 44 digitalWrite(ledPin1, LOW); 45 digitalWrite(ledPin2, LOW); 46 digitalWrite(ledPin3, LOW); 47 digitalWrite(ledPin4, LOW); 48 delay(500); 49 } 50 51 // if the snooze button is pressed 52 else { 53 // alarm stops 54 // turns LEDs off 55 noTone(alarmPin); 56 digitalWrite(ledPin1, LOW); 57 digitalWrite(ledPin2, LOW); 58 digitalWrite(ledPin3, LOW); 59 digitalWrite(ledPin4, LOW); 60 delay (1000); 61 // shuts down until reset pin is pressed 62 LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 63 } 64} 65
textAlert.ino
arduino
1#include "LowPower.h" 2 3// set the corresponding snooze/off button pin number 4const int buttonPin = 8; 5 6// set the four corresponding LED pin numbers 7const int ledPin1 = 4; 8const int ledPin2 = 3; 9const int ledPin3 = 5; 10const int ledPin4 = 6; 11const int alarmPin = 7; 12 13// initialize the snooze/off button state 14int buttonState = 0; 15 16void setup() { 17 // initialize the LED pins as outputs: 18 Serial.begin(9600); 19 pinMode(ledPin1, OUTPUT); 20 pinMode(ledPin2, OUTPUT); 21 pinMode(ledPin3, OUTPUT); 22 pinMode(ledPin4, OUTPUT); 23 24 // initialize the snooze button pin as an input: 25 pinMode(buttonPin, INPUT); 26 27} 28 29void loop() { 30 // after time is up, reads the state of the snooze button: 31 buttonState = digitalRead(buttonPin); 32 33 // checks to see if the snooze/off button is pressed: 34 if (buttonState == LOW) { 35 // alarm rings 36 // LEDs flash 37 tone(alarmPin, 3000); 38 digitalWrite(ledPin1, HIGH); 39 digitalWrite(ledPin2, HIGH); 40 digitalWrite(ledPin3, HIGH); 41 digitalWrite(ledPin4, HIGH); 42 delay (500); 43 noTone(alarmPin); 44 digitalWrite(ledPin1, LOW); 45 digitalWrite(ledPin2, LOW); 46 digitalWrite(ledPin3, LOW); 47 digitalWrite(ledPin4, LOW); 48 delay(500); 49 } 50 51 // if the snooze button is pressed 52 else { 53 // alarm stops 54 // turns LEDs off 55 noTone(alarmPin); 56 digitalWrite(ledPin1, LOW); 57 digitalWrite(ledPin2, LOW); 58 digitalWrite(ledPin3, LOW); 59 digitalWrite(ledPin4, LOW); 60 delay (1000); 61 // shuts down until reset pin is pressed 62 LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 63 } 64} 65
Establishing ESP8266 to Twilio Connection
Download this folder developed by Twilio Developer Education from GitHub. Replace the code in twilio_esp8266_arduino_example.ino with the code in textAlert.ino. Upload this file to the ESP8266 WeMos D1 Mini.
textAlert.ino
arduino
1#include <ESP8266WiFi.h> 2#include <WiFiClientSecure.h> 3#include <ESP8266WebServer.h> 4#include "twilio.hpp" 5 6// Use software serial for debugging? 7#define USE_SOFTWARE_SERIAL 0 8 9// Print debug messages over serial? 10#define USE_SERIAL 1 11 12// Your network SSID and password 13const char* ssid = "XXXXXX"; 14const char* password = "XXXXXXX"; 15 16// Find the api.twilio.com SHA1 fingerprint, this one was valid as 17// of July 2020. This will change, please see 18// https://www.twilio.com/docs/sms/tutorials/how-to-send-sms-messages-esp8266-cpp 19// to see how to update the fingerprint. 20const char fingerprint[] = "72 1C 17 31 85 E2 7E 0D F9 D4 C2 5B A0 0E BD B7 E2 06 26 ED"; 21 22// Twilio account specific details, from https://twilio.com/console 23// Please see the article: 24// https://www.twilio.com/docs/guides/receive-and-reply-sms-and-mms-messages-esp8266-c-and-ngrok 25 26// If this device is deployed in the field you should only deploy a revocable 27// key. This code is only suitable for prototyping or if you retain physical 28// control of the installation. 29const char* account_sid = ""; 30const char* auth_token = ""; 31 32// Details for the SMS we'll send with Twilio. Should be a number you own 33// (check the console, link above). 34String to_number = "+1XXXXXXXXXX"; 35String from_number = "+1XXXXXXXXXX"; 36String message_body = "Emergency! The panic alarm was pressed!"; 37 38// The 'authorized number' to text the ESP8266 for our example 39String master_number = "+1XXXXXXXXXX"; 40 41// Optional - a url to an image. See 'MediaUrl' here: 42// https://www.twilio.com/docs/api/rest/sending-messages 43String media_url = ""; 44 45// Global twilio objects 46Twilio *twilio; 47ESP8266WebServer twilio_server(8000); 48 49// Optional software serial debugging 50#if USE_SOFTWARE_SERIAL == 1 51#include <SoftwareSerial.h> 52// You'll need to set pin numbers to match your setup if you 53// do use Software Serial 54extern SoftwareSerial swSer(14, 4, false, 256); 55#else 56#define swSer Serial 57#endif 58 59/* 60 * Callback function when we hit the /message route with a webhook. 61 * Use the global 'twilio_server' object to respond. 62 */ 63 64 void handle_message() { 65 #if USE_SERIAL == 1 66 swSer.println("Incoming connection! Printing body:"); 67 #endif 68 bool authorized = false; 69 char command = '\\0'; 70 71 // Parse Twilio's request to the ESP 72 for (int i = 0; i < twilio_server.args(); ++i) { 73 #if USE_SERIAL == 1 74 swSer.print(twilio_server.argName(i)); 75 swSer.print(": "); 76 swSer.println(twilio_server.arg(i)); 77 #endif 78 79 if (twilio_server.argName(i) == "From" and 80 twilio_server.arg(i) == master_number) { 81 authorized = true; 82 } else if (twilio_server.argName(i) == "Body") { 83 if (twilio_server.arg(i) == "?" or 84 twilio_server.arg(i) == "0" or 85 twilio_server.arg(i) == "1") { 86 command = twilio_server.arg(i)[0]; 87 } 88 } 89 } // end for loop parsing Twilio's request 90 91 // Logic to handle the incoming SMS 92 // (Some board are active low so the light will have inverse logic) 93 String response = "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>"; 94 if (command != '\\0') { 95 if (authorized) { 96 switch (command) { 97 case '0': 98 digitalWrite(LED_BUILTIN, LOW); 99 response += "<Response><Message>" 100 "Turning light off!" 101 "</Message></Response>"; 102 break; 103 case '1': 104 digitalWrite(LED_BUILTIN, HIGH); 105 response += "<Response><Message>" 106 "Turning light on!" 107 "</Message></Response>"; 108 break; 109 case '?': 110 default: 111 response += "<Response><Message>" 112 "0 - Light off, 1 - Light On, " 113 "? - Help\ 114" 115 "The light is currently: "; 116 response += digitalRead(LED_BUILTIN); 117 response += "</Message></Response>"; 118 break; 119 } 120 } else { 121 response += "<Response><Message>" 122 "Unauthorized!" 123 "</Message></Response>"; 124 } 125 126 } else { 127 response += "<Response><Message>" 128 "Look: a SMS response from an ESP8266!" 129 "</Message></Response>"; 130 } 131 132 twilio_server.send(200, "application/xml", response); 133} 134 135/* 136 * Setup function for ESP8266 Twilio Example. 137 * 138 * Here we connect to a friendly wireless network, set the time, instantiate 139 * our twilio object, optionally set up software serial, then send a SMS 140 * or MMS message. 141 * 142 * 143 */ 144void setup() { 145 WiFi.begin(ssid, password); 146 twilio = new Twilio(account_sid, auth_token, fingerprint); 147 148 #if USE_SERIAL == 1 149 swSer.begin(115200); 150 while (WiFi.status() != WL_CONNECTED) { 151 delay(1000); 152 swSer.print("."); 153 } 154 swSer.println(""); 155 swSer.println("Connected to WiFi, IP address: "); 156 swSer.println(WiFi.localIP()); 157 #else 158 while (WiFi.status() != WL_CONNECTED) delay(1000); 159 #endif 160 161 // Response will be filled with connection info and Twilio API responses 162 // from this initial SMS send. 163 String response; 164 bool success = twilio->send_message( 165 to_number, 166 from_number, 167 message_body, 168 response, 169 media_url 170 ); 171 172 // Set up a route to /message which will be the webhook url 173 twilio_server.on("/message", handle_message); 174 twilio_server.begin(); 175 176 // Use LED_BUILTIN to find the LED pin and set the GPIO to output 177 pinMode(LED_BUILTIN, OUTPUT); 178 179 #if USE_SERIAL == 1 180 swSer.println(response); 181 #endif 182} 183 184 185/* 186 * In our main loop, we listen for connections from Twilio in handleClient(). 187 */ 188void loop() { 189 190 twilio_server.handleClient(); 191}
panicButton.ino
arduino
Upload this code to the Arduino Uno.
1#include "LowPower.h" 2 3// set the corresponding snooze/off button 4 pin number 5const int buttonPin = 8; 6 7// set the four corresponding LED 8 pin numbers 9const int ledPin1 = 4; 10const int ledPin2 = 3; 11const int ledPin3 12 = 5; 13const int ledPin4 = 6; 14const int alarmPin = 7; 15 16// initialize 17 the snooze/off button state 18int buttonState = 0; 19 20void setup() { 21 22 // initialize the LED pins as outputs: 23 Serial.begin(9600); 24 pinMode(ledPin1, 25 OUTPUT); 26 pinMode(ledPin2, OUTPUT); 27 pinMode(ledPin3, OUTPUT); 28 pinMode(ledPin4, 29 OUTPUT); 30 31 // initialize the snooze button pin as an input: 32 pinMode(buttonPin, 33 INPUT); 34 35} 36 37void loop() { 38 // after time is up, reads the state 39 of the snooze button: 40 buttonState = digitalRead(buttonPin); 41 42 // checks 43 to see if the snooze/off button is pressed: 44 if (buttonState == LOW) { 45 // 46 alarm rings 47 // LEDs flash 48 tone(alarmPin, 3000); 49 digitalWrite(ledPin1, 50 HIGH); 51 digitalWrite(ledPin2, HIGH); 52 digitalWrite(ledPin3, HIGH); 53 54 digitalWrite(ledPin4, HIGH); 55 delay (500); 56 noTone(alarmPin); 57 58 digitalWrite(ledPin1, LOW); 59 digitalWrite(ledPin2, LOW); 60 digitalWrite(ledPin3, 61 LOW); 62 digitalWrite(ledPin4, LOW); 63 delay(500); 64 } 65 66 // 67 if the snooze button is pressed 68 else { 69 // alarm stops 70 // turns 71 LEDs off 72 noTone(alarmPin); 73 digitalWrite(ledPin1, LOW); 74 digitalWrite(ledPin2, 75 LOW); 76 digitalWrite(ledPin3, LOW); 77 digitalWrite(ledPin4, LOW); 78 79 delay (1000); 80 // shuts down until reset pin is pressed 81 LowPower.powerDown(SLEEP_FOREVER, 82 ADC_OFF, BOD_OFF); 83 } 84} 85
Downloadable files
Circuit Schematic
Circuit Schematic
Circuit Schematic
Circuit Schematic
Comments
Only logged in users can leave comments