1
+ #include " arduino_secrets.h"
2
+ /*
3
+ ArduinoMqttClient - WiFi Reconnect
4
+
5
+ This example connects to a MQTT broker and publishes a message to
6
+ a topic once a second. If the client is disconnected from the broker,
7
+ it automatically tries to reconnect.
8
+
9
+
10
+ The circuit:
11
+ - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
12
+
13
+
14
+ This example code is in the public domain.
15
+ */
16
+
17
+ #include < ArduinoMqttClient.h>
18
+ #include < WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>
19
+
20
+ #include " arduino_secrets.h"
21
+ // /////please enter your sensitive data in the Secret tab/arduino_secrets.h
22
+ char ssid[] = SECRET_SSID; // your network SSID (name)
23
+ char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
24
+
25
+ // To connect with SSL/TLS:
26
+ // 1) Change WiFiClient to WiFiSSLClient.
27
+ // 2) Change port value from 1883 to 8883.
28
+ // 3) Change broker value to a server with a known SSL/TLS root certificate
29
+ // flashed in the WiFi module.
30
+
31
+ WiFiClient wifiClient;
32
+ MqttClient mqttClient (wifiClient);
33
+
34
+ const char broker[] = " test.mosquitto.org" ;
35
+ int port = 1883 ;
36
+ const char topic[] = " arduino/simple" ;
37
+
38
+ const long interval = 1000 ;
39
+ unsigned long previousMillis = 0 ;
40
+
41
+ int count = 0 ;
42
+
43
+ int attemptReconnect (){
44
+ if (!mqttClient.connect (broker, port)) {
45
+ Serial.print (" MQTT connection failed! Error code = " );
46
+ Serial.println (mqttClient.connectError ());
47
+
48
+ }
49
+ return mqttClient.connectError (); // return status
50
+ }
51
+
52
+ void setup () {
53
+ // Initialize serial and wait for port to open:
54
+ Serial.begin (9600 );
55
+
56
+ while (!Serial) {
57
+ ; // wait for serial port to connect. Needed for native USB port only
58
+ }
59
+
60
+ // attempt to connect to Wifi network:
61
+ Serial.print (" Attempting to connect to WPA SSID: " );
62
+ Serial.println (ssid);
63
+
64
+ while (WiFi.begin (ssid, pass) != WL_CONNECTED) {
65
+ // failed, retry
66
+ Serial.print (" ." );
67
+
68
+ delay (5000 );
69
+ }
70
+
71
+
72
+ Serial.println (" You're connected to the network" );
73
+ Serial.println ();
74
+
75
+
76
+ // You can provide a unique client ID, if not set the library uses Arduino-millis()
77
+ // Each client must have a unique client ID
78
+ // mqttClient.setId("14");
79
+
80
+ // You can provide a username and password for authentication
81
+ // mqttClient.setUsernamePassword(SECRET_MQTT_USER, SECRET_MQTT_PW);
82
+
83
+
84
+ Serial.print (" Attempting to connect to the MQTT broker: " );
85
+ Serial.println (broker);
86
+
87
+
88
+ if (!mqttClient.connect (broker, port)) {
89
+ Serial.print (" MQTT connection failed! Error code = " );
90
+ Serial.println (mqttClient.connectError ());
91
+
92
+ while (1 );
93
+ }
94
+
95
+
96
+ Serial.println (" You're connected to the MQTT broker!" );
97
+ Serial.println ();
98
+
99
+ }
100
+
101
+ void loop () {
102
+ // avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
103
+ // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
104
+ unsigned long currentMillis = millis ();
105
+
106
+ if (currentMillis - previousMillis >= interval) {
107
+ previousMillis = currentMillis;
108
+
109
+ mqttClient.poll (); // poll to avoid beeing disconnected
110
+
111
+
112
+ Serial.print (" Sending message to topic: " );
113
+ Serial.println (topic);
114
+ Serial.print (" hello " );
115
+ Serial.println (count);
116
+
117
+ // send message, the Print interface can be used to set the message contents
118
+ mqttClient.beginMessage (topic);
119
+ mqttClient.print (" hello " );
120
+ mqttClient.print (count);
121
+ mqttClient.endMessage ();
122
+
123
+ Serial.println ();
124
+
125
+ count++;
126
+
127
+ if (!mqttClient.connected ()){ // if the client has been disconnected,
128
+ Serial.println (" Client disconnected, attempting reconnection" );
129
+ Serial.println ();
130
+
131
+ if (!attemptReconnect ()){ // try reconnecting
132
+ Serial.print (" Client reconnected!" );
133
+ Serial.println ();
134
+
135
+ }
136
+ }
137
+ }
138
+ }
0 commit comments