Need Help With Servo Motor in ESP RainMaker

thiagogralves
Posts: 2
Joined: Sun May 28, 2023 9:59 pm

Need Help With Servo Motor in ESP RainMaker

Postby thiagogralves » Sun May 28, 2023 10:06 pm

Hello guys.
Please, can someone help-me make and servo motor work in the Rain Manker.
I've been trying to make it, but there's some problem with my code and I can't make it work.
I need it to work to rotate to 90º when turned on, and going back to 0º when turned off.
The led's and de DHT11 works perfectly.
Here's my code, and thanks for any help.
  1. #include "RMaker.h" //Biblioteca do Rainmaker
  2. #include "WiFi.h" //Necessário para utilizar o wifi do esp32
  3. #include "WiFiProv.h" //Realiza o provisionamento da internet pelo Rainmaker
  4. #include <EEPROM.h> //Memória de leitura
  5. #include <DHT.h> //Biblioteca do sensor de temperatura/umidade
  6. #include <SimpleTimer.h>
  7. #include <Servo.h>
  8.  
  9. #define DEFAULT_Temperature 0
  10. #define DEFAULT_Humidity 0
  11.  
  12. const char *service_name = "WiFi Username";
  13. const char *pop = "WiFi Password";
  14.  
  15. void Send_Sensor();
  16.  
  17. //Nomeia e inicia o Servo
  18. Servo servo;
  19.  
  20. //Definindo os dispositivos
  21.  
  22. char deviceName_1[] = "Led1";
  23. char deviceName_2[] = "Led2";
  24. char deviceName_3[] = "Led3";
  25. char deviceName_4[] = "Led4";
  26. char deviceName_5[] = "Temperatura";
  27. char deviceName_6[] = "Umidade";
  28. char deviceName_7[] = "Garagem";
  29. //char deviceName_7[] = "Movimento";
  30. //char deviceName_8[] = "Fogo";
  31. //char deviceName_9[] = "Garagem";
  32. //char deviceName_10[] = "Noturno";
  33.  
  34.  
  35. // Definindo as portas GPIO conectadas
  36. static uint8_t RelayPin1 = 27;  //D23
  37. static uint8_t RelayPin2 = 26;  //D22
  38. static uint8_t RelayPin3 = 25;  //D21
  39. static uint8_t RelayPin4 = 33;  //D19
  40. static uint8_t RelayPin5 = 13;  //D13
  41. static uint8_t DHTPIN  = 19;  //D18
  42.  
  43.  
  44. //static uint8_t SwitchPin1  ;  //D13
  45. //static uint8_t SwitchPin2  ;  //D12
  46. //static uint8_t SwitchPin3  ;  //D14
  47. //static uint8_t SwitchPin4  ;  //D27
  48. //static uint8_t SwitchPin5  ;  //D33
  49. //static uint8_t SwitchPin6  ;  //D32
  50. //static uint8_t SwitchPin7  ;  //D15
  51. //static uint8_t SwitchPin8  ;  //D04
  52.  
  53.  
  54. //Definindo o botão de reset e led wifi, padrões do rainmaker
  55. static uint8_t wifiLed    = 2;   //D2
  56. static uint8_t gpio_reset = 0;
  57.  
  58.  
  59. // Lendo os status doas conexões
  60. bool toggleState_1 = true; //Define integer to remember the toggle state for relay 1
  61. bool toggleState_2 = true; //Define integer to remember the toggle state for relay 2
  62. bool toggleState_3 = true; //Define integer to remember the toggle state for relay 3
  63. bool toggleState_4 = true; //Define integer to remember the toggle state for relay 4
  64. bool toggleState_5 = true; //Define integer to remember the toggle state for relay 5
  65. //bool toggleState_6 = true; //Define integer to remember the toggle state for relay 6
  66. //bool toggleState_7 = true; //Define integer to remember the toggle state for relay 7
  67. //bool toggleState_8 = true; //Define integer to remember the toggle state for relay 8
  68. float temperatura = 0;
  69. float umidade = 0;
  70.  
  71. //Switch State
  72. bool SwitchState_1 = true;
  73. bool SwitchState_2 = true;
  74. bool SwitchState_3 = true;
  75. bool SwitchState_4 = true;
  76. bool SwitchState_5 = true;
  77. //bool SwitchState_6 = true;
  78. //bool SwitchState_7 = true;
  79. //bool SwitchState_8 = true;
  80.  
  81. DHT dht(DHTPIN, DHT11);
  82. SimpleTimer Timer;
  83.  
  84. //The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
  85. static LightBulb my_switch1(deviceName_1, &RelayPin1);
  86. static LightBulb my_switch2(deviceName_2, &RelayPin2);
  87. static LightBulb my_switch3(deviceName_3, &RelayPin3);
  88. static LightBulb my_switch4(deviceName_4, &RelayPin4);
  89. static TemperatureSensor my_switch5(deviceName_5);
  90. static TemperatureSensor my_switch6(deviceName_6);
  91. static Switch  my_switch7(deviceName_7, &RelayPin5);
  92. //static Switch  my_switch8(deviceName_8, &RelayPin8);
  93. //static Switch  my_switch9(deviceName_9);
  94. //static Switch  my_switch10(deviceName_10);
  95.  
  96.  
  97. void sysProvEvent(arduino_event_t *sys_event)
  98. {
  99.     switch (sys_event->event_id) {      
  100.         case ARDUINO_EVENT_PROV_START:
  101. #if CONFIG_IDF_TARGET_ESP32
  102.         Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
  103.         printQR(service_name, pop, "ble");
  104. #else
  105.         Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
  106.         printQR(service_name, pop, "softap");
  107. #endif        
  108.         break;
  109.         case ARDUINO_EVENT_WIFI_STA_CONNECTED:
  110.         Serial.printf("\nConnected to Wi-Fi!\n");
  111.         digitalWrite(wifiLed, true);
  112.         break;
  113.     }
  114. }
  115.  
  116. void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx)
  117. {
  118.     const char *device_name = device->getDeviceName();
  119.     const char *param_name = param->getParamName();
  120.  
  121.     if(strcmp(device_name, deviceName_1) == 0) {
  122.      
  123.       Serial.printf("Lightbulb = %s\n", val.val.b? "true" : "false");
  124.      
  125.       if(strcmp(param_name, "Power") == 0) {
  126.           Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
  127.         toggleState_1 = val.val.b;
  128.         (toggleState_1 == false) ? digitalWrite(RelayPin1, LOW) : digitalWrite(RelayPin1, HIGH);
  129.         param->updateAndReport(val);
  130.       }
  131.      
  132.     } else if(strcmp(device_name, deviceName_2) == 0) {
  133.      
  134.       Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");
  135.  
  136.       if(strcmp(param_name, "Power") == 0) {
  137.         Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
  138.         toggleState_2 = val.val.b;
  139.         (toggleState_2 == false) ? digitalWrite(RelayPin2, LOW) : digitalWrite(RelayPin2, HIGH);
  140.         param->updateAndReport(val);
  141.       }
  142.  
  143.     } else if(strcmp(device_name, deviceName_3) == 0) {
  144.      
  145.       Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");
  146.  
  147.       if(strcmp(param_name, "Power") == 0) {
  148.         Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
  149.         toggleState_3 = val.val.b;
  150.         (toggleState_3 == false) ? digitalWrite(RelayPin3, LOW) : digitalWrite(RelayPin3, HIGH);
  151.         param->updateAndReport(val);
  152.       }
  153.  
  154.     } else if(strcmp(device_name, deviceName_4) == 0) {
  155.      
  156.       Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");
  157.  
  158.       if(strcmp(param_name, "Power") == 0) {
  159.         Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
  160.         toggleState_4 = val.val.b;
  161.         (toggleState_4 == false) ? digitalWrite(RelayPin4, LOW) : digitalWrite(RelayPin4, HIGH);
  162.         param->updateAndReport(val);
  163.       }
  164.     } else if(strcmp(device_name, deviceName_7) == 0) {
  165.      
  166.       Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");
  167.  
  168.       if(strcmp(param_name, "Power") == 1) {
  169.         Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
  170.         toggleState_5 = val.val.b;
  171.         (toggleState_5 == false) ? digitalWrite(RelayPin5, LOW) : digitalWrite(RelayPin5, HIGH);
  172.         servo.write(90);
  173.         param->updateAndReport(val);
  174.        
  175.       } else if (strcmp(param_name, "Power") == 0) {
  176.         servo.write(0);
  177.         delay(15);
  178.       }
  179.     }
  180. }
  181.  
  182. void setup()
  183. {
  184.     uint32_t chipId = 0;
  185.    
  186.     Serial.begin(115200);
  187.     dht.begin();
  188.     Timer.setInterval(500);
  189.  
  190.     servo.attach(13);
  191.     servo.write(0);
  192.    
  193.     // Set the Relays GPIOs as output mode
  194.     pinMode(RelayPin1, OUTPUT);
  195.     pinMode(RelayPin2, OUTPUT);
  196.     pinMode(RelayPin3, OUTPUT);
  197.     pinMode(RelayPin4, OUTPUT);
  198.     pinMode(RelayPin5, OUTPUT);
  199.     //pinMode(RelayPin6, OUTPUT);
  200.     //pinMode(RelayPin7, OUTPUT);
  201.     //pinMode(RelayPin8, OUTPUT);  
  202.     //pinMode(wifiLed, OUTPUT);
  203.    
  204.     // Configure the input GPIOs
  205.     //pinMode(SwitchPin1, INPUT_PULLUP);
  206.     //pinMode(SwitchPin2, INPUT_PULLUP);
  207.     //pinMode(SwitchPin3, INPUT_PULLUP);
  208.     //pinMode(SwitchPin4, INPUT_PULLUP);
  209.     //pinMode(SwitchPin5, INPUT_PULLUP);
  210.     //pinMode(SwitchPin6, INPUT_PULLUP);
  211.     //pinMode(SwitchPin7, INPUT_PULLUP);
  212.     //pinMode(SwitchPin8, INPUT_PULLUP);
  213.     //pinMode(gpio_reset, INPUT);
  214.    
  215.     // Write to the GPIOs the default state on booting
  216.     digitalWrite(RelayPin1, !toggleState_1);
  217.     digitalWrite(RelayPin2, !toggleState_2);
  218.     digitalWrite(RelayPin3, !toggleState_3);
  219.     digitalWrite(RelayPin4, !toggleState_4);
  220.     digitalWrite(RelayPin5, !toggleState_5);
  221.     //digitalWrite(RelayPin6, !toggleState_6);
  222.     //digitalWrite(RelayPin7, !toggleState_7);
  223.     //digitalWrite(RelayPin8, !toggleState_8);
  224.     //digitalWrite(wifiLed, LOW);
  225.  
  226.     Node my_node;    
  227.     my_node = RMaker.initNode("InDoor Control Unit");
  228.  
  229.     //Standard switch device
  230.     my_switch1.addCb(write_callback);
  231.     my_switch2.addCb(write_callback);
  232.     my_switch3.addCb(write_callback);
  233.     my_switch4.addCb(write_callback);
  234.     my_switch7.addCb(write_callback);
  235.     //my_switch8.addCb(write_callback);
  236.  
  237.     //Add switch device to the node  
  238.     my_node.addDevice(my_switch1);
  239.     my_node.addDevice(my_switch2);
  240.     my_node.addDevice(my_switch3);
  241.     my_node.addDevice(my_switch4);
  242.     my_node.addDevice(my_switch5);
  243.     my_node.addDevice(my_switch6);
  244.     my_node.addDevice(my_switch7);
  245.     //my_node.addDevice(my_switch8);
  246.     //my_node.addDevice(my_switch9);
  247.     //my_node.addDevice(my_switch10);
  248.  
  249.     //Service Name
  250.     for(int i=0; i<17; i=i+8) {
  251.       chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
  252.     }
  253.  
  254.     Serial.printf("\nChip ID:  %d Service Name: %s\n", chipId, service_name);
  255.  
  256.     Serial.printf("\nStarting ESP-RainMaker\n");
  257.     RMaker.start();
  258.  
  259.     WiFi.onEvent(sysProvEvent);
  260. #if CONFIG_IDF_TARGET_ESP32
  261.     WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name);
  262. #else
  263.     WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
  264. #endif
  265.  
  266.     my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  267.     my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  268.     my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  269.     my_switch4.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  270.     my_switch5.updateAndReportParam(ESP_RMAKER_DEF_TEMPERATURE_NAME, false);
  271.     my_switch6.updateAndReportParam(ESP_RMAKER_DEF_TEMPERATURE_NAME, false);
  272.     my_switch7.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  273.     //my_switch8.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  274.     //my_switch9.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  275.     //my_switch10.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
  276. }
  277.  
  278. void loop()
  279. {
  280.  
  281.     if(Timer.isReady()){
  282.       Serial.println("Sending Sensor´s Data");
  283.       Send_Sensor();
  284.       Timer.reset();
  285.     }
  286.    
  287.     // Read GPIO0 (external button to reset device
  288.     if(digitalRead(gpio_reset) == LOW) { //Push button pressed
  289.         Serial.printf("Reset Button Pressed!\n");
  290.         // Key debounce handling
  291.         delay(100);
  292.         int startTime = millis();
  293.         while(digitalRead(gpio_reset) == LOW) delay(50);
  294.         int endTime = millis();
  295.  
  296.         if ((endTime - startTime) > 10000) {
  297.           // If key pressed for more than 10secs, reset all
  298.           Serial.printf("Reset to factory.\n");
  299.           RMakerFactoryReset(2);
  300.         } else if ((endTime - startTime) > 3000) {
  301.           Serial.printf("Reset Wi-Fi.\n");
  302.           // If key pressed for more than 3secs, but less than 10, reset Wi-Fi
  303.           RMakerWiFiReset(2);
  304.         }
  305.     }
  306.     //delay(100);
  307.  
  308.     if (WiFi.status() != WL_CONNECTED)
  309.     {
  310.       //Serial.println("WiFi Not Connected");
  311.       digitalWrite(wifiLed, false);
  312.     }
  313.     else
  314.     {
  315.       //Serial.println("WiFi Connected");
  316.       digitalWrite(wifiLed, true);
  317.     }
  318.  
  319.    
  320. }
  321.  
  322. void Send_Sensor()
  323. {
  324.   float h = dht.readHumidity();
  325.   float t = dht.readTemperature();
  326.  
  327.   Serial.print("Temperatura - "); Serial.print(t); Serial.println("°C");
  328.   Serial.print("Umidade - "); Serial.print(h); Serial.println("%");
  329.   delay(5000);
  330.   my_switch5.updateAndReportParam("Temperature", t);
  331.   my_switch6.updateAndReportParam("Temperature", h);
  332.  
  333. }

