Go Down

Topic: [HELP]Automated curtains (Read 227 times) previous topic - next topic

D3VIL99

Apr 10, 2019, 09:00 am Last Edit: Apr 11, 2019, 07:46 am by D3VIL99
Hi folks



I am trying to make my curtains automated using ESP32 , L298 driver , 2 magnetic reed switch and a geared dc motor (will replace it later with any stepper motor , as i can't move geared dc motor with bare hands)



i got the basic idea from Instructables, will try to implement same but I want with ESP32 and Alexa support



I am using fauxmoesp library

Code: [Select]
/*
 * Rui Santos
 * Complete Project Details http://randomnerdtutorials.com
*/

#include <Arduino.h>
#include <WiFi.h>
#define RELAY_PIN_1 16 //set IN1 pin
#define RELAY_PIN_2 17 //set IN2 pin
#include "fauxmoESP.h"

#define SERIAL_BAUDRATE 115200

#define WIFI_SSID "ssid"
#define WIFI_PASS "pass"

#define LAMP_1 "lamp one"
#define LAMP_2 "lamp two"
const int sensor1 =12; // set the sensor1 pin
const int sensor2 =14; // set the sensor2 pin
int state;



fauxmoESP fauxmo;


// Wi-Fi Connection
void wifiSetup() {
  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);

  // Connect
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  // Wait
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println();

  // Connected!
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}

void setup() {
  // Init serial port and clean garbage
  Serial.begin(SERIAL_BAUDRATE);
  pinMode(sensor1, INPUT_PULLUP);
  pinMode(sensor2, INPUT_PULLUP);

  Serial.println();

  // Wi-Fi connection
  wifiSetup();

  // relay
  pinMode(RELAY_PIN_1, OUTPUT);
  digitalWrite(RELAY_PIN_1, LOW);

  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, LOW);
 
  // By default, fauxmoESP creates it's own webserver on the defined port
  // The TCP port must be 80 for gen3 devices (default is 1901)
  // This has to be done before the call to enable()
  fauxmo.createServer(true); // not needed, this is the default value
  fauxmo.setPort(80); // This is required for gen3 devices

  // You have to call enable(true) once you have a WiFi connection
  // You can enable or disable the library at any moment
  // Disabling it will prevent the devices from being discovered and switched
  fauxmo.enable(true);
  // You can use different ways to invoke alexa to modify the devices state:
  // "Alexa, turn lamp two on"

  // Add virtual devices
  fauxmo.addDevice(LAMP_1);
  fauxmo.addDevice(LAMP_2);

  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
    // Callback when a command from Alexa is received.
    // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
    // State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
    // Just remember not to delay too much here, this is a callback, exit as soon as possible.
    // If you have to do something more involved here set a flag and process it in your main loop.
       
    Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
    if ( (strcmp(device_name, LAMP_1) == 0) ) {
      // this just sets a variable that the main loop() does something about
      Serial.println("RELAY 1 switched by Alexa");
      //digitalWrite(RELAY_PIN_1, !digitalRead(RELAY_PIN_1));
      if (state) {
        digitalWrite(RELAY_PIN_1, HIGH);
      } else {
        digitalWrite(RELAY_PIN_1, LOW);
      }
    }
    if ( (strcmp(device_name, LAMP_2) == 0) ) {
      // this just sets a variable that the main loop() does something about
      Serial.println("RELAY 2 switched by Alexa");
      if (state) {
        digitalWrite(RELAY_PIN_2, HIGH);
      } else {
        digitalWrite(RELAY_PIN_2, LOW);
      }
    }
  });

}

void loop() {
  // fauxmoESP uses an async TCP server but a sync UDP server
  // Therefore, we have to manually poll for UDP packets
  fauxmo.handle();
    state = !digitalRead(sensor1);
 
    if (state == HIGH){
          digitalWrite(RELAY_PIN_1, LOW);
        }
  state = !digitalRead(sensor2);
 
    if (state == HIGH){
          digitalWrite(RELAY_PIN_2, LOW);
        }
  static unsigned long last = millis();
  if (millis() - last > 5000) {
    last = millis();
    Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
  }
   

}




