Skip to content

Commit 7fc79e6

Browse files
committed
Fixed FAN example (using SinricProFanUS.h)
1 parent 864e813 commit 7fc79e6

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

examples/Fan/Fan.ino

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#endif
2828

2929
#include "SinricPro.h"
30-
#include "SinricProFan.h"
30+
#include "SinricProFanUS.h"
3131

3232
#define WIFI_SSID "YOUR-WIFI-SSID"
3333
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
@@ -37,10 +37,10 @@
3737
#define BAUD_RATE 9600 // Change baudrate to your need
3838

3939
// we use a struct to store all states and values for our fan
40-
// powerLevel is used for fan speed (0..100)
40+
// fanSpeed (1..3)
4141
struct {
4242
bool powerState = false;
43-
int powerLevel = 0;
43+
int fanSpeed = 1;
4444
} device_state;
4545

4646
bool onPowerState(const String &deviceId, bool &state) {
@@ -49,16 +49,21 @@ bool onPowerState(const String &deviceId, bool &state) {
4949
return true; // request handled properly
5050
}
5151

52-
bool onPowerLevel(const String &deviceId, int &powerLevel) {
53-
device_state.powerLevel = powerLevel;
54-
Serial.printf("Fan speed changed to %d\r\n", device_state.powerLevel);
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);
5556
return true;
5657
}
5758

58-
bool onAdjustPowerLevel(const String &deviceId, int levelDelta) {
59-
device_state.powerLevel += levelDelta;
60-
Serial.printf("Fan speed changed about %i to %d\r\n", levelDelta, device_state.powerLevel);
61-
levelDelta = device_state.powerLevel;
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
6267
return true;
6368
}
6469

@@ -74,12 +79,12 @@ void setupWiFi() {
7479
}
7580

7681
void setupSinricPro() {
77-
SinricProFan &myFan = SinricPro[FAN_ID];
82+
SinricProFanUS &myFan = SinricPro[FAN_ID];
7883

7984
// set callback function to device
8085
myFan.onPowerState(onPowerState);
81-
myFan.onPowerLevel(onPowerLevel);
82-
myFan.onAdjustPowerLevel(onAdjustPowerLevel);
86+
myFan.onRangeValue(onRangeValue);
87+
myFan.onAdjustRangeValue(onAdjustRangeValue);
8388

8489
// setup SinricPro
8590
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });

0 commit comments

Comments
 (0)