Git Product home page Git Product logo

pubsubclient's People

Contributors

dpslwk avatar egzumer avatar ennui2342 avatar imroy avatar knolleary avatar mactro avatar mcollina avatar protoneer avatar testato avatar thedistractor avatar thkdev avatar tobyjaffey avatar toshik avatar vmarseguerra avatar wilhall avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pubsubclient's Issues

receives the news after 10 times downtime

In qos = 2 cases, receives the news after 10 times downtime
code:

    client.subscribe(MQTT::Subscribe()
                     .add_topic("/snv/" + func + "/" + topicId, 2)
                     .add_topic(topic, 2));

No heap available, failed to malloc 0

Hi,

Notre sure it's an issue because it seems to work anyway but I get this error every 15 seconds when using the basic example (I didn't try the others...).

Anyone else facing this error?

Arduino 1.6.4 with esp package from Adafruit 1.6.2

Recieved string different from sent string

Hi,

I have connected to the Azure IoT hub MQTT service and subscribed to events as described in the following article:

https://azure.microsoft.com/en-gb/documentation/articles/iot-hub-mqtt-support/

Publishing works fine but when messages are sent from azure they are received ok but the contents do not resemble what has been posted see this Screen Capture

As you can see the content length is correct but the content itself does not match the outgoing messages. Also the content of the array is the same no matter the contents of the message.

Any ideas what could be wrong here?

How is connected() meant to work

I noticed that the Basic ESP8266 MQTT seems to work (on Arduino 1.6.7 and ESP-01), it always seems to think is isn't connected, reconnects and then goes on. Occasional I also get a WDT (Watchdog Timeout). I was looking at the code for connected() in the library and I can't seem to work it. It obviously is recursive (calling itself) which might be the cause the WDT. Maybe it should just be reading the _state variable? Or have I got it wrong.

