Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] Should my MQTT be dropping after every publish?

Hi

I am trying to use Paho embedded on a single-thread NodeMCU v1.0 (no OS). Code below (with credentials elided) which is basically a copy of the code that I found on this list.

In the loop() function, connect() is getting called every single time through the loop. Is this normal/expected behaviour? Am I doing something wrong here? I am having significant problems getting paho embedded to work - there seem to be multiple issues. From what I have come across online, a number of people are saying that the ping exchange and QoS2 are broken on the NodeMCU?

Initially, things work fine and the messages get through to a mosquitto broker running on a linux box. But after 14-20 (varies on how many print statements  include) passes  through loop, the program stops reconnecting and continually timesout thereafter.

Any advice on what to try?

Peter




#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <IPStack.h>
#include <Countdown.h>
#include <MQTTClient.h>

//*****************************************************************************

//Wireless access configuration
char* wifiSSID = "*****";
char* wifiPassword = "*****";

//*****************************************************************************

//MQTT configuration
IPAddress mqtt_serverIP(192, 168, 0, 1);
char* mqtt_user = "*****";
char* mqtt_password = "*****";
char* controllerTopic = "obms/controller";
char* inboundTopic = "obms/room1/trv";
int port = 1883;

//*****************************************************************************

void messageArrived(MQTT::MessageData& md)
// Handle incoming message
	{
	MQTT::Message& message = md.message;

	Serial.print("Message arrived: qos ");
	Serial.print(message.qos);
	Serial.print(", retained ");
	Serial.print(message.retained);
	Serial.print(", dup ");
	Serial.print(message.dup);
	Serial.print(", packetid ");
	Serial.println(message.id);
	Serial.print("Payload ");
	Serial.println((char*)message.payload);

	return;
	} // messageArrived()

//*****************************************************************************

WiFiClient wifiClient;
IPStack ipstack(wifiClient);
const int MaxMQTTpacketSize = 50;
const int MaxMessageHandlers = 1;
MQTT::Client<IPStack, Countdown, MaxMQTTpacketSize, MaxMessageHandlers> client
	= MQTT::Client<IPStack, Countdown, MaxMQTTpacketSize, MaxMessageHandlers>(ipstack);

//*****************************************************************************

void connect()
// Connect to MQTT broker
	{
	Serial.print("Connecting to ");
	Serial.print(mqtt_serverIP);
	Serial.print(":");
	Serial.println(port);

	int rc = ipstack.connect(mqtt_serverIP, port);
	if(rc != 1)
		{
		Serial.print("rc from TCP connect is ");
		Serial.println(rc);
		}

	Serial.println("MQTT connecting");

	MQTTPacket_connectData data = ""
	data.MQTTVersion = 3;
	data.clientID.cstring = (char*)"room1-trv";
	data.keepAliveInterval = 60;
	data.cleansession = 0;
	data.username.cstring = mqtt_user;
	data.password.cstring = mqtt_password;

	rc = client.connect(data);
	if(rc != 0)
		{
		Serial.print("rc from MQTT connect is ");
		Serial.println(rc);
		}
	else
		{
		Serial.print("connect failed with rc = ");
		Serial.println(rc);
		}

	Serial.println("MQTT connected");

	rc = client.subscribe(inboundTopic, MQTT::QOS2, messageArrived);
	if(rc != 0)
		{
		Serial.print("rc from MQTT subscribe is ");
		Serial.println(rc);
		}
	Serial.println("MQTT subscribed");

	return;
	} // connect()

//*****************************************************************************

void setup_wifi()
// Connect to wifi network
	{
	delay(10);	// Hack that seems to be required!

	Serial.println();
	Serial.print("Connecting to ");
	Serial.println(wifiSSID);

	WiFi.begin(wifiSSID, wifiPassword);

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

	Serial.println("");
	Serial.print("WiFi connected to ");
	Serial.print("IP address: ");
	Serial.println(WiFi.localIP());

	return;
	} // setup_wifi()

//*****************************************************************************

int noPublishedMessages;

void setup()
// Run-once setup
	{
	Serial.begin(9600);
	setup_wifi();
	connect();

	noPublishedMessages = 0;

	return;
	} // setup()

//*****************************************************************************

void loop()
// Infinite loop
	{
	if(!client.isConnected())
		{
		connect();
		}

	// Send message
	char buffer[28];
	strcpy(buffer, "Hello World! QoS 0 message");
	int rc = client.publish(controllerTopic, (void*)buffer, strlen(buffer) + 1, MQTT::QOS0,	false);

	Serial.print("Published message ");
	Serial.print(++noPublishedMessages);
	Serial.print(" to ");
	Serial.print(controllerTopic);
	Serial.print("  return = ");
	Serial.println(rc);

	Serial.println();
	Serial.println();

	// Enable receiving messages
	client.yield(1000L);

	delay(2000);
	} // loop()

//*****************************************************************************


Back to the top