martin-ger / uMQTTBroker

MQTT Broker library for ESP8266 Arduino
MIT License
448 stars 106 forks source link

Fatal exception 29 error #18

Open Loucotolo opened 6 years ago

Loucotolo commented 6 years ago

Hello I'm using 2 esp8266, 1 as server mqtt using your "uMQTTBrokerSampleOOFull" example, and the other with the mqtt_pub.ino example,

I came across the following: when I first connect the client to the server everything works fine, but if for some reason the client restarts, the server gives me a Fatal exception 29 error (StoreProhibitedCause). I will put my code on both the server and the client and the respective log with the error. I hope someone can help me to find out where the error for this happened

//------------ Client--------------------------

include

include

void myDataCb(const char topic, uint32_t length, const char data, uint32_t Alength); void myPublishedCb(); void myDisconnectedCb(); void myConnectedCb();

define CLIENT_ID "client1"

define CLIENT_RX_MQTT "RX"

// create MQTT object MQTT myMqtt(CLIENT_ID, "192.168.4.1", 1883);

// const char ssid = "ESP8266"; const char password = "123456789";

/ WiFi init stuff / void startWiFiClient() { Serial.println("Connecting to " + (String)ssid); WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) { delay(10); Serial.print("."); } Serial.println("");

Serial.println("WiFi connected"); Serial.println("IP address: " + WiFi.localIP().toString());

Serial.println("Connecting to MQTT server");

// myMqtt.setUserPwd("ESP", "HELLO");

// setup callbacks myMqtt.onConnected(myConnectedCb); myMqtt.onDisconnected(myDisconnectedCb); myMqtt.onPublished(myPublishedCb); myMqtt.onData(myDataCb);

Serial.println("connect mqtt..."); myMqtt.connect();

while (myMqtt.isConnected() == false) { delay(10); }

/ Subscribe to anything / Serial.println("subscribe to topic..."); myMqtt.subscribe("#");

}

// void setup() { Serial.begin(115200); delay(1000);

Serial.println(); startWiFiClient();

delay(10); }

// void loop() { /* int value = analogRead(A0);

String topic = WiFi.macAddress();

char Socket_TX_buffer[255];

Socket_TX_buffer[0] = 170; Socket_TX_buffer[1] = 10; Socket_TX_buffer[2] = 1; Socket_TX_buffer[3] = 0; Socket_TX_buffer[4] = 49; Socket_TX_buffer[5] = 0; Socket_TX_buffer[6] = 1; Socket_TX_buffer[7] = 6; Socket_TX_buffer[8] = 100; Socket_TX_buffer[9] = 0; Socket_TX_buffer[10] = 0; Socket_TX_buffer[11] = 0;

// publish value to topic boolean result = myMqtt.publish(topic, Socket_TX_buffer, 11);

delay(1000); */

if (myMqtt.isConnected() == false) { // startWiFiClient(); }

}

/*

*/ void myConnectedCb() { Serial.println("connected to MQTT server"); }

void myDisconnectedCb() { Serial.println("disconnected. try to reconnect..."); delay(500); myMqtt.connect(); }

void myPublishedCb() { //Serial.println("published."); }

//void myDataCb(String& topic, String& data)

void myDataCb(const char topic, uint32_t length, const char data, uint32_t Alength) { String Atopic = topic; // convert const char in String / Serial.println(topic); // Serial.println(length); // length topic Serial.println(data); Serial.println(Alength); // length data /

if (Atopic != WiFi.macAddress()) { // filtra para nao receber o que enviou char data_str[Alength + 1]; os_memcpy(data_str, data, Alength);

for (int i = 0; i < Alength ; i++) { Serial.write(data_str[i]); }

Serial.println(); }

}

//------------Server----------------- //--------------------- // Important: Use the setting "lwip Variant: 1.4 High Bandwidth" in the "Tools" menu //----------------------------------

//https://github.com/martin-ger/uMQTTBroker

/* uMQTTBroker demo for Arduino (C++-style)

The program defines a custom broker class with callbacks, starts it, subscribes locally to anything, and publishs a topic every second. Try to connect from a remote client and publish something - the console will show this as well. */

include

include "uMQTTBroker.h"

define SERVER_TX_MQTT "SERVER_TX"// esta invertido por causa os clientes

/ Your WiFi config here / char ssid[] = "ESP8266"; // your network SSID (name) char pass[] = "123456789"; // your network password bool WiFiAP = true; // Do yo want the ESP as AP?

/ Custom broker class with overwritten callback functions / class myMQTTBroker: public uMQTTBroker { public: virtual bool onConnect(IPAddress addr, uint16_t client_count) { Serial.println(addr.toString() + " connected"); return true; }

virtual bool onAuth(String username, String password) { Serial.println("Username/Password: " + username + "/" + password); // Aqui pode ser vadidar o user e a pass return true; }

virtual void onData(String topic, const char *data, uint32_t length) { if (topic != SERVER_TX_MQTT) { // filtra para nao receber o que enviou char data_str[length + 1]; os_memcpy(data_str, data, length); // data_str[length] = '\0';

for (int i = 0; i < length ; i++) {
  Serial.write(data_str[i]);
}

} } };

myMQTTBroker myBroker;

/ WiFi init stuff / void startWiFiClient() { Serial.println("Connecting to " + (String)ssid); WiFi.begin(ssid, pass);

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

Serial.println("WiFi connected"); Serial.println("IP address: " + WiFi.localIP().toString()); }

void startWiFiAP() { WiFi.softAP(ssid, pass); Serial.println("AP started"); Serial.println("IP address: " + WiFi.softAPIP().toString()); }

void setup() { Serial.begin(115200); Serial.println(); Serial.println();

// Start WiFi if (WiFiAP) startWiFiAP(); else startWiFiClient();

// Start the broker Serial.println("Starting MQTT broker");

myBroker.init();

/ Subscribe to anything / myBroker.subscribe("#"); }

int counter = 0;

void loop() { / Publish the counter value as String / // myBroker.publish(SERVER_TX_MQTT, (String)counter++);

// wait a second delay(1000); MQTT_server_cleanupClientCons(); }

log .txt

Loucotolo commented 6 years ago

waiting solution.................

AndresPamparas commented 5 years ago

Hello, I have the same problem with two wemos D1 mini boards. One as broker and the other as mqtt client. The fisrt conection is working OK, but when the client is reconected. (resetting the client board for example) the broker reconect it as other client but the previous conection is still active (I can see client_count is incremented by one) Probably the broker, for the same reason, is not disconnecting the former client and is connecting a second client with the same ID, and thats why is crussing The same happend with the pussubclient library loaded in the wemos client. I tried the broker library with other clients reconecting without any problem. I hope it helps a little to solve the problem.