Maybe the line -

  if (this->_state == MQTT_CONNECTED) {

should be -

  if (this->_state != MQTT_CONNECTED) {

????

boolean PubSubClient::connected() {
boolean rc;
if (_client == NULL ) {
rc = false;
} else {
rc = (int)_client->connected();
if (!rc) {
if (this->_state == MQTT_CONNECTED) {
this->_state = MQTT_CONNECTION_LOST;
_client->flush();
_client->stop();
}
}
}
return rc;
}

Different behavior between DHCP and fixed IP when WiFi drops out and returns

I'm testing, testing, testing... with the example mqtt_basic to see the behavior with a bad wifi connection.
My goal is to get the MQTT connection always to be restored after a Wifi drop. My ESP should NOT restart. It is doing other tasks also and they should not be disturbed. MQTT is only used to get some commands and write some status.

My first test is the mqtt_basic example as is. It responds to the subribed item as expected.
Now I switch the AP off and on again and see what happens: No problems with WDT, but it is not responding to the subscibed item anymore. -> The connection to the broker is lost.

Now I bring in some code in void loop() to restore the subscription:

unsigned long previous_time = millis();
void loop()
{
bool mqtt_status=client.loop();
if(!mqtt_status)
{
if (client.connect("arduinoClient"))
{
Serial.println("Connecting to MQTT broker....");
client.publish("outTopic", "hello world");
client.subscribe("inTopic");
}

unsigned long current_time = millis();

if ((current_time - previous_time) > 1000) // print debug information every second
{
previous_time = millis();
Serial.println("loop"); // check if void loop() is still running
if (mqtt_status)
{
Serial.println("MQTT connected");
}
else
{
Serial.println("MQTT fail");
}
}

}

This fires the WDT after a WiFi loss and sometimes after a WiFi restoration. Sometimes it not even reboots. I sometimes get FatalException(9) and FatalException(28), but that is random i guess.

My third test is to set the IP to fixed. Now there are no WDT problems anymore. it reconnects to the broker again and reponds to the subscribed item. I can see the timeout in the library. When my WiFi AP is switched off it still prints "MQTT Connected" for a while and after the timeout period it will print "MQTT fail". Switching the AP on again the ESP restores the WiFi connection and after that the code restores the MQTT connection. This is how it should work and it does.
But now it keeps connecting to the MQTT broker every 10-15 seconds.

My conclusion is that the ESP doesn't like DHCP with the subpubclient if WiFi gets lost. It looks like there is no network adapter anymore and it crashes. Anyway it has different behavior.

The last willmessage on server cann't be readed for callback function

If the esp is connect to the mqtt server and we publish a message on "inChannel" (where we are subscribe us to receive messages), the callback function read the message and we can process it. But, when in server there is a willmessage and the ESP connect to "inChannel", the last willmessage is publish on channel but the ESP don't read this "willmessage".
Anybody know how we can do for the ESP can read the last willmessage that exist on the channel????

Thanks a lot and Regards

MQTT - knolleary or ESP8266 library not working in same directory

Hi Imroy,

I am trying to use MQTT using the following:
Ethernet Arduino board or using ESP2866 module

For the past 3 years I have been using this library, https://github.com/knolleary/pubsubclient this has worked well.

I now wish to use the ESP8266 with MQTT using this library, https://github.com/Imroy/pubsubclient

Both of these have there own PubSubClient.h , so cannot be in my arduino library at the same time !

My problem is when I compile my code for Ethernet Arduino it will only compile error free using the knolleary library and the ESP8266 will only compile error free using the Imroy library.

This gives me the problem of removing one then installing the correct library every time I use ESP8266 or Ethernet Arduino code.

How can I get both to work ?

regards

Gary

unsubscribe bug fix

Hi in the src/MQTT.cpp file you are distributing with your pubsubclient, there are two errors around line 646

// Unsubscribe class
  Unsubscribe::Unsubscribe() :
    Message(SUBSCRIBE),
    _buffer(NULL), _buflen(0)
  {
    _need_packet_id = true;
  }

  Unsubscribe::Unsubscribe(String topic) :
    Message(SUBSCRIBE),
    _buffer(NULL), _buflen(0)
  {

the two instances of Message(SUBSCRIBE) should clearly be Message(UNSUBSCRIBE)

Ive run into this and fixing it makes unsubbing work,thx.

Change the IP address

Hi,

I've started using your fork as the older version no longer compiled on the ESP arduino IDE.

I have got it working apart from one thing, I can't assign the IP address dynamically. I want to read the IP address from the EEPROM then start the MQTT based on this. When i define MQTTserver as below it all works...

      IPAddress MQTTserver(192,168,1,24);
      PubSubClient mqttclient(MQTTserver);

If i do not, and try to assign MQTT server from EEPROM... during set up... I get the right IP but it fails to connect...

     for (int i=0; i<4; i++) {  
    MQTTserver[i] = EEPROM.read( mqttAddress+i);
   }

Any ideas why? I had this working before when the IP address was a char array..

Compile error

PubSubClient.h:55:56: error: section attribute not allowed for 'payload'

for that code:

   bool publish_P(String topic, const uint8_t PROGMEM *payload, unsigned int, bool retained = false);

ESP restarting when publishing

I got a situation where the ESP reboots when publishing any message, restarting the mosquito server fixed the problem immediately.

I have lots of the same binaries running (6 ESPs) so it is not the firmware.

Is there a way to enable debugging of the pubsubclient to work out what the server is sending back on a publish that would cause the ESP to reboot, should this happen again? Then we might be able to implement a fix to stop the ESP rebooting under these circumstances?

Cheers

A

SSL Support

Hi

Started using your library and it is easy and fun to work with. Excellent work. Nice to know you are close by in NSW. I just wanted to check with you if the library handles SSL yet? I thought asking a question would be easier than bashing my head against a brick wall for while.

If not I am more than willing to help in getting this done just my C/C++ skills is little rusty so the going might be a little slow...

N

MQTT work only on string ?

May you confirm this ? MQTT work only on char/string ?

I tried to send the value of a numeric variable, by the constructor:
bool PubSubClient::publish(String topic, const uint8_t* payload, unsigned int plength, bool retained)

but when i send number from 0 to 255 (uint8_t*) to a mosquitto broker, the number is converted to Ascii Character.

So whit this library (all MQTT library ?) we must send always String ?
I must use itoa_function for publish a numeric variable by this library ?
If this is the only way is it usefull write a new constructor whit integrated ITOA conversion

Client connection drop after received each qos 1 message.

I subscribed two topics 'topicA' and 'topicB' respectively with qos 0 and qos 1. Message received with topicA works find, however, Each message receives with topicB cause the client drop its connection. Any Idea why? I am using secured connection in both case. Tq

Compile error using mqtt_auth example - 'sent' was not declared MQTT.cpp

/Development/Arduino/libraries/pubsubclient/src/MQTT.cpp: In member function 'bool MQTT::Message::send(Client&)':
//Development/Arduino/libraries/pubsubclient/src/MQTT.cpp:123:5: error: 'sent' was not >declared in this scope
sent = client.write(const_cast<const uint8_t*>(packet), packet_length);
^
Error compiling.

client.loop() should return after ONE message received

I have this problem: I have a lot (about 10) topics that have retained messages.
After connecting to the broker I subscribe to all of them in one call to client.subscribe.
I start to receive the retained messages but after some of them the esp8266 freezes.

I put some debug instructions inside the callback function and before and after client.loop():
Serial.println("before");
client.loop();
Serial.println("after");
delay(500);

This is what i get:
before
new message
new message
new message

and then the esp8266 freezes. the delay is never called.
So, why client.loop() processes more then one message? How can i slow it down to prevent esp8266 from freezing when it receives a lot of messages at the same time?

I'm using QOS 2 for all pub/sub operations.

QOS 1/2 fail after 254 messages

Hi
Once my ESP module has sent 254 messages in qos mode 1 or 2 it will return with a fail permanently.
My eMqtt server console displays "[error] Session arduinoClient AwaitingRel Timout!"

Once reset, everything is fine again.
I use the one of the default examples to publish messages at an interval of between 20ms - 2sec.
Same result every time.
What can be the problem?

mqtt_basic example throws: "'MQTT' does not name a type" error.

Using the Arduino IDE, and latest version of the PubSubClient library. Hardware is Sparkfun 'Thing' ESP8266 board. Error thrown at:

void callback(const MQTT::Publish& pub)

I've tried reinstalling the whole IDE from scratch, etc.

Thanks in advance :)

Another OTA Issue

Using last version of lib.

Can't compile example OTA

METEOSTATION.ino: In function 'void receive_ota(const MQTT::Publish&)':
METEOSTATION:57: error: 'const class MQTT::Publish' has no member named 'payload_stream'
METEOSTATION:58: error: 'const class MQTT::Publish' has no member named 'payload_stream'
'const class MQTT::Publish' has no member named 'payload_stream'

When including client.loop() in loop(), esp8266 reboots.

So I have the code working, and I was sending the uptime to the mqtt server, but was only getting to 25secs before restarting. I tried changing the number of times I sent out mqtt stuff, but it didn't change anything. Turns out if I removed client.loop() where client is a PubSubClient, it doesn't reboot anymore. So any ideas? Also if I am not using any callbacks do I even need to call PubSubClient::loop()?

.connect always returns false on NodeMCU Amica (v1.0?)

I spent some time trying the examples (mqtt_basic and mqtt_subscriber) and I could never get my device to register to the Mosquitto broker I'm running on a local server, that is to say .connect always returns false. I have verified that I get an IP, and I can see traffic going to and from the server.

I have a NodeRed instance running and I've never seen $SYS/broker/clients/total go above 1 (which is the NodeRed MQTT client).

I ran Wireshark on the server and here's the packets I see back and forth for each loop:
client to server: SYN
s to c: SYN, ACK
c to s: ACK
c to s: PSH, ACK (CONNECT message)
s to c: ACK
s to c: FIN, ACK
c to s: RST, ACK
s to ?: ACK

Here's the CONNECT message in wireshark:
0000 10 19 00 04 4d 51 54 54 04 02 00 0f 00 0d 61 72 ....MQTT......ar
0010 64 75 69 6e 6f 43 6c 69 65 6e 74 xx xx xx xx xx duinoClient

What else do you need to troubleshoot?

Thanks for your work, I'm looking forward to getting this working!

Exception (9) and (28) with 2.1.0-rc2, but not with 2.1.0-rc1

Hi im getting random exception 28 and exception 9 when doing a simple client.publish(), but only with 2.1.0-rc2, with 2.1.0-rc1 it doesn't fail.

perhaps @igrr would be interested in this issue too.

here are the crashes

loop: 53
loop pub ready
Fatal exception 28(LoadProhibitedCause):
epc1=0x40105cb2, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00228000, depc=0x00000000

Exception (28):
epc1=0x40105cb2 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00228000 depc=0x00000000

ctx: cont 
sp: 3fff2c80 end: 3fff3200 offset: 01a0

>>>stack>>>
3fff2e20:  5c5c5c5c 5c5c5c5c 5c5c5c5c 5c5c5c5c  
3fff2e30:  c20b8380 0000016e 40248348 40227286  
3fff2e40:  00000000 00000000 000005b4 97205237  
3fff2e50:  0000005a 36363636 4022a56c 3fff4458  
3fff2e60:  00000000 00000000 0d2c3dde 2683964c  
3fff2e70:  00000055 3fff2e90 4022a78d 08080808  
3fff2e80:  00000001 9693819c 0d2c3dde 2683964c  
3fff2e90:  ade50000 00000000 00000000 4c968326  
3fff2ea0:  ade5e780 9c819396 3fff61e8 00000000  
3fff2eb0:  00000001 402078a3 3fff4510 00000000  
3fff2ec0:  00000055 00000055 00000055 00000055  
3fff2ed0:  00000000 3fff5964 00000000 00000001  
3fff2ee0:  00000055 2683964c 3ffea32a 00000050  
3fff2ef0:  3fff47d8 00000000 00000055 40236b88  
3fff2f00:  00020317 40104c23 3ffef208 40213012  
3fff2f10:  00000023 00000017 00000010 4021306a  
3fff2f20:  0000008a beb8bcb3 3fff2f60 00000000  
3fff2f30:  3fff47d8 3fff4a28 00000023 40236c0f  
3fff2f40:  00000023 beb8bcb3 3fff3030 40212fb8  
3fff2f50:  3fff4a20 00000000 3fff3030 3fff1df4  
3fff2f60:  00000023 3fff4a28 3fff3030 402072b4  
3fff2f70:  00000023 3fff4a28 3fff3030 4020dde7  
3fff2f80:  00000023 40102a84 3fffc200 00000022  
3fff2f90:  00000021 00000000 3ffea32a 4010754c  
3fff2fa0:  3fff3054 0000001d 00000020 00000000  
3fff2fb0:  3fff1db4 3fff3030 00000001 4020e78c  
3fff2fc0:  00000000 00000000 00000001 4021306a  
3fff2fd0:  00000005 00000000 3fff3054 3fff1db4  
3fff2fe0:  3fff3030 3fff1db4 00000001 4020e955  
3fff2ff0:  3fff3030 3fff306c 00000000 402131c0  
3fff3000:  3fff4a20 40107563 3fff3078 4020def5  
3fff3010:  0000000c 3fff47d8 00000001 40212fb8  
3fff3020:  3fff30b0 3fff1db4 00000001 4020e9e5  
3fff3030:  3ffea478 00000003 0000a300 40107500  
3fff3040:  00000000 00000002 00000010 00000000  
3fff3050:  3fff2118 3fff4a90 0000001f 0000001d  
3fff3060:  3fff4ab8 00000002 3fff3001 00000000  
3fff3070:  00000000 00000000 00000000 00000000  
3fff3080:  00000000 3fff1ec8 3fff30b0 402130e8  
3fff3090:  3fff3178 ff000000 3fff2118 40212734  
3fff30a0:  3fff18ec 3fff1ec8 3fff2118 4020581f  
3fff30b0:  3fff3c70 0000000f 00000002 401058c9  
3fff30c0:  4000050c beb72cfa 00000000 4000050c  
3fff30d0:  00000000 00000000 0000001f 401058c9  
3fff30e0:  4000050c 00000023 00000001 00000001  
3fff30f0:  40210a58 00000030 0000000a ffffffff  
3fff3100:  40100114 3fff21e0 40210a50 00000000  
3fff3110:  00000000 6e696874 312f7367 65663a38  
3fff3120:  3a34333a 353a6239 31383a62 6f6f6c2f  
3fff3130:  00000070 3fffdad0 3fff21d8 00000030  
3fff3140:  00000000 3fffdad0 3fff21d8 00000030  
3fff3150:  3fff2118 00000008 3fff2118 40210165  
3fff3160:  3ffe9ac2 00000001 3fff2118 40210165  
3fff3170:  3ffe9438 3fff21d8 3fff4a00 0000001f  
3fff3180:  0000001d 00000000 00000000 00000000  
3fff3190:  40003335 00000001 3fff18ec 4020f3d8  
3fff31a0:  3fffdad0 3fff1914 3fff18ec 40205158  
3fff31b0:  00000000 00000000 00000000 00000000  
3fff31c0:  40203ff8 feefeffe 40210c8c 40210c78  
3fff31d0:  00000000 00000000 00000001 3fff21d8  
3fff31e0:  3fffdad0 00000000 3fff21d1 40210a78  
3fff31f0:  feefeffe feefeffe 3fff21e0 40100114  
<<<stack<<<

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

and

loop: 9
loop pub ready
Fatal exception 9(LoadStoreAlignmentCause):
epc1=0x40105b29, epc2=0x00000000, epc3=0x00000000, excvaddr=0x80fb0f6d, depc=0x00000000

Exception (9):
epc1=0x40105b29 epc2=0x00000000 epc3=0x00000000 excvaddr=0x80fb0f6d depc=0x00000000

ctx: cont 
sp: 3fff2c80 end: 3fff3200 offset: 01a0

>>>stack>>>
3fff2e20:  5c5c5c5c 5c5c5c5c 5c5c5c5c 5c5c5c5c  
3fff2e30:  5c5c5c5c 5c5c5c5c 40248348 40227286  
3fff2e40:  00000000 00000000 000005b4 4cfc6cb5  
3fff2e50:  0000005a 36363636 4022a56c 3fff3f70  
3fff2e60:  00000000 00000000 0864472e 774d269b  
3fff2e70:  00000055 3fff2e90 4022a78d 09090909  
3fff2e80:  00000001 5c741514 0864472e 774d269b  
3fff2e90:  a4640000 00000000 00000000 9b264d77  
3fff2ea0:  a464c1d5 1415745c 3fff5d50 00000000  
3fff2eb0:  00000001 402078a3 3fff40f0 00000000  
3fff2ec0:  00000055 00000055 00000055 00000055  
3fff2ed0:  00000000 3fff560c 00000000 00000001  
3fff2ee0:  00000055 774d269b 3ffea32a 00000050  
3fff2ef0:  3fff4240 00000000 00000055 40236b88  
3fff2f00:  00020317 40104c22 3ffef208 40213012  
3fff2f10:  00000022 00000017 00000010 4021306a  
3fff2f20:  0000008c 1cca2a58 3fff2f60 00000000  
3fff2f30:  3fff4240 3fff46b0 00000022 40236c0f  
3fff2f40:  00000022 1cca2a58 3fff3030 40212fb8  
3fff2f50:  3fff46a8 00000000 3fff3030 3fff1df4  
3fff2f60:  00000022 3fff46b0 3fff3030 402072b4  
3fff2f70:  00000022 3fff46b0 3fff3030 4020dde7  
3fff2f80:  00000022 00000000 0000001f 401058c9  
3fff2f90:  00000020 00000000 3ffea32a 4010754c  
3fff2fa0:  3fff3054 0000001d 00000020 00000000  
3fff2fb0:  3fff1db4 3fff3030 00000001 4020e78c  
3fff2fc0:  00000000 00000000 00000001 4021306a  
3fff2fd0:  00000009 00000000 3fff3054 3fff1db4  
3fff2fe0:  3fff3030 3fff1db4 00000001 4020e955  
3fff2ff0:  3fff3030 3fff306c 00000000 402131c0  
3fff3000:  3fff45c0 40107563 3fff3078 4020def5  
3fff3010:  0000000c 3fff4240 00000001 40212fb8  
3fff3020:  3fff30b0 3fff1db4 00000001 4020e9e5  
3fff3030:  3ffea478 00000003 0000a300 40107500  
3fff3040:  00000000 00000001 00000010 00000000  
3fff3050:  3fff2118 3fff4688 0000001f 0000001d  
3fff3060:  3fff45f0 00000001 3fff3001 00000000  
3fff3070:  00000000 00000000 00000000 00000000  
3fff3080:  00000000 3fff1ec8 3fff30b0 402130e8  
3fff3090:  3fff3178 ff000000 3fff2118 40212734  
3fff30a0:  3fff18ec 3fff1ec8 3fff2118 4020581f  
3fff30b0:  3fff38f8 0000000f 00000001 401058c9  
3fff30c0:  4000050c 00000000 0000001f 401058c9  
3fff30d0:  40000f83 00000030 0000000d ffffffff  
3fff30e0:  40000ea3 00000023 00000001 00000001  
3fff30f0:  3fffdab0 00000000 3fffd9d0 3fff21d8  
3fff3100:  00000000 00000000 00000001 3fff21d1  
3fff3110:  00000000 6e696874 312f7367 65663a38  
3fff3120:  3a34333a 353a6239 31383a62 6f6f6c2f  
3fff3130:  00000070 3fffdad0 3fff21d8 00000030  
3fff3140:  00000000 3fffdad0 3fff21d8 00000030  
3fff3150:  3fff2118 00000008 3fff2118 40210165  
3fff3160:  3ffe9ac2 00000004 3fff2118 40210165  
3fff3170:  3ffe9438 3fff21d8 3fff45a0 0000001f  
3fff3180:  0000001d 00000000 00000000 00000000  
3fff3190:  40200039 00000004 3fff18ec 4020f3d8  
3fff31a0:  3fffdad0 3fff1914 3fff18ec 40205158  
3fff31b0:  00000000 00000000 00000000 00000000  
3fff31c0:  40203ff8 feefeffe 40210c8c 40210c78  
3fff31d0:  00000000 00000000 00000001 3fff21d8  
3fff31e0:  3fffdad0 00000000 3fff21d1 40210a78  
3fff31f0:  feefeffe feefeffe 3fff21e0 40100114  
<<<stack<<<

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

this is my code

    loops++;
    DEBUG.println(); 
    DEBUG.print("loop: ");
    DEBUG.println(loops);
    char s_loops[10];
    sprintf(s_loops, "%d", loops);
    char ptopic[100];
    strcpy(ptopic, topic);
    strcat(ptopic,"loop");
    DEBUG.println("loop pub ready");
    client.publish(ptopic,s_loops);
    DEBUG.println("loop published");

when client.publish crashes, obviously it never gets to say "loop published"

Is it possible to connect to two MQTT brokers at the same time with this library?

From a friend i got an ESP sketch to get P1 readings form an powermeter and send them to an Mosquitto MQTT broker.

It works well.

I try to change the sketch so i can connect to two Mosquitto MQTT brokers at the same time, but I endup in a connection loop when using the client1.loop(); and client2.loop() statements.

Does this library support using two clients simultaniously?

Can't pass 'this' to callback scope

Hello. I'm not super-c++ programmer, but I found that I can't pass 'this' to lambda-function when I work with set_callback(). I want to do something like this:

        mqttClient.set_callback([this](const MQTT::Publish& pub){
            for (int i=0; i<eventsSize; ++i) {
                if (pub.topic() == events[i].name) {
                    events[i].callback(pub.payload_string());
                }
            }
        });

eventsSize, events is members of top-class scope where this code is present. In current PubSubClien.h I do not found way to work with eventsSize, events inside lambda-function.

So I did some modifications for PubSubClien.h:

public:
//typedef void(*callback_t)(const MQTT::Publish&);

//...

std::function<void(const MQTT::Publish&)> _callback;
//callback_t _callback;

//..

PubSubClient& set_callback(std::function<void(const MQTT::Publish&)> cb) { _callback = cb; return *this; }

And now I can pass variables of different scope to lambda-function.

I can do Pull-Request with this modifications. But if you know right way to pass variables of different scope to lambda-function - please tell how I can do this.

Can't connect..

I can't get it to connect. I can connect just fine from a PC. I create my objects like this:

WiFiClient wclient;
PubSubClient mqttClient(wclient);

then set the server ip and port like this mqttClient.set_server(mqttIp, mqttPort); then in loop i do this:

  if (!mqttClient.connected()) {
    Serial.println("Not connected..");
    if (mqttClient.connect("test")) {
      mqttClient.set_callback(mqttCallback);
      mqttClient.subscribe("inTopic");
      mqttClient.publish("outTopic", "Hello!");
    }
  }

  if (mqttClient.connected()) {
    mqttClient.loop();
  }

But it just prints Not connected.. once every 15s which I guess is the timeout... Am I overlooking somthing?

Update library via Arduino IDE

Hello, Please is it possible to add support for updating library in IDE? Now when I download your's library and push update it updates to knolleary's version, but that version doesnt have features as you have.

Thank you very much.

client not detecting drop of broker connection sometimes.

I am running my esp8266 on a mobile 3g wifi hotspot (so sometimes it has latency/dropout issues as it is always moving) but have been finding that it doesn't always detect that the mqtt broker connection has dropped.

from basic example;

if (WiFi.status() == WL_CONNECTED) {
    if (!client.connected()) {
      if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
      }
    }

    if (client.connected())
      client.loop();
  }

in the process of researching this problem I found this handy hint here; knolleary#41

And based my code client.loop(); code off it and it seems to of fixed my problem;

if (!client.loop()) {
    Serial.println("### Client has disconnected...");

    // add you code to reconnect here.
    if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
      }
  }

One more thing, I have also found that sometimes just doing this doesn't work, I find I need to force the disconnect. ie, using client.disconnect(); before calling connect again.

if (!client.loop()) {
    Serial.println("### Client has disconnected...");
    client.disconnect();

    // add you code to reconnect here.
    if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
      }
  }

