Control de motor con Alexa

Hola, quiero controlar el encendido de un motor durante 10s y después se apague desde un comando de Alexa, como tal el motor si lo enciende el tema es que no lo detiene al transcurrir los 10s.
He usado delay() pero esto hace que el esp8266 se bloquee y se reinicie, la otra opción fue usar millis(), el tema es que a la hora de mandar el comando este se enciende en el serial.print pero no activa el motor fisico como tal (espero me haya dado a entender), no se si haya una opción para poder apagar el motor a los 10s.
(espero no incumplir ninguna norma del grupo). Muchas gracias por sus respuestas.

Este es el código que estoy usando:

#include <Arduino.h>
#ifdef ESP32
    #include <WiFi.h>
#else
    #include <ESP8266WiFi.h>
#endif
#include "fauxmoESP.h"
#define WIFI_SSID "xxxxxxxxxx"
#define WIFI_PASS "xxxxxxxxx"

//const int motorPin = D1;  // Pin del motor

fauxmoESP fauxmo;

#define motorPin D6
#define motorPin2 D5
#define DEVICE_NAME "Motor1"
#define DEVICE_NAME2 "Motor2"
unsigned long tiempoInicio = 0;
const unsigned long duracionEncendido = 10000;

void wifiSetup() {

WiFi.mode(WIFI_STA);



  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Conectando a WiFi...");
  }
  Serial.printf("Conectado a %s", WIFI_SSID);

}

void setup() {
  Serial.begin(115200);
  pinMode(motorPin, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  digitalWrite(motorPin, LOW);
  digitalWrite(motorPin2, LOW);


  wifiSetup();
  fauxmo.createServer(true); // not needed, this is the default value
  fauxmo.setPort(80); // This is required for gen3 devices
  fauxmo.enable(true);
  fauxmo.addDevice(DEVICE_NAME);
  fauxmo.addDevice(DEVICE_NAME2);
  

fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
  Serial.printf("Dispositivo #%d (%s) - Estado: %s\n", device_id, device_name, state ? "ENCENDIDO" : "APAGADO");

  if (strcmp(device_name, DEVICE_NAME) == 0) {
    
    unsigned long tiempoActual= millis();
    if (tiempoActual - tiempoInicio > = duracionEncendido) {
        
        digitalWrite(motorpin, LOW);
    }else{
        digitalWrite(motorpin, HIGH);
}
if (strcmp(device_name, DEVICE_NAME2) == 0) {
    
    unsigned long tiempoActual= millis();
    if (tiempoActual - tiempoInicio > = duracionEncendido) {
        
        digitalWrite(motorpin2, LOW);
    }else{
        digitalWrite(motorpin2, HIGH);
}

});

}

void loop() {
  fauxmo.handle();
  
}

Hi,
Seria bueno que hagas un dibujo de como tines alanbrado el motor y como lo estas alimentando.

1 Like

Yo usaría, dentro del handle, un flag y en el loop preguntaría si hay que activar el motor.
En ese caso, activarlo hacer la pausa mills y apagarlo a los 10 segundos. Pero esto todo en el loop
Saludos.

1 Like

Moderador:
Todo tema que no es microcontroladores de Arduino.cc va en microcontroladores.
Movido a esa sección.

1 Like

Hay que hacerlo como dice DanX3. La función lambda solo se llama cuando Alexa cambia el estado, ahí no puedes mirar si ha temporizado. Modifico el código. Añado dos funciones para que bajen el pin cuando termina la temporización. No lo he probado pero compila bien.

Saludos.

#include <Arduino.h>
#ifdef ESP32
    #include <WiFi.h>
#else
    #include <ESP8266WiFi.h>
#endif
#include "fauxmoESP.h"
#define WIFI_SSID "xxxxxxxxxx"
#define WIFI_PASS "xxxxxxxxx"

//const int motorPin = D1;  // Pin del motor

fauxmoESP fauxmo;

#define motorPin D6
#define motorPin2 D5
#define DEVICE_NAME "Motor1"
#define DEVICE_NAME2 "Motor2"
#define duracionEncendido  10000



void wifiSetup() {

  WiFi.mode(WIFI_STA);

    WiFi.begin(WIFI_SSID, WIFI_PASS);
    while (WiFi.status() != WL_CONNECTED) {
      delay(1000);
      Serial.println("Conectando a WiFi...");
    }
    Serial.printf("Conectado a %s", WIFI_SSID);

}

// Lleva el pinmotor a LOW pasado despues de duracionEncendido
void handleTimer1(){
  static uint32_t t1 = 0;

  if(!digitalRead(motorPin)) 
    return;

  if(t1==0){
    t1 = millis();
    return;
    }

  else{
    if(millis()-t1 >= duracionEncendido){
      digitalWrite(motorPin2,LOW);
      t1 = 0;
    }
  }
}

// Lleva el pinmotor2 a LOW pasado despues de duracionEncendido
void handleTimer2(){
  static uint32_t t1 = 0;

  if(!digitalRead(motorPin2)) 
    return;

  if(t1==0){
    t1 = millis();
    return;
    }

  else{
    if(millis()-t1 >= duracionEncendido){
      digitalWrite(motorPin2,LOW);
      t1 = 0;
    }
  }
}


void setup() {
  Serial.begin(115200);
  pinMode(motorPin, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  digitalWrite(motorPin, LOW);
  digitalWrite(motorPin2, LOW);


  wifiSetup();
  fauxmo.createServer(true); // not needed, this is the default value
  fauxmo.setPort(80); // This is required for gen3 devices
  fauxmo.enable(true);
  fauxmo.addDevice(DEVICE_NAME);
  fauxmo.addDevice(DEVICE_NAME2);
  

fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
  Serial.printf("Dispositivo #%d (%s) - Estado: %s\n", device_id, device_name, state ? "ENCENDIDO" : "APAGADO");

  if (strcmp(device_name, DEVICE_NAME) == 0) {
    if(state){
      digitalWrite(motorPin, HIGH);
      handleTimer1();
    }
    else{
      digitalWrite(motorPin, LOW);
    }  

  }

 if (strcmp(device_name, DEVICE_NAME2) == 0) {
    if(state){
      digitalWrite(motorPin2, HIGH);
      handleTimer2();
    }
    else{
      digitalWrite(motorPin2, LOW);
    }  

  }
});

}

void loop() {
  fauxmo.handle();
  handleTimer1();
  handleTimer2();
  
}
1 Like

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