Skip to content

Commit 6e2481d

Browse files
committed
finished implementation
1 parent 343762b commit 6e2481d

File tree

4 files changed

+102
-48
lines changed

4 files changed

+102
-48
lines changed
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+
}

src/YunMQTTClient.cpp

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
11
#include "YunMQTTClient.h"
22

3-
//void messageArrived(MQTT::MessageData& messageData) {
4-
// MQTT::Message &message = messageData.message;
5-
//
6-
// // null terminate topic to create String object
7-
// int len = messageData.topicName.lenstring.len;
8-
// char topic[len+1];
9-
// memcpy(topic, messageData.topicName.lenstring.data, (size_t)len);
10-
// topic[len] = '\0';
11-
//
12-
// // null terminate payload
13-
// char * payload = (char *)message.payload;
14-
// payload[message.payloadlen] = '\0';
15-
// messageReceived(String(topic), String(payload), (char*)message.payload, (unsigned int)message.payloadlen);
16-
//}
17-
183
YunMQTTClient::YunMQTTClient(const char * _hostname, int _port) {
19-
// this->client = new MQTT::Client<Network, Timer, MQTT_BUFFER_SIZE, 0>(this->network);
20-
// this->network.setClient(&_client);
21-
// this->client->setDefaultMessageHandler(messageArrived);
22-
// this->hostname = _hostname;
23-
// this->port = _port;
4+
this->hostname = _hostname;
5+
this->port = _port;
246
}
257

268
boolean YunMQTTClient::connect(const char * clientId) {
27-
// return this->connect(clientId, "", "");
9+
return this->connect(clientId, "", "");
2810
}
2911

3012
boolean YunMQTTClient::connect(const char * clientId, const char * username, const char * password) {
31-
// if(!this->network.connect((char*)this->hostname, this->port)) {
32-
// return false;
33-
// }
34-
//
35-
// MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
36-
// data.clientID.cstring = (char*)clientId;
37-
// if(username && password) {
38-
// data.username.cstring = (char*)username;
39-
// data.password.cstring = (char*)password;
40-
// }
41-
//
42-
// return this->client->connect(data) == 0;
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+
if(strlen(username) > 0 && strlen(password) > 0) {
28+
this->process.print(':');
29+
this->process.print(username);
30+
this->process.print(':');
31+
this->process.print(password);
32+
}
33+
this->process.print('\n');
34+
35+
// wait for answer
36+
String ret = this->process.readStringUntil('\n');
37+
return ret.equals("ca");
4338
}
4439

4540
void YunMQTTClient::publish(String topic) {
@@ -51,43 +46,62 @@ void YunMQTTClient::publish(String topic, String payload) {
5146
}
5247

5348
void YunMQTTClient::publish(const char * topic, String payload) {
54-
// this->publish(topic, payload.c_str());
49+
this->publish(topic, payload.c_str());
5550
}
5651

5752
void YunMQTTClient::publish(const char * topic, const char * payload) {
58-
// MQTT::Message message;
59-
// message.qos = MQTT::QOS0;
60-
// message.retained = false;
61-
// message.dup = false;
62-
// message.payload = (char*)payload;
63-
// message.payloadlen = strlen(payload);
64-
// client->publish(topic, message);
53+
// send publish request
54+
this->process.print("p:");
55+
this->process.print(topic);
56+
this->process.print(':');
57+
this->process.print(payload);
58+
this->process.print('\n');
6559
}
6660

6761
void YunMQTTClient::subscribe(String topic) {
6862
this->subscribe(topic.c_str());
6963
}
7064

7165
void YunMQTTClient::subscribe(const char * topic) {
72-
// client->subscribe(topic, MQTT::QOS0, NULL);
66+
// send subscribe request
67+
this->process.print("s:");
68+
this->process.print(topic);
69+
this->process.print('\n');
7370
}
7471

7572
void YunMQTTClient::unsubscribe(String topic) {
7673
this->unsubscribe(topic.c_str());
7774
}
7875

7976
void YunMQTTClient::unsubscribe(const char * topic) {
80-
// client->unsubscribe(topic);
77+
// send unsubscribe request
78+
this->process.print("u:");
79+
this->process.print(topic);
80+
this->process.print('\n');
8181
}
8282

8383
void YunMQTTClient::loop() {
84-
// this->client->yield();
84+
int av = this->process.available();
85+
if(av > 0) {
86+
String ret = process.readStringUntil('\n');
87+
88+
if(ret.charAt(0) == 'm') {
89+
int startTopic = 2;
90+
int endTopic = ret.indexOf(':', startTopic + 1);
91+
int startPayload = endTopic + 1;
92+
int endPayload = ret.indexOf(':', startPayload + 1);
93+
String topic = ret.substring(startTopic, endTopic);
94+
String payload = ret.substring(startPayload, endPayload);
95+
messageReceived(topic, payload, (char*)payload.c_str(), payload.length());
96+
}
97+
}
8598
}
8699

87100
boolean YunMQTTClient::connected() {
88-
// return this->client->isConnected();
101+
//TODO: fix!
102+
return true;
89103
}
90104

91105
void YunMQTTClient::disconnect() {
92-
// this->client->disconnect();
106+
//TODO: implement!
93107
}

src/YunMQTTClient.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
#define YUN_MQTT_CLIENT_H
33

44
#include <Arduino.h>
5-
#include <Brdige.h>
65
#include <Process.h>
76

87
void messageReceived(String topic, String payload, char * bytes, unsigned int length);
98

109
class YunMQTTClient {
10+
private:
11+
Process process;
12+
const char * hostname;
13+
int port;
1114
public:
1215
YunMQTTClient(const char * hostname, int port);
1316
boolean connect(const char * clientId);

yun/abi.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ Command | Description | Format
99
← | connection denied | `cd`
1010
→ | subscribe | `s:(topic)`
1111
→ | unsubscribe | `u:(topic)`
12-
→ | publish | `s:(topic):(data)`
12+
→ | publish | `p:(topic):(data)`
1313
← | message | `m:(topic):(data)`
1414
→ | disconnect | `d`
1515

1616
Todo:
1717

1818
- length prefix data (may contain `:`)
19+
- add client id

0 commit comments

Comments
 (0)