Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a19d994

Browse files
committedJan 4, 2019
Initial release
0 parents  commit a19d994

18 files changed

+2160
-0
lines changed
 

‎README.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
= ArduinoMqttClient Library for Arduino =
2+
3+
4+
image:https://travis-ci.org/arduino-libraries/ArduinoMqttClient.svg?branch=master["Build Status", link="https://travis-ci.org/arduino-libraries/ArduinoMqttClient"]
5+
6+
Allows you to send and receive MQTT messages using Arduino.
7+
8+
== License ==
9+
10+
Copyright (c) 2019 Arduino SA. All rights reserved.
11+
12+
This library is free software; you can redistribute it and/or
13+
modify it under the terms of the GNU Lesser General Public
14+
License as published by the Free Software Foundation; either
15+
version 2.1 of the License, or (at your option) any later version.
16+
17+
This library is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20+
Lesser General Public License for more details.
21+
22+
You should have received a copy of the GNU Lesser General Public
23+
License along with this library; if not, write to the Free Software
24+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
ArduinoMqttClient - WiFi Advanced Callback
3+
4+
This example connects to a MQTT broker and subscribes to a single topic,
5+
it also publishes a message to another topic every 10 seconds.
6+
When a message is received it prints the message to the serial monitor,
7+
it uses the callback functionality of the library.
8+
9+
It also demonstrates how to set the will message, get/set QoS,
10+
duplicate and retain values of messages.
11+
12+
The circuit:
13+
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
14+
15+
This example code is in the public domain.
16+
*/
17+
18+
#include <ArduinoMqttClient.h>
19+
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>
20+
21+
#include "arduino_secrets.h"
22+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
23+
char ssid[] = SECRET_SSID; // your network SSID (name)
24+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
25+
26+
WiFiClient wifiClient;
27+
MqttClient mqttClient(wifiClient);
28+
29+
const char broker[] = "test.mosquitto.org";
30+
const char willTopic[] = "arduino/will";
31+
const char inTopic[] = "arduino/in";
32+
const char outTopic[] = "arduino/out";
33+
34+
const long interval = 10000;
35+
unsigned long previousMillis = 0;
36+
37+
int count = 0;
38+
39+
void setup() {
40+
//Initialize serial and wait for port to open:
41+
Serial.begin(9600);
42+
while (!Serial) {
43+
; // wait for serial port to connect. Needed for native USB port only
44+
}
45+
46+
// attempt to connect to Wifi network:
47+
Serial.print("Attempting to connect to WPA SSID: ");
48+
Serial.println(ssid);
49+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
50+
// failed, retry
51+
Serial.print(".");
52+
delay(5000);
53+
}
54+
55+
Serial.println("You're connected to the network");
56+
Serial.println();
57+
58+
// You can provide a unique client ID, if not set the library uses Arduin-millis()
59+
// Each client must have a unique client ID
60+
// mqttClient.setId("clientId");
61+
62+
// You can provide a username and password for authentication
63+
// mqttClient.setUsernamePassword("username", "password");
64+
65+
// set a will message, used by the broker when the connection dies unexpectantly
66+
// you must know the size of the message before hand, and it must be set before connecting
67+
String willPayload = "oh no!";
68+
bool willRetain = true;
69+
int willQos = 1;
70+
71+
mqttClient.beginWill(willTopic, willPayload.length(), willRetain, willQos);
72+
mqttClient.print(willPayload);
73+
mqttClient.endWill();
74+
75+
Serial.print("Attempting to connect to the MQTT broker: ");
76+
Serial.println(broker);
77+
78+
if (!mqttClient.connect(broker, 1883)) {
79+
Serial.print("MQTT connection failed! Error code = ");
80+
Serial.println(mqttClient.connectError());
81+
82+
while (1);
83+
}
84+
85+
Serial.println("You're connected to the MQTT broker!");
86+
Serial.println();
87+
88+
// set the message receive callback
89+
mqttClient.onMessage(onMqttMessage);
90+
91+
Serial.print("Subscribing to topic: ");
92+
Serial.println(inTopic);
93+
Serial.println();
94+
95+
// subscribe to a topic
96+
// the second paramter set's the QoS of the subscription,
97+
// the the library supports subscribing at QoS 0, 1, or 2
98+
int subscribeQos = 1;
99+
100+
mqttClient.subscribe(inTopic, subscribeQos);
101+
102+
// topics can be unsubscribed using:
103+
// mqttClient.unsubscribe(inTopic);
104+
105+
Serial.print("Waiting for messages on topic: ");
106+
Serial.println(inTopic);
107+
Serial.println();
108+
}
109+
110+
void loop() {
111+
// call poll() regularly to allow the library to receive MQTT messages and
112+
// send MQTT keep alives which avoids being disconnected by the broker
113+
mqttClient.poll();
114+
115+
// avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
116+
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
117+
unsigned long currentMillis = millis();
118+
119+
if (currentMillis - previousMillis >= interval) {
120+
// save the last time a message was sent
121+
previousMillis = currentMillis;
122+
123+
String payload;
124+
125+
payload += "hello world!";
126+
payload += " ";
127+
payload += count;
128+
129+
Serial.print("Sending message to topic: ");
130+
Serial.println(outTopic);
131+
Serial.println(payload);
132+
133+
// send message, the Print interface can be used to set the message contents
134+
// in this case we know the size ahead of time, so the message payload can be streamed
135+
136+
bool retained = false;
137+
int qos = 1;
138+
bool dup = false;
139+
140+
mqttClient.beginMessage(outTopic, payload.length(), retained, qos, dup);
141+
mqttClient.print(payload);
142+
mqttClient.endMessage();
143+
144+
Serial.println();
145+
146+
count++;
147+
}
148+
}
149+
150+
void onMqttMessage(int messageSize) {
151+
// we received a message, print out the topic and contents
152+
Serial.print("Received a message with topic '");
153+
Serial.print(mqttClient.messageTopic());
154+
Serial.print("', duplicate = ");
155+
Serial.print(mqttClient.messageDup() ? "true" : "false");
156+
Serial.print(", QoS = ");
157+
Serial.print(mqttClient.messageQoS());
158+
Serial.print(", retained = ");
159+
Serial.print(mqttClient.messageRetain() ? "true" : "false");
160+
Serial.print("', length ");
161+
Serial.print(messageSize);
162+
Serial.println(" bytes:");
163+
164+
// use the Stream interface to print the contents
165+
while (mqttClient.available()) {
166+
Serial.print((char)mqttClient.read());
167+
}
168+
Serial.println();
169+
170+
Serial.println();
171+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

‎examples/WiFiEcho/WiFiEcho.ino

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
ArduinoMqttClient - WiFi Echo
3+
4+
This example connects to a MQTT broker and subscribes to a single topic,
5+
it also publishes a message to the same topic once a second.
6+
When a message is received it prints the message to the serial monitor.
7+
8+
The circuit:
9+
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <ArduinoMqttClient.h>
15+
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>
16+
17+
#include "arduino_secrets.h"
18+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
19+
char ssid[] = SECRET_SSID; // your network SSID (name)
20+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
21+
22+
WiFiClient wifiClient;
23+
MqttClient mqttClient(wifiClient);
24+
25+
const char broker[] = "test.mosquitto.org";
26+
const char topic[] = "arduino/echo";
27+
28+
const long interval = 1000;
29+
unsigned long previousMillis = 0;
30+
31+
int count = 0;
32+
33+
void setup() {
34+
//Initialize serial and wait for port to open:
35+
Serial.begin(9600);
36+
while (!Serial) {
37+
; // wait for serial port to connect. Needed for native USB port only
38+
}
39+
40+
// attempt to connect to Wifi network:
41+
Serial.print("Attempting to connect to WPA SSID: ");
42+
Serial.println(ssid);
43+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
44+
// failed, retry
45+
Serial.print(".");
46+
delay(5000);
47+
}
48+
49+
Serial.println("You're connected to the network");
50+
Serial.println();
51+
52+
// You can provide a unique client ID, if not set the library uses Arduino-millis()
53+
// Each client must have a unique client ID
54+
// mqttClient.setId("clientId");
55+
56+
// You can provide a username and password for authentication
57+
// mqttClient.setUsernamePassword("username", "password");
58+
59+
Serial.print("Attempting to connect to the MQTT broker: ");
60+
Serial.println(broker);
61+
62+
if (!mqttClient.connect(broker, 1883)) {
63+
Serial.print("MQTT connection failed! Error code = ");
64+
Serial.println(mqttClient.connectError());
65+
66+
while (1);
67+
}
68+
69+
Serial.println("You're connected to the MQTT broker!");
70+
Serial.println();
71+
72+
Serial.print("Subscribing to topic: ");
73+
Serial.println(topic);
74+
Serial.println();
75+
76+
// subscribe to a topic
77+
mqttClient.subscribe(topic);
78+
79+
// topics can be unsubscribed using:
80+
// mqttClient.unsubscribe(topic);
81+
82+
Serial.print("Waiting for messages on topic: ");
83+
Serial.println(topic);
84+
Serial.println();
85+
}
86+
87+
void loop() {
88+
// check for incoming messages
89+
int messageSize = mqttClient.parseMessage();
90+
if (messageSize) {
91+
// we received a message, print out the topic and contents
92+
Serial.println("Received a message with topic '");
93+
Serial.print(mqttClient.messageTopic());
94+
Serial.print("', length ");
95+
Serial.print(messageSize);
96+
Serial.println(" bytes:");
97+
98+
// use the Stream interface to print the contents
99+
while (mqttClient.available()) {
100+
Serial.print((char)mqttClient.read());
101+
}
102+
Serial.println();
103+
104+
Serial.println();
105+
}
106+
107+
// avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
108+
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
109+
unsigned long currentMillis = millis();
110+
111+
if (currentMillis - previousMillis >= interval) {
112+
// save the last time a message was sent
113+
previousMillis = currentMillis;
114+
115+
Serial.print("Sending message to topic: ");
116+
Serial.println(topic);
117+
Serial.print("echo ");
118+
Serial.println(count);
119+
120+
// send message, the Print interface can be used to set the message contents
121+
mqttClient.beginMessage(topic);
122+
mqttClient.print("echo ");
123+
mqttClient.print(count);
124+
mqttClient.endMessage();
125+
126+
Serial.println();
127+
128+
count++;
129+
}
130+
}