Everything is working fine, but while I say "turn on lamp one" if curtains are still closing and I say "turn on lamp two" then both commands get altered

like if I say "turn on lamp one" then it won't work, I have to say "turn off lamp one" then it close curtains

Gets normal after reset/restart



PS: sorry for wrong commands but I am still debugging so I used which were present in the example



thank you :)


GaryP

You should add your code directly to your post, as it is explained mamy times. And remember to use code tags.
 :)

Cheers,
Kari
The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

tasmod

Does it actually work?

Your call back function is in setup running once only.  I think your code was derived from a Becky Stern example.


Code: [Select]

void callback(uint8_t device_id, const char * device_name, bool state) {
  Serial.print("Device "); Serial.print(device_name);
  Serial.print(" state: ");
  if (state) {
    Serial.println("ON");
    digitalWrite(RELAY_PIN, HIGH);
  } else {
    Serial.println("OFF");
    digitalWrite(RELAY_PIN, LOW);
  }
}



tasmod

This was her example to help you out.

Code: [Select]

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include "ESPAsyncWebServer.h"
#include <ESPAsyncTCP.h>
#include <Hash.h>

#define WIFI_SSID "##############"
#define WIFI_PASS "############"
#define SERIAL_BAUDRATE                 115200

fauxmoESP fauxmo;
#define RELAY_PIN 6   //13
const int  buttonPin = 4;    // the pin that the pushbutton is attached to
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

// -----------------------------------------------------------------------------
// Wifi
// -----------------------------------------------------------------------------

void wifiSetup() {

    // Set WIFI module to STA mode
    WiFi.mode(WIFI_STA);

    // Connect
    Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
    WiFi.begin(WIFI_SSID, WIFI_PASS);

    // Wait
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(100);
    }
    Serial.println();

    // Connected!
    Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}

void callback(uint8_t device_id, const char * device_name, bool state) {
  Serial.print("Device "); Serial.print(device_name);
  Serial.print(" state: ");
  if (state) {
    Serial.println("ON");
    digitalWrite(RELAY_PIN, HIGH);
  } else {
    Serial.println("OFF");
    digitalWrite(RELAY_PIN, LOW);
  }
}

void setup() {
    pinMode(RELAY_PIN, OUTPUT);
    pinMode(buttonPin, INPUT_PULLUP);
    digitalWrite(RELAY_PIN, LOW);
    // Init serial port and clean garbage
    Serial.begin(SERIAL_BAUDRATE);
    Serial.println("FauxMo demo sketch");
    Serial.println("After connection, ask Alexa/Echo to 'turn led on' or 'off'");

    // Wifi
    wifiSetup();

    // Fauxmo
    fauxmo.addDevice("the led");
    fauxmo.onMessage(callback);
}

void loop() {
  fauxmo.handle();
 
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      Serial.println("on");
      digitalWrite(RELAY_PIN, HIGH);
    }
    else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
      digitalWrite(RELAY_PIN, LOW);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
}

D3VIL99

You should add your code directly to your post, as it is explained mamy times. And remember to use code tags.
 :)

Cheers,
Kari

forgot that , fixed :)

Does it actually work?

Your call back function is in setup running once only.  I think your code was derived from a Becky Stern example.


Code: [Select]

void callback(uint8_t device_id, const char * device_name, bool state) {
  Serial.print("Device "); Serial.print(device_name);
  Serial.print(" state: ");
  if (state) {
    Serial.println("ON");
    digitalWrite(RELAY_PIN, HIGH);
  } else {
    Serial.println("OFF");
    digitalWrite(RELAY_PIN, LOW);
  }
}



Yes it works , no its not from Becky Stern example
I have used different example , see the 1st few lines

D3VIL99

I have made some changes with help of my friend

everything is fine now

Can someone please review my code ?

Code: [Select]


/*
 * Rui Santos
 * Complete Project Details http://randomnerdtutorials.com
*/

#include <Arduino.h>
#include <WiFi.h>
#define RELAY_PIN_1 16 //set IN1 pin
#define RELAY_PIN_2 17 //set IN2 pin
#include "fauxmoESP.h"

