Skip to content

Commit 8298499

Browse files
committed
added mqtt library
1 parent 89859f7 commit 8298499

38 files changed

+6336
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Makefile]
2+
indent_style = tab
3+
indent_size = 4
4+
5+
[src/*.h,src/*.cpp,examples/**.ino]
6+
indent_style = space
7+
indent_size = 2
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
cmake-build-debug/
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
language: generic
2+
env:
3+
global:
4+
- IDE_VERSION=1.8.5
5+
matrix:
6+
- EXAMPLE="AdafruitHuzzahESP8266" BOARD="esp8266:esp8266:huzzah:FlashSize=4M3M,CpuFrequency=80"
7+
- EXAMPLE="AdafruitHuzzahESP8266_SSL" BOARD="esp8266:esp8266:huzzah:FlashSize=4M3M,CpuFrequency=80"
8+
- EXAMPLE="ArduinoEthernetShield" BOARD="arduino:avr:uno"
9+
- EXAMPLE="ArduinoMKRGSM1400" BOARD="arduino:samd:mkrgsm1400"
10+
- EXAMPLE="ArduinoMKRGSM1400_SSL" BOARD="arduino:samd:mkrgsm1400"
11+
- EXAMPLE="ArduinoWiFi101_SSL" BOARD="arduino:avr:uno"
12+
- EXAMPLE="ArduinoWiFiShield" BOARD="arduino:avr:uno"
13+
- EXAMPLE="ArduinoYun" BOARD="arduino:avr:yun"
14+
- EXAMPLE="ArduinoYun_SSL" BOARD="arduino:avr:yun"
15+
- EXAMPLE="ESP32DevelopmentBoard" BOARD="espressif:esp32:esp32:FlashFreq=80"
16+
- EXAMPLE="ESP32DevelopmentBoard_SSL" BOARD="espressif:esp32:esp32:FlashFreq=80"
17+
before_install:
18+
- /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16
19+
- sleep 3
20+
- export DISPLAY=:1.0
21+
- wget http://downloads.arduino.cc/arduino-$IDE_VERSION-linux64.tar.xz
22+
- tar xf arduino-$IDE_VERSION-linux64.tar.xz
23+
- mv arduino-$IDE_VERSION ~/arduino-ide
24+
- export PATH=$PATH:~/arduino-ide
25+
- if [[ "$BOARD" =~ "esp8266:esp8266:" ]]; then
26+
arduino --pref "boardsmanager.additional.urls=http://arduino.esp8266.com/stable/package_esp8266com_index.json" --install-boards esp8266:esp8266;
27+
arduino --pref "boardsmanager.additional.urls=" --save-prefs;
28+
fi
29+
- if [[ "$BOARD" =~ "espressif:esp32:" ]]; then
30+
mkdir -p ~/Arduino/hardware/espressif &&
31+
cd ~/Arduino/hardware/espressif &&
32+
git clone https://github.com/espressif/arduino-esp32.git esp32 &&
33+
cd esp32/tools/ &&
34+
python get.py &&
35+
cd $TRAVIS_BUILD_DIR;
36+
fi
37+
- if [[ "$BOARD" =~ "arduino:samd:mkrgsm1400" ]]; then
38+
arduino --install-boards arduino:samd;
39+
arduino --install-library MKRGSM;
40+
fi
41+
- arduino --install-library WiFi101
42+
install:
43+
- mkdir -p ~/Arduino/libraries
44+
- ln -s $PWD ~/Arduino/libraries/.
45+
script:
46+
- arduino --verbose-build --verify --board $BOARD $PWD/examples/$EXAMPLE/$EXAMPLE.ino;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Uncompilable CMake File to enable project editing with CLion IDE
2+
3+
cmake_minimum_required(VERSION 2.8.4)
4+
project(arduino-mqtt)
5+
6+
include_directories(
7+
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/
8+
/Users/256dpi/Development/Arduino/libraries/Ethernet/src
9+
/Users/256dpi/Development/Arduino/libraries/WiFi101/src
10+
/Users/256dpi/Development/Arduino/libraries/MKRGSM/src
11+
/Applications/Arduino.app/Contents/Java/libraries/Bridge/src
12+
/Users/256dpi/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WiFi/src
13+
/Users/256dpi/Development/Arduino/hardware/espressif/esp32/libraries/WiFi/src
14+
/Users/256dpi/Development/Arduino/hardware/espressif/esp32/libraries/WiFiClientSecure/src
15+
src/)
16+
17+
include_directories(src/)
18+
19+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
20+
21+
set(SOURCE_FILES
22+
examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino
23+
examples/AdafruitHuzzahESP8266_SSL/AdafruitHuzzahESP8266_SSL.ino
24+
examples/ArduinoEthernetShield/ArduinoEthernetShield.ino
25+
examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino
26+
examples/ArduinoMKRGSM1400_SSL/ArduinoMKRGSM1400_SSL.ino
27+
examples/ArduinoWiFi101/ArduinoWiFi101.ino
28+
examples/ArduinoWiFi101_SSL/ArduinoWiFi101_SSL.ino
29+
examples/ArduinoWiFiShield/ArduinoWiFiShield.ino
30+
examples/ArduinoYun/ArduinoYun.ino
31+
examples/ArduinoYun_SSL/ArduinoYun_SSL.ino
32+
examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino
33+
examples/ESP32DevelopmentBoard_SSL/ESP32DevelopmentBoard_SSL.ino
34+
src/lwmqtt
35+
src/MQTT.h
36+
src/MQTTClient.h
37+
src/system.cpp
38+
src/system.h)
39+
40+
add_executable(arduino-mqtt ${SOURCE_FILES})
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Joël Gähwiler
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

libraries/arduino-mqtt-2.3.1/Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
all: fmt
2+
3+
fmt:
4+
clang-format -i src/*.cpp src/*.h -style="{BasedOnStyle: Google, ColumnLimit: 120}"
5+
6+
update:
7+
rm -rf ./lwmqtt
8+
git clone --branch v0.5.6 https://github.com/256dpi/lwmqtt.git ./lwmqtt
9+
mkdir -p ./src/lwmqtt
10+
cp -r ./lwmqtt/src/*.c ./src/lwmqtt/
11+
cp -r ./lwmqtt/src/*.h ./src/lwmqtt/
12+
cp -r ./lwmqtt/include/*.h ./src/lwmqtt/
13+
rm -rf ./lwmqtt
14+
sed -i '' "s/<lwmqtt.h>/\"lwmqtt.h\"/g" ./src/lwmqtt/*
+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# arduino-mqtt
2+
3+
[![Build Status](https://travis-ci.org/256dpi/arduino-mqtt.svg?branch=master)](https://travis-ci.org/256dpi/arduino-mqtt)
4+
[![GitHub release](https://img.shields.io/github/release/256dpi/arduino-mqtt.svg)](https://github.com/256dpi/arduino-mqtt/releases)
5+
6+
This library bundles the [lwmqtt](https://github.com/256dpi/lwmqtt) MQTT 3.1.1 client and adds a thin wrapper to get an Arduino like API.
7+
8+
Download the latest version from the [release](https://github.com/256dpi/arduino-mqtt/releases) section. Or even better use the builtin Library Manager in the Arduino IDE and search for "MQTT".
9+
10+
The library is also available on [PlatformIO](https://platformio.org/lib/show/617/MQTT). You can install it by running: `pio lib install "MQTT"`.
11+
12+
## Compatibility
13+
14+
The following examples show how you can use the library with various Arduino compatible hardware:
15+
16+
- [Arduino Yun & Yun-Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun/ArduinoYun.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_SSL/ArduinoYun_SSL.ino))
17+
- [Arduino Ethernet Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino)
18+
- [Arduino WiFi Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino)
19+
- [Adafruit HUZZAH ESP8266](https://github.com/256dpi/arduino-mqtt/blob/master/examples/AdafruitHuzzahESP8266/AdafruitHuzzahESP8266.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/AdafruitHuzzahESP8266_SSL/AdafruitHuzzahESP8266_SSL.ino))
20+
- [Arduino/Genuino WiFi101 Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFi101/ArduinoWiFi101.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFi101_SSL/ArduinoWiFi101_SSL.ino))
21+
- [Arduino MKR GSM 1400](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoMKRGSM1400/ArduinoMKRGSM1400.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoMKRGSM1400_SSL/ArduinoMKRGSM1400_SSL.ino))
22+
- [ESP32 Development Board](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ESP32DevelopmentBoard_SSL/ESP32DevelopmentBoard_SSL.ino))
23+
24+
Other shields and boards should also work if they provide a [Client](https://www.arduino.cc/en/Reference/ClientConstructor) based network implementation.
25+
26+
## Notes
27+
28+
- The maximum size for packets being published and received is set by default to 128 bytes. To change the buffer sizes, you need to use `MQTTClient client(256)` instead of just `MQTTClient client` on the top of your sketch. The passed value denotes the read and write buffer size.
29+
30+
- On the ESP8266 it has been reported that an additional `delay(10);` after `client.loop();` fixes many stability issues with WiFi connections.
31+
32+
- To use the library with shiftr.io, you need to provide the token key (username) and token secret (password) as the second and third argument to `client.connect(name, key, secret)`.
33+
34+
## Example
35+
36+
The following example uses an Arduino MKR1000 to connect to shiftr.io. You can check on your device after a successful connection here: https://shiftr.io/try.
37+
38+
```c++
39+
#include <SPI.h>
40+
#include <WiFi101.h>
41+
#include <MQTT.h>
42+
43+
const char ssid[] = "ssid";
44+
const char pass[] = "pass";
45+
46+
WiFiClient net;
47+
MQTTClient client;
48+
49+
unsigned long lastMillis = 0;
50+
51+
void connect() {
52+
Serial.print("checking wifi...");
53+
while (WiFi.status() != WL_CONNECTED) {
54+
Serial.print(".");
55+
delay(1000);
56+
}
57+
58+
Serial.print("\nconnecting...");
59+
while (!client.connect("arduino", "try", "try")) {
60+
Serial.print(".");
61+
delay(1000);
62+
}
63+
64+
Serial.println("\nconnected!");
65+
66+
client.subscribe("/hello");
67+
// client.unsubscribe("/hello");
68+
}
69+
70+
void messageReceived(String &topic, String &payload) {
71+
Serial.println("incoming: " + topic + " - " + payload);
72+
}
73+
74+
void setup() {
75+
Serial.begin(115200);
76+
WiFi.begin(ssid, pass);
77+
78+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
79+
// You need to set the IP address directly.
80+
client.begin("broker.shiftr.io", net);
81+
client.onMessage(messageReceived);
82+
83+
connect();
84+
}
85+
86+
void loop() {
87+
client.loop();
88+
89+
if (!client.connected()) {
90+
connect();
91+
}
92+
93+
// publish a message roughly every second.
94+
if (millis() - lastMillis > 1000) {
95+
lastMillis = millis();
96+
client.publish("/hello", "world");
97+
}
98+
}
99+
```
100+
101+
## API
102+
103+
Initialize the object using the hostname of the broker, the brokers port (default: `1883`) and the underlying Client class for network transport:
104+
105+
```c++
106+
void begin(const char hostname[], Client &client);
107+
void begin(const char hostname[], int port, Client &client);
108+
```
109+
110+
- Specify port `8883` when using SSL clients for secure connections.
111+
- Local domain names (e.g. `Computer.local` on OSX) are not supported by Arduino. You need to set the IP address directly.
112+
113+
The hostname and port can also be changed after calling `begin()`:
114+
115+
```c++
116+
void setHost(const char hostname[]);
117+
void setHost(const char hostname[], int port);
118+
```
119+
120+
Set a will message (last testament) that gets registered on the broker after connecting:
121+
122+
```c++
123+
void setWill(const char topic[]);
124+
void setWill(const char topic[], const char payload[]);
125+
void setWill(const char topic[], const char payload[], bool retained, int qos);
126+
void clearWill();
127+
```
128+
129+
Register a callback to receive messages:
130+
131+
```c++
132+
void onMessage(MQTTClientCallbackSimple);
133+
// Callback signature: void messageReceived(String &topic, String &payload) {}
134+
135+
void onMessageAdvanced(MQTTClientCallbackAdvanced);
136+
// Callback signature: void messageReceived(MQTTClient *client, char[] topic, char payload[], int payload_length) {}
137+
```
138+
139+
- The set callback is mostly called during a call to `loop()` but may also be called during a call to `subscribe()`, `unsubscribe()` or `publish() // QoS > 0` if messages have been received before receiving the required acknowledgement. Therefore, it is strongly recommended to not call `subscribe()`, `unsubscribe()` or `publish() // QoS > 0` directly in the callback.
140+
141+
Set more advanced options:
142+
143+
```c++
144+
void setOptions(int keepAlive, bool cleanSession, int timeout);
145+
```
146+
147+
- The `keepAlive` option controls the keep alive interval (default: 10).
148+
- The `cleanSession` option controls the session retention on the broker side (default: true).
149+
- The `timeout` option controls the default timeout for all commands in milliseconds (default: 1000).
150+
151+
Connect to broker using the supplied client id and an optional username and password:
152+
153+
```c++
154+
bool connect(const char clientId[]);
155+
bool connect(const char clientId[], const char username[]);
156+
bool connect(const char clientId[], const char username[], const char password[]);
157+
```
158+
159+
- This functions returns a boolean that indicates if the connection has been established successfully.
160+
161+
Publishes a message to the broker with an optional payload:
162+
163+
```c++
164+
bool publish(const String &topic);
165+
bool publish(const char topic[]);
166+
bool publish(const String &topic, const String &payload);
167+
bool publish(const String &topic, const String &payload, bool retained, int qos);
168+
bool publish(const char topic[], const String &payload);
169+
bool publish(const char topic[], const String &payload, bool retained, int qos);
170+
bool publish(const char topic[], const char payload[]);
171+
bool publish(const char topic[], const char payload[], bool retained, int qos);
172+
bool publish(const char topic[], const char payload[], int length);
173+
bool publish(const char topic[], const char payload[], int length, bool retained, int qos);
174+
```
175+
176+
Subscribe to a topic:
177+
178+
```c++
179+
bool subscribe(const String &topic);
180+
bool subscribe(const String &topic, int qos);
181+
bool subscribe(const char topic[]);
182+
bool subscribe(const char topic[], int qos);
183+
```
184+
185+
Unsubscribe from a topic:
186+
187+
```c++
188+
bool unsubscribe(const String &topic);
189+
bool unsubscribe(const char topic[]);
190+
```
191+
192+
Sends and receives packets:
193+
194+
```c++
195+
bool loop();
196+
```
197+
198+
- This function should be called in every `loop`.
199+
200+
Check if the client is currently connected:
201+
202+
```c++
203+
bool connected();
204+
```
205+
206+
Access low-level information for debugging:
207+
208+
```c++
209+
lwmqtt_err_t lastError();
210+
lwmqtt_return_code_t returnCode();
211+
```
212+
213+
- The error codes can be found [here](https://github.com/256dpi/lwmqtt/blob/master/include/lwmqtt.h#L11).
214+
- The return codes can be found [here](https://github.com/256dpi/lwmqtt/blob/master/include/lwmqtt.h#L243).
215+
216+
Disconnect from the broker:
217+
218+
```c++
219+
bool disconnect();
220+
```
221+
222+
## Release Management
223+
224+
- Update version in `library.properties`.
225+
- Create release on GitHub.

0 commit comments

Comments
 (0)