Advertisement
OtroIntentoDe

Esp32 Rainmaker controller

May 7th, 2024 (edited)
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.39 KB | Source Code | 0 0
  1. #include "RMaker.h"
  2. #include "WiFi.h"
  3. #include "WiFiProv.h"
  4. #include <AceButton.h>
  5. using namespace ace_button;
  6. #define DEFAULT_POWER_MODE true
  7. #define DEFAULT_DIMMER_LEVEL 50
  8.  
  9. #define PWM1_Ch    0
  10. #define PWM1_Res   8
  11. #define PWM1_Freq  1000
  12. int PWM1_DutyCycle = DEFAULT_DIMMER_LEVEL;
  13.  
  14. const char *service_name = "lovglomas";
  15. const char *pop = "leiremartinez";
  16.  
  17. // define the Device Names
  18. char deviceName_1[] = "Switch1";
  19. char deviceName_2[] = "Switch2";
  20. char deviceName_3[] = "PwmControl";
  21.  
  22. // define the GPIO connected with Relays and switches
  23. static uint8_t RelayPin1 = 23;  //D23
  24. static uint8_t RelayPin2 = 22;  //D22
  25. static uint8_t PwmPin = 17;
  26.  
  27. static uint8_t SwitchPin1 = 13;  //D13
  28. static uint8_t SwitchPin2 = 12;  //D12
  29. static uint8_t PwmPote = 15;  //D12
  30.  
  31. static uint8_t wifiLed    = 2;   //D2
  32. static uint8_t gpio_reset = 0;
  33.  
  34. /* Variable for reading pin status*/
  35. bool toggleState_1 = LOW; //Define integer to remember the toggle state for relay 1
  36. bool toggleState_2 = LOW; //Define integer to remember the toggle state for relay 2
  37. bool toggleState_3 = LOW;
  38.  
  39. ButtonConfig config1;
  40. AceButton button1(&config1);
  41. ButtonConfig config2;
  42. AceButton button2(&config2);
  43.  
  44. void handleEvent1(AceButton*, uint8_t, uint8_t);
  45. void handleEvent2(AceButton*, uint8_t, uint8_t);
  46.  
  47. //The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
  48. static LightBulb my_switch1(deviceName_1, &RelayPin1);
  49. static LightBulb my_switch2(deviceName_2, &RelayPin2);
  50. static LightBulb my_pwm(deviceName_3, &PwmPin);
  51.  
  52. void sysProvEvent(arduino_event_t *sys_event)
  53. {
  54.     switch (sys_event->event_id) {      
  55.         case ARDUINO_EVENT_PROV_START:
  56. #if CONFIG_IDF_TARGET_ESP32
  57.         Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
  58.         printQR(service_name, pop, "ble");
  59. #else
  60.         Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
  61.         printQR(service_name, pop, "softap");
  62. #endif        
  63.         break;
  64.         case ARDUINO_EVENT_WIFI_STA_CONNECTED:
  65.         Serial.printf("\nConnected to Wi-Fi!\n");
  66.         digitalWrite(wifiLed, true);
  67.         break;
  68.     }
  69. }
  70.  
  71. void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx)
  72. {
  73.     const char *device_name = device->getDeviceName();
  74.     const char *param_name = param->getParamName();
  75.  
  76.     if(strcmp(device_name, deviceName_1) == 0) {
  77.       if(strcmp(param_name, "Power") == 0) {
  78.         Serial.printf("Received value = %s for %s - %s\n",
  79.                       val.val.b ? "true" : "false", device_name, param_name);
  80.         toggleState_1 = val.val.b;
  81.         (toggleState_1 == false) ? digitalWrite(RelayPin1, HIGH) : digitalWrite(RelayPin1, LOW);
  82.         param->updateAndReport(val);
  83.       }
  84.      
  85.     } else if(strcmp(device_name, deviceName_2) == 0) {
  86.       Serial.printf("Received value = %s for %s - %s\n",
  87.                       val.val.b ? "true" : "false", device_name, param_name);
  88.       if(strcmp(param_name, "Power") == 0) {
  89.         toggleState_2 = val.val.b;
  90.  
  91.         (toggleState_2 == false) ? digitalWrite(RelayPin2, HIGH) : digitalWrite(RelayPin2, LOW);
  92.         param->updateAndReport(val);
  93.       }
  94.  
  95.     } else if(strcmp(device_name, deviceName_3) == 0) {
  96.       if(strcmp(param_name, "Power") == 0) {
  97.         Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name);
  98.         toggleState_3 = val.val.b;
  99.         param->updateAndReport(val);
  100.         (toggleState_3 == false) ? ledcWrite(PWM1_Ch, 0) : ledcWrite(PWM1_Ch, PWM1_DutyCycle);
  101.       } else if (strcmp(param_name, "Level") == 0) {
  102.         param->updateAndReport(val);
  103.         PWM1_DutyCycle = map(val.val.i, 0, 100, 0, 255);
  104.         ledcWrite(PWM1_Ch, PWM1_DutyCycle);
  105.         }
  106.     }
  107. }
  108.  
  109.  
  110. void setup()
  111. {
  112.     uint32_t chipId = 0;
  113.    
  114.     Serial.begin(115200);
  115.    
  116.     // Set the Relays GPIOs as output mode
  117.     pinMode(RelayPin1, OUTPUT);
  118.     pinMode(RelayPin2, OUTPUT);
  119.     pinMode(PwmPin, OUTPUT);
  120.     pinMode(wifiLed, OUTPUT);
  121.    
  122.     // Configure the input GPIOs
  123.     pinMode(SwitchPin1, INPUT_PULLUP);
  124.     pinMode(SwitchPin2, INPUT_PULLUP);
  125.     pinMode(gpio_reset, INPUT_PULLUP);
  126.  
  127.     ledcSetup(PWM1_Ch, PWM1_Freq, PWM1_Res);
  128.     ledcAttachPin(PwmPin, PWM1_Ch);
  129.    
  130.     // Write to the GPIOs the default state on booting
  131.     digitalWrite(RelayPin1, !toggleState_1);
  132.     digitalWrite(RelayPin2, !toggleState_2);
  133.     digitalWrite(PwmPin, !toggleState_3);
  134.     digitalWrite(wifiLed, LOW);
  135.  
  136.     config1.setEventHandler(button1Handler);
  137.     config2.setEventHandler(button2Handler);
  138.    
  139.     button1.init(SwitchPin1);
  140.     button2.init(SwitchPin2);
  141.  
  142.     Node my_node;    
  143.     my_node = RMaker.initNode("Disp. de testeo");
  144.  
  145.     Param level_param("Level", "esp.param.brightness", value(DEFAULT_DIMMER_LEVEL), PROP_FLAG_READ | PROP_FLAG_WRITE);
  146.     level_param.addBounds(value(0), value(100), value(1));
  147.     level_param.addUIType(ESP_RMAKER_UI_SLIDER);
  148.     my_pwm.addParam(level_param);
  149.  
  150.     //Standard switch device
  151.     my_switch1.addCb(write_callback);
  152.     my_switch2.addCb(write_callback);
  153.     my_pwm.addCb(write_callback);
  154.  
  155.     //Add switch device to the node  
  156.     my_node.addDevice(my_switch1);
  157.     my_node.addDevice(my_switch2);
  158.     my_node.addDevice(my_pwm);
  159.  
  160.     //This is optional
  161.     RMaker.enableOTA(OTA_USING_PARAMS);
  162.     //If you want to enable scheduling, set time zone for your region using setTimeZone().
  163.     //The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html
  164.     // RMaker.setTimeZone("Asia/Shanghai");
  165.     // Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone
  166.     RMaker.enableTZService();
  167.     RMaker.enableSchedule();
  168.  
  169.     //Service Name
  170.     for(int i=0; i<17; i=i+8) {
  171.       chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
  172.     }
  173.  
  174.     Serial.printf("\nChip ID:  %d Service Name: %s\n", chipId, service_name);
  175.  
  176.     Serial.printf("\nStarting ESP-RainMaker\n");
  177.     RMaker.start();
  178.  
  179.     WiFi.onEvent(sysProvEvent);
  180. #if CONFIG_IDF_TARGET_ESP32
  181.     WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name);
  182. #else
  183.     WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
  184. #endif
  185.  
  186.     my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  187.     my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  188.     my_pwm.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  189. }
  190.  
  191. void loop()
  192. {
  193.     // Read GPIO0 (external button to reset device
  194.     if(digitalRead(gpio_reset) == LOW) { //Push button pressed
  195.         Serial.printf("Reset Button Pressed!\n");
  196.         // Key debounce handling
  197.         delay(100);
  198.         int startTime = millis();
  199.         while(digitalRead(gpio_reset) == LOW) delay(50);
  200.         int endTime = millis();
  201.  
  202.         if ((endTime - startTime) > 10000) {
  203.           // If key pressed for more than 10secs, reset all
  204.           Serial.printf("Reset to factory.\n");
  205.           RMakerFactoryReset(2);
  206.         } else if ((endTime - startTime) > 3000) {
  207.           Serial.printf("Reset Wi-Fi.\n");
  208.           // If key pressed for more than 3secs, but less than 10, reset Wi-Fi
  209.           RMakerWiFiReset(2);
  210.         }
  211.     }
  212.     delay(100);
  213.  
  214.     if (WiFi.status() != WL_CONNECTED)
  215.     {
  216.       //Serial.println("WiFi Not Connected");
  217.       digitalWrite(wifiLed, false);
  218.     }
  219.     else
  220.     {
  221.       //Serial.println("WiFi Connected");
  222.       digitalWrite(wifiLed, true);
  223.     }
  224.  
  225.     button1.check();
  226.     button2.check();
  227. }
  228.  
  229. void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  230.   Serial.println("EVENT1");
  231.   switch (eventType) {
  232.     case AceButton::kEventReleased:
  233.       digitalWrite(RelayPin1, toggleState_1);
  234.       toggleState_1 = !toggleState_1;
  235.       my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1);
  236.       break;
  237.   }
  238. }
  239. void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  240.   Serial.println("EVENT2");
  241.   switch (eventType) {
  242.     case AceButton::kEventReleased:
  243.       digitalWrite(RelayPin2, toggleState_2);
  244.       toggleState_2 = !toggleState_2;
  245.       my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_2);
  246.       break;
  247.   }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement