Skip to content

Commit 74c818c

Browse files
committed
New Example
- MotionSensor
1 parent defc269 commit 74c818c

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Example for SinricPro Motionsensor device:
3+
* - Setup motionsensor device
4+
* - Support onPowerState to turn on / turn off motion sensor
5+
* - Checks a motion sensor connected to MOTIONSENSOR_PIN and send event if state changed
6+
*
7+
* If you encounter any issues:
8+
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
9+
* - ensure all dependent libraries are installed
10+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
11+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
12+
* - open serial monitor and check whats happening
13+
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
14+
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
15+
*/
16+
17+
// Uncomment the following line to enable serial debug output
18+
//#define ENABLE_DEBUG
19+
20+
#ifdef ENABLE_DEBUG
21+
#define DEBUG_ESP_PORT Serial
22+
#define NODEBUG_WEBSOCKETS
23+
#define NDEBUG
24+
#endif
25+
26+
#include <Arduino.h>
27+
#ifdef ESP8266
28+
#include <ESP8266WiFi.h>
29+
#endif
30+
#ifdef ESP32
31+
#include <WiFi.h>
32+
#endif
33+
34+
#include "SinricPro.h"
35+
#include "SinricProMotionsensor.h"
36+
37+
#define WIFI_SSID "YOUR-WIFI-SSID"
38+
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
39+
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
40+
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
41+
#define MOTIONSENSOR_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
42+
#define BAUD_RATE 9600 // Change baudrate to your need
43+
44+
#define MOTIONSENSOR_PIN 5 // PIN where motionsensor is connected to
45+
// LOW = motion is not detected
46+
// HIGH = motion is detected
47+
48+
49+
bool myPowerState = true; // assume device is turned on
50+
bool lastMotionState = false;
51+
unsigned long lastChange = 0;
52+
53+
/**
54+
* @brief Checks motionsensor connected to MOTIONSENSOR_PIN
55+
*
56+
* If motion sensor state has changed, send event to SinricPro Server
57+
* state from digitalRead():
58+
* HIGH = motion detected
59+
* LOW = motion not detected
60+
*/
61+
void handleMotionsensor() {
62+
if (!myPowerState) return; // if device switched off...do nothing
63+
64+
unsigned long actualMillis = millis();
65+
if (actualMillis - lastChange < 250) return; // debounce motionsensor state transitions (same as debouncing a pushbutton)
66+
67+
bool actualMotionState = digitalRead(MOTIONSENSOR_PIN); // read actual state of motion sensor
68+
69+
if (actualMotionState != lastMotionState) { // if state has changed
70+
Serial.printf("Motion %s\r\n", actualMotionState?"detected":"not detected");
71+
lastMotionState = actualMotionState; // update last known state
72+
lastChange = actualMillis; // update debounce time
73+
SinricProMotionsensor &myMotionsensor = SinricPro[MOTIONSENSOR_ID]; // get motion sensor device
74+
myMotionsensor.sendMotionEvent(actualMotionState);
75+
}
76+
}
77+
78+
/**
79+
* @brief Callback for setPowerState request
80+
*
81+
* @param deviceId String containing deviceId (useful if this callback used by multiple devices)
82+
* @param[in] state bool true=turn on device / false=turn off device
83+
* @param[out] state bool true=device turned on / false=device turned off
84+
* @return true request handled properly
85+
* @return false request can't be handled because some kind of error happened
86+
*/
87+
bool onPowerState(const String &deviceId, bool &state) {
88+
Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
89+
myPowerState = state;
90+
return true; // request handled properly
91+
}
92+
93+
94+
// setup function for WiFi connection
95+
void setupWiFi() {
96+
Serial.printf("\r\n[Wifi]: Connecting");
97+
WiFi.begin(WIFI_SSID, WIFI_PASS);
98+
99+
while (WiFi.status() != WL_CONNECTED) {
100+
Serial.printf(".");
101+
delay(250);
102+
}
103+
IPAddress localIP = WiFi.localIP();
104+
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
105+
}
106+
107+
// setup function for SinricPro
108+
void setupSinricPro() {
109+
// add device to SinricPro
110+
SinricProMotionsensor& myMotionsensor = SinricPro[MOTIONSENSOR_ID];
111+
112+
// set callback function to device
113+
myMotionsensor.onPowerState(onPowerState);
114+
115+
// setup SinricPro
116+
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
117+
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
118+
SinricPro.begin(APP_KEY, APP_SECRET);
119+
}
120+
121+
// main setup function
122+
void setup() {
123+
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
124+
125+
pinMode(MOTIONSENSOR_PIN, INPUT);
126+
127+
setupWiFi();
128+
setupSinricPro();
129+
}
130+
131+
void loop() {
132+
handleMotionsensor();
133+
SinricPro.handle();
134+
}

0 commit comments

Comments
 (0)