Skip to content

Commit 02bd57a

Browse files
committed
Merge pull request 256dpi#10 from 256dpi/major-update
Major Update
2 parents c47902f + 00732a3 commit 02bd57a

File tree

9 files changed

+80
-73
lines changed

9 files changed

+80
-73
lines changed

README.md

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The first release of the library only supports QoS0 and the basic features to ge
88

99
This library is an alternative to the [pubsubclient](https://github.com/knolleary/pubsubclient) library by [knolleary](https://github.com/knolleary) which uses a custom protocol implementation.
1010

11-
[Download version 1.7.0 of the library.](https://github.com/256dpi/arduino-mqtt/releases/download/v1.7.0/mqtt.zip)
11+
[Download version 1.8.0 of the library.](https://github.com/256dpi/arduino-mqtt/releases/download/v1.8.0/mqtt.zip)
1212

1313
*Or even better use the newly available Library Manager in the Arduino IDE.*
1414

@@ -35,13 +35,15 @@ Here is a list of platforms that are supported:
3535
#include <MQTTClient.h>
3636

3737
YunClient net;
38-
MQTTClient client("broker.shiftr.io", net);
38+
MQTTClient client;
3939

4040
unsigned long lastMillis = 0;
4141

4242
void setup() {
4343
Bridge.begin();
4444
Serial.begin(9600);
45+
client.begin("broker.shiftr.io", net);
46+
4547
Serial.println("connecting...");
4648
if (client.connect("arduino", "try", "try")) {
4749
Serial.println("connected!");
@@ -54,7 +56,7 @@ void setup() {
5456

5557
void loop() {
5658
client.loop();
57-
// publish message roughly every second
59+
// publish a message roughly every second.
5860
if(millis() - lastMillis > 1000) {
5961
lastMillis = millis();
6062
client.publish("/hello", "world");
@@ -72,55 +74,70 @@ void messageReceived(String topic, String payload, char * bytes, unsigned int le
7274
7375
## API
7476
75-
- **`MQTTClient(const char * hostname, Client& client)`**
76-
- **`MQTTClient(const char * hostname, int port, Client& client)`**
77+
Initialize the object using the hostname of the broker, the brokers port (default: `1883`) and the underlying Client class for network transport:
7778
78-
Constructor for the `MQTTClient` object using the hostname of the broker, the brokers port (default: `1883`) and the underlying Client class for network transport.
79-
80-
- **`YunMQTTClient(const char * hostname)`**
81-
- **`YunMQTTClient(const char * hostname, int port)`**
79+
```c++
80+
void begin(const char * hostname, Client& client);
81+
void begin(const char * hostname, int port, Client& client);
82+
```
8283

83-
Constructor for the `YunMQTTClient` object using the hostname of the broker and the brokers port (default: `1883`).
84+
_The special`YunMQTTClient` does not need the `client` parameter._
8485

85-
- **`int installBridge(boolean force)`**
86+
Set the will message that gets registered on a connect:
8687

87-
Installs the python bridge on the linux processor. Pass `true` to force an update if the code already exists. This function only works in conjunction with the `YunMQTTClient` object. A return value of 0 means that there was an error while installing, 1 means that the bridge is already installed and 2 means that there was an update.
88+
```c++
89+
void setWill(const char * topic);
90+
void setWill(const char * topic, const char * payload);
91+
```
8892
89-
- **`void setWill(const char * topic)`**
90-
- **`void setWill(const char * topic, const char * payload)`**
93+
Connect to broker using the supplied client id and an optional username and password:
9194
92-
Sets the will message that gets registered on a connect.
95+
```c++
96+
boolean connect(const char * clientId);
97+
boolean connect(const char * clientId, const char * username, const char * password);
98+
```
9399

94-
- **`boolean connect(const char * clientId)`**
95-
- **`boolean connect(const char * clientId, const char* username, const char* password)`**
100+
_This functions returns a value that indicates if the connection has been established successfully._
96101

97-
Connects to broker using the supplied client id and an optional username and password. This functions return value indicates if the connection has been established successfully.
102+
Publishes a message to the broker with an optional payload:
98103

99-
- **`void publish(String topic)`**
100-
- **`void publish(String topic, String payload)`**
101-
- **`void publish(const char * topic, String payload)`**
102-
- **`void publish(const char * topic, const char * payload)`**
104+
```c++
105+
void publish(String topic);
106+
void publish(String topic, String payload);
107+
void publish(const char * topic, String payload);
108+
void publish(const char * topic, const char * payload);
109+
```
103110
104-
Publishes a message to the broker with an optional payload.
111+
Subscribe to a topic:
105112
106-
- **`void subscribe(String topic)`**
107-
- **`void subscribe(const char * topic)`**
113+
```c++
114+
void subscribe(String topic);
115+
void subscribe(const char * topic);
116+
```
108117

109-
Subscribes to a topic.
118+
Unsubscribe from a topic:
110119

111-
- **`void unsubscribe(String topic)`**
112-
- **`void unsubscribe(const char * topic)`**
120+
```c++
121+
void unsubscribe(String topic);
122+
void unsubscribe(const char * topic);
123+
```
113124
114-
Unsubscribes from a topic.
125+
Sends and receives packets:
115126
116-
- **`void loop()`**
127+
```c++
128+
void loop();
129+
```
117130

118-
Sends and receives packets. This function should be called as often as possible.
131+
_This function should be called in every `loop`._
119132

120-
- **`boolean connected()`**
133+
Check if the client is currently connected:
121134

122-
Checks if the client is currently connected.
135+
```c++
136+
boolean connected();
137+
```
123138

124-
- **`void disconnect()`**
139+
Disconnects from the broker:
125140

126-
Disconnects from the broker.
141+
```c++
142+
void disconnect();
143+
```

examples/MQTTClient/MQTTClient.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
#include <MQTTClient.h>
44

55
YunClient net;
6-
MQTTClient client("broker.shiftr.io", net);
6+
MQTTClient client;
77

88
unsigned long lastMillis = 0;
99

1010
void setup() {
1111
Bridge.begin();
1212
Serial.begin(9600);
13+
client.begin("broker.shiftr.io", net);
14+
1315
Serial.println("connecting...");
1416
if (client.connect("arduino", "try", "try")) {
1517
Serial.println("connected!");
@@ -22,7 +24,7 @@ void setup() {
2224

2325
void loop() {
2426
client.loop();
25-
// Publish a message roughly every second.
27+
// publish a message roughly every second.
2628
if(millis() - lastMillis > 1000) {
2729
lastMillis = millis();
2830
client.publish("/hello", "world");

examples/YunMQTTClient/YunMQTTClient.ino

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
#include <Bridge.h>
22
#include <YunMQTTClient.h>
33

4-
YunMQTTClient client("broker.shiftr.io");
4+
YunMQTTClient client;
55

66
unsigned long lastMillis = 0;
77

88
void setup() {
99
Bridge.begin();
1010
Serial.begin(9600);
11-
12-
// This will install the required python files (pass true to force update).
13-
// Line can also be commented out to save program space.
14-
// If you update the library you also need to update the bridge!
15-
switch(client.installBridge(false)) {
16-
case 0: Serial.println("error while installing bridge!"); break;
17-
case 1: Serial.println("bridge already installed!"); break;
18-
case 2: Serial.println("bridge updated!"); break;
19-
}
11+
client.begin("broker.shiftr.io");
2012

2113
Serial.println("connecting...");
2214
if (client.connect("arduino", "try", "try")) {

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MQTT
2-
version=1.7.0
2+
version=1.8.0
33
author=Joel Gaehwiler <joel.gaehwiler@gmail.com>
44
maintainer=Joel Gaehwiler <joel.gaehwiler@gmail.com>
55
sentence=MQTT library for Arduino based on the Eclipse Paho projects.

src/MQTTClient.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ void messageArrived(MQTT::MessageData& messageData) {
1717

1818
MQTTClient::MQTTClient() {}
1919

20-
MQTTClient::MQTTClient(const char * hostname, int port, Client& client) {
21-
this->begin(hostname, port, client);
22-
}
23-
2420
void MQTTClient::begin(const char * hostname, Client& client) {
2521
this->begin(hostname, 1883, client);
2622
}

src/MQTTClient.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class MQTTClient {
2626
int port;
2727
public:
2828
MQTTClient();
29-
MQTTClient(const char * hostname, int port, Client& client);
30-
MQTTClient(const char * hostname, Client& client) : MQTTClient(hostname, 1883, client){};
3129
void begin(const char * hostname, Client& client);
3230
void begin(const char * hostname, int port, Client& client);
3331
void setWill(const char * topic);

src/YunMQTTClient.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,23 @@
44

55
#include <FileIO.h>
66

7-
YunMQTTClient::YunMQTTClient(const char * _hostname, int _port) {
8-
this->hostname = _hostname;
9-
this->port = _port;
10-
}
7+
YunMQTTClient::YunMQTTClient() {}
118

12-
int YunMQTTClient::installBridge(boolean force) {
13-
if(!force) {
14-
boolean f1 = FileSystem.exists("/usr/mqtt/mqtt.py");
15-
boolean f2 = FileSystem.exists("/usr/mqtt/bridge.py");
9+
void YunMQTTClient::begin(const char * hostname) {
10+
this->begin(hostname, 1883);
11+
}
1612

17-
if(f1 && f2) {
18-
return 1;
19-
}
20-
}
13+
void YunMQTTClient::begin(const char * hostname, int port) {
14+
this->hostname = hostname;
15+
this->port = port;
16+
}
2117

18+
int YunMQTTClient::updateBridge() {
2219
Process p;
2320

24-
int r1 = p.runShellCommand("mkdir -p /usr/mqtt");
25-
int r2 = p.runShellCommand("wget https://raw.githubusercontent.com/256dpi/arduino-mqtt/master/yun/mqtt.py --no-check-certificate -O /usr/mqtt/mqtt.py");
26-
int r3 = p.runShellCommand("wget https://raw.githubusercontent.com/256dpi/arduino-mqtt/master/yun/bridge.py --no-check-certificate -O /usr/mqtt/bridge.py");
21+
int r1 = p.runShellCommand("mkdir -p /usr/arduino-mqtt");
22+
int r2 = p.runShellCommand("wget -N https://raw.githubusercontent.com/256dpi/arduino-mqtt/v1.8.0/yun/mqtt.py --no-check-certificate -P /usr/arduino-mqtt");
23+
int r3 = p.runShellCommand("wget -N https://raw.githubusercontent.com/256dpi/arduino-mqtt/v1.8.0/yun/bridge.py --no-check-certificate -P /usr/arduino-mqtt");
2724

2825
boolean success = r1 == 0 && r2 == 0 && r3 == 0;
2926

@@ -48,6 +45,10 @@ boolean YunMQTTClient::connect(const char * clientId) {
4845
}
4946

5047
boolean YunMQTTClient::connect(const char * clientId, const char * username, const char * password) {
48+
if(this->updateBridge() == 0) {
49+
return false;
50+
}
51+
5152
this->process.begin("python");
5253
this->process.addParameter("-u");
5354
this->process.addParameter("/usr/mqtt/bridge.py");

src/YunMQTTClient.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ class YunMQTTClient {
1616
const char * willTopic = NULL;
1717
const char * willPayload = NULL;
1818
boolean alive = false;
19+
int updateBridge();
1920
public:
20-
YunMQTTClient(const char * hostname, int port);
21-
YunMQTTClient(const char * hostname) : YunMQTTClient(hostname, 1883){};
22-
int installBridge(boolean force);
21+
YunMQTTClient();
22+
void begin(const char * hostname);
23+
void begin(const char * hostname, int port);
2324
void setWill(const char * topic);
2425
void setWill(const char * topic, const char * payload);
2526
boolean connect(const char * clientId);

yun/mqtt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def _socketpair_compat():
264264
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
265265
sock1.setblocking(0)
266266
try:
267-
sock1.connect(("localhost", port))
267+
sock1.connect(("127.0.0.1", port))
268268
except socket.error as err:
269269
if err.errno != errno.EINPROGRESS and err.errno != errno.EWOULDBLOCK and err.errno != EAGAIN:
270270
raise

0 commit comments

Comments
 (0)