Implemented user/pass auth as per MQTT v3.1 specs

This commit is contained in:
Wil 2012-04-04 03:44:45 -04:00
parent 18fe49c070
commit 87fb3a9895
6 changed files with 74 additions and 5 deletions

0
PubSubClient/CHANGES.txt Normal file → Executable file
View File

0
PubSubClient/LICENSE.txt Normal file → Executable file
View File

38
PubSubClient/PubSubClient.cpp Normal file → Executable file
View File

@ -24,10 +24,19 @@ PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,u
} }
boolean PubSubClient::connect(char *id) { boolean PubSubClient::connect(char *id) {
return connect(id,0,0,0,0); return connect(id,NULL,NULL,0,0,0,0);
} }
boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) { boolean PubSubClient::connect(char *id, char *user, char *pass) {
return connect(id,user,pass,0,0,0,0);
}
boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage)
{
return connect(id,NULL,NULL,0,0,0,0);
}
boolean PubSubClient::connect(char *id, char *user, char *pass, char* willTopic, uint8_t willQos, uint8_t willRetain, char* willMessage) {
if (!connected()) { if (!connected()) {
int result = 0; int result = 0;
@ -45,11 +54,24 @@ boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_
for (j = 0;j<9;j++) { for (j = 0;j<9;j++) {
buffer[length++] = d[j]; buffer[length++] = d[j];
} }
uint8_t v;
if (willTopic) { if (willTopic) {
buffer[length++] = 0x06|(willQos<<3)|(willRetain<<5); v = 0x06|(willQos<<3)|(willRetain<<5);
} else { } else {
buffer[length++] = 0x02; v = 0x02;
} }
if(user != NULL) {
v = v|0x80;
if(pass != NULL) {
v = v|(0x80>>1);
}
}
buffer[length++] = v;
buffer[length++] = ((MQTT_KEEPALIVE) >> 8); buffer[length++] = ((MQTT_KEEPALIVE) >> 8);
buffer[length++] = ((MQTT_KEEPALIVE) & 0xff); buffer[length++] = ((MQTT_KEEPALIVE) & 0xff);
length = writeString(id,buffer,length); length = writeString(id,buffer,length);
@ -57,6 +79,14 @@ boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_
length = writeString(willTopic,buffer,length); length = writeString(willTopic,buffer,length);
length = writeString(willMessage,buffer,length); length = writeString(willMessage,buffer,length);
} }
if(user != NULL) {
length = writeString(user,buffer,length);
if(pass != NULL) {
length = writeString(pass,buffer,length);
}
}
write(MQTTCONNECT,buffer,length); write(MQTTCONNECT,buffer,length);
lastOutActivity = millis(); lastOutActivity = millis();
lastInActivity = millis(); lastInActivity = millis();

4
PubSubClient/PubSubClient.h Normal file → Executable file
View File

@ -58,7 +58,9 @@ public:
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int)); PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int));
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int)); PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int));
boolean connect(char *); boolean connect(char *);
boolean connect(char*, char*, uint8_t, uint8_t, char*); boolean connect(char *, char *, char *);
boolean connect(char *, char *, uint8_t, uint8_t, char *);
boolean connect(char *, char *, char *, char *, uint8_t, uint8_t, char*);
void disconnect(); void disconnect();
boolean publish(char *, char *); boolean publish(char *, char *);
boolean publish(char *, uint8_t *, unsigned int); boolean publish(char *, uint8_t *, unsigned int);

View File

@ -0,0 +1,37 @@
/*
Basic MQTT example
- connects to an MQTT server
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 172, 16, 0, 2 };
byte ip[] = { 172, 16, 0, 100 };
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
PubSubClient client(server, 1883, callback);
void setup()
{
Ethernet.begin(mac, ip);
if (client.connect("arduinoClient", "testuser", "testpass")) {
client.publish("outTopic","hello world");
client.subscribe("inTopic");
}
}
void loop()
{
client.loop();
}

0
PubSubClient/examples/mqtt_basic/mqtt_basic.pde Normal file → Executable file
View File