Closed simonchen007 closed 6 years ago
Need some more infos. What is the setup? Complete esp_uMQTT_broker with my user_main or just the libmqtt in your code? Do you use scripting? What is the error scenario? AP or STA? Who (re-)connects?
hello,
I'm using the libmqtt and uMQTTBrokerSample, added "MQTT_server_onAuth" in AP and AP_STA model.
thing is that , my laptop connected to the esp8266 via the esp's AP ssid, and I run a mqtt client in my laptop.
in my mqtt client:
the 8266 is go reboot. here is the code of mine:
/*
* esp_uMQTT_broker demo for Arduino
*
* The program starts a broker, subscribes 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 <ESP8266WiFi.h>
//#define MQTT_BUF_SIZE 2048
//#define QUEUE_BUFFER_SIZE 4096
#include "mqtt_server.h"
/*
* Your WiFi config here
*/
char ssid[] = "yea_create_working"; // your network SSID (name)
char pass[] = ""; // your network password
const char *ap_ssid = "ESPap";
const char *ap_password = "thereisnospoon";
unsigned int mqttPort = 1883; // the standard MQTT broker port
unsigned int max_subscriptions = 30;
unsigned int max_retained_topics = 30;
void ICACHE_FLASH_ATTR data_callback(uint32_t *client /* we can ignore this */, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh) {
char topic_str[topic_len+1];
os_memcpy(topic_str, topic, topic_len);
topic_str[topic_len] = '\0';
char data_str[lengh+1];
os_memcpy(data_str, data, lengh);
data_str[lengh] = '\0';
Serial.print("received topic '");
Serial.print(topic_str);
Serial.print("' with data '");
Serial.print(data_str);
Serial.println("'");
}
bool ICACHE_FLASH_ATTR auth_data_callback(const char* username, const char *password, struct espconn *pesp_conn){
Serial.println();
Serial.print("username is: ");
Serial.print(username);
Serial.println();
Serial.println();
if ((String)username == "admins" && (String)password == "admins" ) {
return true;
}else{
return false;
}
// if (username == "admins" && password == "admins") {
// return true;
// }else{
// return false;
// }
//password = "admins";
}
void setup()
{
Serial.begin(115200);
Serial.println();
//Serial.print("buff is:");
//Serial.print(MQTT_BUF_SIZE);
Serial.println();
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP); //chose mode
WiFi.softAP(ap_ssid, ap_password);
//WiFi.begin(ssid, pass);
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// Serial.println("");
//
// Serial.println("WiFi connected");
// Serial.println("IP address: ");
// Serial.println(WiFi.localIP());
/*
* Register the callback
*/
MQTT_server_onData(data_callback);
/*
* Start the broker
*/
Serial.println("Starting MQTT broker");
MQTT_server_start(mqttPort, max_subscriptions, max_retained_topics);
MQTT_server_onAuth(auth_data_callback);
/*
* Subscribe to anything
*/
MQTT_local_subscribe((unsigned char *)"#", 0);
}
int counter = 0;
void loop()
{
String myData(counter++);
/*
* Publish the counter value as String
*/
//MQTT_local_publish((unsigned char *)"/MyBroker/count", (unsigned char *)myData.c_str(), myData.length(), 0, 0);
// wait a second
// delay(1000);
}
and my mqtt client named "mqttfx"
Simon, can confirm this problem - Think, I found the problem.
Could you try with the patched version of the lib, I uploaded right now?
Will do some more test and fix and upload the sources later (up to now I just deleted line 832 in mqtt_server.c)...
it runs very good now,
what's the problem ?
and can I please ask a addional questions?
1. if I wanna to change the MQTT_BUF_SIZE , do I need to recompile the lib ?
2.if I wanna to enable the MQTT client mode, do I need to recompile the lib ?
3. how can I use the "struct espconn *pesp_conn" for printing the client IP in auth_data_callback ?
The problem is a double "espconn_disconnect()". In the NONOS SDK I never got a problem here, but memory management in Arduino seems to be different (better checks?) - anyway I will double check the disconnection sequence for the different cases (client vs broker initiated).
Your questions:
thank you for your reply.
1) ok, I will recomple the lib myself. if this MQTT_BUF_SIZE can be change in arduino , this would better. 2) I can use the MQTT client mode like tuanpmt's original esp_mqtt? ok 3) I can not use pesp_conn->proto.tcp->remote_ip in arduino.
hello, the mqtt borker become not stable after enable the MQTT_server_onAuth, once the client disconnected, the system will restart. here is the logs: