Need Help With Servo Motor in ESP RainMaker
Posted: 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.
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.
- #include "RMaker.h" //Biblioteca do Rainmaker
- #include "WiFi.h" //Necessário para utilizar o wifi do esp32
- #include "WiFiProv.h" //Realiza o provisionamento da internet pelo Rainmaker
- #include <EEPROM.h> //Memória de leitura
- #include <DHT.h> //Biblioteca do sensor de temperatura/umidade
- #include <SimpleTimer.h>
- #include <Servo.h>
- #define DEFAULT_Temperature 0
- #define DEFAULT_Humidity 0
- const char *service_name = "WiFi Username";
- const char *pop = "WiFi Password";
- void Send_Sensor();
- //Nomeia e inicia o Servo
- Servo servo;
- //Definindo os dispositivos
- char deviceName_1[] = "Led1";
- char deviceName_2[] = "Led2";
- char deviceName_3[] = "Led3";
- char deviceName_4[] = "Led4";
- char deviceName_5[] = "Temperatura";
- char deviceName_6[] = "Umidade";
- char deviceName_7[] = "Garagem";
- //char deviceName_7[] = "Movimento";
- //char deviceName_8[] = "Fogo";
- //char deviceName_9[] = "Garagem";
- //char deviceName_10[] = "Noturno";
- // Definindo as portas GPIO conectadas
- static uint8_t RelayPin1 = 27; //D23
- static uint8_t RelayPin2 = 26; //D22
- static uint8_t RelayPin3 = 25; //D21
- static uint8_t RelayPin4 = 33; //D19
- static uint8_t RelayPin5 = 13; //D13
- static uint8_t DHTPIN = 19; //D18
- //static uint8_t SwitchPin1 ; //D13
- //static uint8_t SwitchPin2 ; //D12
- //static uint8_t SwitchPin3 ; //D14
- //static uint8_t SwitchPin4 ; //D27
- //static uint8_t SwitchPin5 ; //D33
- //static uint8_t SwitchPin6 ; //D32
- //static uint8_t SwitchPin7 ; //D15
- //static uint8_t SwitchPin8 ; //D04
- //Definindo o botão de reset e led wifi, padrões do rainmaker
- static uint8_t wifiLed = 2; //D2
- static uint8_t gpio_reset = 0;
- // Lendo os status doas conexões
- bool toggleState_1 = true; //Define integer to remember the toggle state for relay 1
- bool toggleState_2 = true; //Define integer to remember the toggle state for relay 2
- bool toggleState_3 = true; //Define integer to remember the toggle state for relay 3
- bool toggleState_4 = true; //Define integer to remember the toggle state for relay 4
- bool toggleState_5 = true; //Define integer to remember the toggle state for relay 5
- //bool toggleState_6 = true; //Define integer to remember the toggle state for relay 6
- //bool toggleState_7 = true; //Define integer to remember the toggle state for relay 7
- //bool toggleState_8 = true; //Define integer to remember the toggle state for relay 8
- float temperatura = 0;
- float umidade = 0;
- //Switch State
- bool SwitchState_1 = true;
- bool SwitchState_2 = true;
- bool SwitchState_3 = true;
- bool SwitchState_4 = true;
- bool SwitchState_5 = true;
- //bool SwitchState_6 = true;
- //bool SwitchState_7 = true;
- //bool SwitchState_8 = true;
- DHT dht(DHTPIN, DHT11);
- SimpleTimer Timer;
- //The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
- static LightBulb my_switch1(deviceName_1, &RelayPin1);
- static LightBulb my_switch2(deviceName_2, &RelayPin2);
- static LightBulb my_switch3(deviceName_3, &RelayPin3);
- static LightBulb my_switch4(deviceName_4, &RelayPin4);
- static TemperatureSensor my_switch5(deviceName_5);
- static TemperatureSensor my_switch6(deviceName_6);
- static Switch my_switch7(deviceName_7, &RelayPin5);
- //static Switch my_switch8(deviceName_8, &RelayPin8);
- //static Switch my_switch9(deviceName_9);
- //static Switch my_switch10(deviceName_10);
- void sysProvEvent(arduino_event_t *sys_event)
- {
- switch (sys_event->event_id) {
- case ARDUINO_EVENT_PROV_START:
- #if CONFIG_IDF_TARGET_ESP32
- Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
- printQR(service_name, pop, "ble");
- #else
- Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
- printQR(service_name, pop, "softap");
- #endif
- break;
- case ARDUINO_EVENT_WIFI_STA_CONNECTED:
- Serial.printf("\nConnected to Wi-Fi!\n");
- digitalWrite(wifiLed, true);
- break;
- }
- }
- void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx)
- {
- const char *device_name = device->getDeviceName();
- const char *param_name = param->getParamName();
- if(strcmp(device_name, deviceName_1) == 0) {
- Serial.printf("Lightbulb = %s\n", val.val.b? "true" : "false");
- if(strcmp(param_name, "Power") == 0) {
- Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
- toggleState_1 = val.val.b;
- (toggleState_1 == false) ? digitalWrite(RelayPin1, LOW) : digitalWrite(RelayPin1, HIGH);
- param->updateAndReport(val);
- }
- } else if(strcmp(device_name, deviceName_2) == 0) {
- Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");
- if(strcmp(param_name, "Power") == 0) {
- Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
- toggleState_2 = val.val.b;
- (toggleState_2 == false) ? digitalWrite(RelayPin2, LOW) : digitalWrite(RelayPin2, HIGH);
- param->updateAndReport(val);
- }
- } else if(strcmp(device_name, deviceName_3) == 0) {
- Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");
- if(strcmp(param_name, "Power") == 0) {
- Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
- toggleState_3 = val.val.b;
- (toggleState_3 == false) ? digitalWrite(RelayPin3, LOW) : digitalWrite(RelayPin3, HIGH);
- param->updateAndReport(val);
- }
- } else if(strcmp(device_name, deviceName_4) == 0) {
- Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");
- if(strcmp(param_name, "Power") == 0) {
- Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
- toggleState_4 = val.val.b;
- (toggleState_4 == false) ? digitalWrite(RelayPin4, LOW) : digitalWrite(RelayPin4, HIGH);
- param->updateAndReport(val);
- }
- } else if(strcmp(device_name, deviceName_7) == 0) {
- Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");
- if(strcmp(param_name, "Power") == 1) {
- Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
- toggleState_5 = val.val.b;
- (toggleState_5 == false) ? digitalWrite(RelayPin5, LOW) : digitalWrite(RelayPin5, HIGH);
- servo.write(90);
- param->updateAndReport(val);
- } else if (strcmp(param_name, "Power") == 0) {
- servo.write(0);
- delay(15);
- }
- }
- }
- void setup()
- {
- uint32_t chipId = 0;
- Serial.begin(115200);
- dht.begin();
- Timer.setInterval(500);
- servo.attach(13);
- servo.write(0);
- // Set the Relays GPIOs as output mode
- pinMode(RelayPin1, OUTPUT);
- pinMode(RelayPin2, OUTPUT);
- pinMode(RelayPin3, OUTPUT);
- pinMode(RelayPin4, OUTPUT);
- pinMode(RelayPin5, OUTPUT);
- //pinMode(RelayPin6, OUTPUT);
- //pinMode(RelayPin7, OUTPUT);
- //pinMode(RelayPin8, OUTPUT);
- //pinMode(wifiLed, OUTPUT);
- // Configure the input GPIOs
- //pinMode(SwitchPin1, INPUT_PULLUP);
- //pinMode(SwitchPin2, INPUT_PULLUP);
- //pinMode(SwitchPin3, INPUT_PULLUP);
- //pinMode(SwitchPin4, INPUT_PULLUP);
- //pinMode(SwitchPin5, INPUT_PULLUP);
- //pinMode(SwitchPin6, INPUT_PULLUP);
- //pinMode(SwitchPin7, INPUT_PULLUP);
- //pinMode(SwitchPin8, INPUT_PULLUP);
- //pinMode(gpio_reset, INPUT);
- // Write to the GPIOs the default state on booting
- digitalWrite(RelayPin1, !toggleState_1);
- digitalWrite(RelayPin2, !toggleState_2);
- digitalWrite(RelayPin3, !toggleState_3);
- digitalWrite(RelayPin4, !toggleState_4);
- digitalWrite(RelayPin5, !toggleState_5);
- //digitalWrite(RelayPin6, !toggleState_6);
- //digitalWrite(RelayPin7, !toggleState_7);
- //digitalWrite(RelayPin8, !toggleState_8);
- //digitalWrite(wifiLed, LOW);
- Node my_node;
- my_node = RMaker.initNode("InDoor Control Unit");
- //Standard switch device
- my_switch1.addCb(write_callback);
- my_switch2.addCb(write_callback);
- my_switch3.addCb(write_callback);
- my_switch4.addCb(write_callback);
- my_switch7.addCb(write_callback);
- //my_switch8.addCb(write_callback);
- //Add switch device to the node
- my_node.addDevice(my_switch1);
- my_node.addDevice(my_switch2);
- my_node.addDevice(my_switch3);
- my_node.addDevice(my_switch4);
- my_node.addDevice(my_switch5);
- my_node.addDevice(my_switch6);
- my_node.addDevice(my_switch7);
- //my_node.addDevice(my_switch8);
- //my_node.addDevice(my_switch9);
- //my_node.addDevice(my_switch10);
- //Service Name
- for(int i=0; i<17; i=i+8) {
- chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
- }
- Serial.printf("\nChip ID: %d Service Name: %s\n", chipId, service_name);
- Serial.printf("\nStarting ESP-RainMaker\n");
- RMaker.start();
- WiFi.onEvent(sysProvEvent);
- #if CONFIG_IDF_TARGET_ESP32
- WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name);
- #else
- WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
- #endif
- my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
- my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
- my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
- my_switch4.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
- my_switch5.updateAndReportParam(ESP_RMAKER_DEF_TEMPERATURE_NAME, false);
- my_switch6.updateAndReportParam(ESP_RMAKER_DEF_TEMPERATURE_NAME, false);
- my_switch7.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
- //my_switch8.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
- //my_switch9.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
- //my_switch10.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false);
- }
- void loop()
- {
- if(Timer.isReady()){
- Serial.println("Sending Sensor´s Data");
- Send_Sensor();
- Timer.reset();
- }
- // Read GPIO0 (external button to reset device
- if(digitalRead(gpio_reset) == LOW) { //Push button pressed
- Serial.printf("Reset Button Pressed!\n");
- // Key debounce handling
- delay(100);
- int startTime = millis();
- while(digitalRead(gpio_reset) == LOW) delay(50);
- int endTime = millis();
- if ((endTime - startTime) > 10000) {
- // If key pressed for more than 10secs, reset all
- Serial.printf("Reset to factory.\n");
- RMakerFactoryReset(2);
- } else if ((endTime - startTime) > 3000) {
- Serial.printf("Reset Wi-Fi.\n");
- // If key pressed for more than 3secs, but less than 10, reset Wi-Fi
- RMakerWiFiReset(2);
- }
- }
- //delay(100);
- if (WiFi.status() != WL_CONNECTED)
- {
- //Serial.println("WiFi Not Connected");
- digitalWrite(wifiLed, false);
- }
- else
- {
- //Serial.println("WiFi Connected");
- digitalWrite(wifiLed, true);
- }
- }
- void Send_Sensor()
- {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- Serial.print("Temperatura - "); Serial.print(t); Serial.println("°C");
- Serial.print("Umidade - "); Serial.print(h); Serial.println("%");
- delay(5000);
- my_switch5.updateAndReportParam("Temperature", t);
- my_switch6.updateAndReportParam("Temperature", h);
- }