hasan_
Posts: 20
Joined: Wed Apr 05, 2023 9:03 am

Re: Need Help With Servo Motor in ESP RainMaker

Postby hasan_ » Thu Jun 15, 2023 10:57 am

Lines 168 to 178 needs to be changed. You are confusing if(strcmp(param_name, "Power") == 1) as if the power/switch in the App is ON, which is NOT the case. Let me explain: if(strcmp(param_name, "Power") == 0) means that the 'param_name' = "Power" (i.e. Power has been pressed) of that device.

To check whether it is 1 or 0 (on or off) you should check val.val.b if it is true (1) or false (0), that's the state of your switch in the app!

(You can also check for toggleState_5 because that's where you're storing val.val.b).

So the flow of your code from line 164 should look like this (just an example to give you the idea):

Code: Select all

if(strcmp(device_name, deviceName_7) == 0) {
  if(strcmp(param_name, "Power") == 0) {
     if(val.val.b == 1) {   // Power state of deviceName_7 is ON
         servo.write(90);
         param->updateAndReport(val); }
     else if(val.val.b == 0) {   // Power state of deviceName_7 is OFF
         servo.write(0);
         param->updateAndReport(val); }
   }
}   
The strcmp syntax simply compares two string names. Hence, the if(strcmp(param_name, "Power") == 0) is actually equivalent to saying if(param_name == "Power"), that 0 means there is 0 difference in 'param_name' and "Power". Which is why the line 168 where you checked == 1 didn't make sense.

Hope that helps.

thiagogralves
Posts: 2
Joined: Sun May 28, 2023 9:59 pm

Re: Need Help With Servo Motor in ESP RainMaker

Postby thiagogralves » Sat Jun 24, 2023 6:08 pm

Thank you for your help.
I'll try to make it work with you recommendations.

Who is online

Users browsing this forum: No registered users and 18 guests