As mention on the other issue on knolleary github this probably isn't a issue, just pointing it out for other people if they have similar issues.

Matt.

Wrong data received via payload_stream()->read()

I don't know where is the bug - in WiFi stack or somewhere else.

I have got broken firmware after OTA update and did some investigations.
Looks like all data after some (random per re-flash, not per reboot/publish) offset returned as 0xFF

As an another corresponding bug - "clearing" OTA publish returns false when called after stream i/o but works if called directly after reboot.

all code based on sample ESP8266-OTA, latest arduino/pubsubclient for now. The hardware is ESP-07 with 4MB re-soldered flash

The sample code for checksum testing (note! printing from ESP cause stack error near 0x16F24 offset so disabled by '#if 0' for checksum-only testing):

// main.c 
// $>gcc main.c
// $>./a.out /tmp/build*.tmp/sketch_name.cpp.bin
#include <stdio.h>

void main(int argc, char *argv[]) {
    unsigned short x = 0;
    unsigned short x16 = 0;
    int l = 0;
    int c;
    FILE *f;
    printf("TEST CSUM\n");
    f = fopen(argv[1], "r");
    while((c=fgetc(f)) != EOF) {

        x += (unsigned char) c;
        x16 += (unsigned char) c;
        if (!l) {
            printf("0000:  ");
        }
        if (l && l%32==0) {
            printf(" : 0x%X\n%04X: ", x16, l);
            x16 = 0;
        }
        if (l && l%4==0) printf(" ");
        printf("%02X ", (unsigned char)c);
        l++;
    }
    fclose(f);
    printf("\nSum %d bytes of '%s': %u (0x%X)\n", l, argv[1], x, x);
}

