So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Gokhan E
#87750 Hi i have a code like this. I try to control step motor with HA. I am sending step values via mqtt. So far no problem.

But if i dont use "disable watchdog" then i get crash around step no 100 and if i use watchdog i get error around step no 1300.



ets Jan 8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x4010f000, len 3456, room 16
tail 0
chksum 0x84
csum 0x84
va5432625
~ld

Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <AccelStepper.h>

#define Motor1_Dir 5
#define Motor1_Step 4

const char* ssid = "xxx";
const char* password = "xxx";
const char* mqtt_server = "xxx";
const char* mqttUser = "xxx";
const char* mqttPassword = "xxx";

int value;
bool flag1;

AccelStepper stepper(1, Motor1_Step, Motor1_Dir);

WiFiClient espClient;
PubSubClient client(espClient);


void setup_wifi()
{
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

value = 0;
  int tensValue = 1;
  for (int i = length - 1; i >=0; i--) // iterate backwards
  {
    value += (payload[i] - '0') * tensValue ;
    tensValue = tensValue * 10;
  }
  flag1 = true;



 
//  for (int i = 0; i < length; i++)
//  {
//   Serial.print((char)payload[i]);
 // value = (char)payload [length -1];
 //   flag1 = true;
//  }
//  Serial.println();
//  Serial.print("Mesaj Geldi");
}


void reconnect()
{
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
   
    if (client.connect("alkan"))
    {
      Serial.println("connected");
      client.subscribe("Alkan/perde");
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 3 seconds");
      delay(3000);
    }
  }
}


void setup()
{
  Serial.begin(115200);
 
  setup_wifi();
 
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  stepper.setMaxSpeed(200);
  stepper.setAcceleration(100); 
//  stepper.setSpeed(200);   
}



void loop()
{
  if (!client.connected())
  {
    reconnect();
  }
  client.loop();
  {

     if( flag1 ==  true )
        {
//        stepper.enableOutputs();
//        Serial.println("1");
//        ESP.wdtFeed();
//        yield();
        ESP.wdtDisable();
        stepper.moveTo(value);
        stepper.runToPosition();
//        yield();
//        ESP.wdtFeed();                       
//        Serial.println("2");
        Serial.println();
        Serial.println("Stop at:");
        delay(100);
        Serial.print(value);
        Serial.println();
        flag1 = false;
//        stepper.disableOutputs();
//        Serial.println("3");
        ESP.wdtEnable(1000);
        }
  }
}