‎examples/WiFiEcho/arduino_secrets.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
ArduinoMqttClient - WiFi Echo
3+
4+
This example connects to a MQTT broker and subscribes to a single topic,
5+
it also publishes a message to the same topic once a second.
6+
When a message is received it prints the message to the serial monitor,
7+
it uses the callback functionality of the library.
8+
9+
The circuit:
10+
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include <ArduinoMqttClient.h>
16+
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>
17+
18+
#include "arduino_secrets.h"
19+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
20+
char ssid[] = SECRET_SSID; // your network SSID (name)
21+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
22+
23+
WiFiClient wifiClient;
24+
MqttClient mqttClient(wifiClient);
25+
26+
const char broker[] = "test.mosquitto.org";
27+
const char topic[] = "arduino/echo";
28+
29+
const long interval = 1000;
30+
unsigned long previousMillis = 0;
31+
32+
int count = 0;
33+
34+
void setup() {
35+
//Initialize serial and wait for port to open:
36+
Serial.begin(9600);
37+
while (!Serial) {
38+
; // wait for serial port to connect. Needed for native USB port only
39+
}
40+
41+
// attempt to connect to Wifi network:
42+
Serial.print("Attempting to connect to WPA SSID: ");
43+
Serial.println(ssid);
44+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
45+
// failed, retry
46+
Serial.print(".");
47+
delay(5000);
48+
}
49+
50+
Serial.println("You're connected to the network");
51+
Serial.println();
52+
53+
// You can provide a unique client ID, if not set the library uses Arduino-millis()
54+
// Each client must have a unique client ID
55+
// mqttClient.setId("clientId");
56+
57+
// You can provide a username and password for authentication
58+
// mqttClient.setUsernamePassword("username", "password");
59+
60+
Serial.print("Attempting to connect to the MQTT broker: ");
61+
Serial.println(broker);
62+
63+
if (!mqttClient.connect(broker, 1883)) {
64+
Serial.print("MQTT connection failed! Error code = ");
65+
Serial.println(mqttClient.connectError());
66+
67+
while (1);
68+
}
69+
70+
Serial.println("You're connected to the MQTT broker!");
71+
Serial.println();
72+
73+
// set the message receive callback
74+
mqttClient.onMessage(onMqttMessage);
75+
76+
Serial.print("Subscribing to topic: ");
77+
Serial.println(topic);
78+
Serial.println();
79+
80+
// subscribe to a topic
81+
mqttClient.subscribe(topic);
82+
83+
// topics can be unsubscribed using:
84+
// mqttClient.unsubscribe(topic);
85+
86+
Serial.print("Waiting for messages on topic: ");
87+
Serial.println(topic);
88+
Serial.println();
89+
}
90+
91+
void loop() {
92+
// call poll() regularly to allow the library to receive MQTT messages and
93+
// send MQTT keep alives which avoids being disconnected by the broker
94+
mqttClient.poll();
95+
96+
// avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
97+
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
98+
unsigned long currentMillis = millis();
99+
100+
if (currentMillis - previousMillis >= interval) {
101+
// save the last time a message was sent
102+
previousMillis = currentMillis;
103+
104+
Serial.print("Sending message to topic: ");
105+
Serial.println(topic);
106+
Serial.print("echo ");
107+
Serial.println(count);
108+
109+
// send message, the Print interface can be used to set the message contents
110+
mqttClient.beginMessage(topic);
111+
mqttClient.print("echo ");
112+
mqttClient.print(count);
113+
mqttClient.endMessage();
114+
115+
Serial.println();
116+
117+
count++;
118+
}
119+
}
120+
121+
void onMqttMessage(int messageSize) {
122+
// we received a message, print out the topic and contents
123+
Serial.print("Received a message with topic '");
124+
Serial.print(mqttClient.messageTopic());
125+
Serial.print("', length ");
126+
Serial.print(messageSize);
127+
Serial.println(" bytes:");
128+
129+
// use the Stream interface to print the contents
130+
while (mqttClient.available()) {
131+
Serial.print((char)mqttClient.read());
132+
}
133+
Serial.println();
134+
135+
Serial.println();
136+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
ArduinoMqttClient - WiFi Simple Receive
3+
4+
This example connects to a MQTT broker and subscribes to a single topic.
5+
When a message is received it prints the message to the serial monitor.
6+
7+
The circuit:
8+
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
9+
10+
This example code is in the public domain.
11+
*/
12+
13+
#include <ArduinoMqttClient.h>
14+
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>
15+
16+
#include "arduino_secrets.h"
17+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
18+
char ssid[] = SECRET_SSID; // your network SSID (name)
19+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
20+
21+
WiFiClient wifiClient;
22+
MqttClient mqttClient(wifiClient);
23+
24+
const char broker[] = "test.mosquitto.org";
25+
const char topic[] = "arduino/simple";
26+
27+
void setup() {
28+
//Initialize serial and wait for port to open:
29+
Serial.begin(9600);
30+
while (!Serial) {
31+
; // wait for serial port to connect. Needed for native USB port only
32+
}
33+
34+
// attempt to connect to Wifi network:
35+
Serial.print("Attempting to connect to WPA SSID: ");
36+
Serial.println(ssid);
37+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
38+
// failed, retry
39+
Serial.print(".");
40+
delay(5000);
41+
}
42+
43+
Serial.println("You're connected to the network");
44+
Serial.println();
45+
46+
// You can provide a unique client ID, if not set the library uses Arduino-millis()
47+
// Each client must have a unique client ID
48+
// mqttClient.setId("clientId");
49+
50+
// You can provide a username and password for authentication
51+
// mqttClient.setUsernamePassword("username", "password");
52+
53+
Serial.print("Attempting to connect to the MQTT broker: ");
54+
Serial.println(broker);
55+
56+
if (!mqttClient.connect(broker, 1883)) {
57+
Serial.print("MQTT connection failed! Error code = ");
58+
Serial.println(mqttClient.connectError());
59+
60+
while (1);
61+
}
62+
63+
Serial.println("You're connected to the MQTT broker!");
64+
Serial.println();
65+
66+
Serial.print("Subscribing to topic: ");
67+
Serial.println(topic);
68+
Serial.println();
69+
70+
// subscribe to a topic
71+
mqttClient.subscribe(topic);
72+
73+
// topics can be unsubscribed using:
74+
// mqttClient.unsubscribe(topic);
75+
76+
Serial.print("Waiting for messages on topic: ");
77+
Serial.println(topic);
78+
Serial.println();
79+
}
80+
81+
void loop() {
82+
int messageSize = mqttClient.parseMessage();
83+
if (messageSize) {
84+
// we received a message, print out the topic and contents
85+
Serial.println("Received a message with topic '");
86+
Serial.print(mqttClient.messageTopic());
87+
Serial.print("', length ");
88+
Serial.print(messageSize);
89+
Serial.println(" bytes:");
90+
91+
// use the Stream interface to print the contents
92+
while (mqttClient.available()) {
93+
Serial.print((char)mqttClient.read());
94+
}
95+
Serial.println();
96+
97+
Serial.println();
98+
}
99+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
ArduinoMqttClient - WiFi Simple Receive Callback
3+
4+
This example connects to a MQTT broker and subscribes to a single topic.
5+
When a message is received it prints the message to the serial monitor,
6+
it uses the callback functionality of the library.
7+
8+
The circuit:
9+
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <ArduinoMqttClient.h>
15+
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>
16+
17+
#include "arduino_secrets.h"
18+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
19+
char ssid[] = SECRET_SSID; // your network SSID (name)
20+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
21+
22+
WiFiClient wifiClient;
23+
MqttClient mqttClient(wifiClient);
24+
25+
const char broker[] = "test.mosquitto.org";
26+
const char topic[] = "arduino/simple";
27+
28+
void setup() {
29+
//Initialize serial and wait for port to open:
30+
Serial.begin(9600);
31+
while (!Serial) {
32+
; // wait for serial port to connect. Needed for native USB port only
33+
}
34+
35+
// attempt to connect to Wifi network:
36+
Serial.print("Attempting to connect to WPA SSID: ");
37+
Serial.println(ssid);
38+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
39+
// failed, retry
40+
Serial.print(".");
41+
delay(5000);
42+
}
43+
44+
Serial.println("You're connected to the network");
45+
Serial.println();
46+
47+
// You can provide a unique client ID, if not set the library uses Arduino-millis()
48+
// Each client must have a unique client ID
49+
// mqttClient.setId("clientId");
50+
51+
// You can provide a username and password for authentication
52+
// mqttClient.setUsernamePassword("username", "password");
53+
54+
Serial.print("Attempting to connect to the MQTT broker: ");
55+
Serial.println(broker);
56+
57+
if (!mqttClient.connect(broker, 1883)) {
58+
Serial.print("MQTT connection failed! Error code = ");
59+
Serial.println(mqttClient.connectError());
60+
61+
while (1);
62+
}
63+
64+
Serial.println("You're connected to the MQTT broker!");
65+
Serial.println();
66+
67+
// set the message receive callback
68+
mqttClient.onMessage(onMqttMessage);
69+
70+
Serial.print("Subscribing to topic: ");
71+
Serial.println(topic);
72+
Serial.println();
73+
74+
// subscribe to a topic
75+
mqttClient.subscribe(topic);
76+
77+
// topics can be unsubscribed using:
78+
// mqttClient.unsubscribe(topic);
79+
80+
Serial.print("Waiting for messages on topic: ");
81+
Serial.println(topic);
82+
Serial.println();
83+
}
84+
85+
void loop() {
86+
// call poll() regularly to allow the library to receive MQTT messages and
87+
// send MQTT keep alives which avoids being disconnected by the broker
88+
mqttClient.poll();
89+
}
90+
91+
void onMqttMessage(int messageSize) {
92+
// we received a message, print out the topic and contents
93+
Serial.println("Received a message with topic '");
94+
Serial.print(mqttClient.messageTopic());
95+
Serial.print("', length ");
96+
Serial.print(messageSize);
97+
Serial.println(" bytes:");
98+
99+
// use the Stream interface to print the contents
100+
while (mqttClient.available()) {
101+
Serial.print((char)mqttClient.read());
102+
}
103+
Serial.println();
104+
105+
Serial.println();
106+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
ArduinoMqttClient - WiFi Simple Sender
3+
4+
This example connects to a MQTT broker and publishes a message to
5+
a topic once a second.
6+
7+
The circuit:
8+
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
9+
10+
This example code is in the public domain.
11+
*/
12+
13+
#include <ArduinoMqttClient.h>
14+
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>
15+
16+
#include "arduino_secrets.h"
17+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
18+
char ssid[] = SECRET_SSID; // your network SSID (name)
19+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
20+
21+
WiFiClient wifiClient;
22+
MqttClient mqttClient(wifiClient);
23+
24+
const char broker[] = "test.mosquitto.org";
25+
const char topic[] = "arduino/simple";
26+
27+
const long interval = 1000;
28+
unsigned long previousMillis = 0;
29+
30+
int count = 0;
31+
32+
void setup() {
33+
//Initialize serial and wait for port to open:
34+
Serial.begin(9600);
35+
while (!Serial) {
36+
; // wait for serial port to connect. Needed for native USB port only
37+
}
38+
39+
// attempt to connect to Wifi network:
40+
Serial.print("Attempting to connect to WPA SSID: ");
41+
Serial.println(ssid);
42+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
43+
// failed, retry
44+
Serial.print(".");
45+
delay(5000);
46+
}
47+
48+
Serial.println("You're connected to the network");
49+
Serial.println();
50+
51+
// You can provide a unique client ID, if not set the library uses Arduino-millis()
52+
// Each client must have a unique client ID
53+
// mqttClient.setId("clientId");
54+
55+
// You can provide a username and password for authentication
56+
// mqttClient.setUsernamePassword("username", "password");
57+
58+
Serial.print("Attempting to connect to the MQTT broker: ");
59+
Serial.println(broker);
60+
61+
if (!mqttClient.connect(broker, 1883)) {
62+
Serial.print("MQTT connection failed! Error code = ");
63+
Serial.println(mqttClient.connectError());
64+
65+
while (1);
66+
}
67+
68+
Serial.println("You're connected to the MQTT broker!");
69+
Serial.println();
70+
}
71+
72+
void loop() {
73+
// call poll() regularly to allow the library to send MQTT keep alives which
74+
// avoids being disconnected by the broker
75+
mqttClient.poll();
76+
77+
// avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
78+
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
79+
unsigned long currentMillis = millis();
80+
81+
if (currentMillis - previousMillis >= interval) {
82+
// save the last time a message was sent
83+
previousMillis = currentMillis;
84+
85+
Serial.print("Sending message to topic: ");
86+
Serial.println(topic);
87+
Serial.print("hello ");
88+
Serial.println(count);
89+
90+
// send message, the Print interface can be used to set the message contents
91+
mqttClient.beginMessage(topic);
92+
mqttClient.print("hello ");
93+
mqttClient.print(count);
94+
mqttClient.endMessage();
95+
96+
Serial.println();
97+
98+
count++;
99+
}
100+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

‎keywords.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
############################################
2+
# Syntax Coloring Map For ArduinoMqttClient
3+
############################################
4+
# Class
5+
############################################
6+
7+
ArduinoMqttClient KEYWORD1
8+
MqttClient KEYWORD1
9+
10+
############################################
11+
# Methods and Functions
12+
############################################
13+
14+
onMessage KEYWORD2
15+
16+
parseMessage KEYWORD2
17+
messageTopic KEYWORD2
18+
messageDup KEYWORD2
19+
messageQoS KEYWORD2
20+
messageRetain KEYWORD2
21+
22+
beginMessage KEYWORD2
23+
endMessage KEYWORD2
24+
beginWill KEYWORD2
25+
endWill KEYWORD2
26+
27+
subscribe KEYWORD2
28+
unsubscribe KEYWORD2
29+
30+
poll KEYWORD2
31+
32+
connect KEYWORD2
33+
write KEYWORD2
34+
available KEYWORD2
35+
read KEYWORD2
36+
peek KEYWORD2
37+
flush KEYWORD2
38+
stop KEYWORD2
39+
connected KEYWORD2
40+
41+
setId KEYWORD2
42+
setUsernamePassword KEYWORD2
43+
44+
setKeepAliveInterval KEYWORD2
45+
setConnectionTimeout KEYWORD2
46+
47+
connectError KEYWORD2
48+
subscribeQoS KEYWORD2
49+
50+
############################################
51+
# Constants
52+
############################################

‎library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=ArduinoMqttClient
2+
version=0.0.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=[BETA] Allows you to send and receive MQTT messages using Arduino.
6+
paragraph=
7+
category=Communication
8+
url=https://github.com/arduino-libraries/ArduinoMqttClient
9+
architectures=*
10+
includes=ArduinoMqttClient.h

‎src/ArduinoMqttClient.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
This file is part of the ArduinoMqttClient library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _ARDUINO_MQTT_CLIENT_H_
21+
#define _ARDUINO_MQTT_CLIENT_H_
22+
23+
#include "MqttClient.h"
24+
25+
#endif

‎src/MqttClient.cpp

Lines changed: 1127 additions & 0 deletions
Large diffs are not rendered by default.

‎src/MqttClient.h

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
This file is part of the ArduinoMqttClient library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _MQTT_CLIENT_H_
21+
#define _MQTT_CLIENT_H_
22+
23+
#include <Arduino.h>
24+
#include <Client.h>
25+
26+
#define MQTT_CONNECTION_REFUSED -2
27+
#define MQTT_CONNECTION_TIMEOUT -1
28+
#define MQTT_SUCCESS 0
29+
#define MQTT_UNACCEPTABLE_PROTOCOL_VERSION 1
30+
#define MQTT_IDENTIFIER_REJECTED 2
31+
#define MQTT_SERVER_UNAVAILABLE 3
32+
#define MQTT_BAD_USER_NAME_OR_PASSWORD 4
33+
#define MQTT_NOT_AUTHORIZED 5
34+
35+
class MqttClient : public Client {
36+
public:
37+
MqttClient(Client& client);
38+
virtual ~MqttClient();
39+
40+
void onMessage(void(*)(int));
41+
42+
int parseMessage();
43+
String messageTopic() const;
44+
int messageDup() const;
45+
int messageQoS() const;
46+
int messageRetain() const;
47+
48+
int beginMessage(const char* topic, unsigned long size, bool retain = false, uint8_t qos = 0, bool dup = false);
49+
int beginMessage(const String& topic, unsigned long size, bool retain = false, uint8_t qos = 0, bool dup = false);
50+
int beginMessage(const char* topic, bool retain = false, uint8_t qos = 0, bool dup = false);
51+
int beginMessage(const String& topic, bool retain = false, uint8_t qos = 0, bool dup = false);
52+
int endMessage();
53+
54+
int beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos);
55+
int beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos);
56+
int beginWill(const char* topic, bool retain, uint8_t qos);
57+
int beginWill(const String& topic, bool retain, uint8_t qos);
58+
int endWill();
59+
60+
int subscribe(const char* topic, uint8_t qos = 0);
61+
int subscribe(const String& topic, uint8_t qos = 0);
62+
int unsubscribe(const char* topic);
63+
int unsubscribe(const String& topic);
64+
65+
void poll();
66+
67+
// from Client
68+
virtual int connect(IPAddress ip, uint16_t port = 1883);
69+
virtual int connect(const char *host, uint16_t port = 1883);
70+
virtual size_t write(uint8_t);
71+
virtual size_t write(const uint8_t *buf, size_t size);
72+
virtual int available();
73+
virtual int read();
74+
virtual int read(uint8_t *buf, size_t size);
75+
virtual int peek();
76+
virtual void flush();
77+
virtual void stop();
78+
virtual uint8_t connected();
79+
virtual operator bool();
80+
81+
void setId(const char* id);
82+
void setId(const String& id);
83+
84+
void setUsernamePassword(const char* username, const char* password);
85+
void setUsernamePassword(const String& username, const String& password);
86+
87+
void setKeepAliveInterval(unsigned long interval);
88+
void setConnectionTimeout(unsigned long timeout);
89+
90+
int connectError() const;
91+
int subscribeQoS() const;
92+
93+
private:
94+
int connect(IPAddress ip, const char* host, uint16_t port);
95+
int publishHeader(size_t length);
96+
void puback(uint16_t id);
97+
void pubrec(uint16_t id);
98+
void pubrel(uint16_t id);
99+
void pubcomp(uint16_t id);
100+
void ping();
101+
void disconnect();
102+
103+
int beginPacket(uint8_t type, uint8_t flags, size_t length, uint8_t* buffer);
104+
int writeString(const char* s, uint16_t length);
105+
int write8(uint8_t val);
106+
int write16(uint16_t val);
107+
int writeData(const void* data, size_t length);
108+
int endPacket();
109+
110+
uint8_t clientConnected();
111+
int clientAvailable();
112+
int clientRead();
113+
int clientTimedRead();
114+
int clientPeek();
115+
size_t clientWrite(const uint8_t *buf, size_t size);
116+
117+
private:
118+
Client* _client;
119+
120+
void (*_onMessage)(int);
121+
122+
String _id;
123+
String _username;
124+
String _password;
125+
126+
unsigned long _keepAliveInterval;
127+
unsigned long _connectionTimeout;
128+
129+
int _connectError;
130+
bool _connected;
131+
int _subscribeQos;
132+
133+
int _rxState;
134+
uint8_t _rxType;
135+
uint8_t _rxFlags;
136+
size_t _rxLength;
137+
uint32_t _rxLengthMultiplier;
138+
int _returnCode;
139+
140+
String _rxMessageTopic;
141+
size_t _rxMessageTopicLength;
142+
bool _rxMessageDup;
143+
uint8_t _rxMessageQoS;
144+
bool _rxMessageRetain;
145+
uint16_t _rxPacketId;
146+
uint8_t _rxMessageBuffer[3];
147+
size_t _rxMessageIndex;
148+
unsigned long _lastRx;
149+
150+
String _txMessageTopic;
151+
bool _txMessageRetain;
152+
uint8_t _txMessageQoS;
153+
bool _txMessageDup;
154+
uint16_t _txPacketId;
155+
uint8_t* _txBuffer;
156+
size_t _txBufferIndex;
157+
bool _txStreamPayload;
158+
uint8_t* _txPayloadBuffer;
159+
size_t _txPayloadBufferIndex;
160+
unsigned long _lastPingTx;
161+
162+
uint8_t* _willBuffer;
163+
uint16_t _willBufferIndex;
164+
size_t _willMessageIndex;
165+
uint8_t _willFlags;
166+
};
167+
168+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.