This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Blinds.ino
134 lines (107 loc) · 4.02 KB
/
Blinds.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/****************************************************************************************************************************
Blinds.ino
For ESP32/ESP8266 boards
Based on and modified from SinricPro libarary (https://github.com/sinricpro/)
to support other boards such as SAMD21, SAMD51, Adafruit's nRF52 boards, etc.
Built by Khoi Hoang https://github.com/khoih-prog/SinricPro_Generic
Licensed under MIT license
**********************************************************************************************************************************/
/*
Example for how to use SinricPro Blinds device
If you encounter any issues:
- check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
- ensure all dependent libraries are installed
- see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
- see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
- open serial monitor and check whats happening
- check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
- visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
*/
#if !(defined(ESP8266) || defined(ESP32))
#error This code is intended to run on the ESP32/ESP8266 boards ! Please check your Tools->Board setting.
#endif
// 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
#if (ESP8266)
#include <ESP8266WiFi.h>
#elif (ESP32)
#include <WiFi.h>
#endif
#include "SinricPro_Generic.h"
#include "SinricProBlinds.h"
#define WIFI_SSID "YOUR-WIFI-SSID"
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
#define APP_KEY "YOUR-APP-KEY" // 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 BLINDS_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 115200 // Change baudrate to your need
int blindsPosition = 0;
bool powerState = false;
bool onPowerState(const String &deviceId, bool &state)
{
Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state ? "on" : "off");
powerState = state;
return true; // request handled properly
}
bool onSetPosition(const String &deviceId, int &position)
{
Serial.printf("Device %s set position to %d\r\n", deviceId.c_str(), position);
return true; // request handled properly
}
bool onAdjustPosition(const String &deviceId, int &positionDelta)
{
blindsPosition += positionDelta;
Serial.printf("Device %s position changed about %i to %d\r\n", deviceId.c_str(), positionDelta, blindsPosition);
positionDelta = blindsPosition; // calculate and return absolute position
return true; // request handled properly
}
// setup function for WiFi connection
void setupWiFi()
{
Serial.print("\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(250);
}
Serial.print("\n[WiFi]: IP-Address is ");
Serial.println(WiFi.localIP());
}
void setupSinricPro()
{
// get a new Blinds device from SinricPro
SinricProBlinds &myBlinds = SinricPro[BLINDS_ID];
myBlinds.onPowerState(onPowerState);
myBlinds.onSetPosition(onSetPosition);
myBlinds.onAdjustPosition(onAdjustPosition);
// setup SinricPro
SinricPro.onConnected([]()
{
Serial.println("Connected to SinricPro");
});
SinricPro.onDisconnected([]()
{
Serial.println("Disconnected from SinricPro");
});
SinricPro.begin(APP_KEY, APP_SECRET);
}
// main setup function
void setup()
{
Serial.begin(BAUD_RATE);
while (!Serial);
Serial.println("\nStarting Blinds on " + String(ARDUINO_BOARD));
Serial.println("Version : " + String(SINRICPRO_VERSION_STR));
setupWiFi();
setupSinricPro();
}
void loop()
{
SinricPro.handle();
}