An corresponding code in ESP:

Serial.print(" * Receive OTA: "); Serial.print(size); Serial.println(" bytes...");

    uint16_t csum = 0;
    uint16_t c16 = 0;
    for(int i=0;i<size;i++) {
      unsigned char c = (unsigned char)pub.payload_stream()->read();
   #if 0
      c16 += c;
        if (!i) Serial.print("0000:  ");
        if (i && i%32==0) {
          Serial.print(" : 0x");
          Serial.println(c16, HEX);
          if (i < 0x0010) Serial.print("000");
          else if (i < 0x0100) Serial.print("00");
              else if (i < 0x1000) Serial.print("0");
          Serial.print(i, HEX);
          Serial.print(": ");
          c16 = 0;
        }
        if (i && i%4==0) Serial.print(" ");
        if (c < 16) Serial.print("0");
        Serial.print(c,HEX);
        Serial.print(" ");
      delay(0);
 #endif
      csum += c;
    }
    pub.payload_stream()->stop();
    for(int i=0;i<5;i++) {
      if (mqtt.clear_ota()) break; // pubsubclient.publish(TOPIC, (byte*)"", 0, true) here
      mqtt.loop();
      delay(10);
    }
    for(int i=0;i<10;i++) {
      mqtt.loop();
      delay(10);
    }
    Serial.print(" > OTA CSUM: ");
    Serial.println(csum);

