Skip to content

Commit 438a9c0

Browse files
committed
Merge branch 'master' into dev-2.4.0
2 parents feba7ce + 359e49d commit 438a9c0

File tree

2 files changed

+113
-9
lines changed

2 files changed

+113
-9
lines changed

examples/Fan/Fan.ino

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* If you encounter any issues:
3+
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
4+
* - ensure all dependent libraries are installed
5+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
6+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
7+
* - open serial monitor and check whats happening
8+
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
9+
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
10+
*/
11+
12+
// Uncomment the following line to enable serial debug output
13+
//#define ENABLE_DEBUG
14+
15+
#ifdef ENABLE_DEBUG
16+
#define DEBUG_ESP_PORT Serial
17+
#define NODEBUG_WEBSOCKETS
18+
#define NDEBUG
19+
#endif
20+
21+
#include <Arduino.h>
22+
#ifdef ESP8266
23+
#include <ESP8266WiFi.h>
24+
#endif
25+
#ifdef ESP32
26+
#include <WiFi.h>
27+
#endif
28+
29+
#include "SinricPro.h"
30+
#include "SinricProFanUS.h"
31+
32+
#define WIFI_SSID "YOUR-WIFI-SSID"
33+
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
34+
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
35+
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
36+
#define FAN_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
37+
#define BAUD_RATE 9600 // Change baudrate to your need
38+
39+
// we use a struct to store all states and values for our fan
40+
// fanSpeed (1..3)
41+
struct {
42+
bool powerState = false;
43+
int fanSpeed = 1;
44+
} device_state;
45+
46+
bool onPowerState(const String &deviceId, bool &state) {
47+
Serial.printf("Fan turned %s\r\n", state?"on":"off");
48+
device_state.powerState = state;
49+
return true; // request handled properly
50+
}
51+
52+
// Fan rangeValue is from 1..3
53+
bool onRangeValue(const String &deviceId, int &rangeValue) {
54+
device_state.fanSpeed = rangeValue;
55+
Serial.printf("Fan speed changed to %d\r\n", device_state.fanSpeed);
56+
return true;
57+
}
58+
59+
// Fan rangeValueDelta is from -3..+3
60+
bool onAdjustRangeValue(const String &deviceId, int rangeValueDelta) {
61+
device_state.fanSpeed += rangeValueDelta;
62+
if (device_state.fanSpeed < 1) device_state.fanSpeed = 1;
63+
if (device_state.fanSpeed > 3) device_state.fanSpeed = 3;
64+
Serial.printf("Fan speed changed about %i to %d\r\n", rangeValueDelta, device_state.fanSpeed);
65+
66+
rangeValueDelta = device_state.fanSpeed; // return absolute fan speed
67+
return true;
68+
}
69+
70+
void setupWiFi() {
71+
Serial.printf("\r\n[Wifi]: Connecting");
72+
WiFi.begin(WIFI_SSID, WIFI_PASS);
73+
74+
while (WiFi.status() != WL_CONNECTED) {
75+
Serial.printf(".");
76+
delay(250);
77+
}
78+
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
79+
}
80+
81+
void setupSinricPro() {
82+
SinricProFanUS &myFan = SinricPro[FAN_ID];
83+
84+
// set callback function to device
85+
myFan.onPowerState(onPowerState);
86+
myFan.onRangeValue(onRangeValue);
87+
myFan.onAdjustRangeValue(onAdjustRangeValue);
88+
89+
// setup SinricPro
90+
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
91+
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
92+
SinricPro.begin(APP_KEY, APP_SECRET);
93+
}
94+
95+
// main setup function
96+
void setup() {
97+
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
98+
setupWiFi();
99+
setupSinricPro();
100+
}
101+
102+
void loop() {
103+
SinricPro.handle();
104+
}

examples/Switch/Switch.ino

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
#define SWITCH_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
4242
#define BAUD_RATE 9600 // Change baudrate to your need
4343

44-
#define BTN_FLASH 0
44+
#define BUTTON_PIN 0 // GPIO for BUTTON (inverted: LOW = pressed, HIGH = released)
45+
#define LED_PIN 2 // GPIO for LED (inverted)
4546

4647
bool myPowerState = false;
4748
unsigned long lastBtnPress = 0;
@@ -62,19 +63,19 @@ unsigned long lastBtnPress = 0;
6263
bool onPowerState(const String &deviceId, bool &state) {
6364
Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
6465
myPowerState = state;
65-
digitalWrite(LED_BUILTIN, myPowerState?LOW:HIGH);
66+
digitalWrite(LED_PIN, myPowerState?LOW:HIGH);
6667
return true; // request handled properly
6768
}
6869

6970
void handleButtonPress() {
7071
unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis
71-
if (digitalRead(BTN_FLASH) == LOW && actualMillis - lastBtnPress > 1000) { // is button pressed (inverted logic! button pressed = LOW) and debounced?
72+
if (digitalRead(BUTTON_PIN) == LOW && actualMillis - lastBtnPress > 1000) { // is button pressed (inverted logic! button pressed = LOW) and debounced?
7273
if (myPowerState) { // flip myPowerState: if it was true, set it to false, vice versa
7374
myPowerState = false;
7475
} else {
7576
myPowerState = true;
7677
}
77-
digitalWrite(LED_BUILTIN, myPowerState?LOW:HIGH); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)
78+
digitalWrite(LED_PIN, myPowerState?LOW:HIGH); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)
7879

7980
// get Switch device back
8081
SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
@@ -95,8 +96,7 @@ void setupWiFi() {
9596
Serial.printf(".");
9697
delay(250);
9798
}
98-
IPAddress localIP = WiFi.localIP();
99-
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
99+
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
100100
}
101101

102102
// setup function for SinricPro
@@ -115,9 +115,9 @@ void setupSinricPro() {
115115

116116
// main setup function
117117
void setup() {
118-
pinMode(BTN_FLASH, INPUT_PULLUP); // GPIO 0 as input, pulled high
119-
pinMode(LED_BUILTIN, OUTPUT); // define LED GPIO as output
120-
digitalWrite(LED_BUILTIN, HIGH); // turn off LED on bootup
118+
pinMode(BUTTON_PIN, INPUT_PULLUP); // GPIO 0 as input, pulled high
119+
pinMode(LED_PIN, OUTPUT); // define LED GPIO as output
120+
digitalWrite(LED_PIN, HIGH); // turn off LED on bootup
121121

122122
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
123123
setupWiFi();

0 commit comments

Comments
 (0)