pkerspe / ESP-StepperMotor-Server

Turn your ESP32 into a complete stepper motor control server with web UI, REST API and serial control interface
MIT License
225 stars 39 forks source link

Wifi is not reconecting after it lose signal #29

Closed dibg closed 2 years ago

dibg commented 2 years ago

Wi-Fi is not reconnecting automatically if the wi-fi connection is dropped. The solution now is to cycle the power on the eps32 so it can connect again on the specified network.

pkerspe commented 2 years ago

Hi @dibg , Can you please explain how this is related to the esp stepper server? The wifi Stack is not touched by this library, so not sure what you expectations are in regards to this report?

BenSpex commented 2 years ago

Hi,

I wrote a small workaround, as I had a similar issue. But this is more related to the way how ESP32 boards handle wifi.
It is an async hanlder that checks if the Wifi is connected, and if not just reconnects. With this code the system is now running without any issues for over 2 weeks now.

Let me know if this resolves your problem @dibg

#include <ESPStepperMotorServer.h>
#include <ESPStepperMotorServer_PositionSwitch.h>
#include <ESPStepperMotorServer_StepperConfiguration.h>
#include <ESP_FlexyStepper.h>
#include <Arduino.h>
#include <AsyncTimer.h>

ESPStepperMotorServer *stepperMotorServer;
ESPStepperMotorServer_StepperConfiguration *stepperConfiguration;

// Gear Ratio 15.5
// Steps per Rotaiton = 800
// Total steps per Whell rotation = 15.5 x 800
// Wheel Diameter = 100
// Steps per mm = pi * diameter / Total steps per wheel rotation 
// (15.5 * 800) / (2 * pi * 100/2)
#define PIl 3.141592653589793238462643383279502884L 
#define GEAR_RATIO 15.5
#define WHEEL_DIAMETER 100
#define STEP_PIN 32
#define DIRECTION_PIN 33
#define LIMIT_SWITCH_PIN 23
#define MICROSTEPS_PER_STEP 1
#define STEPS_PER_REV 1600

const char *wifiName= ""; // enter the SSID of the wifi network to connect to
const char *wifiSecret = ""; // enter the password of the the existing wifi network here

#define HOME_SPEED_MM_S 5.0f
#define MAX_DISTANCE_TO_HOME_MM 20000l

// time in ms to check if wifi is still there
const long interval = 60000; 

AsyncTimer t;

//ESPStepperMotorServer stepperMotorServer;

void checkWifi(){

  Serial.println("Checking WiFi");
  if (WiFi.status()==WL_CONNECTED){
      Serial.println("Still connected all good move on");
  }
  else{
    Serial.println("Trying to connect to Wifi");
    WiFi.persistent(false);
    WiFi.disconnect();
    delay(500);
    WiFi.mode(WIFI_OFF);
    WiFi.mode(WIFI_STA);
    int WLcount = 0;
    int UpCount =0;
    WiFi.begin(wifiName, wifiSecret);
    while (WiFi.status() != WL_CONNECTED && WLcount < 200 ) 
    {
      delay( 100 );
        Serial.printf(".");
        if (UpCount >= 60) 
        {
            UpCount = 0;
              Serial.printf("\n");
        }
      ++UpCount;
      ++WLcount;
    }
    WiFi.setAutoReconnect(true);
    WiFi.setAutoConnect(true);
    WiFi.persistent(true);

    WiFi.setSleep(false);

    Serial.println(WiFi.status()==WL_CONNECTED);
  }
}

void setup()
{
  t.setup();
  delay(6500);
  Serial.begin(115200);
  WiFi.setSleep(false);
  // now create a new ESPStepperMotorServer instance (this must be done AFTER the Serial interface has been started)
  // In this example We create the server instance with only the serial command line interface enabled
  stepperMotorServer = new ESPStepperMotorServer(ESPServerRestApiEnabled);
  stepperMotorServer->setWifiCredentials(wifiName, wifiSecret);
  stepperMotorServer->setWifiMode(ESPServerWifiModeClient); //start the server as a wifi client (DHCP client of an existing wifi network)

  //create a new configuration for a stepper
  stepperConfiguration = new ESPStepperMotorServer_StepperConfiguration(STEP_PIN, DIRECTION_PIN);
  stepperConfiguration->setDisplayName("X-Axis");
  //configure the step size and microstepping setup of the drive/motor
  stepperConfiguration->setMicrostepsPerStep(1);
  stepperConfiguration->setStepsPerMM(158/2);
  stepperConfiguration->setStepsPerRev(int(STEPS_PER_REV*GEAR_RATIO));

  // now add the configuration to the server
  unsigned int stepperId = stepperMotorServer->addOrUpdateStepper(stepperConfiguration);

  pinMode(LIMIT_SWITCH_PIN, INPUT);

  //you can now also add switch and rotary encoder configurations to the server and link them to the steppers id if needed
  //here an example for a limit switch connected to the previously created stepper motor configuration (make sure the pin is not floating (use pull up or pull down resistor if needed))
  ESPStepperMotorServer_PositionSwitch *positionSwitch = new ESPStepperMotorServer_PositionSwitch(LIMIT_SWITCH_PIN, stepperId,(1<< (SWITCHTYPE_LIMITSWITCH_POS_BEGIN_BIT -1)| 1<< (SWITCHTYPE_STATE_ACTIVE_LOW_BIT-1)), "Limit Switch", 0L);
  stepperMotorServer->addOrUpdatePositionSwitch(positionSwitch);

  //start the server
  pinMode(LIMIT_SWITCH_PIN, INPUT);

  WiFi.persistent(true);

  // Start the Server
  stepperMotorServer->start();

  WiFi.setAutoReconnect(true);
  WiFi.setAutoConnect(true);
  WiFi.persistent(true);
  WiFi.setSleep(false);

  // Setup Async Function to check if Wifi is still there every *interval* ms
  t.setInterval([]() { checkWifi(); }, interval);

}

void loop()
{
  //Handle for the setInterval Function 
  t.handle();
}
pkerspe commented 2 years ago

since this is a problem that is related to the WiFi stack of the ESP32 rather than this library and since @BenSpex provided a viable workaround, I will close this issue