0

I am wondering if there is a way to check and see if the MQTT connection is still active.

The reason for this is that I am having trouble with the connection dropping after a short while and not seeming to reconnect.

/*
 ArduinoMqttClient - WiFi Simple Sender
 This example connects to a MQTT broker and publishes a message to
 a topic once a second.
 The circuit:
 - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board
 This example code is in the public domain.
*/
#include <ArduinoMqttClient.h>
#include <MKRGSM.h>
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// APN data
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
GSMClient client;
GPRS gprs;
GSM gsmAccess;
MqttClient mqttClient(client);
const char broker[] = "*****";
int port = 1883;
const char topic[] = "Tempdata";
const long interval = 500000;
unsigned long previousMillis = 0;
float SERIESRESISTOR = 9.87;
float TEMPERATURENOMINAL = 25.0;
float BCOEFFICIENT = 3540.0;
float thermistor = 0.0;
float THERMISTORNOMINAL = 10;
int count = 0;
void setup() {
 // Initialize serial and wait for port to open:
 Serial.begin(9600);
 Serial.println("Starting Arduino web client.");
 // connection state
 bool connected = false;
 // After starting the modem with GSM.begin()
 // attach the shield to the GPRS network with the APN, login and password
 while (!connected) {
 if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
 connected = true;
 } else {
 Serial.println("Not connected");
 delay(1000);
 }
 }
 Serial.println("You're connected to the network");
 Serial.println();
 // You can provide a unique client ID, if not set the library uses Arduino-millis()
 // Each client must have a unique client ID
 mqttClient.setId("pump-house-gsm");
 // You can provide a username and password for authentication
 //mqttClient.setUsernamePassword("****", "****");
 Serial.print("Attempting to connect to the MQTT broker: ");
 Serial.println(broker);
 if (!mqttClient.connect(broker, port)) {
 Serial.print("MQTT connection failed! Error code = ");
 Serial.println(mqttClient.connectError());
 while (1);
 }
 Serial.println("You're connected to the MQTT broker!");
 Serial.println();
 // set analog resulotion to 12
 analogReadResolution(12);
}
void loop() {
 // call poll() regularly to allow the library to send MQTT keep alives which
 // avoids being disconnected by the broker
 mqttClient.poll();
 // to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
 // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
 unsigned long currentMillis = millis();
 if (currentMillis - previousMillis >= interval) {
 /*Read analog outputof NTC module,
 i.e the voltage across the thermistor */
 float average = ((float)analogRead(A0) / 4095.0) * 3.3;
 average = (average * SERIESRESISTOR) / (3.3 - average);
 float steinhart;
 steinhart = average / THERMISTORNOMINAL; // (R/Ro)
 steinhart = log(steinhart); // ln(R/Ro)
 steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
 steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
 steinhart = 1.0 / steinhart; // Invert
 steinhart -= 273.15; // convert absolute temp to C
 // save the last time a message was sent
 previousMillis = currentMillis;
 // Keep track of times a message was sent
 // this is to reconnect every hour.
 count = count + 1;
 // send message, the Print interface can be used to set the message contents
 mqttClient.beginMessage(topic);
 mqttClient.print("The temperature is ");
 mqttClient.print(steinhart);
 mqttClient.endMessage();
 }
}

Maybe endMessage, beginMessage, or poll has a return that says it has failed to send a message?

I've looked at endMessage and beginMessage and it does not seem like it to me, but poll is much more complicated and I can't really tell.

ocrdu
1,7953 gold badges12 silver badges24 bronze badges
asked Dec 29, 2023 at 2:04

1 Answer 1

1

I found the solution with the connected function included in the ArduinoMqttClient (found it browsing the library code).

Here is a code snippet showing what I added:

...
...
if (currentMillis - previousMillis >= interval) {
 if (!mqttClient.connected()) {
 if (mqttClient.connect(broker, port)) {
 Serial.println("reconnected");
 }
 }
 /*Read analog outputof NTC module,
 i.e the voltage across the thermistor */
 float average = ((float)analogRead(A0) / 4095.0) * 3.3;
...
...

It is working so far!

ocrdu
1,7953 gold badges12 silver badges24 bronze badges
answered Dec 29, 2023 at 23:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.