-
-
Save sivar2311/34fef76c5de2f6ebd02c252a4a5a6028 to your computer and use it in GitHub Desktop.
SinricProRCSwitchReceive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <Arduino.h> | |
#include <SinricProSwitch.h> | |
class Device { | |
public: | |
Device(String deviceId, unsigned long rf_code, int gpio); | |
void turn_state(bool new_state, bool send_event); | |
void flip_state(bool sendEvent); | |
const String& getDeviceId(); | |
const unsigned long getRFCode(); | |
protected: | |
String deviceId; | |
unsigned long rf_code; | |
int gpio; | |
bool current_state; | |
unsigned long last_flip; | |
}; | |
Device::Device(String deviceId, unsigned long rf_code, int gpio) | |
: deviceId(deviceId) | |
, rf_code(rf_code) | |
, gpio(gpio) | |
, current_state(false) | |
, last_flip(0) { | |
} | |
void Device::turn_state(bool new_state, bool send_event) { | |
if (current_state == new_state) return; | |
current_state = new_state; | |
digitalWrite(gpio, current_state); | |
if (send_event) { | |
SinricProSwitch& sinricProSwitch = SinricPro[deviceId]; | |
sinricProSwitch.sendPowerStateEvent(current_state); | |
} | |
} | |
void Device::flip_state(bool sendEvent) { | |
unsigned long current_millis = millis(); | |
if (last_flip && current_millis - last_flip < 1000) return; | |
last_flip = current_millis; | |
bool new_state = !current_state; | |
turn_state(new_state, sendEvent); | |
} | |
const String& Device::getDeviceId() { | |
return deviceId; | |
}; | |
const unsigned long Device::getRFCode() { | |
return rf_code; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <RCSwitch.h> | |
#include <SinricPro.h> | |
#include <SinricProSwitch.h> | |
#include "Device.h" | |
// configuration | |
#ifdef ESP32 | |
#include <WiFi.h> | |
#define RF_RECEIVER 13 | |
#define RELAY_PIN_1 12 | |
#define RELAY_PIN_2 14 | |
#else | |
#include <ESP8266WiFi.h> | |
#define RF_RECEIVER 5 | |
#define RELAY_PIN_1 4 | |
#define RELAY_PIN_2 14 | |
#endif | |
#define RF_CODE_1 6819768 | |
#define RF_CODE_2 9463928 | |
#define wifi_ssid "your-wifi-ssid" | |
#define wifi_pass "your-wifi-pass" | |
#define app_key "your-app-key" | |
#define app_sec "your-app-secret" | |
#define DEVICE_ID_1 "sinric-device-id-1-here" | |
#define DEVICE_ID_2 "sinric-device-id-2-here" | |
Device devices[]{ | |
{DEVICE_ID_1, RF_CODE_1, RELAY_PIN_1}, | |
{DEVICE_ID_2, RF_CODE_2, RELAY_PIN_2}, | |
}; | |
// end of configuration | |
RCSwitch rc_switch; | |
bool onPowerState(const String& deviceId, bool& new_state) { | |
for (auto& device : devices) { | |
if (device.getDeviceId() == deviceId) { | |
device.turn_state(new_state, false); | |
return true; | |
} | |
} | |
return false; | |
} | |
void handleRCSwitches() { | |
if (!rc_switch.available()) return; | |
unsigned long rf_code = rc_switch.getReceivedValue(); | |
rc_switch.resetAvailable(); | |
for (auto& device : devices) { | |
if (device.getRFCode() == rf_code) { | |
device.flip_state(true); | |
return; | |
} | |
} | |
} | |
void setupRCSwitch() { | |
rc_switch.enableReceive(RF_RECEIVER); | |
} | |
void setupWiFi() { | |
Serial.println("Connecting WiFi..."); | |
WiFi.begin(wifi_ssid, wifi_pass); | |
while (WiFi.status() != WL_CONNECTED) { | |
handleRCSwitches(); | |
}; | |
Serial.println("connected"); | |
} | |
void setupSinricPro() { | |
for (auto& device : devices) { | |
SinricProSwitch& sinricProSwitch = SinricPro[device.getDeviceId()]; | |
sinricProSwitch.onPowerState(onPowerState); | |
} | |
SinricPro.begin(app_key, app_sec); | |
} | |
void setup() { | |
Serial.begin(115200); | |
setupRCSwitch(); | |
setupWiFi(); | |
setupSinricPro(); | |
} | |
void loop() { | |
SinricPro.handle(); | |
handleRCSwitches(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment