Advertisement
rhandycan1

HOME AUTOMATION

Feb 18th, 2024
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.81 KB | None | 0 0
  1. /**********************************************************************************
  2. Modified by: Randy N. CaƱada
  3.  *  TITLE: ESP RainMaker  + IR + Manual Button control 2 Relays using ESP32 (Real time feedback + no WiFi control)
  4.  *  Download the libraries
  5.  *  AceButton Library (1.9.2): https://github.com/bxparks/AceButton
  6.  *  IRremote Library (3.6.1): https://github.com/Arduino-IRremote/Arduino-IRremote
  7.  **********************************************************************************/
  8.  
  9. #include "RMaker.h"
  10. #include "WiFi.h"
  11. #include "WiFiProv.h"
  12. #include <IRremote.h>
  13. #include <AceButton.h>
  14. using namespace ace_button;
  15.  
  16. const char *service_name = "PROV_1234RB8";
  17. const char *pop = "12345RB8";
  18.  
  19. // define the Chip Id
  20. uint32_t espChipId = 0;
  21.  
  22. // define the Node Name
  23. char nodeName[] = "ESP32_Relay_2B";
  24.  
  25. // define the Device Names
  26. char deviceName_1[] = "Fan";
  27. char deviceName_2[] = "Switch2";
  28.  
  29. //Update the HEX code of IR Remote buttons 0x<HEX CODE>
  30. #define IR_Button_1   0xF30CFF00  //fan
  31. #define IR_Button_2   0xE718FF00  //switch 2
  32. #define IR_All_on     0xB847FF00  //all on
  33. #define IR_All_Off    0xBA45FF00  // all off
  34.  
  35. // define the GPIO connected with Relays and switches
  36. static uint8_t RelayPin1 = 13;  //D13
  37. static uint8_t  LEDrelay1= 33;   //pilot light relay 1        
  38. static uint8_t RelayPin2 = 12;  //D12
  39. static uint8_t  LEDrelay2= 32;   //pilot light relay2
  40.  
  41. static uint8_t SwitchPin1 = 23;  //D23
  42. static uint8_t SwitchPin2 = 22;  //D12
  43.  
  44.  
  45. static uint8_t wifiLed      = 2;  // D2
  46. static uint8_t gpio_reset   = 0;  // Press BOOT for reset WiFi
  47. static uint8_t IRpin        =35;  // D35 (IR receiver pin)
  48.  
  49.  
  50. /* Variable for reading pin status*/
  51. bool toggleState_1 = LOW; //Define integer to remember the toggle state for relay 1
  52. bool toggleState_2 = LOW; //Define integer to remember the toggle state for relay 2
  53. bool isLEDon       = HIGH;
  54.  
  55. IRrecv IR(IRpin);
  56.  
  57.  
  58. ButtonConfig config1;
  59. AceButton button1(&config1);
  60. ButtonConfig config2;
  61. AceButton button2(&config2);
  62.  
  63.  
  64. void handleEvent1(AceButton*, uint8_t, uint8_t);
  65. void handleEvent2(AceButton*, uint8_t, uint8_t);
  66.  
  67.  
  68. //The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
  69. static Switch my_switch1(deviceName_1, &RelayPin1);
  70. static Switch my_switch2(deviceName_2, &RelayPin2);
  71.  
  72. void sysProvEvent(arduino_event_t *sys_event)
  73. {
  74.     switch (sys_event->event_id) {      
  75.         case ARDUINO_EVENT_PROV_START:
  76. #if CONFIG_IDF_TARGET_ESP32
  77.         Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
  78.         printQR(service_name, pop, "ble");
  79. #else
  80.         Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
  81.         printQR(service_name, pop, "softap");
  82. #endif        
  83.         break;
  84.         case ARDUINO_EVENT_WIFI_STA_CONNECTED:
  85.         Serial.printf("\nConnected to Wi-Fi!\n");
  86.         digitalWrite(wifiLed, true);
  87.         break;
  88.     }
  89. }
  90.  
  91. //------This function is the one to control the rainmaker app and google/alexa voice control--------------
  92. void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx)
  93. {
  94.     const char *device_name = device->getDeviceName();
  95.     const char *param_name = param->getParamName();
  96.  
  97.     if(strcmp(device_name, deviceName_1) == 0) {
  98.      
  99.       Serial.printf("Lightbulb = %s\n", val.val.b? "true" : "false");
  100.      
  101.       if(strcmp(param_name, "Power") == 0) {
  102.           Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
  103.         toggleState_1 = val.val.b;
  104.         (toggleState_1 == false) ? digitalWrite(RelayPin1, HIGH) : digitalWrite(RelayPin1, LOW);
  105.         isLEDon = val.val.b;
  106.         (isLEDon== true)         ? digitalWrite(LEDrelay1,HIGH)  : digitalWrite(LEDrelay1,LOW);
  107.         param->updateAndReport(val);
  108.       }
  109.     }
  110.     if(strcmp(device_name, deviceName_2) == 0) {
  111.      
  112.       Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");
  113.  
  114.       if(strcmp(param_name, "Power") == 0) {
  115.         Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
  116.         toggleState_2 = val.val.b;
  117.         (toggleState_2 == false) ? digitalWrite(RelayPin2, HIGH) : digitalWrite(RelayPin2, LOW);
  118.         isLEDon = val.val.b;
  119.         (isLEDon== true)         ? digitalWrite(LEDrelay2,HIGH)  : digitalWrite(LEDrelay2,LOW);
  120.         param->updateAndReport(val);
  121.       }
  122.    }
  123.  }
  124.  
  125. void ir_remote(){
  126.  while (IR.decode()) {
  127.   Serial.println(IR.decodedIRData.decodedRawData,HEX);
  128.  
  129.       switch(IR.decodedIRData.decodedRawData){
  130.           case IR_Button_1:  
  131.             digitalWrite(RelayPin1,toggleState_1 );
  132.             digitalWrite(LEDrelay1,!toggleState_1);
  133.             toggleState_1 = !toggleState_1;
  134.             my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1);
  135.             break;
  136.           case IR_Button_2:  
  137.             digitalWrite(RelayPin2, toggleState_2);
  138.             digitalWrite(LEDrelay2,!toggleState_2);
  139.             toggleState_2 = !toggleState_2;
  140.             delay(100);            
  141.             break;
  142.           case IR_All_on:  
  143.             digitalWrite(RelayPin1, toggleState_1);
  144.             digitalWrite(LEDrelay1,!toggleState_1);
  145.             toggleState_1 = !toggleState_1;
  146.             my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1);
  147.  
  148.             digitalWrite(RelayPin2, toggleState_2);
  149.             digitalWrite(LEDrelay2,!toggleState_2);
  150.             toggleState_2 = !toggleState_2;
  151.             my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_2);
  152.             delay(100);            
  153.             break;
  154.           case IR_All_Off:
  155.             all_SwitchOff();
  156.             digitalWrite(LEDrelay1,LOW);
  157.             digitalWrite(LEDrelay2,LOW);
  158.             break;
  159.           default : break;        
  160.         }  
  161.       delay(100);  
  162.       IR.resume();
  163.   }
  164. }
  165. void all_SwitchOff(){
  166.   toggleState_1 = 0; digitalWrite(RelayPin1, HIGH); my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1); delay(100);
  167.   toggleState_2 = 0; digitalWrite(RelayPin2, HIGH); my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_2); delay(100);
  168.  
  169. }
  170.  
  171. void all_SwitchOn(){
  172.   toggleState_1 = 1; digitalWrite(RelayPin1, LOW); my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1); delay(100);
  173.   toggleState_2 = 1; digitalWrite(RelayPin2, LOW); my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_2); delay(100);
  174.  
  175. }
  176.  
  177. void setup()
  178. {  
  179.     Serial.begin(115200);
  180.  
  181.    
  182.     // Set the Relays GPIOs as output mode
  183.     pinMode(RelayPin1, OUTPUT);
  184.     pinMode(RelayPin2, OUTPUT);
  185.     pinMode(LEDrelay1,OUTPUT);
  186.     pinMode(LEDrelay2,OUTPUT);
  187.     pinMode(wifiLed, OUTPUT);
  188.    
  189.     // Configure the input GPIOs
  190.     pinMode(SwitchPin1, INPUT_PULLUP);
  191.     pinMode(SwitchPin2, INPUT_PULLUP);
  192.     pinMode(gpio_reset, INPUT);
  193.    
  194.     // Write to the GPIOs the default state on booting
  195.     digitalWrite(RelayPin1, !toggleState_1);
  196.     digitalWrite(RelayPin2, !toggleState_2);
  197.  
  198.     IR.enableIRIn(); // Enabling IR sensor
  199.  
  200.     config1.setEventHandler(button1Handler);
  201.     config2.setEventHandler(button2Handler);
  202.    
  203.     button1.init(SwitchPin1);
  204.     button2.init(SwitchPin2);
  205.    
  206.  
  207.     Node my_node;    
  208.     my_node = RMaker.initNode(nodeName);
  209.  
  210.     //Standard switch device
  211.     my_switch1.addCb(write_callback);
  212.     my_switch2.addCb(write_callback);
  213.    
  214.  
  215.     //Add switch device to the node  
  216.     my_node.addDevice(my_switch1);
  217.     my_node.addDevice(my_switch2);
  218.    
  219.  
  220.     //This is optional
  221.     RMaker.enableOTA(OTA_USING_PARAMS);
  222.     //If you want to enable scheduling, set time zone for your region using setTimeZone().
  223.     //The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html
  224.     // RMaker.setTimeZone("Asia/Shanghai");
  225.     // Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone
  226.     RMaker.enableTZService();
  227.     RMaker.enableSchedule();
  228.  
  229.     //Service Name
  230.     for(int i=0; i<17; i=i+8) {
  231.       espChipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
  232.     }
  233.  
  234.     Serial.printf("\nChip ID:  %d Service Name: %s\n", espChipId, service_name);
  235.  
  236.     Serial.printf("\nStarting ESP-RainMaker\n");
  237.     RMaker.start();
  238.  
  239.     WiFi.onEvent(sysProvEvent);
  240. #if CONFIG_IDF_TARGET_ESP32
  241.     WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name);
  242. #else
  243.     WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
  244. #endif
  245.  
  246.     my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  247.     my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  248. }
  249.  
  250. void loop()
  251. {
  252.     // Read GPIO0 (external button to reset device
  253.     if(digitalRead(gpio_reset) == LOW) { //Push button pressed
  254.         Serial.printf("Reset Button Pressed!\n");
  255.         // Key debounce handling
  256.         delay(100);
  257.         int startTime = millis();
  258.         while(digitalRead(gpio_reset) == LOW) delay(50);
  259.         int endTime = millis();
  260.  
  261.         if ((endTime - startTime) > 10000) {
  262.           // If key pressed for more than 10secs, reset all
  263.           Serial.printf("Reset to factory.\n");
  264.           RMakerFactoryReset(2);
  265.         } else if ((endTime - startTime) > 3000) {
  266.           Serial.printf("Reset Wi-Fi.\n");
  267.           // If key pressed for more than 3secs, but less than 10, reset Wi-Fi
  268.           RMakerWiFiReset(2);
  269.         }
  270.     }
  271.     delay(100);
  272.  
  273.     if (WiFi.status() != WL_CONNECTED)
  274.     {
  275.       //Serial.println("WiFi Not Connected");
  276.       digitalWrite(wifiLed, false);
  277.     }
  278.     else
  279.     {
  280.       //Serial.println("WiFi Connected");
  281.       digitalWrite(wifiLed, true);
  282.     }
  283.  
  284.     ir_remote(); //IR remote Control
  285.     button1.check();
  286.     button2.check();
  287. }
  288.  
  289. //-----This function is for the physical button------------
  290. void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  291.   Serial.println("EVENT1");
  292.   switch (eventType) {
  293.     case AceButton::kEventReleased:
  294.       digitalWrite(RelayPin1, toggleState_1);
  295.       digitalWrite(LEDrelay1,!toggleState_1);
  296.       toggleState_1 = !toggleState_1;
  297.       my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1);
  298.       break;
  299.   }
  300. }
  301. void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  302.   Serial.println("EVENT2");
  303.   switch (eventType) {
  304.     case AceButton::kEventReleased:
  305.       digitalWrite(RelayPin2, toggleState_2);
  306.       digitalWrite(LEDrelay2,!toggleState_2);
  307.       toggleState_2 = !toggleState_2;
  308.       my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_2);
  309.       break;
  310.   }
  311. }
  312.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement