Created
May 13, 2020 16:35
-
-
Save mokjpn/6966a6f6551fbb8b48f1338ade9ea0fd to your computer and use it in GitHub Desktop.
M5Atom Sinric Motion Sensor
This file contains hidden or 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 <M5Atom.h> | |
#include <WiFi.h> | |
#include <SinricPro.h> | |
#include <SinricProMotionsensor.h> | |
#define WIFI_SSID "YOUR_SSID" | |
#define WIFI_PASS "YOUR_WIFIPASS" | |
#define APP_KEY "YOUR_APPKEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" | |
#define APP_SECRET "YOUR_APP_SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" | |
#define MOTIONSENSOR_ID "YOUR_DEVICEID" // Should look like "5dc1564130xxxxxxxxxxxxxx" | |
#define MOTIONSENSOR_PIN 32 // M5Atom + M5 Motion Sensor Unit | |
bool myPowerState = true; // assume device is turned on | |
bool lastMotionState = false; | |
unsigned long lastChange = 0; | |
uint8_t DisBuff[2 + 5 * 5 * 3]; | |
bool onPowerState(const String &deviceId, bool &state) { | |
Serial.printf("device %s turned %s\r\n", deviceId.c_str(), state?"on":"off"); | |
return true; // indicate that callback handled correctly | |
} | |
void handleMotionsensor() { | |
if (!myPowerState) return; // if device switched off...do nothing | |
unsigned long actualMillis = millis(); | |
if (actualMillis - lastChange < 250) return; // debounce motionsensor state transitions (same as debouncing a pushbutton) | |
bool actualMotionState = digitalRead(MOTIONSENSOR_PIN); // read actual state of motion sensor | |
if (actualMotionState != lastMotionState) { // if state has changed | |
Serial.printf("Motion %s\r\n", actualMotionState?"detected":"not detected"); | |
if(actualMotionState != 0) { | |
setBuff(0x00, 0xff, 0x00); // if any motion detected, set color to green. | |
M5.dis.displaybuff(DisBuff); | |
} else { | |
setBuff(0x80, 0x00, 0x80); // if any motion detected, set color to magenda. | |
M5.dis.displaybuff(DisBuff); | |
} | |
lastMotionState = actualMotionState; // update last known state | |
lastChange = actualMillis; // update debounce time | |
SinricProMotionsensor &myMotionsensor = SinricPro[MOTIONSENSOR_ID]; // get motion sensor device | |
myMotionsensor.sendMotionEvent(actualMotionState); | |
} | |
} | |
// setup function for WiFi connection | |
void setupWiFi() { | |
Serial.printf("\r\n[Wifi]: Connecting"); | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.printf("."); | |
delay(250); | |
} | |
IPAddress localIP = WiFi.localIP(); | |
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); | |
} | |
// setup function for SinricPro | |
void setupSinricPro() { | |
// add device to SinricPro | |
SinricProMotionsensor& myMotionsensor = SinricPro[MOTIONSENSOR_ID]; | |
// set callback function to device | |
myMotionsensor.onPowerState(onPowerState); | |
// setup SinricPro | |
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); | |
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); }); | |
SinricPro.begin(APP_KEY, APP_SECRET); | |
} | |
void setBuff(uint8_t Rdata, uint8_t Gdata, uint8_t Bdata) | |
{ | |
DisBuff[0] = 0x05; | |
DisBuff[1] = 0x05; | |
for (int i = 0; i < 25; i++) | |
{ | |
DisBuff[2 + i * 3 + 0] = Rdata; | |
DisBuff[2 + i * 3 + 1] = Gdata; | |
DisBuff[2 + i * 3 + 2] = Bdata; | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
M5.begin(true, false, true); | |
delay(10); | |
setBuff(0xff, 0x00, 0x00); // set color to red. | |
M5.dis.displaybuff(DisBuff); | |
// PIR sensor unit connected to M5Atom port. | |
pinMode(MOTIONSENSOR_PIN, INPUT); | |
setupWiFi(); | |
setupSinricPro(); | |
setBuff(0x00, 0xff, 0x00); // after initializing, set color to green. | |
M5.dis.displaybuff(DisBuff); | |
} | |
static uint32_t count = 0; | |
static bool isdark = false; | |
void loop() { | |
handleMotionsensor(); | |
M5.update(); | |
SinricPro.handle(); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment