Skip to content

Commit 356bb36

Browse files
committed
Merge branch 'yun-client'
2 parents d916de0 + a9dbfff commit 356bb36

File tree

15 files changed

+378
-81
lines changed

15 files changed

+378
-81
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
build/
33
node_modules/
44
mqtt.zip
5+
yun/.idea

CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
cmake_minimum_required(VERSION 2.8.4)
44
project(arduino_mqtt)
55

6-
include_directories(/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino)
6+
include_directories(/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/)
7+
include_directories(/Applications/Arduino.app/Contents/Java/libraries/Bridge/src)
78

89
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
910

@@ -30,6 +31,12 @@ set(SOURCE_FILES
3031
src/Network.cpp
3132
src/Network.h
3233
src/Timer.cpp
33-
src/Timer.h)
34+
src/Timer.h
35+
src/YunMQTTClient.cpp
36+
src/YunMQTTClient.h
37+
src/YunMQTTInstall.cpp
38+
src/YunMQTTInstall.h
39+
examples/MQTTClient/MQTTClient.ino
40+
examples/YunClientInstall/YunClientInstall.ino)
3441

3542
add_executable(arduino_mqtt ${SOURCE_FILES})

README.md

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# arduino-mqtt
22

3-
**paho mqtt library wrapper for arduino**
3+
**MQTT library for arduino based on the Eclipse Paho projects*
44

