Created
October 20, 2016 13:36
-
-
Save yene/168ca7984fef4638ae6655ef88058a02 to your computer and use it in GitHub Desktop.
laundy-sensor.ino
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
// MPU6050 Includes | |
#include "Wire.h" | |
#include "I2Cdev.h" | |
#include "MPU6050.h" | |
// ESP8266 Includes | |
#include <ESP8266WiFi.h> | |
// DEFINE NODEMCU PINS | |
#define D0 16 | |
#define D1 5 // I2C Bus SCL (clock) | |
#define D2 4 // I2C Bus SDA (data) | |
#define D3 0 | |
#define D4 2 // Same as "LED_BUILTIN", but inverted logic | |
#define D9 3 // RX0 (Serial console) | |
#define D10 1 // TX0 (Serial console) | |
// DEFINE STATES | |
#define CLOSED 0 //washer door closed | |
#define OPEN 1 //washer door open | |
#define EMPTY 0 //washer empty | |
#define RUNNING 1 //washer running | |
#define COMPLETE 2 //washer complete | |
#define MOVEMENT_DETECTED 0 | |
#define MOVEMENT_NOT_DETECTED 1 | |
// ESP8266 WIFI ---------------------------------------------------------------- | |
const char* ssid = ""; | |
const char* password = ""; | |
// END ESP8266 WIFI ------------------------------------------------------------ | |
// MPU-6050 Accelerometers ------------------------------------------------------ | |
MPU6050 MPU_WASHER(0x68); //WASHER | |
int16_t washer_ax, washer_ay, washer_az; | |
int16_t washer_gx, washer_gy, washer_gz; | |
int16_t washer_ax_min = 0, washer_ax_max = 0, washer_ax_range; | |
int16_t washer_ay_min = 0, washer_ay_max = 0, washer_ay_range; | |
int16_t washer_az_min = 0, washer_az_max = 0, washer_az_range; | |
// end MPU-6050------------------------------------------------------------------- | |
unsigned long sample_time = 0; //millis of last reading | |
// accelerometer sensor (Washer) ============================================== | |
int washer_reading = 0; //reading = 1 mean no noise, 0=noise | |
int washer_reading_previous = 0; | |
int washer_state = RUNNING; //1 = running, 0 = empty, 2 = complete | |
int washer_detector_count = 0; //number of captures | |
int washer_detected_count = 0; //number of readings showing sound | |
char washerString[50]; | |
// CONFIGURABLE THRESHOLDS | |
int washer_ay_threshold = 18000; | |
int washer_detected_threshold = 3; | |
boolean washer_detailed_reporting = true; | |
void setup() { | |
Serial.begin(9600); // setup serial | |
setup_wifi(); | |
Wire.begin(); | |
MPU_WASHER.initialize(); | |
Serial.println(MPU_WASHER.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed")); | |
} | |
void setup_wifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void webhookDone() { | |
// Get key from maker channel https://ifttt.com/maker | |
// And connect with IF app on iOS | |
const char* key = ""; | |
const char* eventName = "washer"; | |
const char* host = "maker.ifttt.com"; | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
Serial.println("connection failed"); | |
ESP.deepSleep(0); | |
return; | |
} | |
char url[50]; | |
sprintf(url, "/trigger/%s/with/key/%s", eventName, key); | |
Serial.print("Requesting URL: "); | |
Serial.println(url); | |
// This will send the request to the server | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); | |
delay(500); | |
// Read all the lines of the reply from server and print them to Serial | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
Serial.println(); | |
Serial.println("closing connection"); | |
// deep sleep forever | |
ESP.deepSleep(0); | |
} | |
void loop() { | |
MPU_WASHER.getMotion6(&washer_ax, &washer_ay, &washer_az, &washer_gx, &washer_gy, &washer_gz); | |
trackMinMax(washer_ax, &washer_ax_min, &washer_ax_max); | |
trackMinMax(washer_ay, &washer_ay_min, &washer_ay_max); | |
trackMinMax(washer_az, &washer_az_min, &washer_az_max); | |
if (sample_time > millis()) { | |
sample_time = millis(); | |
} | |
// samples every 5 sec | |
if ((millis() - sample_time) > 5000) { | |
sample_time = millis(); //reset sample_time to wait for next Xms | |
// Calculate Range for each accelerometer direction. | |
washer_ax_range = washer_ax_max - washer_ax_min; | |
washer_ay_range = washer_ay_max - washer_ay_min; | |
washer_az_range = washer_az_max - washer_az_min; | |
// Reset Range Counters | |
washer_ax_min = 0, washer_ax_max = 0; | |
washer_ay_min = 0, washer_ay_max = 0; | |
washer_az_min = 0, washer_az_max = 0; | |
if (washer_detailed_reporting) { | |
sprintf(washerString, "washer %d, %d, %d, %d", washer_state, washer_ax_range, washer_ay_range, washer_az_range); | |
Serial.println(washerString); | |
} | |
if (abs(washer_ax_range) > washer_ay_threshold) { | |
washer_reading = MOVEMENT_DETECTED; | |
Serial.println("MOVEMENT_DETECTED"); | |
} | |
washer_detector_count = washer_detector_count + 1; //count how many times we listened | |
if (washer_reading == MOVEMENT_DETECTED) { //count how many times detected movement | |
washer_detected_count = washer_detected_count + 1; | |
} | |
washer_reading = MOVEMENT_NOT_DETECTED; //reset | |
} //end reading every 5 seconds | |
if (washer_detector_count >= 15) { | |
if (washer_detected_count >= washer_detected_threshold) { | |
washer_state = RUNNING; | |
Serial.println("running"); | |
} else if (washer_state == RUNNING) { | |
washer_state = COMPLETE; | |
Serial.println("completed"); | |
webhookDone(); | |
} | |
washer_detector_count = 0; | |
washer_detected_count = 0; | |
} | |
} | |
int16_t trackMinMax(int16_t current, int16_t *min, int16_t *max) { | |
if (current > *max) { | |
*max = current; | |
} else if (current < *min) { | |
*min = current; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get an error message when trying to compile. What do I do wrong?
Archiving built core (caching) in: C:\Users\work\AppData\Local\Temp\arduino_cache_868268\core\core_esp8266_esp8266_nodemcuv2_CpuFrequency_80,UploadSpeed_115200,FlashSize_4M3M_0525d7961506ba0e78aacb222a0a5fc4.a sketch\laundry.ino.cpp.o: In function
setup_wifi()':C:\Users\work\Documents\Arduino\laundry/laundry.ino:77: undefined reference to `MPU6050::initialize()'
C:\Users\work\Documents\Arduino\laundry/laundry.ino:82: undefined reference to `MPU6050::testConnection()'
sketch\laundry.ino.cpp.o: In function `setup':
C:\Users\work\Documents\Arduino\laundry/laundry.ino:85: undefined reference to `MPU6050::initialize()'
C:\Users\work\Documents\Arduino\laundry/laundry.ino:86: undefined reference to `MPU6050::testConnection()'
C:\Users\work\Documents\Arduino\laundry/laundry.ino:65: undefined reference to `MPU6050::MPU6050(unsigned char)'
sketch\laundry.ino.cpp.o: In function `__static_initialization_and_destruction_0':
C:\Users\work\Documents\Arduino\laundry/laundry.ino:65: undefined reference to `MPU6050::MPU6050(unsigned char)'
sketch\laundry.ino.cpp.o: In function `webhookDone()':
C:\Users\work\Documents\Arduino\laundry/laundry.ino:118: undefined reference to `MPU6050::getMotion6(short*, short*, short*, short*, short*, short*)'
C:\Users\work\Documents\Arduino\laundry/laundry.ino:123: undefined reference to `MPU6050::getMotion6(short*, short*, short*, short*, short*, short*)'
collect2.exe: error: ld returned 1 exit status
exit status 1
Fehler beim Kompilieren für das Board NodeMCU 1.0 (ESP-12E Module).
`