Skip to content

Commit

Permalink
Merge pull request #10 from 256dpi/major-update
Browse files Browse the repository at this point in the history
Major Update
  • Loading branch information
256dpi committed Sep 29, 2015
2 parents c47902f + 00732a3 commit 02bd57a
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 73 deletions.
87 changes: 52 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The first release of the library only supports QoS0 and the basic features to ge

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.

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

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

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

YunClient net;
MQTTClient client("broker.shiftr.io", net);
MQTTClient client;

unsigned long lastMillis = 0;

void setup() {
Bridge.begin();
Serial.begin(9600);
client.begin("broker.shiftr.io", net);

Serial.println("connecting...");
if (client.connect("arduino", "try", "try")) {
Serial.println("connected!");
Expand All @@ -54,7 +56,7 @@ void setup() {

void loop() {
client.loop();
// publish message roughly every second
// publish a message roughly every second.
if(millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/hello", "world");
Expand All @@ -72,55 +74,70 @@ void messageReceived(String topic, String payload, char * bytes, unsigned int le
## API
- **`MQTTClient(const char * hostname, Client& client)`**
- **`MQTTClient(const char * hostname, int port, Client& client)`**
Initialize the object using the hostname of the broker, the brokers port (default: `1883`) and the underlying Client class for network transport:
Constructor for the `MQTTClient` object using the hostname of the broker, the brokers port (default: `1883`) and the underlying Client class for network transport.
- **`YunMQTTClient(const char * hostname)`**
- **`YunMQTTClient(const char * hostname, int port)`**
```c++
void begin(const char * hostname, Client& client);
void begin(const char * hostname, int port, Client& client);
```

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

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

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.
```c++
void setWill(const char * topic);
void setWill(const char * topic, const char * payload);
```
- **`void setWill(const char * topic)`**
- **`void setWill(const char * topic, const char * payload)`**
Connect to broker using the supplied client id and an optional username and password:
Sets the will message that gets registered on a connect.
```c++
boolean connect(const char * clientId);
boolean connect(const char * clientId, const char * username, const char * password);
```

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

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.
Publishes a message to the broker with an optional payload:

- **`void publish(String topic)`**
- **`void publish(String topic, String payload)`**
- **`void publish(const char * topic, String payload)`**
- **`void publish(const char * topic, const char * payload)`**
```c++
void publish(String topic);
void publish(String topic, String payload);
void publish(const char * topic, String payload);
void publish(const char * topic, const char * payload);
```
Publishes a message to the broker with an optional payload.
Subscribe to a topic:
- **`void subscribe(String topic)`**
- **`void subscribe(const char * topic)`**
```c++
void subscribe(String topic);
void subscribe(const char * topic);
```

Subscribes to a topic.
Unsubscribe from a topic:

- **`void unsubscribe(String topic)`**
- **`void unsubscribe(const char * topic)`**
```c++
void unsubscribe(String topic);
void unsubscribe(const char * topic);
```
Unsubscribes from a topic.
Sends and receives packets:
- **`void loop()`**
```c++
void loop();
```

Sends and receives packets. This function should be called as often as possible.
_This function should be called in every `loop`._

- **`boolean connected()`**
Check if the client is currently connected:

Checks if the client is currently connected.
```c++
boolean connected();
```

- **`void disconnect()`**
Disconnects from the broker:

Disconnects from the broker.
```c++
void disconnect();
```
6 changes: 4 additions & 2 deletions examples/MQTTClient/MQTTClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include <MQTTClient.h>

YunClient net;
MQTTClient client("broker.shiftr.io", net);
MQTTClient client;

unsigned long lastMillis = 0;

void setup() {
Bridge.begin();
Serial.begin(9600);
client.begin("broker.shiftr.io", net);

Serial.println("connecting...");
if (client.connect("arduino", "try", "try")) {
Serial.println("connected!");
Expand All @@ -22,7 +24,7 @@ void setup() {

void loop() {
client.loop();
// Publish a message roughly every second.
// publish a message roughly every second.
if(millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/hello", "world");
Expand Down
12 changes: 2 additions & 10 deletions examples/YunMQTTClient/YunMQTTClient.ino
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
#include <Bridge.h>
#include <YunMQTTClient.h>

YunMQTTClient client("broker.shiftr.io");
YunMQTTClient client;

unsigned long lastMillis = 0;

void setup() {
Bridge.begin();
Serial.begin(9600);

// This will install the required python files (pass true to force update).
// Line can also be commented out to save program space.
// If you update the library you also need to update the bridge!
switch(client.installBridge(false)) {
case 0: Serial.println("error while installing bridge!"); break;
case 1: Serial.println("bridge already installed!"); break;
case 2: Serial.println("bridge updated!"); break;
}
client.begin("broker.shiftr.io");

Serial.println("connecting...");
if (client.connect("arduino", "try", "try")) {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=MQTT
version=1.7.0
version=1.8.0
author=Joel Gaehwiler <[email protected]>
maintainer=Joel Gaehwiler <[email protected]>
sentence=MQTT library for Arduino based on the Eclipse Paho projects.
Expand Down
4 changes: 0 additions & 4 deletions src/MQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ void messageArrived(MQTT::MessageData& messageData) {

MQTTClient::MQTTClient() {}

MQTTClient::MQTTClient(const char * hostname, int port, Client& client) {
this->begin(hostname, port, client);
}

void MQTTClient::begin(const char * hostname, Client& client) {
this->begin(hostname, 1883, client);
}
Expand Down
2 changes: 0 additions & 2 deletions src/MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class MQTTClient {
int port;
public:
MQTTClient();
MQTTClient(const char * hostname, int port, Client& client);
MQTTClient(const char * hostname, Client& client) : MQTTClient(hostname, 1883, client){};
void begin(const char * hostname, Client& client);
void begin(const char * hostname, int port, Client& client);
void setWill(const char * topic);
Expand Down
31 changes: 16 additions & 15 deletions src/YunMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@

#include <FileIO.h>

YunMQTTClient::YunMQTTClient(const char * _hostname, int _port) {
this->hostname = _hostname;
this->port = _port;
}
YunMQTTClient::YunMQTTClient() {}

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

if(f1 && f2) {
return 1;
}
}
void YunMQTTClient::begin(const char * hostname, int port) {
this->hostname = hostname;
this->port = port;
}

int YunMQTTClient::updateBridge() {
Process p;

int r1 = p.runShellCommand("mkdir -p /usr/mqtt");
int r2 = p.runShellCommand("wget https://raw.githubusercontent.com/256dpi/arduino-mqtt/master/yun/mqtt.py --no-check-certificate -O /usr/mqtt/mqtt.py");
int r3 = p.runShellCommand("wget https://raw.githubusercontent.com/256dpi/arduino-mqtt/master/yun/bridge.py --no-check-certificate -O /usr/mqtt/bridge.py");
int r1 = p.runShellCommand("mkdir -p /usr/arduino-mqtt");
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");
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");

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

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

boolean YunMQTTClient::connect(const char * clientId, const char * username, const char * password) {
if(this->updateBridge() == 0) {
return false;
}

this->process.begin("python");
this->process.addParameter("-u");
this->process.addParameter("/usr/mqtt/bridge.py");
Expand Down
7 changes: 4 additions & 3 deletions src/YunMQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class YunMQTTClient {
const char * willTopic = NULL;
const char * willPayload = NULL;
boolean alive = false;
int updateBridge();
public:
YunMQTTClient(const char * hostname, int port);
YunMQTTClient(const char * hostname) : YunMQTTClient(hostname, 1883){};
int installBridge(boolean force);
YunMQTTClient();
void begin(const char * hostname);
void begin(const char * hostname, int port);
void setWill(const char * topic);
void setWill(const char * topic, const char * payload);
boolean connect(const char * clientId);
Expand Down
2 changes: 1 addition & 1 deletion yun/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def _socketpair_compat():
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
sock1.setblocking(0)
try:
sock1.connect(("localhost", port))
sock1.connect(("127.0.0.1", port))
except socket.error as err:
if err.errno != errno.EINPROGRESS and err.errno != errno.EWOULDBLOCK and err.errno != EAGAIN:
raise
Expand Down

0 comments on commit 02bd57a

Please sign in to comment.