Latest log for binary:

Sum 348080 bytes of '/tmp/build3005941742736874158.tmp/sensor-v2.cpp.bin': 8989 (0x231D)

corresponding serial output:

* Receive OTA: 348080 bytes...
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > OTA CSUM: 37309
 * Receive OTA: 348080 bytes...
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > OTA CSUM: 18836
 * Receive OTA: 348080 bytes...
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > STOP_OTA: '/dev/esp-9e82d0/ota' ->0
 > OTA CSUM: 18836
...

QOS 1 Puback

When the client receives a qos = 1 publish from the server, it does not include the packet ID of the received packet in the puback.

after webconfig state=-2 on reset

After webconfig, when moving from AP mode to wifi client mode, the reset button works for wifi connect but not for pubsubclient connect. In order to get pubsubclient to connect I need to power down the Wemos mini. After wifi is setup, reset works fine for pubsubclient connect. Is there some command that I am missing?

WiFiClient espClient;
PubSubClient client(espClient);

void setup(){
  Serial.begin(115200);
  EEPROM.begin(512);
  Serial.println();
  Serial.println("--------------------------");
  Serial.println("ESP8266 multifile");
  Serial.println("--------------------------");
  getOnline();
  strcpy(cmd, devid);
  strcat(cmd,"/cmd");
  client.setServer(ip, 1883);
  client.setCallback(handleMqttIn);  
}

