Skip to content

Commit 80c0a64

Browse files
committed
Add setTls API for Yun client to support TLS connections
1 parent c25681a commit 80c0a64

File tree

6 files changed

+86
-1
lines changed

6 files changed

+86
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This library is an alternative to the [pubsubclient](https://github.com/knollear
1919
The following examples show how you can use the library with various Arduino compatible hardware:
2020

2121
- [Arduino Yun (MQTTClient)](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_MQTTClient/ArduinoYun_MQTTClient.ino)
22-
- [Arduino Yun (YunMQTTClient)](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_YunMQTTClient/ArduinoYun_YunMQTTClient.ino)
22+
- [Arduino Yun (YunMQTTClient)](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_YunMQTTClient/ArduinoYun_YunMQTTClient.ino) ([SSL](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoYun_YunMQTTClient_SSL/ArduinoYun_YunMQTTClient_SSL.ino))
2323
- [Arduino Ethernet Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoEthernetShield/ArduinoEthernetShield.ino)
2424
- [Arduino WiFi Shield](https://github.com/256dpi/arduino-mqtt/blob/master/examples/ArduinoWiFiShield/ArduinoWiFiShield.ino)
2525
- [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))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// This example uses an Arduino Yun and the
2+
// YunMQTTClient to connect to shiftr.io.
3+
//
4+
// The YunMQTTClient uses a Linux side python
5+
// script to manage the connection which results
6+
// in less program space and memory used on the Arduino.
7+
//
8+
// You can check on your device after a successful
9+
// connection here: https://shiftr.io/try.
10+
//
11+
// by Joël Gähwiler
12+
// https://github.com/256dpi/arduino-mqtt
13+
14+
#include <Bridge.h>
15+
#include <YunMQTTClient.h>
16+
17+
YunMQTTClient client;
18+
19+
unsigned long lastMillis = 0;
20+
21+
void setup() {
22+
Bridge.begin();
23+
Serial.begin(9600);
24+
client.begin("broker.shiftr.io", 8883); // MQTT brokers usually use port 8883 for secure connections
25+
client.setTls("/etc/ssl/certs/AddTrust_External_Root.crt"); // select the CA for the broker
26+
27+
connect();
28+
}
29+
30+
void connect() {
31+
Serial.print("connecting...");
32+
while (!client.connect("arduino", "try", "try")) {
33+
Serial.print(".");
34+
delay(1000);
35+
}
36+
37+
Serial.println("\nconnected!");
38+
39+
client.subscribe("/example");
40+
// client.unsubscribe("/example");
41+
}
42+
43+
void loop() {
44+
client.loop();
45+
46+
if(!client.connected()) {
47+
connect();
48+
}
49+
50+
// publish a message roughly every second.
51+
if(millis() - lastMillis > 1000) {
52+
lastMillis = millis();
53+
client.publish("/hello", "world");
54+
}
55+
}
56+
57+
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
58+
Serial.print("incoming: ");
59+
Serial.print(topic);
60+
Serial.print(" - ");
61+
Serial.print(payload);
62+
Serial.println();
63+
}

src/YunMQTTClient.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ void YunMQTTClient::setWill(const char * topic, const char * payload) {
3636
this->willPayload = payload;
3737
}
3838

39+
void YunMQTTClient::setTls(const char * caCerts) {
40+
this->tlsCaCerts = caCerts;
41+
}
42+
3943
boolean YunMQTTClient::connect(const char * clientId) {
4044
return this->connect(clientId, "", "");
4145
}
@@ -61,6 +65,13 @@ boolean YunMQTTClient::connect(const char * clientId, const char * username, con
6165
this->process.print('\n');
6266
}
6367

68+
// set TLS if available
69+
if(strlen(this->tlsCaCerts) > 0) {
70+
this->process.print("t:");
71+
this->process.print(this->tlsCaCerts);
72+
this->process.print(";\n");
73+
}
74+
6475
// send connect request
6576
this->process.print("c:");
6677
this->process.print(this->hostname);

src/YunMQTTClient.h

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class YunMQTTClient {
1515
int port;
1616
const char * willTopic = "";
1717
const char * willPayload = "";
18+
const char * tlsCaCerts = "";
1819
boolean alive = false;
1920
boolean updateBridge();
2021
public:
@@ -23,6 +24,7 @@ class YunMQTTClient {
2324
boolean begin(const char * hostname, int port);
2425
void setWill(const char * topic);
2526
void setWill(const char * topic, const char * payload);
27+
void setTls(const char * caCerts);
2628
boolean connect(const char * clientId);
2729
boolean connect(const char * clientId, const char* username, const char* password);
2830
void publish(String topic);

yun/abi.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The following commands are exchanged between the python script and the arduino l
88
---|-------------|------------------------------------
99
<- | boot | `b;`
1010
-> | will | `w:topic:payload_len;(payload)`
11+
-> | tls | `t:ca_certs;`
1112
-> | connect | `c:host:port:id:(user):(pass);`
1213
<- | approved | `a;`
1314
<- | rejected | `r;`

yun/bridge.py

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def __init__(self):
1111
self.client = None
1212
self.will_topic = ""
1313
self.will_payload = ""
14+
self.tls_ca_certs = ""
1415
self.stopped = False
1516

1617
# Bridge Callbacks
@@ -29,6 +30,8 @@ def parse_command(self, line):
2930
remaining = segments[1:]
3031
if cmd == 'w':
3132
self.do_will(remaining)
33+
elif cmd == 't':
34+
self.do_tls(remaining)
3235
elif cmd == 'c':
3336
self.do_connect(remaining)
3437
elif cmd == 's':
@@ -48,6 +51,9 @@ def do_will(self, args):
4851
self.will_topic = args[0]
4952
self.will_payload = self.read_chunk(int(args[1]))
5053

54+
def do_tls(self, args):
55+
self.tls_ca_certs = args[0]
56+
5157
def do_connect(self, args):
5258
self.client = mqtt.Client(args[2])
5359
self.client.on_connect = self.on_connect
@@ -58,6 +64,8 @@ def do_connect(self, args):
5864
if len(self.will_topic) > 0:
5965
self.client.will_set(self.will_topic, self.will_payload)
6066
try:
67+
if len(self.tls_ca_certs) > 0:
68+
self.client.tls_set(self.tls_ca_certs)
6169
self.client.connect(args[0], int(args[1]))
6270
self.client.loop_start()
6371
except:

0 commit comments

Comments
 (0)