Comments (3)
Hi @fabltd 👋
On the very basic level you could try something like this
void loop()
{
/* ... */
if (!mqttClient.connected()) {
Serial.println("MQTT connection lost");
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT reconnection error ");
Serial.println(mqttClient.connectError());
}
}
}
from arduinomqttclient.
@aentinger This was very helpful. I had searched for a while before I found this. I built out this in my main loop to make sure that I'm always connected. It will block the main loop if it can't connect but in my case if I can't connect to the MQTT broker then the rest of my code is not useful anyway as the sensor data has no where to go.
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
if (!mqttClient.connected()) {
mySerial.println("MQTT Not Connected");
while (!mqttClient.connect(broker, port)) {
mySerial.print("MQTT connection failed! Error code = ");
mySerial.println(mqttClient.connectError());
delay (10000);
}
mySerial.println("You're connected to the MQTT broker!");
mySerial.println();
mqttClient.onMessage(onMqttMessage);
mySerial.println("Getting Inital Subscriptions");
mqttClient.subscribe(commandUpdate);
mqttClient.subscribe(commandStartOffset);
mqttClient.subscribe(commandHouseOffset);
delay(1000);
}
from arduinomqttclient.
I'm glad to hear this was of help to you @jason-a-alexander 😉
I'm closing this issue, since the OP has disappeared.
from arduinomqttclient.
Related Issues (20)
- Return values of `MqttClient::subscribe` not documented HOT 4
- Unit of mqttClient.setKeepAliveInterval is undocumented HOT 1
- docs?
- Does not compile on ESP8266 when calling connect() with port HOT 3
- wifiecho example error -2 on uno r4 HOT 2
- `-Wvla` compiler warnings HOT 1
- [Question] TLS PSK Authentication HOT 1
- SSL/TLS with Aduino UNO Wifi REV2 HOT 1
- This MQTT library needs IPv6 support
- can not set setKeepAliveInterval HOT 1
- MQTT not connecting to AWS IOT with MKR NB 15000 HOT 3
- Doesn't connect to ThingsBoard HOT 1
- Giga R1 WiFi works with a small change HOT 3
- Failure to connect to 127.0.0.1 (localhost) using Mosquitto Broker HOT 1
- Slow and then non responsive mqtt.read
- json string is "damaged" on transmitting HOT 2
- How to send MQTT server's username and password HOT 2
- mqttClient.connected() always 0 despite being connected HOT 3
- Add Uno R4 WiFi support to examples HOT 1
- Linker error undefined reference to .. if I use the MQTT_CLIENT_STD_FUNCTION_CALLBACK HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduinomqttclient.