Merge pull request #8 from WilHall/master
Implemented user/pass authentication
This commit is contained in:
commit
7e53f612f2
0
PubSubClient/CHANGES.txt
Normal file → Executable file
0
PubSubClient/CHANGES.txt
Normal file → Executable file
0
PubSubClient/LICENSE.txt
Normal file → Executable file
0
PubSubClient/LICENSE.txt
Normal file → Executable file
38
PubSubClient/PubSubClient.cpp
Normal file → Executable file
38
PubSubClient/PubSubClient.cpp
Normal file → Executable file
@ -24,10 +24,19 @@ PubSubClient::PubSubClient(char* domain, uint16_t port, void (*callback)(char*,u
|
||||
}
|
||||
|
||||
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()) {
|
||||
int result = 0;
|
||||
|
||||
@ -45,11 +54,24 @@ boolean PubSubClient::connect(char *id, char* willTopic, uint8_t willQos, uint8_
|
||||
for (j = 0;j<9;j++) {
|
||||
buffer[length++] = d[j];
|
||||
}
|
||||
|
||||
uint8_t v;
|
||||
if (willTopic) {
|
||||
buffer[length++] = 0x06|(willQos<<3)|(willRetain<<5);
|
||||
v = 0x06|(willQos<<3)|(willRetain<<5);
|
||||
} 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) & 0xff);
|
||||
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(willMessage,buffer,length);
|
||||
}
|
||||
|
||||
if(user != NULL) {
|
||||
length = writeString(user,buffer,length);
|
||||
if(pass != NULL) {
|
||||
length = writeString(pass,buffer,length);
|
||||
}
|
||||
}
|
||||
|
||||
write(MQTTCONNECT,buffer,length);
|
||||
lastOutActivity = millis();
|
||||
lastInActivity = millis();
|
||||
|
2
PubSubClient/PubSubClient.h
Normal file → Executable file
2
PubSubClient/PubSubClient.h
Normal file → Executable file
@ -58,7 +58,9 @@ public:
|
||||
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int));
|
||||
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int));
|
||||
boolean connect(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();
|
||||
boolean publish(char *, char *);
|
||||
boolean publish(char *, uint8_t *, unsigned int);
|
||||
|
37
PubSubClient/examples/mqtt_auth/mqtt_auth.pde
Executable file
37
PubSubClient/examples/mqtt_auth/mqtt_auth.pde
Executable 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
0
PubSubClient/examples/mqtt_basic/mqtt_basic.pde
Normal file → Executable file
Loading…
x
Reference in New Issue
Block a user