5-
This library bundles the [Embedded MQTT C/C++ Client](https://eclipse.org/paho/clients/c/embedded/) library of the eclipse paho project and adds a thin wrapper to get an Arduino like API.
5+
This library bundles the [Embedded MQTT C/C++ Client](https://eclipse.org/paho/clients/c/embedded/) library of the Eclipse Paho project and adds a thin wrapper to get an Arduino like API. Additionally there is an drop-in alternative for the Arduino Yùn that uses a python based client on the linux processor and a binary interface to lower resources usage on the Arduino side.
66

77
The first release of the library only supports QoS0 and the basic features to get going. In the next releases more of the features will be available.
88

@@ -16,52 +16,8 @@ This library is an alternative to the [pubsubclient](https://github.com/knollear
1616

1717
This library has been only tested on the **Arduino Yùn** yet. Other boards and shields should work if they properly extend the Client API.
1818

19-
## Example
19+
## Examples
2020

21-
```c++
22-
#include <Bridge.h>
23-
#include <YunClient.h>
24-
#include <MQTTClient.h>
25-
26-
YunClient net;
27-
MQTTClient client("connect.shiftr.io", 1883, net);
28-
29-
unsigned long lastMillis = 0;
30-
31-
void setup() {
32-
Bridge.begin();
33-
Serial.begin(9600);
34-
Serial.println("connecting...");
35-
if (client.connect("arduino", "demo", "demo")) {
36-
Serial.println("connected!");
37-
client.subscribe("/another/topic");
38-
// client.unsubscribe("/another/topic");
39-
} else {
40-
Serial.println("not connected!");
41-
}
42-
}
43-
44-
void loop() {
45-
client.loop();
46-
// publish message roughly every second
47-
if(millis() - lastMillis > 1000) {
48-
lastMillis = millis();
49-
client.publish("/topic", "Hello world!");
50-
}
51-
}
52-
53-
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
54-
Serial.print("incomming: ");
55-
Serial.print(topic);
56-
Serial.print(" - ");
57-
Serial.print(payload);
58-
Serial.println();
59-
}
60-
```
61-
62-
## Configuration
63-
64-
```c++
65-
// this buffer gets allocated two times to hold the outgoing and incomming message
66-
#define MQTT_BUFFER_SIZE 64 // default 128
67-
```
21+
- [Example using the standard MQTTClient]().
22+
- [Example using the alternative YunMQTTClient]().
23+
- [Sketch to install the YunMQTTClient python client script]().
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <Bridge.h>
2+
#include <YunMQTTInstall.h>
3+
4+
void setup() {
5+
Bridge.begin();
6+
Serial.begin(9600);
7+
8+
while (!Serial);
9+
delay(1000);
10+
11+
YunMQTTInstall();
12+
}
13+
14+
void loop() {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <Bridge.h>
2+
#include <YunMQTTClient.h>
3+
4+
YunMQTTClient client("connect.shiftr.io", 1883);
5+
6+
unsigned long lastMillis = 0;
7+
8+
void setup() {
9+
Bridge.begin();
10+
Serial.begin(9600);
11+
Serial.println("connecting...");
12+
if (client.connect("arduino", "demo", "demo")) {
13+
Serial.println("connected!");
14+
client.subscribe("/another/topic");
15+
// client.unsubscribe("/another/topic");
16+
} else {
17+
Serial.println("not connected!");
18+
}
19+
}
20+
21+
void loop() {
22+
client.loop();
23+
// publish message roughly every second
24+
if(millis() - lastMillis > 1000) {
25+
lastMillis = millis();
26+
client.publish("/topic", "Hello world!");
27+
}
28+
}
29+
30+
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
31+
Serial.print("incomming: ");
32+
Serial.print(topic);
33+
Serial.print(" - ");
34+
Serial.print(payload);
35+
Serial.println();
36+
}

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name=MQTT
22
version=1.3.2
33
author=Joel Gaehwiler <joel.gaehwiler@gmail.com>
44
maintainer=Joel Gaehwiler <joel.gaehwiler@gmail.com>
5-
sentence=Paho MQTT library wrapper for Arduino.
6-
paragraph=This library bundles the Embedded MQTT C/C++ Client library of the eclipse paho project and adds a thin wrapper to get an Arduino like API.
5+
sentence=MQTT library for arduino based on the Eclipse Paho projects.
6+
paragraph=This library bundles the Embedded MQTT C/C++ Client library of the Eclipse Paho project and adds a thin wrapper to get an Arduino like API. Additionally there is an drop-in alternative for the Arduino Yùn that uses a python based client on the linux processor and a binary interface to lower resources usage on the Arduino side.
77
url=https://github.com/256dpi/arduino-mqtt
88
architectures=*

src/MQTTClient.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,46 +42,46 @@ boolean MQTTClient::connect(const char * clientId, const char * username, const
4242
return this->client->connect(data) == 0;
4343
}
4444

45-
boolean MQTTClient::publish(String topic) {
46-
return this->publish(topic.c_str(), "");
45+
void MQTTClient::publish(String topic) {
46+
this->publish(topic.c_str(), "");
4747
}
4848

49-
boolean MQTTClient::publish(String topic, String payload) {
50-
return this->publish(topic.c_str(), payload.c_str());
49+
void MQTTClient::publish(String topic, String payload) {
50+
this->publish(topic.c_str(), payload.c_str());
5151
}
5252

53-
boolean MQTTClient::publish(const char * topic, String payload) {
54-
return this->publish(topic, payload.c_str());
53+
void MQTTClient::publish(const char * topic, String payload) {
54+
this->publish(topic, payload.c_str());
5555
}
5656

57-
boolean MQTTClient::publish(const char * topic, const char * payload) {
57+
void MQTTClient::publish(const char * topic, const char * payload) {
5858
MQTT::Message message;
5959
message.qos = MQTT::QOS0;
6060
message.retained = false;
6161
message.dup = false;
6262
message.payload = (char*)payload;
6363
message.payloadlen = strlen(payload);
64-
return client->publish(topic, message) == 0;
64+
client->publish(topic, message);
6565
}
6666

67-
boolean MQTTClient::subscribe(String topic) {
68-
return this->subscribe(topic.c_str());
67+
void MQTTClient::subscribe(String topic) {
68+
this->subscribe(topic.c_str());
6969
}
7070

71-
boolean MQTTClient::subscribe(const char * topic) {
72-
return client->subscribe(topic, MQTT::QOS0, NULL) == 0;
71+
void MQTTClient::subscribe(const char * topic) {
72+
client->subscribe(topic, MQTT::QOS0, NULL);
7373
}
7474

75-
boolean MQTTClient::unsubscribe(String topic) {
76-
return this->unsubscribe(topic.c_str());
75+
void MQTTClient::unsubscribe(String topic) {
76+
this->unsubscribe(topic.c_str());
7777
}
7878

79-
boolean MQTTClient::unsubscribe(const char * topic) {
80-
return client->unsubscribe(topic) == 0;
79+
void MQTTClient::unsubscribe(const char * topic) {
80+
client->unsubscribe(topic);
8181
}
8282

83-
boolean MQTTClient::loop() {
84-
return this->client->yield() == 0;
83+
void MQTTClient::loop() {
84+
this->client->yield();
8585
}
8686

8787
boolean MQTTClient::connected() {

src/MQTTClient.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ class MQTTClient {
2727
MQTTClient(const char * hostname, int port, Client& client);
2828
boolean connect(const char * clientId);
2929
boolean connect(const char * clientId, const char* username, const char* password);
30-
boolean publish(String topic);
31-
boolean publish(String topic, String payload);
32-
boolean publish(const char * topic, String payload);
33-
boolean publish(const char * topic, const char * payload);
34-
boolean subscribe(String topic);
35-
boolean subscribe(const char * topic);
36-
boolean unsubscribe(String topic);
37-
boolean unsubscribe(const char * topic);
38-
boolean loop();
30+
void publish(String topic);
31+
void publish(String topic, String payload);
32+
void publish(const char * topic, String payload);
33+
void publish(const char * topic, const char * payload);
34+
void subscribe(String topic);
35+
void subscribe(const char * topic);
36+
void unsubscribe(String topic);
37+
void unsubscribe(const char * topic);
38+
void loop();
3939
boolean connected();
4040
void disconnect();
4141
};

src/YunMQTTClient.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "YunMQTTClient.h"
2+
3+
YunMQTTClient::YunMQTTClient(const char * _hostname, int _port) {
4+
this->hostname = _hostname;
5+
this->port = _port;
6+
}
7+
8+
boolean YunMQTTClient::connect(const char * clientId) {
9+
return this->connect(clientId, "", "");
10+
}
11+
12+
boolean YunMQTTClient::connect(const char * clientId, const char * username, const char * password) {
13+
this->process.begin("python");
14+
this->process.addParameter("-u");
15+
this->process.addParameter("/usr/client.py");
16+
this->process.runAsynchronously();
17+
this->process.setTimeout(10000);
18+
19+
// wait for script to launch
20+
this->process.readStringUntil('\n');
21+
22+
// send connect request
23+
this->process.print("c:");
24+
this->process.print(this->hostname);
25+
this->process.print(':');
26+
this->process.print(this->port);
27+
this->process.print(':');
28+
this->process.print(clientId);
29+
if(strlen(username) > 0 && strlen(password) > 0) {
30+
this->process.print(':');
31+
this->process.print(username);
32+
this->process.print(':');
33+
this->process.print(password);
34+
}
35+
this->process.print('\n');
36+
37+
// wait for answer
38+
String ret = this->process.readStringUntil('\n');
39+
this->alive = ret.equals("ca");
40+
41+
if(!this->alive) {
42+
this->process.close();
43+
return false;
44+
} else {
45+
return true;
46+
}
47+
}
48+
49+
void YunMQTTClient::publish(String topic) {
50+
this->publish(topic.c_str(), "");
51+
}
52+
53+
void YunMQTTClient::publish(String topic, String payload) {
54+
this->publish(topic.c_str(), payload.c_str());
55+
}
56+
57+
void YunMQTTClient::publish(const char * topic, String payload) {
58+
this->publish(topic, payload.c_str());
59+
}
60+
61+
void YunMQTTClient::publish(const char * topic, const char * payload) {
62+
// send publish request
63+
this->process.print("p:");
64+
this->process.print(topic);
65+
this->process.print(':');
66+
this->process.print(payload);
67+
this->process.print('\n');
68+
}
69+
70+
void YunMQTTClient::subscribe(String topic) {
71+
this->subscribe(topic.c_str());
72+
}
73+
74+
void YunMQTTClient::subscribe(const char * topic) {
75+
// send subscribe request
76+
this->process.print("s:");
77+
this->process.print(topic);
78+
this->process.print('\n');
79+
}
80+
81+
void YunMQTTClient::unsubscribe(String topic) {
82+
this->unsubscribe(topic.c_str());
83+
}
84+
85+
void YunMQTTClient::unsubscribe(const char * topic) {
86+
// send unsubscribe request
87+
this->process.print("u:");
88+
this->process.print(topic);
89+
this->process.print('\n');
90+
}
91+
92+
void YunMQTTClient::loop() {
93+
int av = this->process.available();
94+
if(av > 0) {
95+
String ret = process.readStringUntil('\n');
96+
97+
if(ret.startsWith("m")) {
98+
int startTopic = 2;
99+
int endTopic = ret.indexOf(':', startTopic + 1);
100+
int startPayload = endTopic + 1;
101+
int endPayload = ret.indexOf(':', startPayload + 1);
102+
String topic = ret.substring(startTopic, endTopic);
103+
String payload = ret.substring(startPayload, endPayload);
104+
messageReceived(topic, payload, (char*)payload.c_str(), payload.length());
105+
} else if(ret.startsWith("e")) {
106+
this->alive = false;
107+
}
108+
}
109+
}
110+
111+
boolean YunMQTTClient::connected() {
112+
return this->alive;
113+
}
114+
115+
void YunMQTTClient::disconnect() {
116+
// send disconnect request
117+
this->process.print("d\n");
118+
}

0 commit comments

Comments
 (0)