Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT Subscribe not working #163

Open
hemalbavisi opened this issue May 16, 2016 · 73 comments
Open

MQTT Subscribe not working #163

hemalbavisi opened this issue May 16, 2016 · 73 comments

Comments

@hemalbavisi
Copy link

hemalbavisi commented May 16, 2016

I am very new to ESP8266 and MQTT. So Apologies if I haven't done it properly. I am using test.mosquitto.org server and trying to publish and subscribe the data. I am able to publish the data but subscribe part is not working. Nevertheless, I have tested subscribe part between two mobile and I am able to receive the data but I am not getting any data in the callback routine though I subscribed explicitly in the code. I have attached my code for your reference.

`

include <ESP8266WiFi.h>

include <PubSubClient.h>

define wifi_ssid ""

define wifi_password ""

define mqtt_server "test.mosquitto.org"

define mqtt_user "test"

define mqtt_password "test"

define topic1 "SmartIOT/Temperature"

define topic2 "SmartIOT/Humidity"

define topic3 "SmartIOT/Switch"

void callback(char* topic, byte* payload, unsigned int length);
WiFiClient espClient;
PubSubClient client(mqtt_server, 1883, callback, espClient);
void setup() {
Serial.begin(115200);
setup_wifi();
//client.setServer(mqtt_server, 1883);
client.subscribe(topic3);
}

// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
// In order to republish this payload, a copy must be made
// as the orignal payload buffer will be overwritten whilst
// constructing the PUBLISH packet.
Serial.println("Incoming data : ");
Serial.println(topic);
//Serial.println(payload);
// Allocate the correct amount of memory for the payload copy
byte* p = (byte*)malloc(length);
// Copy the payload to the new buffer
memcpy(p,payload,length);
//client.publish("outTopic", p, length);
// Free the memory
free(p);
}

void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("TestMQTT")) { //* See //NOTE below
Serial.println("connected");

    } else {
        Serial.print("failed, rc=");
        Serial.print(client.state());
        Serial.println(" try again in 5 seconds");
        // Wait 5 seconds before retrying
        delay(5000);
    }
}

}
//NOTE: if a user/password is used for MQTT connection use:
//if(client.connect("TestMQTT", mqtt_user, mqtt_password)) {
void pubMQTT(String topic,float topic_val){
Serial.print("Newest topic " + topic + " value:");
Serial.println(String(topic_val).c_str());
client.publish(topic.c_str(), String(topic_val).c_str(), true);
}
//Variables used in loop()
long lastMsg = 0;
float t1 = 75.5;
float t2 = 50.5;
void loop() {
if (!client.connected()) {
reconnect();
client.subscribe(topic3);
}
client.loop();
//2 seconds minimum between Read Sensors and Publish
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
//Read Sensors (simulate by increasing the values, range:0-90)
t1 = t1>90 ? 0 : ++t1;
t2 = t2>90 ? 0 : ++t2;
//Publish Values to MQTT broker
pubMQTT(topic1,t1);
pubMQTT(topic2,t2);
}
}
`

@hemalbavisi
Copy link
Author

One more point -> Connection to server is frequently getting disconnected. However, I am able to see almost all the data from ESP2866 client to which my mobile application is subscribed to.

@MartijnvdB
Copy link

You did not specify that a callback should be executed when a message is received.

In setup(), after you do client.setServer(arg, arg), you specify which callback to execute, as follows:

client.setCallback(callbackName); // callback executed when message received

@hemalbavisi
Copy link
Author

Thanks for your quick response. I posted little older code first time but then I corrected it. Please check below mentioned code

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

PubSubClient client(mqtt_server, 1883, callback, espClient);

void setup() {
Serial.begin(115200);
setup_wifi();
//client.setServer(mqtt_server, 1883);
client.subscribe(topic3);
}

Best Regards,
Hemal Bavishi

