Skip to content

Commit c5c0909

Browse files
committed
fix mqtt for new slip protocol
1 parent c1cc9f5 commit c5c0909

File tree

7 files changed

+236
-293
lines changed

7 files changed

+236
-293
lines changed

ELClient/ELClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ typedef enum {
2222
CMD_GET_TIME, // get current time in seconds since the unix epoch
2323
//CMD_GET_INFO,
2424

25-
CMD_MQTT_INIT = 10,
25+
CMD_MQTT_SETUP = 10,
2626
CMD_MQTT_PUBLISH,
2727
CMD_MQTT_SUBSCRIBE,
2828
CMD_MQTT_LWT,

ELClient/ELClientMqtt.cpp

+105-187
Original file line numberDiff line numberDiff line change
@@ -1,197 +1,115 @@
1+
// Copyright (c) 2016 by B. Runnels and T. von Eicken
2+
3+
#include <Arduino.h>
14
#include "ELClientMqtt.h"
25

6+
// constructor
37
ELClientMqtt::ELClientMqtt(ELClient* elc) :_elc(elc) {}
48

5-
boolean ELClientMqtt::init() {
6-
uint16_t crc;
7-
uint32_t cb_data;
8-
9-
crc = _elc->Request(CMD_MQTT_INIT, 0, 1, 4);
10-
11-
cb_data = (uint32_t)&connectedCb;
12-
crc = _elc->Request(crc, (uint8_t*)&cb_data, 4);
13-
14-
cb_data = (uint32_t)&disconnectedCb;
15-
crc = _elc->Request(crc, (uint8_t*)&cb_data, 4);
16-
17-
cb_data = (uint32_t)&publishedCb;
18-
crc = _elc->Request(crc, (uint8_t*)&cb_data, 4);
19-
20-
cb_data = (uint32_t)&dataCb;
21-
crc = _elc->Request(crc, (uint8_t*)&cb_data, 4);
22-
23-
_elc->Request(crc);
24-
25-
if (_elc->WaitReturn() == false || _elc->return_cmd == 0 || _elc->return_value == 0)
26-
return false;
27-
remote_instance = _elc->return_value;
28-
return true;
29-
}
30-
31-
boolean ELClientMqtt::lwt(const char* topic, const char* message, uint8_t qos, uint8_t retain) {
32-
uint16_t crc;
33-
34-
crc = _elc->Request(CMD_MQTT_LWT, 0, 1, 5);
35-
crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
36-
crc = _elc->Request(crc, (uint8_t*)topic, strlen(topic));
37-
crc = _elc->Request(crc, (uint8_t*)message, strlen(message));
38-
crc = _elc->Request(crc, (uint8_t*)&qos, 1);
39-
crc = _elc->Request(crc, (uint8_t*)&retain, 1);
40-
_elc->Request(crc);
41-
if (_elc->WaitReturn() && _elc->return_value)
42-
return true;
43-
return false;
44-
}
45-
46-
boolean ELClientMqtt::lwt(const __FlashStringHelper* topic, const __FlashStringHelper* message, uint8_t qos, uint8_t retain) {
47-
uint16_t crc;
48-
49-
crc = _elc->Request(CMD_MQTT_LWT, 0, 1, 5);
50-
crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
51-
crc = _elc->Request(crc, topic, strlen_P((const char*)topic));
52-
crc = _elc->Request(crc, message, strlen_P((const char*)message));
53-
crc = _elc->Request(crc, (uint8_t*)&qos, 1);
54-
crc = _elc->Request(crc, (uint8_t*)&retain, 1);
55-
_elc->Request(crc);
56-
if (_elc->WaitReturn() && _elc->return_value)
57-
return true;
58-
return false;
59-
}
60-
61-
boolean ELClientMqtt::lwt(const char* topic, const char* message) {
62-
return lwt(topic, message, 0, 0);
63-
}
64-
65-
boolean ELClientMqtt::lwt(const __FlashStringHelper* topic, const __FlashStringHelper* message) {
66-
return lwt(topic, message, 0, 0);
67-
}
68-
69-
//void MQTT::connect(const char* host, uint32_t port, boolean security) {
70-
// uint16_t crc;
71-
// crc = _elc->Request(CMD_MQTT_CONNECT, 0, 0, 4);
72-
// crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
73-
// crc = _elc->Request(crc, (uint8_t*)host, strlen(host));
74-
// crc = _elc->Request(crc, (uint8_t*)&port, 4);
75-
// crc = _elc->Request(crc, (uint8_t*)&security, 1);
76-
// _elc->Request(crc);
77-
//}
78-
//
79-
//void MQTT::connect(const __FlashStringHelper* host, uint32_t port, boolean security) {
80-
// uint16_t crc;
81-
// crc = _elc->Request(CMD_MQTT_CONNECT, 0, 0, 4);
82-
// crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
83-
// crc = _elc->Request(crc, host, strlen_P((const char*)host));
84-
// crc = _elc->Request(crc, (uint8_t*)&port, 4);
85-
// crc = _elc->Request(crc, (uint8_t*)&security, 1);
86-
// _elc->Request(crc);
87-
//}
88-
//
89-
//void MQTT::connect(const char* host, uint32_t port) {
90-
// connect(host, port, false);
91-
//}
92-
//
93-
//void MQTT::connect(const __FlashStringHelper* host, uint32_t port) {
94-
// connect(host, port, false);
95-
//}
96-
97-
//void MQTT::disconnect() {
98-
// uint16_t crc;
99-
// crc = _elc->Request(CMD_MQTT_DISCONNECT, 0, 0, 1);
100-
// crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
101-
// _elc->Request(crc);
102-
//}
9+
// setup -- push callbacks to esp-link
10+
void ELClientMqtt::setup(void) {
11+
Serial.print("ConnectedCB is 0x"); Serial.println((uint32_t)&connectedCb, 16);
12+
_elc->Request(CMD_MQTT_SETUP, 0, 4);
13+
uint32_t cb = (uint32_t)&connectedCb;
14+
_elc->Request(&cb, 4);
15+
cb = (uint32_t)&disconnectedCb;
16+
_elc->Request(&cb, 4);
17+
cb = (uint32_t)&publishedCb;
18+
_elc->Request(&cb, 4);
19+
cb = (uint32_t)&dataCb;
20+
_elc->Request(&cb, 4);
21+
_elc->Request();
22+
}
23+
24+
// LWT
25+
26+
void ELClientMqtt::lwt(const char* topic, const char* message, uint8_t qos, uint8_t retain) {
27+
_elc->Request(CMD_MQTT_LWT, 0, 4);
28+
_elc->Request(topic, strlen(topic));
29+
_elc->Request(message, strlen(message));
30+
_elc->Request(&qos, 1);
31+
_elc->Request(&retain, 1);
32+
_elc->Request();
33+
}
34+
35+
void ELClientMqtt::lwt(const __FlashStringHelper* topic, const __FlashStringHelper* message,
36+
uint8_t qos, uint8_t retain)
37+
{
38+
_elc->Request(CMD_MQTT_LWT, 0, 4);
39+
_elc->Request(topic, strlen_P((const char*)topic));
40+
_elc->Request(message, strlen_P((const char*)message));
41+
_elc->Request(&qos, 1);
42+
_elc->Request(&retain, 1);
43+
_elc->Request();
44+
}
45+
46+
// SUBSCRIBE
10347

10448
void ELClientMqtt::subscribe(const char* topic, uint8_t qos) {
105-
uint16_t crc;
106-
crc = _elc->Request(CMD_MQTT_SUBSCRIBE, 0, 0, 3);
107-
crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
108-
crc = _elc->Request(crc, (uint8_t*)topic, strlen(topic));
109-
crc = _elc->Request(crc, (uint8_t*)&qos, 1);
110-
_elc->Request(crc);
49+
_elc->Request(CMD_MQTT_SUBSCRIBE, 0, 2);
50+
_elc->Request(topic, strlen(topic));
51+
_elc->Request(&qos, 1);
52+
_elc->Request();
11153
}
11254

11355
void ELClientMqtt::subscribe(const __FlashStringHelper* topic, uint8_t qos) {
114-
uint16_t crc;
115-
crc = _elc->Request(CMD_MQTT_SUBSCRIBE, 0, 0, 3);
116-
crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
117-
crc = _elc->Request(crc, topic, strlen_P((const char*)topic));
118-
crc = _elc->Request(crc, (uint8_t*)&qos, 1);
119-
_elc->Request(crc);
120-
}
121-
122-
void ELClientMqtt::subscribe(const char* topic) {
123-
subscribe(topic, 0);
124-
}
125-
126-
void ELClientMqtt::subscribe(const __FlashStringHelper* topic) {
127-
subscribe(topic, 0);
128-
}
129-
130-
void ELClientMqtt::publish(const char* topic, const uint8_t* data, const uint16_t len, uint8_t qos, uint8_t retain) {
131-
uint16_t crc;
132-
crc = _elc->Request(CMD_MQTT_PUBLISH, 0, 0, 6);
133-
crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
134-
crc = _elc->Request(crc, (uint8_t*)topic, strlen(topic));
135-
crc = _elc->Request(crc, (uint8_t*)data, len);
136-
crc = _elc->Request(crc, (uint8_t*)&len, 2);
137-
crc = _elc->Request(crc, (uint8_t*)&qos, 1);
138-
crc = _elc->Request(crc, (uint8_t*)&retain, 1);
139-
_elc->Request(crc);
140-
}
141-
142-
void ELClientMqtt::publish(const __FlashStringHelper* topic, const __FlashStringHelper* data, const uint16_t len, uint8_t qos, uint8_t retain) {
143-
uint16_t crc;
144-
crc = _elc->Request(CMD_MQTT_PUBLISH, 0, 0, 6);
145-
crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
146-
crc = _elc->Request(crc, topic, strlen_P((const char*)topic));
147-
crc = _elc->Request(crc, data, len);
148-
crc = _elc->Request(crc, (uint8_t*)&len, 2);
149-
crc = _elc->Request(crc, (uint8_t*)&qos, 1);
150-
crc = _elc->Request(crc, (uint8_t*)&retain, 1);
151-
_elc->Request(crc);
152-
}
153-
154-
void ELClientMqtt::publish(const char* topic, const __FlashStringHelper* data, const uint16_t len, uint8_t qos, uint8_t retain) {
155-
uint16_t crc;
156-
crc = _elc->Request(CMD_MQTT_PUBLISH, 0, 0, 6);
157-
crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
158-
crc = _elc->Request(crc, (uint8_t*)topic, strlen(topic));
159-
crc = _elc->Request(crc, data, len);
160-
crc = _elc->Request(crc, (uint8_t*)&len, 2);
161-
crc = _elc->Request(crc, (uint8_t*)&qos, 1);
162-
crc = _elc->Request(crc, (uint8_t*)&retain, 1);
163-
_elc->Request(crc);
164-
}
165-
166-
void ELClientMqtt::publish(const __FlashStringHelper* topic, const uint8_t* data, const uint16_t len, uint8_t qos, uint8_t retain) {
167-
uint16_t crc;
168-
crc = _elc->Request(CMD_MQTT_PUBLISH, 0, 0, 6);
169-
crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4);
170-
crc = _elc->Request(crc, topic, strlen_P((const char*)topic));
171-
crc = _elc->Request(crc, (uint8_t*)data, len);
172-
crc = _elc->Request(crc, (uint8_t*)&len, 2);
173-
crc = _elc->Request(crc, (uint8_t*)&qos, 1);
174-
crc = _elc->Request(crc, (uint8_t*)&retain, 1);
175-
_elc->Request(crc);
56+
_elc->Request(CMD_MQTT_SUBSCRIBE, 0, 2);
57+
_elc->Request(topic, strlen_P((const char*)topic));
58+
_elc->Request(&qos, 1);
59+
_elc->Request();
60+
}
61+
62+
// PUBLISH
63+
64+
void ELClientMqtt::publish(const char* topic, const uint8_t* data, const uint16_t len,
65+
uint8_t qos, uint8_t retain)
66+
{
67+
_elc->Request(CMD_MQTT_PUBLISH, 0, 5);
68+
_elc->Request(topic, strlen(topic));
69+
_elc->Request(data, len);
70+
_elc->Request(&len, 2);
71+
_elc->Request(&qos, 1);
72+
_elc->Request(&retain, 1);
73+
_elc->Request();
74+
}
75+
76+
void ELClientMqtt::publish(const char* topic, const char* data, uint8_t qos, uint8_t retain)
77+
{
78+
publish(topic, (uint8_t*)data, strlen(data), qos, retain);
79+
}
80+
81+
void ELClientMqtt::publish(const __FlashStringHelper* topic, const __FlashStringHelper* data,
82+
const uint16_t len, uint8_t qos, uint8_t retain)
83+
{
84+
_elc->Request(CMD_MQTT_PUBLISH, 0, 5);
85+
_elc->Request(topic, strlen_P((const char*)topic));
86+
_elc->Request(data, len);
87+
_elc->Request(&len, 2);
88+
_elc->Request(&qos, 1);
89+
_elc->Request(&retain, 1);
90+
_elc->Request();
91+
}
92+
93+
void ELClientMqtt::publish(const char* topic, const __FlashStringHelper* data,
94+
const uint16_t len, uint8_t qos, uint8_t retain)
95+
{
96+
_elc->Request(CMD_MQTT_PUBLISH, 0, 5);
97+
_elc->Request(topic, strlen(topic));
98+
_elc->Request(data, len);
99+
_elc->Request(&len, 2);
100+
_elc->Request(&qos, 1);
101+
_elc->Request(&retain, 1);
102+
_elc->Request();
103+
}
104+
105+
void ELClientMqtt::publish(const __FlashStringHelper* topic, const uint8_t* data,
106+
const uint16_t len, uint8_t qos, uint8_t retain)
107+
{
108+
_elc->Request(CMD_MQTT_PUBLISH, 0, 5);
109+
_elc->Request(topic, strlen_P((const char*)topic));
110+
_elc->Request(data, len);
111+
_elc->Request(&len, 2);
112+
_elc->Request(&qos, 1);
113+
_elc->Request(&retain, 1);
114+
_elc->Request();
176115
}
177-
178-
void ELClientMqtt::publish(const char* topic, const char* data, uint8_t qos, uint8_t retain) {
179-
publish(topic, (const uint8_t*)data, strlen(data), qos, retain);
180-
}
181-
182-
void ELClientMqtt::publish(const char* topic, const char* data) {
183-
publish(topic, (const uint8_t*)data, strlen(data), 0, 0);
184-
}
185-
186-
void ELClientMqtt::publish(const __FlashStringHelper* topic, const __FlashStringHelper* data) {
187-
publish(topic, data, strlen_P((const char*)data), 0, 0);
188-
}
189-
190-
void ELClientMqtt::publish(const char* topic, const __FlashStringHelper* data) {
191-
publish(topic, data, strlen_P((const char*)data), 0, 0);
192-
}
193-
194-
void ELClientMqtt::publish(const __FlashStringHelper* topic, const char* data) {
195-
publish(topic, (const uint8_t*)data, strlen(data), 0, 0);
196-
}
197-

