Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <RCSwitch.h>
- #include <BlynkSimpleEsp8266.h>
- #include <SimpleTimer.h>
- //******************************************************************
- // Defines
- //******************************************************************
- // 433Mhz Settings
- #define PACKET_LENGTH 24
- // Weihanchtsbaum weiß
- #define SWITCH_1_ON 1001110000
- #define SWITCH_1_OFF 1001110000
- // Weinachtsbaum bunt
- #define SWITCH_2_ON 1001101000
- #define SWITCH_2_OFF 1001101000
- // Girlande Tür
- #define SWITCH_3_ON 1001100100
- #define SWITCH_3_OFF 1001100100
- // Girlande Treppe
- #define SWITCH_4_ON 1001100010
- #define SWITCH_4_OFF 1001100010
- // Winterdorf / TV
- #define SWITCH_5_ON 1101101000
- #define SWITCH_5_OFF 1101101000
- //******************************************************************
- // Create Class-Objects
- //******************************************************************
- RCSwitch mySwitch = RCSwitch();
- WidgetLED led1(V10); //register to virtual pin 1
- WidgetLED led2(V11); //register to virtual pin 1
- SimpleTimer timer;
- //******************************************************************
- // Global Variables
- //******************************************************************
- const char* blynk_auth_token = "";
- const char* ssid = "";
- const char* password = "";
- bool BlynkConExist = false;
- //******************************************************************
- // Setup Function
- //******************************************************************
- void setup()
- {
- // work arround for a bug, to disable access point...
- WiFi.persistent(false);
- // Initialize RCSwitch and select Pin which is used for sending the data
- mySwitch.enableTransmit(16);
- Serial.begin(115200);
- // Initialize Blynk connection
- Blynk.begin(blynk_auth_token, ssid, password);
- Blynk.connect();
- // waint until WiFi becomes connected
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- // Set initial LED States
- led1.off();
- led2.off();
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- // check every 10 seconds if still connected
- timer.setInterval(10000L, CheckConnection);
- }
- //******************************************************************
- // Check WiFi Connection and try to establish if disconnected
- //******************************************************************
- void CheckConnection(){
- // Check if connected and the Flag Value, Set connected Flag is connected
- if (Blynk.connected() && (BlynkConExist == false) ) {
- BlynkConExist = true;
- Blynk.notify("RC433: is now online");
- }
- // Reset connected Flag if not connected
- if (!(Blynk.connected()) && (BlynkConExist == true) ) {
- BlynkConExist = false;
- }
- if(!BlynkConExist){
- Blynk.connect(40000); // 40000 ~ 10Sec timeout
- }
- }
- //******************************************************************
- // Turn once Switches ON/OFF
- //******************************************************************
- //### Action for Virtual Pin 0: Switch 1 ON
- BLYNK_WRITE(0) {
- mySwitch.send(SWITCH_1_ON, PACKET_LENGTH);
- }
- //### Action for Virtual Pin 1: Switch 1 OFF
- BLYNK_WRITE(1) {
- mySwitch.send(SWITCH_1_OFF, PACKET_LENGTH);
- }
- //### Action for Virtual Pin 2: Switch 2 ON
- BLYNK_WRITE(2) {
- mySwitch.send(SWITCH_2_ON, PACKET_LENGTH);
- }
- //### Action for Virtual Pin 3: Switch 2 OFF
- BLYNK_WRITE(3) {
- mySwitch.send(SWITCH_2_OFF, PACKET_LENGTH);
- }
- //### Action for Virtual Pin 4: Switch 3 ON
- BLYNK_WRITE(4) {
- mySwitch.send(SWITCH_3_ON, PACKET_LENGTH);
- }
- //### Action for Virtual Pin 5: Switch 3 OFF
- BLYNK_WRITE(5) {
- mySwitch.send(SWITCH_3_OFF, PACKET_LENGTH);
- }
- //### Action for Virtual Pin 6: Switch 4 ON
- BLYNK_WRITE(6) {
- mySwitch.send(SWITCH_4_ON, PACKET_LENGTH);
- }
- //### Action for Virtual Pin 7: Switch 4 OFF
- BLYNK_WRITE(7) {
- mySwitch.send(SWITCH_4_OFF, PACKET_LENGTH);
- }
- //### Action for Virtual Pin 8: Switch 5 ON
- BLYNK_WRITE(6) {
- mySwitch.send(SWITCH_5_ON, PACKET_LENGTH);
- }
- //### Action for Virtual Pin 9: Switch 5 OFF
- BLYNK_WRITE(7) {
- mySwitch.send(SWITCH_5_OFF, PACKET_LENGTH);
- }
- //******************************************************************
- // Turn all Switches ON or OFF
- //******************************************************************
- //### Action for Virtual Pin 6: All ON
- //BLYNK_WRITE(6) {
- // mySwitch.send(SWITCH_1_ON, PACKET_LENGTH);
- //delay(100);
- //mySwitch.send(SWITCH_2_ON, PACKET_LENGTH);
- //delay(100);
- //mySwitch.send(SWITCH_3_ON, PACKET_LENGTH);
- //}
- //### Action for Virtual Pin 7: All OFF
- //BLYNK_WRITE(7) {
- // mySwitch.send(SWITCH_1_OFF, PACKET_LENGTH);
- //delay(100);
- //mySwitch.send(SWITCH_2_OFF, PACKET_LENGTH);
- //delay(100);
- //mySwitch.send(SWITCH_3_OFF, PACKET_LENGTH);
- //}
- //******************************************************************
- // Timer Controlled ON/OFF Operation on Switch 1 and 3
- //******************************************************************
- //### Timer 1: Turn Switch 1 ON than OFF
- //BLYNK_WRITE(8)
- //{
- // if(param.asInt() == 1) {
- // mySwitch.send(SWITCH_1_ON, PACKET_LENGTH);
- // led1.on();
- // Blynk.notify("RC433: Timer controlled Switch 1 is now ON");
- // }
- // else {
- // mySwitch.send(SWITCH_1_OFF, PACKET_LENGTH);
- // led1.off();
- // Blynk.notify("RC433: Timer controlled Switch 1 is now OFF");
- // }
- //}
- //### Timer 2: Turn Switch 3 ON than OFF
- //BLYNK_WRITE(9)
- //{
- // if(param.asInt() == 1) {
- // mySwitch.send(SWITCH_3_ON, PACKET_LENGTH);
- // Blynk.notify("RC433: Timer controlled Switch 3 is now ON");
- // led2.on();
- // }
- // else {
- // mySwitch.send(SWITCH_3_OFF, PACKET_LENGTH);
- // Blynk.notify("RC433: Timer controlled Switch 3 is now OFF");
- // led2.off();
- // }
- //}
- //******************************************************************
- // Endless Loop (main() function)
- //******************************************************************
- void loop() {
- if(BlynkConExist){
- Blynk.run();
- }
- timer.run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement