Last active
November 12, 2018 22:02
-
-
Save thomasbrueggemann/e6214168f2ddaa8e53feb1d961e6143d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Wire.h" | |
#include <ESP8266HTTPClient.h> | |
#include <ESP8266WiFi.h> | |
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050 | |
const int MOVING_DELTA_THRESHOLD = 1000; | |
const int ALARM_PENALTY_MILLIS = 60000; | |
const char* WIFI_SSID = "Rumblebuff"; // The SSID (name) of the Wi-Fi network you want to connect to | |
const char* WIFI_PASSWORD = "7696298557852265"; // The password of the Wi-Fi network | |
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ; | |
int16_t lastAcX, lastAcY, lastAcZ; | |
bool initialRound = true; | |
unsigned long lastAlarm = 0; | |
void setup() | |
{ | |
// open serial connection | |
Serial.begin(115200); | |
// wifi connection | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
Serial.print("Connecting to "); | |
Serial.print(WIFI_SSID); | |
Serial.println(" ..."); | |
// wait for the Wi-Fi to connect | |
int i = 0; | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(1000); | |
Serial.print(++i); | |
Serial.print(' '); | |
} | |
Serial.println('\n'); | |
Serial.println("Connection established!"); | |
Serial.print("IP address:\t"); | |
// print the IP address of the ESP8266 | |
Serial.println(WiFi.localIP()); | |
Wire.begin(); | |
// check that there is an MPU | |
check_I2c(MPU_ADDR); | |
Wire.beginTransmission(MPU_ADDR); | |
Wire.write(0x6B); // PWR_MGMT_1 register | |
Wire.write(0); // set to zero (wakes up the MPU-6050) | |
Wire.endTransmission(true); | |
} | |
void loop() | |
{ | |
Wire.beginTransmission(MPU_ADDR); | |
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) | |
Wire.endTransmission(false); | |
Wire.requestFrom(MPU_ADDR, 14, true); // request a total of 14 registers | |
AcX = Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L) | |
AcY = Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) | |
AcZ = Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L) | |
Tmp = Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L) | |
GyX = Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L) | |
GyY = Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L) | |
GyZ = Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L) | |
if(initialRound == false) | |
{ | |
// calculate deltas | |
int16_t deltaAcX = abs(lastAcX - AcX); | |
int16_t deltaAcY = abs(lastAcY - AcY); | |
int16_t deltaAcZ = abs(lastAcZ - AcZ); | |
// we are moving! | |
if(deltaAcX > MOVING_DELTA_THRESHOLD || deltaAcY > MOVING_DELTA_THRESHOLD || deltaAcZ > MOVING_DELTA_THRESHOLD) | |
{ | |
// see if the last alarm was reported at least X minutes ago | |
unsigned long alarmDelta = millis() - lastAlarm; | |
if(alarmDelta >= ALARM_PENALTY_MILLIS || lastAlarm == 0) | |
{ | |
lastAlarm = millis(); | |
Serial.println("ALARM!"); | |
triggerIFTTT(); | |
} | |
else | |
{ | |
Serial.println("moved"); | |
} | |
} | |
} | |
// store last values | |
lastAcX = AcX; | |
lastAcY = AcY; | |
lastAcZ = AcZ; | |
initialRound = false; | |
delay(500); // Wait 0.5 seconds and scan again | |
} | |
int triggerIFTTT() | |
{ | |
Serial.println(WiFi.status()); | |
HTTPClient http; | |
http.begin("http://maker.ifttt.com/trigger/mailbox-open/with/key/dV8aGPm8fpV905zlw6xGLQ"); | |
int httpCode = http.GET(); | |
Serial.println(httpCode); | |
// close connection | |
http.end(); | |
return httpCode; | |
} | |
byte check_I2c(byte addr) | |
{ | |
// we are using the return value of | |
// the Write.endTransmisstion to see if | |
// a device did acknowledge to the address. | |
byte error; | |
Wire.beginTransmission(addr); | |
error = Wire.endTransmission(); | |
if (error == 0) | |
{ | |
Serial.print(" Device Found at 0x"); | |
Serial.println(addr,HEX); | |
} | |
else | |
{ | |
Serial.print(" No Device Found at 0x"); | |
Serial.println(addr,HEX); | |
} | |
return error; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment