[Trash]

.

Then you already know that your alexa code is wrong. Now please go and debug it.

zwieblum:
Then you already know that your alexa code is wrong. Now please go and debug it.

updated the post , please check again

From the AccelStepper documentation for setSpeed()

Speeds of more than 1000 steps per second are unreliable

...R

Robin2:
From the AccelStepper documentation for setSpeed()
…R

thank you for pointing out but i have tested it more than 5-10 min and its quite stable for me with 1200
Also it works fine with single clockwise or anti-clockwise but problem occurs only when i add delay and move at both way

i want to use it with Fauxmoesp so want to keep it simple and short code

D3VIL99:
Also it works fine with single clockwise or anti-clockwise but problem occurs only when i add delay

I had incorrectly assumed you were using runSpeedToPosition() simply because you CANNOT use delay() with runSpeed().

The function runSpeed() needs to be called very frequently (faster than the required step rate) and delay() prevents that.

You need to call the function for the period of time that you want it to run (or the number of steps). Something like this pseudo code

void loop() {
   check the time
     if it is still OK for the motor to run
         runSpeed()
    else
       wait for a second
       change the direction
       restart the clock
    }
}

Alternatively the runSpeedToPosition() function may do what you want.

...R

Robin2:
I had incorrectly assumed you were using runSpeedToPosition() simply because you CANNOT use delay() with runSpeed().

The function runSpeed() needs to be called very frequently (faster than the required step rate) and delay() prevents that.

You need to call the function for the period of time that you want it to run (or the number of steps). Something like this pseudo code

void loop() {

check the time
    if it is still OK for the motor to run
        runSpeed()
    else
      wait for a second
      change the direction
      restart the clock
    }
}





Alternatively the runSpeedToPosition() function may do what you want.

...R

I don’t want to rotate it with pre-defined time/steps
I want to keep it rotating until stopped by reed switch (mc-38)

thing is i want to integrate it with fauxmoesp library , making a automated curtain

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include <AccelStepper.h>

#define SERIAL_BAUDRATE 115200

#define dirPin 4 // direction pin
#define stepPin 14 // steps pin
#define enbPin 15 //enable pin

AccelStepper stepper(1,stepPin,dirPin); //motor interface type must be set to 1 when using a driver.

#define LAMP_1 "lamp one"
#define LAMP_2 "lamp two"

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

const int sensor1 =12; // set the sensor1 pin
const int sensor2 =14; // set the sensor2 pin


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() {
  stepper.setMaxSpeed(1200); //maximum steps per second

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

    Serial.println();
    Serial.println();
  // Wi-Fi connection
  wifiSetup();

  // relay

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enbPin, OUTPUT);
  digitalWrite(enbPin, HIGH); // put driver on sleep mode


  
  // 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);

        // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
        // Otherwise comparing the device_name is safer.

        if (strcmp(device_name, LAMP_1)==0) {
    Serial.println("RELAY 1 switched by Alexa Clockwise");
    digitalWrite(enbPin, LOW);
    stepper.setSpeed(1200);
    stepper.runSpeed();
        } else if (strcmp(device_name, LAMP_2)==0) {
    Serial.println("RELAY 2 switched by Alexa , Anti-clockwise");
    digitalWrite(enbPin, LOW);
    stepper.setSpeed(-1200);
    stepper.runSpeed();
        }

    });

}

void loop() {

    // fauxmoESP uses an async TCP server but a sync UDP server
    // Therefore, we have to manually poll for UDP packets
    fauxmo.handle();

    // This is a sample code to output free heap every 5 seconds
    // This is a cheap way to detect memory leaks
    static unsigned long last = millis();
    if (millis() - last > 5000) {
        last = millis();
        Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
    }

    // If your device state is changed by any other means (MQTT, physical button,...)
    // you can instruct the library to report the new state to Alexa on next request:
    // fauxmo.setState(ID_YELLOW, true, 255);

}

Line - 100 to 108

D3VIL99:
I don't want to rotate it with pre-defined time/steps
I want to keep it rotating until stopped by reed switch (mc-38)

The replace the time check in my pseudo code with a check for the reed switch

...R

got it working with pre-defined time after replacing delay with millis (it will hardly take 30 sec but i wan to stop it with reed switch)
but now issue is reed switch part is not working
where i am doing it wrong ?

