Added packet type constants and other stylistic changes
This commit is contained in:
parent
ce9fff9349
commit
632c44a4d1
@ -8,10 +8,6 @@
|
|||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
|
||||||
#define MQTTCONNECT 1<<4
|
|
||||||
#define MQTTPUBLISH 3<<4
|
|
||||||
#define MQTTSUBSCRIBE 8<<4
|
|
||||||
|
|
||||||
PubSubClient::PubSubClient() : _client(0) {
|
PubSubClient::PubSubClient() : _client(0) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,7 +22,7 @@ int PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t wi
|
|||||||
if (!connected()) {
|
if (!connected()) {
|
||||||
if (_client.connect()) {
|
if (_client.connect()) {
|
||||||
nextMsgId = 1;
|
nextMsgId = 1;
|
||||||
uint8_t d[9] = {0x00,0x06,0x4d,0x51,0x49,0x73,0x64,0x70,0x03};
|
uint8_t d[9] = {0x00,0x06,'M','Q','I','s','d','p',MQTTPROTOCOLVERSION};
|
||||||
uint8_t length = 0;
|
uint8_t length = 0;
|
||||||
int j;
|
int j;
|
||||||
for (j = 0;j<9;j++) {
|
for (j = 0;j<9;j++) {
|
||||||
@ -93,15 +89,15 @@ int PubSubClient::loop() {
|
|||||||
if (connected()) {
|
if (connected()) {
|
||||||
long t = millis();
|
long t = millis();
|
||||||
if (t - lastActivity > KEEPALIVE) {
|
if (t - lastActivity > KEEPALIVE) {
|
||||||
_client.write(192);
|
_client.write(MQTTPINGREQ);
|
||||||
_client.write((uint8_t)0);
|
_client.write((uint8_t)0);
|
||||||
lastActivity = t;
|
lastActivity = t;
|
||||||
}
|
}
|
||||||
if (_client.available()) {
|
if (_client.available()) {
|
||||||
uint8_t len = readPacket();
|
uint8_t len = readPacket();
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
uint8_t type = buffer[0]>>4;
|
uint8_t type = buffer[0]&0xF0;
|
||||||
if (type == 3) { // PUBLISH
|
if (type == MQTTPUBLISH) {
|
||||||
if (callback) {
|
if (callback) {
|
||||||
uint8_t tl = (buffer[2]<<3)+buffer[3];
|
uint8_t tl = (buffer[2]<<3)+buffer[3];
|
||||||
char topic[tl+1];
|
char topic[tl+1];
|
||||||
@ -113,8 +109,8 @@ int PubSubClient::loop() {
|
|||||||
uint8_t *payload = buffer+4+tl;
|
uint8_t *payload = buffer+4+tl;
|
||||||
callback(topic,payload,len-4-tl);
|
callback(topic,payload,len-4-tl);
|
||||||
}
|
}
|
||||||
} else if (type == 12) { // PINGREG
|
} else if (type == MQTTPINGREQ) {
|
||||||
_client.write(208);
|
_client.write(MQTTPINGRESP);
|
||||||
_client.write((uint8_t)0);
|
_client.write((uint8_t)0);
|
||||||
lastActivity = t;
|
lastActivity = t;
|
||||||
}
|
}
|
||||||
@ -154,9 +150,7 @@ int PubSubClient::publish(char* topic, uint8_t* payload, uint8_t plength, uint8_
|
|||||||
int PubSubClient::write(uint8_t header, uint8_t* buf, uint8_t length) {
|
int PubSubClient::write(uint8_t header, uint8_t* buf, uint8_t length) {
|
||||||
_client.write(header);
|
_client.write(header);
|
||||||
_client.write(length);
|
_client.write(length);
|
||||||
for (int i=0;i<length;i++) {
|
_client.write(buf,length);
|
||||||
_client.write(buf[i]);
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +168,7 @@ void PubSubClient::subscribe(char* topic) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PubSubClient::disconnect() {
|
void PubSubClient::disconnect() {
|
||||||
_client.write(224);
|
_client.write(MQTTDISCONNECT);
|
||||||
_client.write((uint8_t)0);
|
_client.write((uint8_t)0);
|
||||||
_client.stop();
|
_client.stop();
|
||||||
lastActivity = millis();
|
lastActivity = millis();
|
||||||
|
@ -12,6 +12,23 @@
|
|||||||
#define MAX_PACKET_SIZE 128
|
#define MAX_PACKET_SIZE 128
|
||||||
#define KEEPALIVE 15000 // max value = 255000
|
#define KEEPALIVE 15000 // max value = 255000
|
||||||
|
|
||||||
|
// from mqtt-v3r1
|
||||||
|
#define MQTTPROTOCOLVERSION 3
|
||||||
|
#define MQTTCONNECT 1 << 4 // Client request to connect to Server
|
||||||
|
#define MQTTCONNACK 2 << 4 // Connect Acknowledgment
|
||||||
|
#define MQTTPUBLISH 3 << 4 // Publish message
|
||||||
|
#define MQTTPUBACK 4 << 4 // Publish Acknowledgment
|
||||||
|
#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
|
||||||
|
#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
|
||||||
|
#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
|
||||||
|
#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
|
||||||
|
#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
|
||||||
|
#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
|
||||||
|
#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
|
||||||
|
#define MQTTPINGREQ 12 << 4 // PING Request
|
||||||
|
#define MQTTPINGRESP 13 << 4 // PING Response
|
||||||
|
#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
|
||||||
|
#define MQTTReserved 15 << 4 // Reserved
|
||||||
|
|
||||||
class PubSubClient {
|
class PubSubClient {
|
||||||
private:
|
private:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user