From: MartijnvdB [mailto:[email protected]]
Sent: 17 May 2016 11:32
To: knolleary/pubsubclient [email protected]
Cc: Hemal Bavishi [email protected]; Author [email protected]
Subject: Re: [knolleary/pubsubclient] MQTT Subscribe not working (#163)

You did not specify that a callback should be executed when a message is received.

In setup(), after you do client.setServer(arg, arg), you specify which callback to execute, as follows:

client.setCallback(callbackName); // callback executed when message received


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHubhttps://github.com//issues/163#issuecomment-219626975

@MartijnvdB
Copy link

Hi,

Did you uncommment the line that says:

//client.setServer(mqtt_server, 1883);

and did you add the following line after that:

client.setCallback(callback); // callback executed when message received

?

@hemalbavisi
Copy link
Author

Yes, I commented this line as I’ve added this line on the top -> PubSubClient client(mqtt_server, 1883, callback, espClient);

Isn’t above line serve the same purpose?

Nevertheless, I can try uncommenting this line and also add the line you suggested and will get back to you.

client.setServer(mqtt_server, 1883);

client.setCallback(callback); // callback executed when message received
Many Thanks!

@MartijnvdB
Copy link

PubSubClient client(mqtt_server, 1883, callback, espClient);

Hm, that should work too, indeed (according to the docs).
Haven't used that one yet, myself.

@hemalbavisi
Copy link
Author

Thanks. I'll check and revert back to you in few hours.

@hemalbavisi
Copy link
Author

Unfortunately it doesn't work. Sometime data appears but most of the time it doesn't. What could be the problem?

@edwin-oetelaar
Copy link
Contributor

Check the data flowing to the server using WireShark.
Use a debugger?
Write log files?
Use brain?

@knolleary
Copy link
Owner

Any update?

@Kerr1st
Copy link

Kerr1st commented Nov 29, 2016

Hi Knolleary, Picking up the issue...Mqtt_Esp8266 example doesn't receive/serial print on inTopic (publishes outTopic fine) nor toggle LED. See Serial Output below. Two other clients connected and receive publish outTopic and published inTopics. Wemos D1 mini is ESP8266 module.

serial output:
WiFi connected
IP address:
192.168.1.96
Attempting MQTT connection...connected
Publish message: hello world #1
Publish message: hello world #2
Publish message: hello world #3
Publish message: hello world #4
Publish message: hello world #5
Publish message: hello world #6
Publish message: hello world #7
Publish message: hello world #8
Publish message: hello world #9
Publish message: hello world #10
Publish message: hello world #11
Publish message: hello world #12
Publish message: hello world #13
Publish message: hello world #14
Publish message: hello world #15

@edwin-oetelaar
Copy link
Contributor

edwin-oetelaar commented Nov 30, 2016 via email

@fdrevet
Copy link

fdrevet commented Dec 7, 2016

Hi there, here is my experience with ESP8266 (01 and 12 models) and MQTT using PubSubClient.

The publishing works quite well and is also reliable (with an ESP8266-12), but the subscribing is just a real pain (IMO !)... for me the subscribe method mostly returns 0, "randomly" and very few times it returns 1... same behaviour with 3 ESP8266-12 modules...

Note : I'm using "EasyIot Cloud" MQTT server, dunno if it can be the reason ! I'll try a local MQTT server and send the result here.

@hudanar
Copy link

hudanar commented Dec 9, 2016

Hi there, iam using 2 Wemos D1 R2, 1 for publisher and 1 for subscriber. connected to mosquitto broker on my pc, but everytime publisher publish message, the subscriber got disconneted and reconnecting to the broker, so the subscriber never get the message from the publisher, anyone know how to fix that?

@knolleary
Copy link
Owner

@hudanar please don't ask your own questions on other issues.

My guess is you are using the same clientid for both clients and they are kicking each other off the broker. Make use you give each client a unique id

@hatfieldr
Copy link

hatfieldr commented Jan 5, 2017

Hi Nick
I have a similar problem using PubSubClient. It connects to wifi & to MQTT server & publishes fine but does not receive subscribe data.
I have an ESP8266-01 connected to Arduino Mega (with level conversions)
Code:

#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
//#include "SoftwareSerial.h"
#include <PubSubClient.h>

IPAddress server(192,168,1,xxx);
char ssid[] = "xxxxxxxxx";           // your network SSID (name)
char pass[] = "xxxxxxxxxx";           // your network password
int status = WL_IDLE_STATUS;   // the Wifi radio's status


// Initialize the Ethernet client object
WiFiEspClient espClient;

PubSubClient client(espClient);

//SoftwareSerial soft(2,3); // RX, TX
void setup() {
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");

  //connect to MQTT server
  client.setServer(server, 1883);
  client.setCallback(callback);
}

//print any message received for subscribed topic
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
 
  Serial.print("] ");
  for (int i=0;i<length;i++) {
   char receivedChar = (char)payload[i]; 
    Serial.print(receivedChar);
    if (receivedChar == '0')
    Serial.println("Off");
    if (receivedChar == '1')
    Serial.println("On");
    
  }
  Serial.println();
}

