Created
September 29, 2023 09:16
-
-
Save kakopappa/e968bee3afd66401b1fb7020e591dd3d to your computer and use it in GitHub Desktop.
Dimmable Switch with RobotDyn AC Light Dimmer Module. https://help.sinric.pro/pages/tutorials/tutorials/dimmable-switch/robotdyn-ac-light-dimmer.html
This file contains hidden or 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
| //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 | |
| #include <Arduino.h> | |
| #if defined(ESP8266) | |
| #include <ESP8266WiFi.h> | |
| #elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040) | |
| #include <WiFi.h> | |
| #endif | |
| #include <RBDdimmer.h> //https://github.com/RobotDynOfficial/RBDDimmer | |
| #include "SinricPro.h" | |
| #include "SinricProDimSwitch.h" | |
| #define WIFI_SSID "" | |
| #define WIFI_PASS "" | |
| #define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" | |
| #define APP_SECRET "" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" | |
| #define DIMSWITCH_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx" | |
| #define BAUD_RATE 115200 // Change baudrate to your need | |
| const int ZC_PIN = 4; | |
| const int PWM_PIN = 2; | |
| const int MIN_POWER = 0; | |
| const int MAX_POWER = 80; | |
| int power = 0; | |
| dimmerLamp acd(PWM_PIN, ZC_PIN); | |
| // we use a struct to store all states and values for our dimmable switch | |
| struct { | |
| bool powerState = false; | |
| int powerLevel = 0; | |
| } device_state; | |
| void setPWM(int powerLevel) { | |
| int power = map(powerLevel, 0, 100, MIN_POWER, MAX_POWER); // Map power level from 0 to 100, to a value between 0 to 80 | |
| acd.setPower(power); | |
| delay(30); | |
| } | |
| 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; | |
| setPWM(state ? 100: 0); | |
| return true; // request handled properly | |
| } | |
| bool onPowerLevel(const String &deviceId, int &powerLevel) { | |
| device_state.powerLevel = powerLevel; | |
| setPWM(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; | |
| setPWM(levelDelta); | |
| return true; | |
| } | |
| void setupWiFi() { | |
| Serial.printf("\r\n[Wifi]: Connecting"); | |
| #if defined(ESP8266) | |
| WiFi.setSleepMode(WIFI_NONE_SLEEP); | |
| WiFi.setAutoReconnect(true); | |
| #elif defined(ESP32) | |
| WiFi.setSleep(false); | |
| WiFi.setAutoReconnect(true); | |
| #endif | |
| WiFi.begin(WIFI_SSID, WIFI_PASS); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| Serial.printf("."); | |
| delay(250); | |
| } | |
| IPAddress localIP = WiFi.localIP(); | |
| Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); | |
| } | |
| 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.printf("Connected to SinricPro\r\n"); }); | |
| SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); }); | |
| SinricPro.begin(APP_KEY, APP_SECRET); | |
| } | |
| void setupRBDdimmer(){ | |
| acd.begin(NORMAL_MODE, ON); | |
| } | |
| // main setup function | |
| void setup() { | |
| Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); | |
| setupRBDdimmer(); | |
| setupWiFi(); | |
| setupSinricPro(); | |
| } | |
| void loop() { | |
| SinricPro.handle(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment