NodeMCU сбрасывается в MQTT - PullRequest
0 голосов
/ 20 апреля 2019

Я отправляю значение GPS через MQTT.после нескольких отправок MQTT падает для подключения к серверу MQTT и сбрасывает себя.

MQTT автоматически отключается, а nodemcu сбрасывается со следующим сообщением.Сбой MQTT в состоянии -2

8 января 2013 г., первая причина: 2, режим загрузки: (3,7)

нагрузка 0x4010f000, длина 1384, комната 16, хвост 8, chksum 0x2d csum 0x2dv951aeffa ~ ld

В ожидании решения.

Я использовал nodemcu lolin и для получения GPS я использовал 8m ublox m8n.

Когда используется для отправки нормальной строки через MQTTработает отлично но после отправки значений GPS через MQTT.MQTT завершается с ошибкой в ​​течение нескольких секунд.

#include <PubSubClient.h>;


const char* ssid     = "test";
const char* password = "newagain";

const char* mqttServer = "XXXXXXXXXXXX";
const int mqttPort = 2123;


WiFiClient espClient;
PubSubClient client(espClient);

char MQTT_SERIAL_PUBLISH_CH[15]  = "stat" ;
char MQTT_SERIAL_SUB_CH[10]  = "bs1" ;


// GPS 
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
TinyGPSPlus gps;  // The TinyGPS++ object
SoftwareSerial ss(4, 5); // The serial connection to the GPS device
String GPSinfo = "HAI";


 void wifistart()
{
WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }

}


void setup() {
 Serial.begin(115200);
   ss.begin(9600);
  wifistart();
  Serial.println("Connected to the WiFi network");
   reconnect1();
}

void loop() 
{
  if(WiFi.status() == WL_CONNECTED)
  {
                   if (client.connected()) 
                   {
                    client.loop();
                    GPSS();
                   }
                   else if (!client.connected()) 
                   {
                    client.disconnect();
                    Serial.println("Forcing MQTT to connect");
                    reconnect1();
                   }
  }
  else
  {
    wifistart();
  }
//   delay(1000);
  }


    char * str1 ="HI";
void GPSS()
{

//  Serial.println("INSIDE GPS");
   while (ss.available() > 0)
   {
        if (gps.encode(ss.read()))
        {
              if (gps.location.isValid())
              {

                client.loop();
                 GPSinfo = String((gps.location.lat()),6) + String(",") + String((gps.location.lng()),6) + String(",") + String(gps.speed.kmph()) + String(",") + String(gps.altitude.feet()) + String(",") + String(gps.date.value()) ;

                        strtochar();
//                       GPSinfo.toCharArray(str1, 50 );
                        Serial.println(" ");
                        Serial.print(client.publish(MQTT_SERIAL_PUBLISH_CH, str1 ));
//                        Serial.println("delay Starts");
                        delay(1000);
//                        Serial.println("delay Ends");
                        if( client.publish(MQTT_SERIAL_PUBLISH_CH, str1 ) != 1)
                        {
                          reconnect1();
                        }

//                        Serial.print("Published DATA ");Serial.println(String(str1));
                        str1 ="0";
                        GPSinfo = "0";

              }
        }
   }
}



void strtochar()
{
   String s = GPSinfo ; 

    int n = s.length(); 

    char char_array[n + 1]; 

    strcpy(char_array, s.c_str()); 
  strcpy(str1, s.c_str()); 
}




void reconnect1()
{
  client.disconnect();
  delay(1000);
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);
  if (!client.connected()) {
    Serial.println("Connecting to MQTT...");
 String clientId = "test";
      clientId += String(random(1,1100), HEX);
    if (client.connect(clientId.c_str())) 
    {

      Serial.println("connected");  

    }
    else 
    { 
            if(WiFi.status() == WL_CONNECTED)
          {
            Serial.print("failed with state ");
            Serial.print(client.state());
              Serial.print("  ");
            Serial.println(client.connected());

          }
          else
          {
            wifistart();
          }

    }


  }

  client.publish(MQTT_SERIAL_PUBLISH_CH, "Started");
  client.subscribe(MQTT_SERIAL_SUB_CH);

}


void callback(char* topic, byte* payload, unsigned int length) 
{

  Serial.print("Message arrived in topic: ");
  Serial.println(topic);

  Serial.print("Message:");
  for (int i = 0; i < length; i++) 
  {
    Serial.print((char)payload[i]);
  }

  Serial.println();
  Serial.println("-----------------------");

}

Это выходные данные, которые я получил при выполнении приведенного выше кода ..

Connecting to WiFi..
Connected to the WiFi network
Connecting to MQTT...
connected

1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
1 
0Connecting to MQTT...
failed with state -2  0
.077691,76.989806,0.43,0.00,200419  //Im not printing this but some how its here.
Connecting to MQTT...
failed with state -2  0
.077691,76.989806,0.43,0.00,200419
Connecting to MQTT...
failed with state -2  0
.077691,76.989806,0.43,0.00,200419
Connecting to MQTT...
failed with state -2  0
.077691,76.989806,0.43,0.00,200419
Connecting to MQTT...
failed with state -2  0
.077691,76.989806,0.43,0.00,200419
Connecting to MQTT...
failed with state -2  0
.077691,76.989806,0.43,0.00,200419
Connecting to MQTT...
failed with state -2  0
.077691,76.989806,0.43,0.00,200419
Connecting to MQTT...
failed with state -2  0
.077691,76.989806,0.43,0.00,200419
Connecting to MQTT...
failed with state -2  0
.077691,76.989806,0.43,0.00,200419
Connecting to MQTT...
failed with state -2  0
.077691,76.989806,0.43,0.00,200419
Connecting to MQTT...

Soft WDT reset

>>>stack>>>    

ctx: sys
sp: 3fffed40 end: 3fffffb0 offset: 01b0


3fffffa0:  3fffdad0 00000000 3ffeea7c 40205c00  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v951aeffa
~ld
Connecting to WiFi..
Connecting to WiFi..
Connected to the WiFi network
Connecting to MQTT...
connected
...