void loop(){
  server.handleClient();
  if(NEW_MAIL){processIncoming();}
  if(!client.connected() && !NEEDS_RESET){
     reconn();
  }else{
    client.loop();
  }
}

void reconn() {
  Serial.print("Attempting MQTT connection...");
  if (client.connect(devid)) {
    Serial.println("connected");
    client.subscribe(cmd);
    return;
  } else {

(arduino 1.6.7)

ESP8266 lost MQTT connection, no TCP to reconnect

I'm trying to run the PubSubClient with my ESP8266-12e.
I run the following Code on my ESP:

/*
 Basic MQTT example 

  - connects to an MQTT server
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic"
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "MySecret.h"


// Update these with values suitable for your network.
IPAddress server(192, 168, 1, 203);
long lastMsg = 0;

void callback(const MQTT::Publish& pub) {
  // handle message arrived
}

WiFiClient wclient;
PubSubClient client(wclient, server);

void setup() {
  // Setup console
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();

  client.set_callback(callback);
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println("...");
    WiFi.begin(ssid, password);

    if (WiFi.waitForConnectResult() != WL_CONNECTED)
      return;
    Serial.println("WiFi connected");
  }

  if (WiFi.status() == WL_CONNECTED) {
    if (!client.connected()) {
      Serial.println("MQTT-Client disconnected");
      if (client.connect("arduinoClient")) {
        Serial.println("MQTT-Client connected");
        //client.publish("outTopic","hello world");
        client.subscribe("inTopic");
      }
    }

    if (client.connected())
      client.loop();
  }

    long now = millis();
  if (now - lastMsg > 300000) {
    lastMsg = now;
    client.publish("/30/Status/MQTT","on");
  }

}

Generally the Code runs, but after a while it seams that the ESP lost his TCP-connection to the MQTT-Server.

At this point the debug :

Connecting to Pf@nne-NET...
WiFi connected
MQTT-Client disconnected
MQTT-Client connected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected
MQTT-Client disconnected

At this point the TCP-connection should be renewed, but no network activity will shown on my WireShark monitor!

Maybe, the TCP-Connection is always active.
The WiFi-connection schould be stable.

Is the someone who has problems like this?
I´m not a C expert, so maybe someone has fun to help me to set some more debug-informations to solve the problem.

The MQTT-broker runs on a Rapberry.
Used version of the Mosquitto broker is 1.4.4.

Thaks for your Help.......

[Solved] No Broker connection

I tryed this example but it not work,
WiFi connected message is OK
But after i receive always No Broker connection !

The broker on 192.168.100.254 is working because i publish to it from my PC by mqttspy.
May you try this code whit last version of pubsubclient ?

/*
 Basic MQTT example

  - connects to an MQTT server
  - publishes "22" to the topic "home/temperature"
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char *ssid =  "xxx";      // cannot be longer than 32 characters!
const char *pass =  "xxx";      //

// Update these with values suitable for your network.
IPAddress server(192, 168, 100, 254);

void callback(const MQTT::Publish& pub) {
  // handle message arrived
}

PubSubClient client(server);

void setup()
{
  // Setup console
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();

  client.set_callback(callback);

  WiFi.begin(ssid, pass);

  int retries = 0;
  while ((WiFi.status() != WL_CONNECTED) && (retries < 20)) {
    retries++;
    delay(1000);
    Serial.print(".");
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("");
    Serial.println("WiFi connected");
  }

  if (client.connect("Esp8266_Client")) {
    client.publish("home/temperature", "22");
    Serial.println("Published !");
  }
  else
  {
    Serial.println("No Broker connection !");
  }
}

void loop()
{
  client.loop();
}

Error on connect code -2

Hi!

Version: Arduino IDE 1.6.6 (and test 1.6.5), Arduino ESP8266 (2.0.0) when try to connect error -2.
Try use anonymous too. Used tcpdump on Mosquitto server packages have. Have any ideas?

WiFi connected
IP address:
192.168.1.3
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

maximum length of message

What is the maximum length of message?

I'm getting quite a truncation at about 50B, when i can send massive messages using MQTT-SPY

OTA issues

Hi Iml having trouble getting OTA to work reliably over MQTT

I'm using @Imroy 's MQTT example as a base for my code, it uses UpdaterClass::writeStream

I added a lot of extra debugging to Updater.cpp to see why it was failing 98% of the time. I found the mqtt transfer stream was "bogging down", i.e. it starts out fast but gets slower and slower and eventually times out. I added data.setTimeout(15000); to the top of writeStream and this helped but I still get timeouts. This is from a broker located in a Chicago hosting co, so only about 70ms away and no packet loss.

In order to eliminate network conditions as being the cause, I tried also from a broker on my local LAN, i.e. less than 1ms latency, and things look much better, i can get 6 out of 10 OTAs to work perfect. I get the "Staged" message and it restarts as it should.

Im getting lmac.c 662 the other 4 times out of 10 when testing from the LAN broker.

Back to testing from my internet-hosted broker (which is ultimately where it needs to work from), it mostly just bogs down and times out with ERROR[5]: Stream Read Timeout , sometimes I get lmac.c 662 also right after starting the OTA. The packets start out coming in at 4096 bytes each nice and rapidly, but they seem to slow down i.e. they get longer and longer gaps between them, eventually I start seeing some smaller-than-4096 packets, this is a pretty good sign its about to timeout, then it does. I added Serial.println(toRead); Serial.println(written); in the while inside writeStream to view the progress, heres the output

.
.
.
4096
143360
4096
147456
4096
151552
4096
155648
4096
159744
4096
163840
4096
167936
4096
172032
4096
176128
4096
180224
4096
184320
4096
188416
4096
192512
4096
196608
4096
200704
1026
204800
1460
205826
0
207286
ERROR[5]: Stream Read Timeout
ERROR[5]: Stream Read Timeout

There is no packet loss from the esp to my gateway and no loss from the gateway to the server, no congestion anywhere. It almost feels like my server is rate-limiting or throttling this large mqtt transfer, is that even possible? I'm running mosquito 1.4.3 broker.

Any ideas?

can't connect with String hostname

I've set up a DDNS on my router and I'm tring to connect the ESP with your library using the constructor client(wclient, server) where server is a String with my hostname, Ex. "mybroker.ddns.net".

However this does not work.

Problem with subscribe ESP got hang

Hello I have problem with subscribe when it received data from subscribe.

if(mqtt_connected) {
    client.subscribe("topic/setting/+")
    // payload will be `topica/a`
}

void callback(const MQTT::Publish& pub) {
   String topic = pub.topic();
   String data = pub.payload_string();
    // I cut string with `setting` on topic
    if (receive topic == "setting") {
        client.subscribe(payload);
    }
}

and I use client to subscribe with retained like.

topic = topic/setting/1
payload = topica/a
retained

topic = topic/setting/2
payload = topicb/b
retained
       // so when esp boot it will be 3 subscribe
       // 1.topic/setting/+

       this 2 will subscribe in if condition.
       // 2.topica/a
       // 3.topicb/b

NOW I got esp hang. if I set retain more than one topic.

but If I use retain with one topic like.

topic = topic/setting/1
payload = topica/a
retained

my esp not hang.

don't know what the problem are.

How Clean Session work?

Hi lmroy,
I'm trying my esp retrieve messages from the server that may have existed while he was off. I tried to connect with the variable:

  • Set_clean_session (true)
  • Set_clean_session (false)
  • Unset_clean_session ()
    but I can not get my esp recover lost messages.
    Must I configure anything something on the mosca server?

Thanks and Regards

Wonderfull library!!!!!!!!!!!!!

Using an instance method as callback.

First of all, thanks for the awesome library. It really helps me in the development of my MQTT projects.

I'm currently trying to use an instance method as the callback, but unfortunately, the set mqtt.set_callback(mqttCallback) method expects a general function. mqtt.set_callback(this.mqttCallback) or something like mqtt.set_callback(MyObj::mqttCallback, this) does not work.

Do you know a way around this, without making my instance static?

Receives it's own message

Hello, I have been really enjoying this library however I've hit a dead end in my code. My device needs to send and receive messages on the same topic but when I publish a message, it also receives the same message.

I was wondering if there is a workaround for this issue or if it is a issue with the library itself.

mosquitto v1.3 on ubuntu 14 does not accept mqtt header

Might be worth changing the readme to reflect this?

mosquitto[21836]: Invalid protocol "MQTT" in CONNECT from 192.168.0.7

mattsmac:2107: /usr/sbin/mosquitto
1437687094: mosquitto version 0.15 (build date 2013-08-23 19:23:43+0000) starting
1437687094: Opening ipv4 listen socket on port 1883.

mattsmac:2109: cat /etc/issue
Ubuntu 14.04.2 LTS \n \l

client.publish() introduces huge delays

I have following code running on ESP:

void loop()
{
    if(millis() - t  > 10) {
        static int i = 0;
        String topic = "test/" + String(i % 10);
        String msg = "some test message " + String(t);
        mqtt->publish(topic, msg);
        i++;
        t = millis();
    }   
  mqtt->loop(); 
}

When I subscribe to the test/# topic I would expect the counter on the end of the message to have difference close to 10 between subsequent messages, but I usually get something around 100-200. What is even more bizzare, when I set up a python script on the PC to publish messages in 30ms intervals to a topic that ESP is subscribed to, the publishing rate accelerates to something around 20ms. Unfortunately after some time, ESP reboots with

rst cause:4, boot mode:(3,6)

wdt reset
load 0x40100000, len 28780, room 16 
tail 12
chksum 0x35
ho 0 tail 12 room 4
load 0x3ffe8000, len 1548, room 12 
tail 0
chksum 0x90
load 0x3ffe8610, len 3300, room 8 
tail 12
chksum 0x88
csum 0x88

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.