void loop() {
  // put your main code here, to run repeatedly:
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect, just a name to identify the client
    if (client.connect("AMega","xxxxxx","xxxxxxxxxxx")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
 //  client.publish("Hello World");
      // ... and resubscribe
      client.subscribe("switch");
      
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

This connects to WiFi fine & to an MQTT server running on a raspberry Pi
It posts to the Pi but does not receive subscribed messages.
I also have an MQTT client on my android phone. This communicates fine with the RPi
Again the Arduino can publish to my phone but cannot receive with subscribe

I am not sure where to go next?
Any help would be appreciated

Richard

@fdrevet
Copy link

fdrevet commented Jan 6, 2017 via email

@hatfieldr
Copy link

hatfieldr commented Jan 6, 2017

Hi Florian
Thanks for replying
I am using a Mosquitto MQTT broker (1.4.10) on the RPi (3) It's part of the emonpi software from the OpenEnergyMonitor group.
The arduino sketch publishes fine to the RPi and to the client on my android phone (MQTT Client App)
The phone communicates both ways with the RPi
But the arduino shetch does not appear to receive anything from either my phone or the RPi with client.subscribe("")
The arduino stays "connected" but nothing happens!
It does automatically disconnect after about 30 seconds & then reconnects to the MQTT broker

I am at a loss as to what to try next!
Any suggestions gratefully received!

Regards

Richard

@fdrevet
Copy link

fdrevet commented Jan 16, 2017

Hi Richard,

I'm using Mosquitto without any issue (Raspberry B model 2)
I had a lot of problems with random connections and subscriptions, but it was coming from my Wifi router (Livebox, just a shame) now I'm using a WRT54GL and it works like a charm.
=> dunno what Wifi access point you're using but I guess you're connecting to your raspberry through your Wifi router ? Did you tried to connect from your phone (wifi access point) for exemple ? (you will need a NAT rule to access it from the outside)

As I previously asked it to you, did you checked the subscribe("switch") return ? (should be 0 if it don't succeed to subscribe) Also, did you tried to make a retry logic ? (I think the subscribe method should fails)

Best regards,

Florian

@hatfieldr
Copy link

Hi Florian

I have a BT router, all my current experiments are on my home network, I don't think its a router problem. The mosquitto server on my Pi 3 seems to work fine with other devices (eg the MQTT client on my android phone)
client.subscribe("switch",0) returns 1 so I guess "subscribe" may be working & the problem is with callback?
no messages for topic "switch" seem to be received
I have tried lots of different message formats

would love to solve it!
Its holding up my project!

Regards

Richard

@fdrevet
Copy link

fdrevet commented Jan 18, 2017

Hi Richard,

I was asking you about the router because my smartphones was perfectly working (connect, subscribe, publish), but my ESP8266 was rarely and randomly working.

It's very strange that subscribe() returns 1 and if you don't receive anything on callback method.

I'm calling setCallback() before setServer(), don't know if it can change anything...

You seems to have a working broker, but have you tried mosquitto_pub mosquitto_sub batch calls directly on your Raspberry ?

If you want, I can send to you my working code by mail, if it can helps.

Florian

@hatfieldr
Copy link

Hi Florian
My sketch is based on one of the examples so I guess it has the right format.
The broker on the Pi seems to be working fine .... it communicates with the client on my android phone both ways...
I wondered if there was any way of "sniffing" communication with the ESP8266?
when I fist connected the ESP to my arduino mega I used 10K & 20K resitors in the voltage dividers on the mega output lines ..... this resulted in very erratic transmission at 115200 baud but when I used 1k & 2K resistors it connected to wifi & the MQTT broker fine....

I'm not sure what to try next....

Regards
Richard

@slavino
Copy link

slavino commented Jan 20, 2017

Hi,
if your issues are only HW related give it a try with Logic Level converter 3V3/5V (example: http://www.ebay.com/itm/2-Channel-Way-Bi-directional-Logic-Level-Shifter-Converter-5V-3-3V-TTL-Module-/121736138032?var=&hash=item1c580a0d30:m:mGYrmVBtrJ153d9DsH5Ld6g).
ArduinoMega has 5V logic and ESP8266 should be 3V3 only.

@hatfieldr
Copy link

Hi
I do know about the different voltages! my issues are not related to level shifting I originally used resistive dividers to shit levels, I now use a level shifter. It made no difference. The arduino communicates fine with the ESP. It logs on to the network & the MQTT broker It publishes fine & subscribe() returns 1 but no messages are received for the subscribed topic....

@slavino
Copy link

slavino commented Jan 20, 2017

Ah, got it now - sorry for misunderstanding... Then off-topic reaction: What about bypassing the issue for now with regular querying the channel for retained messages?

@hatfieldr
Copy link

Update 2

command line publish on the Rpi works fine
$mosquitto_pub -u 'xxxxx' -P 'xxxxxxx' -t 'switch' -m '1switchon'
This is received on my android mqtt client but not the arduino

I have tried installing mosquitto on my PC....
The results are exactly the same!

I have tried an ESP8266-12 with the Mega with the same result It connects to the WiFi network & to the MQTT broker, It publishes fine client.subscribe() returns true but no messages are received

Desperate for an answer............
Any help appreciated.....,......

Richard

@tororm
Copy link

tororm commented Jan 30, 2017

Hi @hatfieldr ,

I was tidying up some working code and I experienced the same issue. I believe I'm on to something, but it's too late for me to investigate further.
My mqtt payload looks like this:
{"desired_hysterisis":0,"desired_temp": 50,"desired_humidity":20,"desired_FanOn":off,"desired_FanBreak":5.0}

However, try as I may, the callback is not entered. Subscribe Status is 1.

I then change the payload to something simple like 'Hello'.

It arrives!

Hoping this helps. I will do some more testing in coming days.

Edit: scratch that. Changing the QOS to 1 and waiting for a few loops results in the message arriving.

@hatfieldr
Copy link

I solved the problem by calling:
if (!client.connected())
reconnect();
&
client.loop(); every 100 ms instead of every loop
If you are not flashing the code to the ESP but are using an arduino connected to the ESP via serial I think the problem is calling these functions every loop causes the serial interface to be overwhelmed with status messages blocking the incoming message. I am a newbie programmer especially with the ESP & MQTT so I'm not sure................
But my setup is now working (After a week of tearing my hair out!!)
Richard

@anupam19
Copy link

anupam19 commented Jan 30, 2017 via email

@epicycler
Copy link

epicycler commented Jan 14, 2018

I have ESP8266 with pubsub. Arduino IDE. No Arduino hardware, just ESP8266 ESP-01.

If both publish and subscribe then nothing useful in callback. Device resets frequently.
If just subscribe then some hieroglyphics in callback.
Inside function boolean PubSubClient::loop() in pubsub I can successfully trace received messages as far as where callback is called. Serial.write for each character in topic and payload.
eg. (buffer is 255, I changed the message size.

					Serial.print("topic ");
					for (int i=0; i<tl; i++) {
						Serial.write(topic[i]);
					}
					Serial.println(" ");

and (because I don't know the length of payload)

						Serial.print("payload ");
						for (int i=0; i<255-tl; i++) {
							Serial.write(payload[i]);
						}
						Serial.println(" ");

Uncomment callback in pubsub and in my sketch the topic can be printed. However, length is consistently 5 and payload "not human readable".

A difference between topic and payload is that topic is char* and payload is uint8_t. Probably red herring. But keep in back of mind as similar problems to what we are seeing occur in pubsub when using arduinojson-master to prepare json payload and publish.

Picking up on confusedtoo's comments about scope (whether variables are persistent outside the function in which they are declared) I added static to the declaration of both payload and topic in pubsub function boolean PubSubClient::loop()
thus:-

static uint8_t *payload;
static char *topic = (char*) buffer+llen+2;

buffer is already declared global.

This lets me successfully use the following in my sketch callback function:-

 Serial.println(topic);
 for (int i=0; i < 50; i++) {
   Serial.write(payload[i]);    
 }

The device seems to maintain its connection more reliably.

FWIW I met similar problem (scope) a couple of weeks ago. A library which compiled and ran on Arduino Uno and Mega hung on a Teensy. Its quite possible that people are getting different results from pubsub on different hardware.

I haven't yet figured out why the length of the payload provided by callback is always 5.

I also haven't yet tested with publishing and subscribing at the same time. Nor do I understand why the message is frequently garbled when first received into buffer. Experience suggests there's more than one problem with pubsub on ESP8266.

I'm not a C++ programmer. I have limited understanding.

@epicycler
Copy link

epicycler commented Jan 14, 2018

after some more testing my ESP8266 sketch now both publishes and subscribes. Note that with Arduino IDE (on Win 10) sketches compiled for ESP8266 (Adafruit HUZZAH) have different outcome to same sketch compiled for Uno or Mega. In ESP8266 variables declared inside a function do not persist after completion of function.

The problem occurs here in callback. It applies generally to return from any function.

following are required changes to PubSubClient.cpp

line 304 declare *payload as static
static uint8_t *payload;

line 313 declare *topic as static
static char topic = (char) buffer+llen+2;

add line 314 declare new variable plen as static
static uint8_t plen = 0;

add line 319 assign value (calculated length of payload) to plen
plen = len-llen-3-tl-2;

change line 320 callback with new variable plen
callback(topic,payload,plen);

add line 331 assign value (calculated length of payload) to plen
plen = len-llen-3-tl;

change line 332 callback with new variable plen
callback(topic,payload,plen);

Thanks for very useful library. I've now tested publishing at 1000 messages per second and receive occasional subscribed messages with mosquitto on raspberry pi 3.

@knolleary
Copy link
Owner

@epicycler much easier if you can propose changes as a pull request

@epicycler
Copy link

@knolleary thanks. I'm totally new to Github and all things Linux. Its very foreign to me. Not your fault, but when I read the info on pull request I left with absolutely no idea. Common problem for me, lots of info on how but not a lot about why.
I'll create a pull request. How does that eventually result in changes to the library? I have a totally unrelated library which is worthy of publishing but absolutely no idea what the whole process looks like.

@knolleary
Copy link
Owner

@epicycler there's no guarantees if/when these changes will get into the library. You'll see there are lots of outstanding PRs against this library, all waiting for me to find the time and energy to review and decide what to merge. The point of a PR is it puts the proposed changes into context.

GitHub makes it quite easy to do a pull request on a single file. Go to that files page (https://github.com/knolleary/pubsubclient/blob/master/src/PubSubClient.cpp), click the edit button in the top right and it'll walk you through.

@epicycler
Copy link

well that was interesting. I expected to see my pull request appear in the list of pull requests.

how do I delete epicycler/pubsubclient?

@xibriz
Copy link

xibriz commented Jan 14, 2018

Aslo having troubles on an ESP8266-12 with subscribe.

Are using the example code. Publish works fine, subscribe don't work. The code changes @epicycler suggested did not change anything in my setup.

@epicycler
Copy link

I started with the ESP8266 example. client.loop() is where pubsubclient reads incoming. While debugging I also published some messages with retain flag set.

@xibriz
Copy link

xibriz commented Jan 15, 2018

I have done some more testing. Seems like my problem was not related to this issue.

I have already build an ESP with this library where I set the clientId to a static string. When I reused that code in a new project I had two ESP with the same clientId.

Changing the clientId fixed my issues and the subscribe function is OK with the original library code base.

@vignesh-123
Copy link

Hello,
I am also having the callback problem.
My client is successfully connected.
My subscription is also returning true.
Only the callback function is not getting called

#include <PubSubClient.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
unsigned long myChannelNumber = 409468;
char mqttUserName[] = "MQTTtrial";
char mqttPass[] = " mqttpass
const char* server = "mqtt.thingspeak.com";
char readAPIKey[] = "read api key";
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 20L * 1000L;
static const char alphanum[] ="0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
PubSubClient mqttClient(client);

void setup()
{
Ethernet.begin(mac);
Serial.begin(9600);
mqttClient.setServer(server, 1883);
mqttClient.setCallback(callback);
delay(1500);
}

void loop()
{
if (!mqttClient.connected())
{
reconnect();
}
mqttClient.loop();
}

void reconnect()
{
char clientID[10];
while (!mqttClient.connected())
{
Serial.print("Attempting MQTT connection...");
for (int i = 0; i < 8; i++) {
clientID[i] = alphanum[random(51)];
delay(50);
}

// Connect to the MQTT broker
if (mqttClient.connect(clientID,mqttUserName,mqttPass))
{
Serial.print("Connected with Client");
int a = mqttSubscribe(myChannelNumber,1,readAPIKey);
Serial.print(" value :");
Serial.println(a);

} else
{
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
}

int mqttSubscribe(long subChannelID,int field,char* readKey){

String myTopic="channels/"+String(subChannelID)+"/subscribe/fields/field"+String(field)+"/"+String(readKey);
Serial.println("Subscribed to " +myTopic);
Serial.print("State= " + String(mqttClient.state()));
char charBuf[myTopic.length()+1];
myTopic.toCharArray(charBuf,myTopic.length()+1);
return mqttClient.subscribe(charBuf);
}

Anyone please help me out
Thank you so much.
BTW I was able to publish without any problems

@3dh-de
Copy link

3dh-de commented Mar 4, 2018

Please edit your last message and format your code! Nobody wants to read this messed up code. Thx!

@McMornan
Copy link

McMornan commented Apr 2, 2018

@vignesh: you have forgotten the delay in the loop() ... without that no wlan for esp8266 ..

@trentbrown13
Copy link

trentbrown13 commented Apr 3, 2018

The comment from sunnydevtale about adding a delay(100) after client.loop() worked for me. This same code used to work without the delay months ago, not sure why it does not now. This tcpdump shows the difference with and without the added delay

sudo tcpdump -i wlan0 port 1883 -Xvf | grep -i -B6 -A6 setSleepTime
without the delay

0x0020:  5019 7210 d63a 0000 3020 001c 7765 6d6f  P.r..:..0...wemo
        0x0030:  7354 6573 7443 6c69 656e 742f 7365 7453  sTestClient/setS
        0x0040:  6c65 6570 5469 6d65 3830 3020 001c 7765  leepTime800...we
        0x0050:  6d6f 7354 6573 7443 6c69 656e 742f 7365  mosTestClient/se
        0x0060:  7453 6c65 6570 5469 6d65 3930 3021 001c  tSleepTime900!..
        0x0070:  7765 6d6f 7354 6573 7443 6c69 656e 742f  wemosTestClient/
        0x0080:  7365 7453 6c65 6570 5469 6d65 3130 30    setSleepTime100
14:53:25.274139 IP (tos 0x0, ttl 128, id 35451, offset 0, flags [none], proto TCP (6), length 80)
    ESP_Living_Room.lan.11429 > MesaPi1.lan.1883: Flags [P.], cksum 0x15b2 (correct), seq 7280:7320, ack 21, win 4926, length 40
        0x0000:  4500 0050 8a7b 0000 8006 6564 c0a8 6489  E..P.{....ed..d.
        0x0010:  c0a8 64ee 2ca5 075b 17a4 9617 a158 4dcf  ..d.,..[.....XM.
        0x0020:  5018 133e 15b2 0000 3026 001e 2f4c 6976  P..>....0&../Liv
        0x0030:  696e 6750 6174 696f 436c 6965 6e74 2f74  ingPatioClient/t
--
        0x0020:  5019 7210 d63a 0000 3020 001c 7765 6d6f  P.r..:..0...wemo
        0x0030:  7354 6573 7443 6c69 656e 742f 7365 7453  sTestClient/setS
        0x0040:  6c65 6570 5469 6d65 3830 3020 001c 7765  leepTime800...we
        0x0050:  6d6f 7354 6573 7443 6c69 656e 742f 7365  mosTestClient/se
        0x0060:  7453 6c65 6570 5469 6d65 3930 3021 001c  tSleepTime900!..
        0x0070:  7765 6d6f 7354 6573 7443 6c69 656e 742f  wemosTestClient/
        0x0080:  7365 7453 6c65 6570 5469 6d65 3130 30    setSleepTime100
14:53:54.668319 IP (tos 0x0, ttl 64, id 46201, offset 0, flags [DF], proto TCP (6), length 40)
    MesaPi1.lan.1883 > 192.168.100.139.9831: Flags [F.], cksum 0xfe83 (correct), seq 113, ack 1488, win 29200, length 0
        0x0000:  4500 0028 b479 4000 4006 3b8c c0a8 64ee  E..(.y@.@.;...d.
        0x0010:  c0a8 648b 075b 2667 dc4c 2e0e 00a2 bbb5  ..d..[&g.L......
        0x0020:  5011 7210 fe83 0000                      P.r.....
14:53:54.901311 IP (tos 0x0, ttl 64, id 46202, offse

and with the delay

:34:12.401553 IP (tos 0x0, ttl 64, id 14086, offset 0, flags [DF], proto TCP (6), length 79)
    MesaPi1.lan.1883 > 192.168.100.139.2753: Flags [P.], cksum 0xe89b (correct), seq 44:83, ack 101, win 29200, length 39
        0x0000:  4500 004f 3706 4000 4006 b8d8 c0a8 64ee  E..O7.@[email protected].
        0x0010:  c0a8 648b 075b 0ac1 3dcf 1372 0001 6572  ..d..[..=..r..er
        0x0020:  5018 7210 e89b 0000 9003 0003 0031 2000  P.r..........1..
        0x0030:  1c77 656d 6f73 5465 7374 436c 6965 6e74  .wemosTestClient
        0x0040:  2f73 6574 536c 6565 7054 696d 6533 30    /setSleepTime30
19:34:12.428381 IP (tos 0x0, ttl 128, id 1035, offset 0, flags [none], proto TCP (6), length 77)
    192.168.100.139.2753 > MesaPi1.lan.1883: Flags [P.], cksum 0xc488 (correct), seq 101:138, ack 83, win 5758, length 37
        0x0000:  4500 004d 040b 0000 8006 ebd5 c0a8 648b  E..M..........d.
        0x0010:  c0a8 64ee 0ac1 075b 0001 6572 3dcf 1399  ..d....[..er=...
        0x0020:  5018 167e c488 0000 3023 001b 7765 6d6f  P..~....0#..wemo
        0x0030:  7354 6573 7443 6c69 656e 742f 7465 6d70  sTestClient/temp

Sorry, I should have added ...
Mosquito broker and Node-Red running on a RPI 3 B+
Wemos d1 mini using modem sleep on the same network as the RPI
Arduino IDE (win 10)

@tsknightstorm
Copy link

tsknightstorm commented Aug 20, 2018

This seems to be a common problem even after years. Also I have the problem, that I can connect and send MQTT messages without problems, even the subscribtion gives back true, but my callback function is not called at all. Last summer (a year ago), almoast the same code worked fine. I think the main thing I did this time is updating all libraries. After reading the full storry here and searching for the bug two full days, I would be very happy if someone can help me anf maybe also others with answer.

Hardware:

  • ESP8266 12-F
  • RPi 3 with Mosquitto
  • Win10 and AtmelStudio / visual Micro

My Code:
I added many delays for tests. Of corse, I do not let them all stay in my code. Also I post just the MQTT part. I splited the WiFi code and some logic things with buttons to other .cpp / .h files.

#define mqtt_waitTimeMs 100

WiFiClient wifiClient;
PubSubClient  mqttClient(wifiClient);

void mqtt_setup(void){
	delay(200);
	// Loop until we're reconnected
	while(! mqttClient.connected()){
		Serial.print("Setup MQTT \t| ");
		
		String mqttClientID = _generateUniqID();
		
		mqttClient.setServer(mqtt_server,mqtt_port);
		delay(100);
		mqttClient.setCallback(_mqtt_callback);
		delay(100);
		
		Serial.print("Attempting MQTT connection...");
		// Attempt to connect
		if( mqttClient.connect(mqttClientID.c_str(), mqtt_user, mqtt_password)){
			
			// subscribe to Topics
			_mqtt_subscribe();
			
			Serial.println("connected...rc=" + String(mqttClient.state()));
			
			if (mqttClient.publish("debug", mqttClientID.c_str())) { // comment for start
				Serial.println("MQTT Publish ok");
			}else {
				Serial.println("Publish failed!...");
			}
			Serial.println();
			
		}else{
			Serial.print("failed, rc=");
			Serial.print( mqttClient.state());
			Serial.println(" try again in 5 seconds");
			// Wait 5 seconds before retrying, blocking because nothing should run without connection
			delay(5000);
		}
	}
}


void mqtt_loop(void){
	static long oldTimeMs = millis();
	
	if( (millis()-oldTimeMs) > mqtt_waitTimeMs){
		mqtt_setup(); //to reconnect
		oldTimeMs = millis();
	}
	mqttQueue_DequeueAndSend();
}

void mqtt_publish(String topic, byte payload){
	mqttQueue_Enqueue(topic, payload);	
}

void mqtt_publish_directOut(char* topic, char* payload){
	if ( mqttClient.publish(topic, payload) ){
		Serial.println("OK");
	}else{
		Serial.println(" ERROR: Send MQTT fail........................");
	}
}

void _mqtt_callback(char* topic, byte* payload , unsigned int length){
	Serial.println("........Debug: MQTT callback");
	
	byte numPayload = _bytePToInt(payload, length);
	byte numInChannel;
	byte numOutChannel;
	
	if(_inTopicToChannel(topic, &numInChannel)){
		led_setValue(numInChannel, numPayload);
	}
	if(_outTopicToChannel(topic, &numOutChannel)){
		switch_setState(numOutChannel, numPayload);
	}
	
	Serial.println(	"Receive MQTT\t| topic: " + 
					String(topic) + 
					"\t| payload: " + 
					String(numPayload) + 
					"\t| channel: " +
					String(numInChannel)
	);
}

void _mqtt_subscribe(void){
	Serial.println("........Debug: MQTT Subscribe start");
	mqttClient.subscribe("debug");
	// subscribe to update LED's
	for(int i = 0; i < numberOfLed; i++){
		if(mqttClient.subscribe(mqtt_inTopicString[i].c_str())){
			Serial.println("........Debug: MQTT Subscribe true");
		}else{
			Serial.println("........Debug: MQTT Subscribe false");
		}
		delay(100);
	}
	
	// subscribe to update switch states
	for(int i = 0; i < numberOfSwitch; i++){
		//mqttClient.subscribe((char*)mqtt_outTopicString[i].c_str()); //////////// mqtt connection crashes with both arrays subscribed
		delay(100);
	}
	Serial.println("........Debug: MQTT Subscribe end");
}

@trentbrown13
Copy link

trentbrown13 commented Aug 20, 2018 via email

@tsknightstorm
Copy link

Thanks a lot! With your comment, you were able to bring me to solve my problem... It was too obviouse...

I never did call client.loop()....

At some point, I must have deleted this line by acident and while reading the comments in this forum, I was thinking that everyboadi is talking about the general loop not this particular function.

@trentbrown13
Copy link

trentbrown13 commented Aug 20, 2018 via email

@MrSteffenME
Copy link

haai all, I've tried everything. my code looks bad with all your parts in it

i cant subscribe
from Arduino --> Mosquitto --> Node-red --> MSSQL

Iam coding in VS Code, and everything is running on WIN10 servers
My publish is working fine

now,,, i know its the callback

when i run:
Serial.println(client.subscribe("SQLTest/test")); //which is a simple test switch, on or off

I only get 1`s

I can toggle the switch in Node-red ui, it updates in the DB but cant read
*also in the ui I have graphs which is subscribed to the newly published data from arduino

can someone please send a link with a working .ino
and what versions Libraries did you used

awe

The SQLTest/# is my publish
Temp and Humidity is my live Subscribes
The test button is right bottom corner
All node-red is from examples thats works

thank you in advance

@Noodleyman
Copy link

I too am facing issues with the Subscribe messages not being processed reliably, although the sending of data is working flawlessly.

I am using an Arduino Mega with an ESP8266 module connected via pins 18 and 19 with a baud rate of 115200. The serial interface for output of messages is set at a baud rate of 9600. Note: I've also tested the ESP8266 at 9600 and it provides the same results as outlined below. I am using a Raspberry Pi 2 with Node Red, which is also running Mosquitto MQTT. I am also using the latest build of the PubSubClient, version 2.7

I originally faced a problem with the connection dropping and showing the -4, MQTT_CONNECTION_TIMEOUT error. to solve this I changed the MQTT_VERSION to use 3_1 instead of 3_1_1. I had to add a delay of 1ms in the main loop(). This stopped the connection dropping every couple of minutes and remained stable in a 12 hour test.

I noted that if I spam the queue with requests, eventually the Arduino picks up the message. It's as if the Arduino is only listening/monitoring for 0.2 of a second out of every 1 second and unless the message hits the queue in that 0.2 window, it's not noticed and processed.

I've built, and replicated the issue with the sample code below, which is as bare bones as I could get it for the purpose of testing. The goal, just output "D" to the interface if it picks up a message. Note, that other devices on the same network ARE picking up these messages flawlessly, it's not a network issue or an issue with the broker.

I've tried all of the suggestions within this thread with one exception, but none have solved the problem. The exception being the comments from vignesh-123 in February. I wasn't able to find exact matches to the code referenced, so didn't make these changes.

I have also tried the same code on pins 2,3 using an Arduino Uno with the same results.

I'm currently out of ideas and couldn't find any additional recommendations to test. If anybody has any suggestions I would be grateful and willing to test them.

The ESP8266 module is currently drawing power from the onboard 3.3v pin of the Arduino, although I am going to hook this up to a power supply shortly in case the issue is caused by lack of power to the module. I didn't see others comment on their module setup, so not sure if perhaps this is a common theme between them. I'll follow up with the result of this test once it's completed.

Sample code: (masked usernames/passwords etc).

#include "WiFiEsp.h"
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include "SoftwareSerial.h"
#include <PubSubClient.h>

#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(18, 19); // RX, TX
#endif

/*#### MESSAGE QUEUE SETTINGS ####*/
#define mqtt_server                                   "xxx"
#define mqtt_port                                     1883
#define mqtt_client_id                                "PWR1"
#define mqtt_client_user                              "xxx"
#define mqtt_client_pw                                "xxx"
#define birthMsg                                      "CONNECTED"
#define willMsg                                       "DISCONNECTED"
#define mqtt_client_topic_power_status                "PowerManager/PwrController1"

/*#### POWER CONTROLLER SETTINGS ####*/
String  SensorName            =   "PWR1";
String  mqttSeperator         =   "/";

/*#### WIFI SETTINGS ####*/
char ssid[] = "xxx";                  // your network SSID (name)
char pass[] = "xxx";              // your network password
int status = WL_IDLE_STATUS;            // the Wifi radio's status

/*#### DEFAULT VALUES ####*/
char bla2[60];

// Setup the WIFI Client
WiFiEspClient espClient;

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println("D");
}

PubSubClient client(mqtt_server, mqtt_port, callback, espClient);

void setup()
{
  // initialize serial for debugging
  Serial.begin(9600);

  // initialize serial for ESP8266 module
  //Serial1.begin(9600);
  Serial1.begin(115200);

  // initialize ESP8266 module
  WiFi.init(&Serial1);

  // check for the presence of the ESP8266 module
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("ESP8266 module not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("Connected");

  // Comment this out for a LIVE build, it's only really needed in setup/testing
  //printWifiStatus();
}

void loop()
{
  if (!client.connected()) {
    Serial.println(client.state());
    reconnect();
  }
  client.loop();
  delay(1); // Needed to add a delay of 1ms otherwise the connection drops with -4 error. after adding this the connection is stable
}

void reconnect() {
  // Loop until we're reconnected

  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");

    //mqtt_client_topic_send_status
    String basePath = "ArduinoData" + mqttSeperator + SensorName + mqttSeperator;
    String thisSensor = basePath + "Status";
    thisSensor.toCharArray(bla2, thisSensor.length() + 1);

    // Attempt to connect, just a name to identify the client
    if (client.connect(mqtt_client_id, mqtt_client_user, mqtt_client_pw, bla2, 1, 1, willMsg)) {
      //Serial.println("connected");
      client.publish(bla2, birthMsg);
      Serial.println(client.subscribe("PowerManager/PwrController1/#"));
      client.loop();
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

@xibriz
Copy link

xibriz commented Dec 21, 2018

@Noodleyman Try changing the mqtt_client_id. If you have more than one ESP using the same mqtt_client_id subscription won't work. Add a random number at the end or something as the example code shows.

// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);

@Noodleyman
Copy link

@xibriz , thanks for the suggestion. I've just tested that out, but the behaviour remained the same. Here is the revised code to do as you suggested.

I did have another thought, two things to validate.

  1. Power to ESP8266, perhaps isn't enough and causing issues (will validate in a week or so when I get time to rebuild the circuit and feed the module it's own power.

  2. The software/firmware of the ESP8266 module itself may be a factor. For those who have this working reliably, what firmware are you using?

#include "WiFiEsp.h"
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include "SoftwareSerial.h"
#include <PubSubClient.h>

#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(18, 19); // RX, TX
#endif

/*#### MESSAGE QUEUE SETTINGS ####*/
#define mqtt_server                                   "xxx"
#define mqtt_port                                     1883
#define mqtt_client_id                                "PWR1"
#define mqtt_client_user                              "xxx"
#define mqtt_client_pw                                "xxx"
#define birthMsg                                      "CONNECTED"
#define willMsg                                       "DISCONNECTED"
#define mqtt_client_topic_power_status                "PowerManager/PwrController1"

/*#### POWER CONTROLLER SETTINGS ####*/
String  SensorName            =   "PWR1";
String  mqttSeperator         =   "/";

/*#### WIFI SETTINGS ####*/
char ssid[] = "xxx";                  // your network SSID (name)
char pass[] = "xxx";              // your network password
int status = WL_IDLE_STATUS;            // the Wifi radio's status

/*#### DEFAULT VALUES ####*/
char bla2[60];
char bla3[60];

// Setup the WIFI Client
WiFiEspClient espClient;

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println("D");
}

PubSubClient client(mqtt_server, mqtt_port, callback, espClient);

void setup()
{
  // initialize serial for debugging
  Serial.begin(9600);

  // initialize serial for ESP8266 module
  Serial1.begin(9600);
  //Serial1.begin(115200);

  // initialize ESP8266 module
  WiFi.init(&Serial1);

  // check for the presence of the ESP8266 module
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("ESP8266 module not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("Connected");

  // Comment this out for a LIVE build, it's only really needed in setup/testing
  //printWifiStatus();
}

void loop()
{
  if (!client.connected()) {
    Serial.println(client.state());
    reconnect();
  }
  client.loop();
  delay(1); // Needed to add a delay of 1ms otherwise the connection drops with -4 error. after adding this the connection is stable
}

void reconnect() {
  // Loop until we're reconnected

  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");

    //mqtt_client_topic_send_status
    String basePath = "ArduinoData" + mqttSeperator + SensorName + mqttSeperator;
    String thisSensor = basePath + "Status";
    thisSensor.toCharArray(bla2, thisSensor.length() + 1);

    // Attempt to connect, just a name to identify the client
    //if (client.connect(mqtt_client_id, mqtt_client_user, mqtt_client_pw, bla2, 1, 1, willMsg)) {

      // Create a random client ID
      String clientId = "ESP8266Client-";
      clientId += String(random(0xffff), HEX);    
      clientId.toCharArray(bla3, clientId.length() + 1);
    if (client.connect(bla3, mqtt_client_user, mqtt_client_pw, bla2, 1, 1, willMsg)) {      
      //Serial.println("connected");
      client.publish(bla2, birthMsg);
      Serial.println(client.subscribe("PowerManager/PwrController1/#"));
      client.loop();
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

@pmarcomini
Copy link

Thank you so much xibriz!
After loosing a couple hours dealing with this problem, when trying to connect to my local MQTT broker, I could discover my error thanks to your post.
It happend that was I using same client ID of another Wemos D1 that was already connected to this broker.
That made impossible to receive subscriptions from server (eventhough, I could publish any topic on it without any problem)
My solution was to use a similar code than the one you suggested but using client MAC Address instead, to avoid any further error (even copying the code to another sketch) and making it easy to trace the device communication within mosquitto mqtt broker log.
Great!

@Patave
Copy link

Patave commented Feb 10, 2021

Thanks @pmarcomini
Same error, I had reused an existing code from another project with the same Id.

@bangkokguy
Copy link

I am very new to ESP8266 and MQTT. So Apologies if I haven't done it properly. I am using test.mosquitto.org server and trying to publish and subscribe the data. I am able to publish the data but subscribe part is not working. Nevertheless, I have tested subscribe part between two mobile and I am able to receive the data but I am not getting any data in the callback routine though I subscribed explicitly in the code. I have attached my code for your reference.

I found a buffer overrun in pubsubclient.cpp. After changing the buffer size with
client.setBufferSize(<desired buffer size>);
runs everything flawlessly.

@gazzat5
Copy link

gazzat5 commented Mar 15, 2021

I just wanted to add that I had the same issue subscribing to a retained topic where i was only calling the client.loop() a few times in setup() due to running off battery and switching to deepsleep. It turned out that the client.loop has to run between 100-200 times before the callback is triggered, even on retained messages.

The solution in my case was to create a for loop that ran the client.loop() upto 200 times (with no delay required) to ensure the callback fires.

Seems odd to me that it requires this many loops, but it works!

@xdien
Copy link

xdien commented Mar 6, 2024

I am very new to ESP8266 and MQTT. So Apologies if I haven't done it properly. I am using test.mosquitto.org server and trying to publish and subscribe the data. I am able to publish the data but subscribe part is not working. Nevertheless, I have tested subscribe part between two mobile and I am able to receive the data but I am not getting any data in the callback routine though I subscribed explicitly in the code. I have attached my code for your reference.

I found a buffer overrun in pubsubclient.cpp. After changing the buffer size with client.setBufferSize(<desired buffer size>); runs everything flawlessly.

Thank you my problem was resolved after increasing buffer size to 4096.

@walterunt
Copy link

walterunt commented Apr 8, 2024

I am very new to ESP8266 and MQTT. So Apologies if I haven't done it properly. I am using test.mosquitto.org server and trying to publish and subscribe the data. I am able to publish the data but subscribe part is not working. Nevertheless, I have tested subscribe part between two mobile and I am able to receive the data but I am not getting any data in the callback routine though I subscribed explicitly in the code. I have attached my code for your reference.

I found a buffer overrun in pubsubclient.cpp. After changing the buffer size with client.setBufferSize(<desired buffer size>); runs everything flawlessly.

Thanks! This worked for me. The incoming message from Thingspeak was > 256 (MQTT_MAX_PACKET_SIZE) for me, since I have subscribed to the full channel, and not to one single field of this channel.

I.e. Subscribing to the full channel with topic:
channels/33301/subscribe
returns the data:
{"channel_id":2500770,"created_at":"2024-04-08T18:08:43Z","entry_id":137,"field1":null,"field2":null,"field3":null,"field4":null,"field5":null,"field6":null,"field7":null,"field8":"\"modes22\"","latitude":null,"longitude":null,"elevation":null,"status":null} which is most likely above the default packet size limit.

Subscribing to only one field with topic:
channels/33301/subscribe/fields/field1
worked also with the default packet size limit.

@christianruppert
Copy link

Just to repeat a solution which worked for me (its around 1/3 of the thread) - super simple, might not occur in real projects but if you try a demo solution (Mega with ESP) and have nothing in loop beside client.loop() - try adding delay of 100 or 300. Then callbacks started working for me.

@jibrilsharafi
Copy link

Just adding a my bit: the subscribe() method was not working as the client did not have the permissions to do so (I am using AWS IoT Core).

I ended up using this policy for testing purposes:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Connect",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Publish",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Subscribe",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Receive",
      "Resource": "*"
    }
  ]
}

and it immediately worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests