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?