ELClient/ELClientMqtt.h

+44-25
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,59 @@
1+
// Copyright (c) 2016 by B. Runnels and T. von Eicken
2+
13
#ifndef _EL_CLIENT_MQTT_H_
24
#define _EL_CLIENT_MQTT_H_
35

46
#include <stdint.h>
5-
#include <Arduino.h>
67
#include "FP.h"
78
#include "ELClient.h"
89

10+
// Class to send and receive MQTT messages. This class should be used with a singleton object
11+
// because the esp-link implementation currently only supports a single MQTT server, so there is
12+
// no value in instantiating multiple ELClientMqtt objects (although it's possible).
13+
// All the server settings are made in esp-link and esp-link takes care to automatically
14+
// reconnect and retry if the connection is lost. This means that on the arduino side the only
15+
// code that is necessary is to send and receive messsages.
916
class ELClientMqtt {
1017
public:
18+
// Initialize with an ELClient object
1119
ELClientMqtt(ELClient* elc);
12-
boolean init();
13-
boolean lwt(const char* topic, const char* message, uint8_t qos, uint8_t retain);
14-
boolean lwt(const __FlashStringHelper* topic, const __FlashStringHelper* message, uint8_t qos, uint8_t retain);
15-
boolean lwt(const char* topic, const char* message);
16-
boolean lwt(const __FlashStringHelper* topic, const __FlashStringHelper* message);
17-
void subscribe(const char* topic, uint8_t qos);
18-
void subscribe(const __FlashStringHelper* topic, uint8_t qos);
19-
void subscribe(const char* topic);
20-
void subscribe(const __FlashStringHelper* topic);
21-
void publish(const char* topic, const uint8_t* data, const uint16_t len, uint8_t qos, uint8_t retain);
22-
void publish(const __FlashStringHelper* topic, const __FlashStringHelper* data, const uint16_t len, uint8_t qos, uint8_t retain);
23-
void publish(const char* topic, const __FlashStringHelper* data, const uint16_t len, uint8_t qos, uint8_t retain);
24-
void publish(const __FlashStringHelper* topic, const uint8_t* data, const uint16_t len, uint8_t qos, uint8_t retain);
25-
void publish(const char* topic, const char* data, uint8_t qos, uint8_t retain);
26-
void publish(const char* topic, const char* data);
27-
void publish(const __FlashStringHelper* topic, const __FlashStringHelper* data);
28-
void publish(const char* topic, const __FlashStringHelper* data);
29-
void publish(const __FlashStringHelper* topic, const char* data);
30-
31-
FP<void, void*> connectedCb;
32-
FP<void, void*> disconnectedCb;
33-
FP<void, void*> publishedCb;
34-
FP<void, void*> dataCb;
20+
21+
// setup transmits the set of callbacks to esp-link. It assumes that the desired callbacks
22+
// have previously been attached using something like mqtt->connectedCb.attach(myCallbackFun).
23+
// After setup is called either the connectedCb or the disconnectedCb is invoked to provide
24+
// information about the initial connection status.
25+
void setup(void);
26+
27+
// callbacks that can be attached prior to calling setup
28+
FP<void, void*> connectedCb; // callback with no args when MQTT is connected
29+
FP<void, void*> disconnectedCb; // callback with no args when MQTT is disconnected
30+
FP<void, void*> publishedCb; // callback when a message is published (should this be for qos=1 only?)
31+
FP<void, void*> dataCb; // callback when a message is received, called with two arguments:
32+
// the topic and the message (max ~110 bytes for both)
33+
34+
// subscribe to a topic, the default qos is 0. When messages are recevied for the topic the
35+
// data callback is invoked.
36+
void subscribe(const char* topic, uint8_t qos=0);
37+
void subscribe(const __FlashStringHelper* topic, uint8_t qos=0);
38+
39+
// publish a message to a topic
40+
void publish(const char* topic, const uint8_t* data,
41+
const uint16_t len, uint8_t qos=0, uint8_t retain=0);
42+
void publish(const char* topic, const char* data,
43+
uint8_t qos=0, uint8_t retain=0);
44+
void publish(const __FlashStringHelper* topic, const __FlashStringHelper* data,
45+
const uint16_t len, uint8_t qos=0, uint8_t retain=0);
46+
void publish(const char* topic, const __FlashStringHelper* data,
47+
const uint16_t len, uint8_t qos=0, uint8_t retain=0);
48+
void publish(const __FlashStringHelper* topic, const uint8_t* data,
49+
const uint16_t len, uint8_t qos=0, uint8_t retain=0);
50+
51+
// set a last-will topic & message
52+
void lwt(const char* topic, const char* message, uint8_t qos=0, uint8_t retain=0);
53+
void lwt(const __FlashStringHelper* topic, const __FlashStringHelper* message,
54+
uint8_t qos=0, uint8_t retain=0);
3555

3656
private:
37-
uint32_t remote_instance;
3857
ELClient* _elc;
3958
};
4059

0 commit comments

Comments
 (0)