Last active
October 2, 2023 03:21
-
-
Save kakopappa/f4fd3d769144689f15cfc80d783e8c94 to your computer and use it in GitHub Desktop.
Sinric Pro Motion Sensor. Tutorial: https://help.sinric.pro/pages/tutorials/motion-sensors/HC-SR501-HC-SR505-AM312-HC-SR312.html
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
// Uncomment the following line to enable serial debug output | |
//#define ENABLE_DEBUG | |
#ifdef ENABLE_DEBUG | |
#define DEBUG_ESP_PORT Serial | |
#define NODEBUG_WEBSOCKETS | |
#define NDEBUG | |
#endif | |
#include <Arduino.h> | |
#if defined(ESP8266) | |
#include <ESP8266WiFi.h> | |
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040) | |
#include <WiFi.h> | |
#endif | |
#include "SinricPro.h" | |
#include "SinricProMotionsensor.h" | |
#define WIFI_SSID "" // Your WiFI SSID name | |
#define WIFI_PASS "" // Your WiFi Password. | |
#define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" (Get it from Portal -> Secrets) | |
#define APP_SECRET "" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" (Get it from Portal -> Secrets) | |
#define MOTION_SENSOR_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx" (Get it from Portal -> Devices) | |
#define BAUD_RATE 115200 // Change baudrate to your need (used for serial monitor) | |
#if defined(ESP8266) | |
#define SENSOR_PIN D1 | |
#elif defined(ESP32) | |
#define SENSOR_PIN 26 | |
#elif (ARDUINO_ARCH_RP2040) | |
#define SENSOR_PIN 5 | |
#endif | |
int newState; | |
int oldState; | |
void handleMotionSensor() { | |
newState = digitalRead(SENSOR_PIN); // read current state | |
if(oldState != newState) { | |
if (newState == HIGH) { | |
Serial.println("Movement detected!"); | |
} else { | |
Serial.println("Movement not detected!"); | |
} | |
// newState == true (HIGH) : if motion has been detected | |
// newState == false (LOW): if no motion has been detected | |
SinricProMotionsensor &myMotionsensor = SinricPro[MOTION_SENSOR_ID]; // get motion sensor device | |
myMotionsensor.sendMotionEvent(newState); | |
oldState = newState; | |
} | |
} | |
// setup function for WiFi connection | |
void setupWiFi() { | |
Serial.printf("\r\n[Wifi]: Connecting"); | |
#if defined(ESP8266) | |
WiFi.setSleepMode(WIFI_NONE_SLEEP); | |
WiFi.setAutoReconnect(true); | |
#elif defined(ESP32) | |
WiFi.setSleep(false); | |
WiFi.setAutoReconnect(true); | |
#endif | |
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 &mySensor = SinricPro[MOTION_SENSOR_ID]; | |
// setup SinricPro | |
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); | |
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); }); | |
//SinricPro.restoreDeviceStates(true); // Uncomment to restore the last known state from the server. | |
SinricPro.begin(APP_KEY, APP_SECRET); | |
} | |
void setupMotionSensor() { | |
pinMode(SENSOR_PIN, INPUT); | |
Serial.printf("Wait 10 secs for the PIR sensor to calibrate properly\r\n"); | |
delay(1000 * 10); | |
} | |
// main setup function | |
void setup() { | |
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); | |
setupMotionSensor(); | |
setupWiFi(); | |
setupSinricPro(); | |
} | |
void loop() { | |
SinricPro.handle(); | |
handleMotionSensor(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment