motor stops around step50

I have a code like this. I try to send step no via MQTT. I can recieve the value but when the motor start moving it stops around step 50. no matter what new value is it randomly spin around 50 again.

I use TB6560 as a driver

Here is the serial read and code

WiFi connected
IP address:
192.168.1.33
Attempting MQTT connection...connected
Message arrived [Alkan/perde] 532
Durdugu Yer: (where it stop)
50
Message arrived [Alkan/perde] 1193
Durdugu Yer:
51
Message arrived [Alkan/perde] 1675
Durdugu Yer:
53
Message arrived [Alkan/perde] 693
Durdugu Yer:
51
Message arrived [Alkan/perde] 336
Durdugu Yer:
54
Message arrived [Alkan/perde] 675
Durdugu Yer:
53

#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("] ");
  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(400);
  stepper.setAcceleration(100); 
//  stepper.setSpeed(400);   
}



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

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

Those values of about 50 are the ASCII characters. 50 is '2', 51 is '3' etc.

You are not converting the received text (for example 532) into the integer value 532. I think this code

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

always gives you the ASCII value of the last digit - which is not much use.

What you need to do is convert all of the numeric characters to a single value -- I think this might work, but I have not tested it. (I'm assuming that length is the length of the payload only)

void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  value = 0;
  int tensValue = 1;
  int i = length - 1; i >=0; i--) // iterate backwards
  {
    value += (payload[i] - '0') * tensValue ;
    tensValue = tensValue * 10;
  }
  flag1 = true;
}

Of course if payload is a cstring you can do it all in one line with

value = atoi(payload);

...R

thank you its kind of okay like that.

#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(400);
  stepper.setAcceleration(100);  
//  stepper.setSpeed(40);    
}



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

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

but somatimes i get watchdog error. I think when i send value greater than ~100

Soft WDT reset
16:15:21.525 ->
16:15:21.525 -> >>>stack>>>
16:15:21.525 ->
16:15:21.525 -> ctx: cont
16:15:21.525 -> sp: 3ffffdd0 end: 3fffffc0 offset: 01a0
16:15:21.525 -> 3fffff70: 3ffee4bd 3ffee4bc 3ffee46c 40203875
16:15:21.525 -> 3fffff80: 3ffee4bd 3ffee4bc 3ffee46c 40203aef
16:15:21.525 -> 3fffff90: 3fffdad0 00000000 3ffee46c 402012cf
16:15:21.525 -> 3fffffa0: 3fffdad0 00000000 3ffee5e8 40204820
16:15:21.525 -> 3fffffb0: feefeffe feefeffe 3ffe8500 40100fc5
16:15:21.525 -> <<<stack<<<
16:15:21.562 ->
16:15:21.562 -> ets Jan 8 2013,rst cause:2, boot mode:(3,7)
16:15:21.562 ->
16:15:21.562 -> load 0x4010f000, len 3456, room 16
16:15:21.562 -> tail 0
16:15:21.562 -> chksum 0x84
16:15:21.562 -> csum 0x84
16:15:21.562 -> va5432625
16:15:21.562 -> ~ld

i tired to disable wtd and now i get error on higher number.

i dont know its the right move to add wtd disable

        ESP.wdtDisable();
        stepper.moveTo(value);
        stepper.runToPosition();

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

I am not familiar with the ESP8266 or the WDT so I can't help.

...R