From 74b0849051b597a2902c8f2680b67bfd41b8c6bb Mon Sep 17 00:00:00 2001 From: "valerio\\new" Date: Sat, 28 Sep 2019 11:32:39 +0200 Subject: [PATCH 1/3] Add files via upload --- .../WiFiSimpleSenderReconnect.ino | 138 ++++++++++++++++++ .../arduino_secrets.h | 4 + .../WiFiSimpleSenderReconnect/sketch.json | 1 + 3 files changed, 143 insertions(+) create mode 100644 examples/WiFiSimpleSenderReconnect/WiFiSimpleSenderReconnect.ino create mode 100644 examples/WiFiSimpleSenderReconnect/arduino_secrets.h create mode 100644 examples/WiFiSimpleSenderReconnect/sketch.json diff --git a/examples/WiFiSimpleSenderReconnect/WiFiSimpleSenderReconnect.ino b/examples/WiFiSimpleSenderReconnect/WiFiSimpleSenderReconnect.ino new file mode 100644 index 0000000..880df95 --- /dev/null +++ b/examples/WiFiSimpleSenderReconnect/WiFiSimpleSenderReconnect.ino @@ -0,0 +1,138 @@ +#include "arduino_secrets.h" +/* + ArduinoMqttClient - WiFi Reconnect + + This example connects to a MQTT broker and publishes a message to + a topic once a second. If the client is disconnected from the broker, + it automatically tries to reconnect. + + + The circuit: + - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board + + + This example code is in the public domain. +*/ + +#include +#include // for MKR1000 change to: #include + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) + +// To connect with SSL/TLS: +// 1) Change WiFiClient to WiFiSSLClient. +// 2) Change port value from 1883 to 8883. +// 3) Change broker value to a server with a known SSL/TLS root certificate +// flashed in the WiFi module. + +WiFiClient wifiClient; +MqttClient mqttClient(wifiClient); + +const char broker[] = "test.mosquitto.org"; +int port = 1883; +const char topic[] = "arduino/simple"; + +const long interval = 1000; +unsigned long previousMillis = 0; + +int count = 0; + +int attemptReconnect(){ + if (!mqttClient.connect(broker, port)) { + Serial.print("MQTT connection failed! Error code = "); + Serial.println(mqttClient.connectError()); + + } + return mqttClient.connectError(); // return status +} + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // attempt to connect to Wifi network: + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + + while (WiFi.begin(ssid, pass) != WL_CONNECTED) { + // failed, retry + Serial.print("."); + + delay(5000); + } + + + 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("14"); + + // You can provide a username and password for authentication + // mqttClient.setUsernamePassword(SECRET_MQTT_USER, SECRET_MQTT_PW); + + + 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(); + +} + +void loop() { + // 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) { + previousMillis = currentMillis; + + mqttClient.poll(); // poll to avoid beeing disconnected + + + Serial.print("Sending message to topic: "); + Serial.println(topic); + Serial.print("hello "); + Serial.println(count); + + // send message, the Print interface can be used to set the message contents + mqttClient.beginMessage(topic); + mqttClient.print("hello "); + mqttClient.print(count); + mqttClient.endMessage(); + + Serial.println(); + + count++; + + if(!mqttClient.connected()){ // if the client has been disconnected, + Serial.println("Client disconnected, attempting reconnection"); + Serial.println(); + + if(!attemptReconnect()){ // try reconnecting + Serial.print("Client reconnected!"); + Serial.println(); + + } + } + } +} \ No newline at end of file diff --git a/examples/WiFiSimpleSenderReconnect/arduino_secrets.h b/examples/WiFiSimpleSenderReconnect/arduino_secrets.h new file mode 100644 index 0000000..39f89f4 --- /dev/null +++ b/examples/WiFiSimpleSenderReconnect/arduino_secrets.h @@ -0,0 +1,4 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" +#define SECRET_MQTT_USER "" +#define SECRET_MQTT_PW "" diff --git a/examples/WiFiSimpleSenderReconnect/sketch.json b/examples/WiFiSimpleSenderReconnect/sketch.json new file mode 100644 index 0000000..294175b --- /dev/null +++ b/examples/WiFiSimpleSenderReconnect/sketch.json @@ -0,0 +1 @@ +{"cpu":{"name":"Arduino MKR WiFi 1010","com_name":"COM23","fqbn":"arduino:samd:mkrwifi1010","flavour":"default"},"secrets":[{"name":"SECRET_SSID","value":"","isOptional":false},{"name":"SECRET_PASS","value":"","isOptional":false},{"name":"SECRET_MQTT_USER","value":"","isOptional":true},{"name":"SECRET_MQTT_PW","value":"","isOptional":true}]} From 52369e7734b919389176fcf73cf53622f7fba8d3 Mon Sep 17 00:00:00 2001 From: "valerio\\new" Date: Sat, 28 Sep 2019 11:33:12 +0200 Subject: [PATCH 2/3] removed redundant include --- .../WiFiSimpleSenderReconnect/WiFiSimpleSenderReconnect.ino | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/WiFiSimpleSenderReconnect/WiFiSimpleSenderReconnect.ino b/examples/WiFiSimpleSenderReconnect/WiFiSimpleSenderReconnect.ino index 880df95..dfc4d12 100644 --- a/examples/WiFiSimpleSenderReconnect/WiFiSimpleSenderReconnect.ino +++ b/examples/WiFiSimpleSenderReconnect/WiFiSimpleSenderReconnect.ino @@ -1,4 +1,3 @@ -#include "arduino_secrets.h" /* ArduinoMqttClient - WiFi Reconnect @@ -135,4 +134,4 @@ void loop() { } } } -} \ No newline at end of file +} From d3849f88e87339b936d4fa86488b4655c9cce3b5 Mon Sep 17 00:00:00 2001 From: "valerio\\new" Date: Sat, 28 Sep 2019 11:36:36 +0200 Subject: [PATCH 3/3] Update WiFiSimpleSender.ino --- examples/WiFiSimpleSender/WiFiSimpleSender.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/WiFiSimpleSender/WiFiSimpleSender.ino b/examples/WiFiSimpleSender/WiFiSimpleSender.ino index 8128dd8..6e5cfee 100644 --- a/examples/WiFiSimpleSender/WiFiSimpleSender.ino +++ b/examples/WiFiSimpleSender/WiFiSimpleSender.ino @@ -77,10 +77,6 @@ void setup() { } void loop() { - // call poll() regularly to allow the library to send MQTT keep alives which - // avoids being disconnected by the broker - mqttClient.poll(); - // 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(); @@ -88,6 +84,10 @@ void loop() { if (currentMillis - previousMillis >= interval) { // save the last time a message was sent previousMillis = currentMillis; + + // call poll() regularly to allow the library to send MQTT keep alives which + // avoids being disconnected by the broker + mqttClient.poll(); Serial.print("Sending message to topic: "); Serial.println(topic);