在MQTT中重置NodeMCU

时间:2019-04-20 11:42:13

标签: arduino mqtt esp8266 nodemcu arduino-esp8266

我正在通过MQTT发送GPS值。几次发送后,MQTT掉线了,无法连接到MQTT服务器,它会自行重置。

MQTT会自动断开连接,并通过以下消息将nodemcu重置。 MQTT失败,状态为-2

ets 2013年1月8日,第一个原因:2,启动模式:(3,7)

load 0x4010f000,len 1384,房间16 尾巴8 chksum 0x2d csum 0x2d v951aeffa 〜ld

期待解决方案。

我用了nodemcu lolin,为了获得GPS,我用了8m ublox m8n。

当用于通过MQTT发送普通字符串时,它工作正常,但在通过MQTT发送GPS值之后。 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

0 个答案:

没有答案