D3VIL99:
[but now issue is reed switch part is not working

The word "reed" does not appear in your program so I have no idea which part of the program you are referring to.

...R

Robin2:
The word "reed" does not appear in your program so I have no idea which part of the program you are referring to.

...R

Sorry i have named it to sensor

const int sensor1 =12; // set the sensor1 pin
const int sensor2 =13; // set the sensor2 pin

rest is in loop

i have checked reed switches with led / other sketch and its working fine

Using this Reed switch/Door sensor - 10Sets-Magnetic-Recessed-Security-Normally

This boggles the mind

  state1 != digitalRead(sensor2);

Wouldn’t it be a lot more sensible to say

  sensor2state != digitalRead(sensor2);

In any case, all that reading either sensor seems to do is set enbPin HIGH. And there does not seem to be any code to set it back to LOW.

…R

Robin2:
This boggles the mind

  state1 != digitalRead(sensor2);

Wouldn't it be a lot more sensible to say

  sensor2state != digitalRead(sensor2);

In any case, all that reading either sensor seems to do is set enbPin HIGH. And there does not seem to be any code to set it back to LOW.

...R

Okay sir , will change that

I am running motor for selected time then reed switch will trigger enbPin HIGH which will turn of motor driver.
The issue is Alexa part is working fine , motor is rotating but reed switch is not turning on enbPin HIGH.

i have set enbPin to LOW also just to check if reed is working or not but its not working, or work just 1 time then i have to press reset button ! after removing fauxmo code reed is working fine

not getting where i am doing it wrong :confused:

If what you call the "Alexa part" is this line of code

   fauxmo.handle();

then how do you expect us to know what it does?

My wild wild wild guess is that the sensor is being read and its effect is being immediately over-written.

Why don't you put some Serial.print() statements in your program so you can know if the sensor is being detected.

...R

Robin2:
If what you call the "Alexa part" is this line of code

   fauxmo.handle();

then how do you expect us to know what it does?

My wild wild wild guess is that the sensor is being read and its effect is being immediately over-written.

Why don't you put some Serial.print() statements in your program so you can know if the sensor is being detected.

...R

That's for alexa(Fauxmoesp library) sir
Basic example for Fauxmo -

fauxmoESP_Basic

i don't think so , i have made one previously using DC motor , L298 motor driver and both sensor was working perfectly with immediate action

Will give a try using Serial.print() statements

Thanks. I now have a slightly better understanding of how it is intended to work.

You need to include Serial.print() statements into your code so you can see what is happening - for example when does an Alexa message arrive? What happens to the variables when it arrives? Does the sensor actually get triggered? etc etc

Another thought … How long does the line fauxmo.handle(); take to complete. If it is slow it may be a cause for missing a brief switch closure.

Or is the complete problem here

 while((millis() - Prev_Millis) < 50000) // 50 sec
  {
     stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
     ESP.wdtFeed(); // Reset the watch dog timer in case of long rotations.
  }

Nothing is capable of being detected during that WHILE loop. Change it to IF.

…R

Robin2:
Thanks. I now have a slightly better understanding of how it is intended to work.

You need to include Serial.print() statements into your code so you can see what is happening - for example when does an Alexa message arrive? What happens to the variables when it arrives? Does the sensor actually get triggered? etc etc

Another thought … How long does the line fauxmo.handle(); take to complete. If it is slow it may be a cause for missing a brief switch closure.

Or is the complete problem here

 while((millis() - Prev_Millis) < 50000) // 50 sec

{
    stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
    ESP.wdtFeed(); // Reset the watch dog timer in case of long rotations.
  }



Nothing is capable of being detected during that WHILE loop. Change it to IF.


...R

Don’t know how but i made reed switch working ! but still not fixed to 100%

reed switch is working , doing its job but only after motor complete its rotation with pre-defined time

i want to stop it (by enabling enbPin high) while its rotating not after its complete its rotation

crash log after removing ESP.wdtFeed();

13:54:05.824 -> 
13:54:05.858 -> [WIFI] Connecting to AndroidAP ...................................
13:54:10.311 -> [WIFI] STATION Mode, SSID: AndroidAP, IP address: 192.168.1.115
13:54:15.306 -> [MAIN] Free heap: 51472 bytes
13:54:20.337 -> [MAIN] Free heap: 50968 bytes
13:54:25.095 -> [MAIN] Device #0 (clockwise) state: ON value: 255
13:54:25.095 -> clockwise
13:54:28.223 -> 
13:54:28.223 -> Soft WDT reset
13:54:28.223 -> 
13:54:28.223 -> >>>stack>>>
13:54:28.223 -> 
13:54:28.223 -> ctx: sys
13:54:28.223 -> sp: 3fffeb10 end: 3fffffb0 offset: 01b0
13:54:28.223 -> 3fffecc0:  3ffe860e 3ffefb94 3ffee520 40203df6  
13:54:28.223 -> 3fffecd0:  3ffef4c4 3ffee470 3ffee420 40201342  
13:54:28.223 -> 3fffece0:  3ffef4c4 3fffed10 3ffeee34 4020142a  
13:54:28.223 -> 3fffecf0:  00000000 3fffed10 3ffee478 4020298c  
13:54:28.223 -> 3fffed00:  000000ff 4023f49d 3fffedbc 3fffed60  
13:54:28.257 -> 3fffed10:  73227b5b 65636375 3a227373 6c2f227b  
13:54:28.257 -> 3fffed20:  74686769 2f312f73 74617473 6e6f2f65  
13:54:28.257 -> 3fffed30:  72743a22 7d7d6575 73227b2c 65636375  
13:54:28.257 -> 3fffed40:  3a227373 6c2f227b 74686769 2f312f73  
13:54:28.257 -> 3fffed50:  74617473 72622f65 323a2269 7d7d3535  
13:54:28.257 -> 3fffed60:  3ffe005d 0000000c 3fffedb0 40204ba6  
13:54:28.257 -> 3fffed70:  00000000 00000000 ff006570 40204c04  
13:54:28.257 -> 3fffed80:  00000000 3ffefb94 3fffed60 4023d545  
13:54:28.291 -> 3fffed90:  0000003c 3fffee0c 3ffee478 00000000  
13:54:28.291 -> 3fffeda0:  3fffee00 3fffee0c 3ffee478 40202a5c  
13:54:28.291 -> 3fffedb0:  3ffeee1c 000c000f ffffee00 3ffefc74  
13:54:28.291 -> 3fffedc0:  003c003f ff20f196 3fffee00 40204c04  
13:54:28.291 -> 3fffedd0:  3ffefb94 00000000 00000001 40204c44  
13:54:28.291 -> 3fffede0:  3ffef88e 00000182 00000182 3ffefb94  
13:54:28.291 -> 3fffedf0:  3ffee478 3ffef7de 3ffef88e 40202b1c  
13:54:28.325 -> 3fffee00:  3ffeee04 000c000f ffc6a7f0 3ffef8a4  
13:54:28.325 -> 3fffee10:  003c003f fffefb94 401001e0 00004c7e  
13:54:28.325 -> 3fffee20:  00000009 3ffef74e 3ffef80c 4020fd21  
13:54:28.325 -> 3fffee30:  005872ce 00000000 3ffef80c 00000000  
13:54:28.325 -> 3fffee40:  3ffefb94 3ffef794 00000000 40202b61  
13:54:28.325 -> 3fffee50:  3ffefb94 3ffef794 3ffee931 4020362c  
13:54:28.325 -> 3fffee60:  3ffef6d4 00000001 3ffef824 40207040  
13:54:28.325 -> 3fffee70:  00000000 00000000 3ffef6d4 3ffee94c  
13:54:28.359 -> 3fffee80:  3ffef6d4 3ffee930 3ffee931 40203670  
13:54:28.359 -> 3fffee90:  3ffef6d4 3ffee930 3ffee931 40212c3c  
13:54:28.359 -> 3fffeea0:  00000000 4bc6a7f0 bbe76c8b 000000f8  
13:54:28.359 -> 3fffeeb0:  3ffeeae8 7301a8c0 00000108 40206e5c  
13:54:28.359 -> 3fffeec0:  40210000 000000f6 3fffef30 3ffeeae8  
13:54:28.359 -> 3fffeed0:  3ffeeb90 3ffef794 3ffef7b2 40217a2d  
13:54:28.359 -> 3fffeee0:  00000014 00000000 3ffeeae8 40206c18  
13:54:28.393 -> 3fffeef0:  3fffdc80 3ffef21c 3ffeee04 3ffef29c  
13:54:28.393 -> 3fffef00:  00000008 3ffeeae8 3ffef794 4020f8a1  
13:54:28.393 -> 3fffef10:  3fffdc80 3ffef21c 3ffeee04 4020f6d7  
13:54:28.393 -> 3fffef20:  4022ea86 3ffef21c 3ffeee04 4022ea97  
13:54:28.393 -> 3fffef30:  3ffef7a4 3ffef794 00000000 3fffdcb0  
13:54:28.393 -> 3fffef40:  3ffede48 00000000 3ffeee04 402306ff  
13:54:28.393 -> 3fffef50:  40000f49 3fffdab0 3fffdab0 40000f49  
13:54:28.393 -> 3fffef60:  40000e19 40001878 00000002 00000000  
13:54:28.427 -> 3fffef70:  3fffff10 ffffffff ffffffff 4010442c  
13:54:28.427 -> 3fffef80:  40104432 00000002 00000000 0000650b  
13:54:28.427 -> 3fffef90:  4010000d 0500020b 00005c0b 020e0209  
13:54:28.427 -> 3fffefa0:  00000000 3fffef3c 00000000 3ffffed8  
13:54:28.427 -> 3fffefb0:  3fffffc0 00000000 00000000 feefeffe  
13:54:28.427 -> 3fffefc0:  feefeffe feefeffe feefeffe feefeffe  
13:54:28.427 -> 3fffefd0:  feefeffe feefeffe feefeffe feefeffe  
13:54:28.461 -> 3fffefe0:  feefeffe feefeffe feefeffe feefeffe  
13:54:28.461 -> 3fffeff0:  feefeffe feefeffe feefeffe feefeffe  
13:54:28.461 -> 3ffff000:  feefeffe feefeffe feefeffe feefeffe  
13:54:28.461 -> 3ffff010:  feefeffe feefeffe feefeffe feefeffe  
13:54:28.461 -> 3ffff020:  feefeffe feefeffe feefeffe feefeffe  
13:54:28.461 -> 3ffff030:  feefeffe feefeffe feefeffe feefeffe  
13:54:28.461 -> 3ffff040:  feefeffe feefeffe feefeffe feefeffe  
13:54:28.461 -> 3ffff050:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.140 -> 3ffff970:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.140 -> 3ffff980:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.140 -> 3ffff990:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.140 -> 3ffff9a0:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.174 -> 3ffff9b0:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.174 -> 3ffff9c0:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.174 -> 3ffff9d0:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.174 -> 3ffff9e0:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.174 -> 3ffff9f0:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.310 -> 3ffffbb0:  7fffffff 3ffe93f8 3ffe93f8 00000001  
13:54:29.310 -> 3ffffbc0:  00000001 00000040 feefeffe feefeffe  
13:54:29.310 -> 3ffffbd0:  feefeffe 0005bb0b 00000000 4000050c  
13:54:29.310 -> 3ffffbe0:  3fffc278 40101ff0 3fffc200 00000022  
13:54:29.310 -> 3ffffbf0:  3ffe9404 feefeffe feefeffe feefeffe  
13:54:29.310 -> 3ffffc00:  400043d5 00000030 00000016 ffffffff  
13:54:29.344 -> 3ffffc10:  400044ab 3fffc718 3ffffd00 08000000  
13:54:29.344 -> 3ffffc20:  60000200 08000000 00000003 00000000  
13:54:29.344 -> 3ffffc30:  0000ffff 00042035 00002035 003fe000  
13:54:29.344 -> 3ffffc40:  4023f230 0000049c 003fe000 00000030  
13:54:29.344 -> 3ffffc50:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.344 -> 3ffffc60:  feefeffe feefeffe feefeffe feefeffe  
13:54:29.344 -> 3ffffc70:  feefeffe feefeffe feefeffe 00000000  
13:54:29.344 -> 3ffffc80:  3ffffde0 00000000 3ffffde0 4020d6b2  
13:54:29.378 -> 3ffffc90:  00000005 3ffffd83 4022aafb 00000001  
13:54:29.378 -> 3ffffca0:  00000005 00000000 0000000a 00000000  
13:54:29.378 -> 3ffffcb0:  00000005 00000000 0000000a 00000000  
13:54:29.378 -> 3ffffcc0:  fffffffe 3ffeed0c 3ffffcf0 3ffeed0c  
13:54:29.378 -> 3ffffcd0:  00000000 a0000000 00000000 0000001c  
13:54:29.378 -> 3ffffce0:  00005000 3ffeed0c 00005000 00000000  
13:54:29.378 -> 3ffffcf0:  3ffffe50 00000000 3ffffe50 4020d6b2  
13:54:29.412 -> 3ffffd00:  0000a000 3ffffdf3 3ffef8ec 00000001  
13:54:29.412 -> 3ffffd10:  4022ac56 3ffec2d0 402092d0 00000005  
13:54:29.412 -> 3ffffd20:  3ffffe50 00000005 3ffffe50 4020d6b2  
13:54:29.412 -> 3ffffd30:  3ffffdb0 3ffffdee 3ffffd60 00000000  
13:54:29.412 -> 3ffffd40:  00000000 4021cfdb 3ffed798 4020d5e4  
13:54:29.412 -> 3ffffd50:  3ffffe50 3ffffdee 3ffffdb0 402093d8  
13:54:29.412 -> 3ffffd60:  00000005 00000000 00000020 40100f2a  
13:54:29.412 -> 3ffffd70:  00000005 00000000 00000020 40100f2a  
13:54:29.446 -> 3ffffd80:  3ffe8b65 40104323 3ffec5a0 40100f2a  
13:54:29.446 -> 3ffffd90:  00000005 00000000 00000020 40100f2a  
13:54:29.548 -> 3fffff00:  00000000 3fffdad0 3ffee63c 00000030  
13:54:29.548 -> 3fffff10:  3ffee498 3ffef6d4 3ffef44c 40201d97  
13:54:29.548 -> 3fffff20:  3fffdad0 3ffee478 3ffee60c 40203058  
13:54:29.548 -> 3fffff30:  00000000 4bc6a7f0 cd916872 00000000  
13:54:29.548 -> 3fffff40:  00000000 00000000 4bc6a7f0 00000000  
13:54:29.548 -> 3fffff50:  ffffdad0 3ffee410 401001e0 00004c7d  
13:54:29.548 -> 3fffff60:  00000000 00000001 3ffee478 402061f4  
13:54:29.582 -> 3fffff70:  00587175 00000000 3fffff50 3ffee63c  
13:54:29.582 -> 3fffff80:  3fffdad0 3ffee478 3ffee60c 3ffee63c  
13:54:29.582 -> 3fffff90:  00000000 00000000 00000001 40205719  
13:54:29.582 -> 3fffffa0:  3fffdad0 00000000 3ffee60c 402057ae  
13:54:29.582 -> <<<stack<<<
13:54:29.582 -> 
13:54:29.582 ->  ets Jan  8 2013,rst cause:2, boot mode:(3,6)
13:54:29.615 -> 
13:54:29.615 -> load 0x4010f000, len 1384, room 16 
13:54:29.615 -> tail 8
13:54:29.615 -> chksum 0x2d
13:54:29.615 -> csum 0x2d
13:54:29.615 -> v8b899c12
13:54:29.615 -> ~ld
13:54:29.683 -> 
13:54:29.683 -> 
13:54:29.683 -> [WIFI] Connecting to AndroidAP ................................................................
13:54:37.027 -> [WIFI] STATION Mode, SSID: AndroidAP, IP address: 192.168.1.115
13:54:42.022 -> [MAIN] Free heap: 51472 bytes
13:54:47.019 -> [MAIN] Free heap: 50968 bytes
[code]

D3VIL99:
Don't know how but i made reed switch working ! but still not fixed to 100%

reed switch is working , doing its job but only after motor complete its rotation with pre-defined time

It is very difficult to follow along with you when you don't respond to my comments. It's as if Pat is talking to Mike but Mike is having another conversation with Gerry.

...R

Robin2:
It is very difficult to follow along with you when you don't respond to my comments. It's as if Pat is talking to Mike but Mike is having another conversation with Gerry.

...R

sorry . i have replied that issue but while adding log due to char limit i had to remove it

Changing while to if did trick but after that motor is not rotating , enable is low but seems its not detecting rotation functions, reed is working as it should !

and instead of Serial.print i had used fauxmo.setState which helps me to see if reed is working not by changing device status