publish works (QoS 0)

This commit is contained in:
ladyada 2015-05-31 23:19:13 -04:00
parent 0a943076c9
commit 1a0e611e01
4 changed files with 96 additions and 13 deletions

View File

@ -67,20 +67,50 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
p[1] = 0;
p+=2;
packet[1] = totallen;
return totallen+2;
}
uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, char *topic, char *data, uint8_t qos) {
uint8_t *p = packet;
uint16_t len = strlen(topic);
Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, char *feed) {
mqtt = mqttserver;
strncpy(feedname, feed, FEEDNAME_SIZE);
feedname[FEEDNAME_SIZE-1] = 0;
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
// fill in packet[1] last
p+=2;
p[0] = len >> 8;
p++;
p[0] = len & 0xFF;
p++;
memcpy(p, topic, len);
p+=len;
memcpy(p, data, strlen(data));
p+=strlen(data);
len = p - packet;
packet[1] = len-2;
return len;
}
Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT &mqttserver, char *feed, uint8_t q) {
mqtt = &mqttserver;
strncpy(topic, feed, FEEDNAME_SIZE);
topic[FEEDNAME_SIZE-1] = 0;
qos = q;
errno = 0;
}
bool Adafruit_MQTT_Publish::publish(int32_t i) {
char payload[18];
itoa(i, payload, 10);
return mqtt->publish(topic, payload, qos);
}
bool Adafruit_MQTT_Publish::publish(uint32_t i) {
char payload[18];
itoa(i, payload, 10);
return mqtt->publish(topic, payload, qos);
}

View File

@ -11,13 +11,18 @@
#define MQTT_CTRL_CONNECT 0x1
#define MQTT_CTRL_CONNECTACK 0x2
#define MQTT_CTRL_PUBLISH 0x3
#define MQTT_QOS_1 0x1
#define MQTT_QOS_0 0x0
#define SERVERNAME_SIZE 25
#define USERNAME_SIZE 25
#define KEY_SIZE 41
#define FEEDNAME_SIZE 25
#define FEEDNAME_SIZE 65
#define CONNECT_TIMEOUT_MS 3000
#define PUBLISH_TIMEOUT_MS 500
#define MQTT_CONN_USERNAMEFLAG 0x80
#define MQTT_CONN_PASSWORDFLAG 0x40
@ -27,12 +32,16 @@
#define MQTT_CONN_CLEANSESSION 0x02
#define MQTT_CONN_KEEPALIVE 15 // in seconds
#define MAXBUFFERSIZE 200
#define MAXBUFFERSIZE 130
class Adafruit_MQTT {
public:
Adafruit_MQTT(char *server, uint16_t port, char *user, char *key);
uint8_t connectPacket(uint8_t *packet);
virtual boolean publish(char *topic, char *payload, uint8_t qos) {}
uint8_t publishPacket(uint8_t *packet, char *topic, char *payload, uint8_t qos);
protected:
int8_t errno;
char servername[SERVERNAME_SIZE];
@ -46,15 +55,17 @@ class Adafruit_MQTT {
class Adafruit_MQTT_Publish {
public:
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, char *feed);
Adafruit_MQTT_Publish(Adafruit_MQTT& mqttserver, char *feed, uint8_t qos = 0);
bool publish(char *s);
bool publish(double f);
//bool publish(char *s);
//bool publish(double f);
bool publish(int32_t i);
bool publish(uint32_t i);
private:
Adafruit_MQTT *mqtt;
char feedname[FEEDNAME_SIZE];
char topic[FEEDNAME_SIZE];
uint8_t qos;
int8_t errno;
};

View File

@ -105,3 +105,43 @@ uint16_t Adafruit_MQTT_CC3000::readPacket(uint8_t *buffer, uint8_t maxlen, uint1
int32_t Adafruit_MQTT_CC3000::close(void) {
return mqttclient.close();
}
boolean Adafruit_MQTT_CC3000::publish(char *topic, char *data, uint8_t qos) {
uint8_t len = publishPacket(buffer, topic, data, qos);
Serial.println("MQTT publish packet:");
for (uint8_t i=0; i<len; i++) {
if (isprint(buffer[i]))
Serial.write(buffer[i]);
else
Serial.print(" ");
Serial.print(" [0x");
if (buffer[i] < 0x10)
Serial.print("0");
Serial.print(buffer[i],HEX);
Serial.print("], ");
if (i % 8 == 7) Serial.println();
}
Serial.println();
if (mqttclient.connected()) {
uint16_t ret = mqttclient.write(buffer, len);
//Serial.print("returned: "); Serial.println(ret);
if (ret != len) return false;
} else {
Serial.println(F("Connection failed"));
return false;
}
if (qos > 0) {
Serial.println(F("Reply:"));
len = readPacket(buffer, 4, PUBLISH_TIMEOUT_MS);
for (uint8_t i=0; i<len; i++) {
Serial.write(buffer[i]); Serial.print(" [0x"); Serial.print(buffer[i], HEX); Serial.print("], ");
}
Serial.println();
return true;
} else {
return true;
}
}

View File

@ -13,6 +13,8 @@ class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, uint16_t timeout);
int32_t close(void);
boolean publish(char *topic, char *payload, uint8_t qos);
private:
Adafruit_CC3000 *cc3000;
Adafruit_CC3000_Client mqttclient;