#define SERIAL_BAUDRATE 115200

#define WIFI_SSID "SSID"
#define WIFI_PASS "PASS"

#define LAMP_1 "lamp one"
#define LAMP_2 "lamp two"
const int sensor1 =12; // set the sensor1 pin
const int sensor2 =14; // set the sensor2 pin
int state;
int flag1=0,flag2=0;



fauxmoESP fauxmo;


// Wi-Fi Connection
void wifiSetup() {
  // Set WIFI module to STA mode
  WiFi.mode(WIFI_STA);

  // Connect
  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  // Wait
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println();

  // Connected!
  Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}

void setup() {
  // Init serial port and clean garbage
  Serial.begin(SERIAL_BAUDRATE);
  pinMode(sensor1, INPUT_PULLUP);
  pinMode(sensor2, INPUT_PULLUP);

  Serial.println();

  // Wi-Fi connection
  wifiSetup();

  // relay
  pinMode(RELAY_PIN_1, OUTPUT);
  digitalWrite(RELAY_PIN_1, LOW);

  pinMode(RELAY_PIN_2, OUTPUT);
  digitalWrite(RELAY_PIN_2, LOW);
  
  // By default, fauxmoESP creates it's own webserver on the defined port
  // The TCP port must be 80 for gen3 devices (default is 1901)
  // This has to be done before the call to enable()
  fauxmo.createServer(true); // not needed, this is the default value
  fauxmo.setPort(80); // This is required for gen3 devices

  // You have to call enable(true) once you have a WiFi connection
  // You can enable or disable the library at any moment
  // Disabling it will prevent the devices from being discovered and switched
  fauxmo.enable(true);
  // You can use different ways to invoke alexa to modify the devices state:
  // "Alexa, turn lamp two on"

  // Add virtual devices
  fauxmo.addDevice(LAMP_1);
  fauxmo.addDevice(LAMP_2);

  fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
    // Callback when a command from Alexa is received.
    // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
    // State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
    // Just remember not to delay too much here, this is a callback, exit as soon as possible.
    // If you have to do something more involved here set a flag and process it in your main loop.
        
    Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
    if ( (strcmp(device_name, LAMP_1) == 0) ) {
      // this just sets a variable that the main loop() does something about
      Serial.println("RELAY 1 switched by Alexa");
      //digitalWrite(RELAY_PIN_1, !digitalRead(RELAY_PIN_1));
      if (state==1&&flag2==0) {
        digitalWrite(RELAY_PIN_1, HIGH);
        flag1=1;
      } else if(state==1&&flag2==1)
      {
        digitalWrite(RELAY_PIN_2, LOW);
        flag2=0;
        digitalWrite(RELAY_PIN_1, HIGH);
        flag1=1;
        }
        else {
        digitalWrite(RELAY_PIN_1, LOW);
        flag1=0;
      }
    }
    if ( (strcmp(device_name, LAMP_2) == 0) ) {
      // this just sets a variable that the main loop() does something about
      Serial.println("RELAY 2 switched by Alexa");
      if (state==1&&flag1==0) {
        digitalWrite(RELAY_PIN_2, HIGH);
        flag2=1;
      }else if(state==1&&flag1==1)
      {
        digitalWrite(RELAY_PIN_1, LOW);
        flag1=0;
        digitalWrite(RELAY_PIN_2, HIGH);
        flag2=1;
        }
      else {
        digitalWrite(RELAY_PIN_2, LOW);
        flag2=0;
      }
    }
  });

}

void loop() {
  // fauxmoESP uses an async TCP server but a sync UDP server
  // Therefore, we have to manually poll for UDP packets
  fauxmo.handle();
    state = !digitalRead(sensor1);
  
    if (state == HIGH){
          digitalWrite(RELAY_PIN_1, LOW);
        }
  state = !digitalRead(sensor2);
  
    if (state == HIGH){
          digitalWrite(RELAY_PIN_2, LOW);
        }
  static unsigned long last = millis();
  if (millis() - last > 5000) {
    last = millis();
    Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
  }
    

}

Go Up