Skip to content

Commit a19d994

Browse files
committed
Initial release
0 parents  commit a19d994

18 files changed

+2160
-0
lines changed

README.adoc

+24
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
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

examples/WiFiEcho/WiFiEcho.ino

+130
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

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

0 commit comments

Comments
 (0)