Close relay1 with Alexa and water flow sensor

hey guys, I've one issue.

I've this code bellow and it works perfectly:

#include <ESP8266WiFi.h>

const char *ssid = "xxxxxx";
const char *password = "xxxxxx";

byte valve01On[] = { 0xA0, 0x01, 0x01, 0xA2 };
byte valve01Off[] = { 0xA0, 0x01, 0x00, 0xA1 };
byte valve02On[] = { 0xA0, 0x02, 0x01, 0xA3 };
byte valve02Off[] = { 0xA0, 0x02, 0x00, 0xA2 };

bool valveOpen = false;

void setup() {
  Serial.begin(115200);
  ConexaoWifi();
}

void loop() {
  valveTest();
}

void ConexaoWifi() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.println("");
    Serial.println("Conectando ao Wi-Fi");
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("Conectado ao Wi-Fi");
    Serial.print("Endereço IP: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("Já conectado ao Wi-Fi");
  }
}

void openValve(int valve) {
  if (valve == 1 && !valveOpen) {
    Serial.write(valve01On, sizeof(valve01On));
    valveOpen = true;
  } else if (valve == 2 && !valveOpen) {
    Serial.write(valve02On, sizeof(valve02On));
    valveOpen = true;
  }
}

void closeValve(int valve) {
  if (valve == 1 && valveOpen) {
    Serial.write(valve01Off, sizeof(valve01Off));
    valveOpen = false;
  } else if (valve == 2 && valveOpen) {
    Serial.write(valve02Off, sizeof(valve02Off));
    valveOpen = false;
  }
}

void valveTest() {
  openValve(1);
  delay(5000);
  closeValve(1);
  delay(5000);
  
  openValve(2);
  delay(5000);
  closeValve(2);
  delay(5000);
}

and I've this another following code:

#include <ESP8266WiFi.h>
#include <Espalexa.h>

const char *ssid = "xxxxxx";
const char *password = "xxxxxxx";

byte valve01On[] = { 0xA0, 0x01, 0x01, 0xA2 };
byte valve01Off[] = { 0xA0, 0x01, 0x00, 0xA1 };
byte valve02On[] = { 0xA0, 0x02, 0x01, 0xA3 };
byte valve02Off[] = { 0xA0, 0x02, 0x00, 0xA2 };

bool valveOpen = false;

Espalexa espalexa;

void setup() {
  Serial.begin(115200);
  ConexaoWifi();
  espalexa.addDevice("Rele1", onRele1);
  espalexa.begin();
}

void loop() {
  espalexa.loop();
  checkArduinoCommand();
}

void ConexaoWifi() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.println("");
    Serial.println("Conectando ao Wi-Fi");
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("Conectado ao Wi-Fi");
    Serial.print("Endereço IP: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("Já conectado ao Wi-Fi");
  }
}

void openValve(int valve) {
  if (valve == 1 && !valveOpen) {
    Serial.write(valve01On, sizeof(valve01On));
    valveOpen = true;
  } else if (valve == 2 && !valveOpen) {
    Serial.write(valve02On, sizeof(valve02On));
    valveOpen = true;
  }
}

void closeValve(int valve) {
  if (valve == 1 && valveOpen) {
    Serial.write(valve01Off, sizeof(valve01Off));
    valveOpen = false;
  } else if (valve == 2 && valveOpen) {
    Serial.write(valve02Off, sizeof(valve02Off));
    valveOpen = false;
  }
}

void onRele1(uint8_t brightness) {
  openValve(1);
}

void offRele1(uint8_t brightness) {
  closeValve(1);
}

void checkArduinoCommand() {
  if (Serial.available() > 0) {
    String comando = Serial.readStringUntil('\n');
    if (comando == "FecharModuloRele") {
      Serial.println("Recebido comando para fechar o módulo de relé. Executando ação.");
      closeValve(1);
      
    }
  }
}

my idea is open the relay1 with Alexa (it's working), and close it when my water flow sensor (connected to my Arduino UNO) display "FecharModuloRele" in the SerialMonitor. idk why isn't working to close it, even if I try to close it with Alexa.

someone can help me, please?

What is printed when if you print comando to the Serial Monitor ?

nothing, until my water flow sensor display the message, so the "comando" shows "FecharModuloRele". but even if I try to turn off my relay with Alexa, (should work in void offRele1(uint8_t brightness), the relay still on

Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Topic moved.

1 Like

hey all, I tried one crazy thing here and worked, I can't explain cause nether am I know what I did lol
btw, thanks for the help

What do you say to Alexa to turn the relay ON? OFF?

Use Serial.print for key variblaes, and serial monitor to see what's going on inside the code.

I just say to turn on/off the "rele1"

I don't know this library that you are using but I notice that you have one of these lines to attach your onRele1 function to a word "Rele1". Do you need to do something similar for the offRele1 command?

If you want to use the same command for both on and off, like a toggle, then you need to do it all in one function that checks the state of the relay and then changes it.

1 Like

If I was Alexa, I would be confused if you said this to me...

If I were setting the voice commands, I would do this:

"Alexa... Relay... On"
"Alexa... Relay... Off"

If your language has "on" and "off" sounding close (example: AUF and AUS in German), I would do this:

"Alexa... Relay... Cheeseburger" (meaning ON - because Alexa knows nothing)
"Alexa... Relay... Worms" (meaning OFF)

[edit]
//**************************************************
// ** ORIGINAL POST
//**************************************************
hi everyone,

In my code, I'm able just to turn on the first relay with Alexa, but I can't turn off this relay (with Alexa too). someone please can help me?

#include <ESP8266WiFi.h>
#include <Espalexa.h>

const char *ssid = "XXXXXX";
const char *password = "XXXXXX";

byte valve01On[] = { 0xA0, 0x01, 0x01, 0xA2 };
byte valve01Off[] = { 0xA0, 0x01, 0x00, 0xA1 };
byte valve02On[] = { 0xA0, 0x02, 0x01, 0xA3 };
byte valve02Off[] = { 0xA0, 0x02, 0x00, 0xA2 };

bool valveOpen = false;

Espalexa espalexa;

void setup() {
  Serial.begin(115200);
  ConexaoWifi();
  espalexa.addDevice("Rele1", onRele1);
  espalexa.begin();
}

void loop() {
  espalexa.loop(); 
}

void ConexaoWifi() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.println("");
    Serial.println("Conectando ao Wi-Fi");
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("Conectado ao Wi-Fi");
    Serial.print("Endereço IP: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("Já conectado ao Wi-Fi");
  }
}

void openValve(int valve) {
  if (valve == 1 && !valveOpen) {
    Serial.write(valve01On, sizeof(valve01On));
    valveOpen = true;
  } else if (valve == 2 && !valveOpen) {
    Serial.write(valve02On, sizeof(valve02On));
    valveOpen = true;
  }
}

void closeValve(int valve) {
  if (valve == 1 && valveOpen) {
    Serial.write(valve01Off, sizeof(valve01Off));
    valveOpen = false;
  } else if (valve == 2 && valveOpen) {
    Serial.write(valve02Off, sizeof(valve02Off));
    valveOpen = false;
  }
}

void onRele1(uint8_t brightness) {
  openValve(1);
}

void offRele1(uint8_t brightness) {
  closeValve(1);
}
1 Like

This thread has a partner:

OP, Please one thread per issue. This keeps us from having to drag the background info out of you over and over.

I have merged your cross-posts @rbarbaroto.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.