This will send an email with a link and password when free fall and impact thresholds are detected.......
I am not very able and fall regularly..............
/*
* This is a slight extension of the work of J.A. Acosta to allow the "secure" viewing of an attitude monitor, MPU6050
* this can be a trigger for email the device can also "unlock" the page based on sensor conditions for use as a Fall Detector.
*
* @Gengus..............
* _______________________________________________________________________________________________________
* This work is an improvement on previous tutorials published here ot other webs
* Particular thanks to Rui Santos and its Arduino-Ethernet Shield tutorials from which tis the base for this work (and the CSS styleSheet is used here)
* Visit: http://randomnerdtutorials.com for more details
*
* Also the "Garage Door Opener V2 by SWilson http://www.esp8266.com/viewtopic.php?f=29&t=2696 provided most of the help
* very useful info needed to handle ajax html and other WEB-Related technologies can be found at http://www.w3schools.com/js/default.asp
*
* Fell free to copy/improve/etc onle compensation wanted is you share your ESP8266 projects here
*
* WARNING: this project is released "as is" with no warranty, you are responsible for it, use it as learning tool not as part for a commercial
* or production device.
*
* LICENSE: MIT (which means you can copy and if you like to share it again as you like, no snake GPL viral clauses.
*
* this project for testing uses an css style sheet read from Rui Santosś examples "ethernetcss.css" you can find it at its Arduino-Ethernet sample
* my safety suggestion if you decide to use an CSS style sheet derived or no from 'santos, is you should host it at your own controlled server,
* this way minimizes DDoS Attacks or other Attacks on your IoT network.
* Preferable to use ths solution on an Intranet with VPN access from the exterior and not allow the IoT modules to receive externa connexions.
*
* I'don't speak English natively, I do may best effort to express here, sorry for any error
*
* Thanks
* J.A. Acosta
*
*/
#include <ESP8266WiFi.h>
#include <Wire.h>
// Create placeholders fot MPU6050..........
int16_t AcX, AcY, AcZ, Tmpt, Tmp, GyX, GyY, GyZ, AcXo, AcYo, AcZo, GyXo, GyYo, GyZo;
#define serverPort 80
// define (map) GPIO to HTML Fields (buttons/Indicators), note: not using 0 as base index deliberately
ADC_MODE(ADC_VCC);
#define input1 16
#define input2 14
#define input3 0
#define statusLEDr 15
#define statusLEDg 12
#define statusLEDb 13
#define output1 4
#define output2 5
#define output3 2
//#define output4 0
boolean got_user = false;
extern "C"
{
#include "user_interface.h"
}
WiFiServer server(serverPort);
WiFiClient client;
//*-- IoT Information
const char* _SSID = "Your-SSID";
const char* _PASS = "Your-Password";
const char* _passcode = "1234";//note: the field "passcode in the html form "frm1" must match the passcode lenght
char passcodeOK='0';//start Code is blank...
String readString;
float analogReadOut = 0;
int dispcont = 0;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(57600);
Wire.begin();
Wire.beginTransmission(0x68); // 0x68 I2C address of the MPU-6050
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
pinMode(output3, OUTPUT);
// avoid using pin0 while on development, cause is more difficult to upload code since ESP_COMM must be connected before owncode start
//pinMode(output4, OUTPUT); //uncomment to control output #4
pinMode(statusLEDr, OUTPUT);
pinMode(statusLEDg, OUTPUT);
pinMode(statusLEDb, OUTPUT);
pinMode(input1, INPUT);
pinMode(input2, INPUT);
pinMode(input3, INPUT);
delay(50);
Serial.print("Connecting to ");
Serial.println(_SSID);
WiFi.begin(_SSID, _PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected to:");
Serial.println(_SSID);
delay(1000);
ESP.wdtEnable(WDTO_4S);
//Start the server
server.begin();
Serial.println("Server started");
//Print the IP Address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print(":");
Serial.print(serverPort);
Serial.println("/");
delay(1000);
}
void loop() {
//reset code for re-validation
passcodeOK='0';
// Create a client connection
WiFiClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
}
// HTTP request has ended
if (c == '\n') {
if (readString.indexOf(String("sensorupdrq"))>0){//this branch purpose is to discriminate ajax update request from standad web request to, if you dont need
Serial.println("\r\najax sensor upd req:"); //comment the entire branch all '//<<<' lines
Serial.print(readString);//<<<
client.println("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: keep-alive"); // <<<
readInputs(client);//<<<
}//<<<
else//<<<
{
Serial.println("web request:");
Serial.print(readString);
if (readString.indexOf(String("passcode="))>0){
if (readString.indexOf(String(_passcode))>0) {
passcodeOK='1';
Serial.println("Password OK");
digitalWrite(statusLEDg, HIGH);
digitalWrite(statusLEDr, LOW);
}else{
passcodeOK='0';
Serial.println("Password NotOK");
digitalWrite(statusLEDg, LOW);
digitalWrite(statusLEDr, HIGH);
}
}
client.println("HTTP/1.1 200 OK\r\nContent-Type: text/html");
client.println("\r\n<HTML>");
client.println("<HEAD>");
client.println("<meta name=\"viewport\" content=\"width=380\">");
// Important, edit the Following line to match your CSS style sheet location, I suggest you to host at your own controlled server
client.println("<link rel='stylesheet' type='text/css' href='http://192.168.1.22/style.css' />");
client.println("<TITLE>DDT Fall Detector</TITLE>");
// simple javascritp code to intercept anchor clientick and trick HREF field with the Button (or action) ID and add the password from the input field
client.println("\r\n<script>\r\nfunction SndCommand( obx,swtchNO ){ \r\nobx.href=\"button\"+swtchNO+\"&passcode=\"+document.getElementById(\"frm1\").elements[0].value;\r\n}\r\n</script>\r\n");
// not so simple script to allow sensor data to be automatically updated
client.println("\r\n<script>\r\nfunction GetSensorUpd() {\r\nnocache = \"&nocache=\" + Math.random() * 1000000;");
client.println("var ajaxreq = new XMLHttpRequest();");
client.println("ajaxreq.open(\"GET\", \"sensorupdrq\" + nocache, true);");
client.println("ajaxreq.onreadystatechange = function() {");
client.println("if (ajaxreq.readyState == 4 && ajaxreq.status == 200) {");
client.println("document.getElementById(\"readX\").innerHTML = ajaxreq.responseText;");
client.println("}\r\n}\r\najaxreq.send();");
client.println("setTimeout('GetSensorUpd()', 5000);\r\n}\r\n</script>\r\n");//updates each half second
client.println("</HEAD>");
client.println("<BODY onload=\"GetSensorUpd()\">");//comment thiss if you do not want automatic sensor update
// client.println("<BODY>");//uncomment this if yoy do not want auto update
client.println("<H1>Demo to Display the Motion Data of MPU6050 Securely<br />With<br /> Open Sensor/Switch Reads on the MCU</H1>");
client.println("<br />");
client.print("<div id=\"passx\"><form id=\"frm1\">Please Input Password<BR>To Unlock Data:<BR><input name=\"passcode\" size=\"5\" maxlength=\"4\" type=\"password\" value=\"");
if (passcodeOK== '1') client.print(_passcode);
client.println("\" style='font-size:24pt'></form></div>");
client.print("<div id=\"readX\">");
readInputs(client);
client.println("</div>");
// first ON/OFF Buttons pair
client.println("<BR /><a href=\"\" onclick=\"SndCommand(this,11)\">Output 1 On</a>");
//SndCommand is a javascript code running on the clientiet (aka ajax) Parameters are outputIDx and State
client.println("<a href=\"\" onclick=\"SndCommand(this,10)\">Output 1 Off</a><br /><br />");
// then repeat for every gpio as output to control
client.println("<a href=\"\" onclick=\"SndCommand(this,21)\">Output 2 On</a>");
client.println("<a href=\"\" onclick=\"SndCommand(this,20)\">Output 2 Off</a><br /><br />");
client.println("<a href=\"\" onclick=\"SndCommand(this,31)\">Output 3 On</a>");
client.println("<a href=\"\" onclick=\"SndCommand(this,30)\">Output 3 Off</a><br /><br />");
client.println("<BR>the use of static html with data refreshed dynamically </BODY>");
client.println("</HTML>");
//Processing client request
// Button0
if (readString.indexOf("button11") >0 && passcodeOK== '1'){
digitalWrite(output1, HIGH);
}
if (readString.indexOf("button10")>0 && passcodeOK== '1'){
digitalWrite(output1, LOW);
}
if (readString.indexOf("button21") >0 && passcodeOK== '1'){
digitalWrite(output2, HIGH);
}
if (readString.indexOf("button20")>0 && passcodeOK== '1'){
digitalWrite(output2, LOW);
}
if (readString.indexOf("button31") >0 && passcodeOK== '1'){
digitalWrite(output3, HIGH);
}
if (readString.indexOf("button30")>0 && passcodeOK== '1'){
digitalWrite(output3, LOW);
}
}
delay(1);
client.stop();
delay(0);
//clean readString for next read
readString="";
digitalWrite(statusLEDg, LOW);
digitalWrite(statusLEDr, LOW);
dispcont++;
if (dispcont>25){got_user=false;dispcont=0;}// clear the user flag
}
}
}
}
}
// send back GPIO Data Refresh
void readInputs(WiFiClient cl) {
cl.println("\r\n<p>");//dont miss to wrap the request with some tag, mandatory for ajax to work properly
analogReadOut = ESP.getVcc();
// Accelerometer 0x43
// Gyro 0x3B
Wire.beginTransmission(0x68);
Wire.write(0x3B); // starting with register 0x43 (GYRO_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(0x68, 14, true); // request a total of 6 registers or What you need
GyX = Wire.read() << 8 | Wire.read();
GyY = Wire.read() << 8 | Wire.read();
GyZ = Wire.read() << 8 | Wire.read();
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
AcX = Wire.read() << 8 | Wire.read();
AcY = Wire.read() << 8 | Wire.read();
AcZ = Wire.read() << 8 | Wire.read();
Wire.endTransmission(true);
Tmpt = Tmp/340.00+36.53;
if (analogReadOut < 3000){
cl.print("<p>System Voltage:<span style='background-color:#FF0000; font-size:18pt'>");
cl.print(analogReadOut/=1000,3);
cl.println(" Volts</span></p>");
}else{
cl.print("<p>System Voltage:<span style='background-color:#00FF00; font-size:18pt'>");
cl.print(analogReadOut/=1000,3);
cl.println(" Volts</span></p>");
}
//________________________________Secured Data Start______________________________________________
// this will ensure sensor condition or user verification before displaying this section
if ((digitalRead(input3))&&(passcodeOK== '0')&&(got_user==false)) {
}else{
got_user = true;
cl.println("<BR>Attitude is relative to facing the Device LED<BR><BR>");
if (Tmpt < 20){
cl.print("<p>Body Temperature:<span style='background-color:#FF0000; font-size:18pt'>");
cl.print(Tmpt);
cl.println("°C</span></p>");
}else{
cl.print("<p>Body Temperature:<span style='background-color:#00FF00; font-size:18pt'>");
cl.print(Tmpt);
cl.println("°C</span></p>");
}
GyXo = GyX/=100;
if (GyXo < 0){
cl.print("<p>Device is Upside Down :<span style='background-color:#FF0000; font-size:18pt'>");
cl.print(GyXo);
cl.println("°</span></p>");
}else{
cl.print("<p>Right Side Up 180° is Verticle:<span style='background-color:#00FF00; font-size:18pt'>");
cl.print(GyXo);
cl.println("°</span></p>");
}
GyYo = GyY/=100;
if (GyYo < 0){
cl.print("<p>Left Inclination:<span style='background-color:#0000FF; font-size:18pt'>");
cl.print(GyYo);
cl.println("°</span></p>");
}else{
cl.print("<p>Right Inclination:<span style='background-color:#00FF00; font-size:18pt'>");
cl.print(GyYo);
cl.println("°</span></p>");
}
GyZo = GyZ/=100;
if (GyZo < 0){
cl.print("<p>Backward Inclination:<span style='background-color:#0000FF; font-size:18pt'>");
cl.print(GyZo);
cl.println("°</span></p>");
}else{
cl.print("<p>Forward Inclination:<span style='background-color:#00FF00; font-size:18pt'>");
cl.print(GyZo);
cl.println("°</span></p>");
}
}
// ______________________________________secured data end___________________________________________
cl.println("<br />");
if (digitalRead(input1)) {
cl.println("<p>Sensor ONE is: <span style='background-color:#00FF00; font-size:18pt'>ALL OK</span></p>");
}else{
cl.println("<p>Sensor ONE is: <span style='background-color:#FF0000; font-size:18pt'>Assistance</span></p>");
}
cl.println("<br />");
if (digitalRead(input2)) {
cl.println("<p>Sensor TWO is: <span style='background-color:#00FF00; font-size:18pt'>ALL OK</span></p>");
}else{
cl.println("<p>Sensor TWO is: <span style='background-color:#FF0000; font-size:18pt'>Assistance</span></p>");
}
cl.println("<br />");
if (digitalRead(input3)) {
cl.println("<p>Flash Button is: <span style='background-color:#00FF00; font-size:18pt'> ALL OK</span></p>");
}else{
cl.println("<p>Flash Button is: <span style='background-color:#0000FF; font-size:18pt'>Assistance</span></p>");
}
cl.println("</p>");//this tag completes the request wrap
Serial.println("ajax sensor upd sent\r\n");
}