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
/
DimSwitch.ino
137 lines (112 loc) · 4.1 KB
/
DimSwitch.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
135
136
137
/****************************************************************************************************************************
DimSwitch.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
**********************************************************************************************************************************/
/*
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 "SinricProDimSwitch.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 DIMSWITCH_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 115200 // Change baudrate to your need
// we use a struct to store all states and values for our dimmable switch
struct
{
bool powerState = false;
int powerLevel = 0;
} device_state;
bool onPowerState(const String &deviceId, bool &state)
{
Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state ? "on" : "off");
device_state.powerState = state;
return true; // request handled properly
}
bool onPowerLevel(const String &deviceId, int &powerLevel)
{
device_state.powerLevel = powerLevel;
Serial.printf("Device %s power level changed to %d\r\n", deviceId.c_str(), device_state.powerLevel);
return true;
}
bool onAdjustPowerLevel(const String &deviceId, int &levelDelta)
{
device_state.powerLevel += levelDelta;
Serial.printf("Device %s power level changed about %i to %d\r\n", deviceId.c_str(), levelDelta,
device_state.powerLevel);
levelDelta = device_state.powerLevel;
return true;
}
// 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()
{
SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];
// set callback function to device
myDimSwitch.onPowerState(onPowerState);
myDimSwitch.onPowerLevel(onPowerLevel);
myDimSwitch.onAdjustPowerLevel(onAdjustPowerLevel);
// 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 DimSwitch on " + String(ARDUINO_BOARD));
Serial.println("Version : " + String(SINRICPRO_VERSION_STR));
setupWiFi();
setupSinricPro();
}
void loop()
{
SinricPro.handle();
}