kopia lustrzana https://github.com/martin-ger/uMQTTBroker
Added authentication in the sample
rodzic
5e1431c406
commit
1b9c1fe425
|
@ -8,18 +8,23 @@
|
|||
#include <ESP8266WiFi.h>
|
||||
|
||||
#include "uMQTTBroker.h"
|
||||
#include "espconn.h"
|
||||
|
||||
/*
|
||||
* Your WiFi config here
|
||||
*/
|
||||
char ssid[] = "MySSID"; // your network SSID (name)
|
||||
char pass[] = "MyPassword"; // your network password
|
||||
char ssid[] = "ssid"; // your network SSID (name)
|
||||
char pass[] = "password"; // your network password
|
||||
|
||||
|
||||
unsigned int mqttPort = 1883; // the standard MQTT broker port
|
||||
unsigned int max_subscriptions = 30;
|
||||
unsigned int max_retained_topics = 30;
|
||||
|
||||
/*
|
||||
* Data callback: called on any locally subscribed topic
|
||||
*/
|
||||
|
||||
void data_callback(uint32_t *client /* we can ignore this */, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh) {
|
||||
char topic_str[topic_len+1];
|
||||
os_memcpy(topic_str, topic, topic_len);
|
||||
|
@ -29,11 +34,32 @@ void data_callback(uint32_t *client /* we can ignore this */, const char* topic,
|
|||
os_memcpy(data_str, data, lengh);
|
||||
data_str[lengh] = '\0';
|
||||
|
||||
Serial.print("received topic '");
|
||||
Serial.print(topic_str);
|
||||
Serial.print("' with data '");
|
||||
Serial.print(data_str);
|
||||
Serial.println("'");
|
||||
Serial.println("received topic '"+(String)topic_str+"' with data '"+(String)data_str+"'");
|
||||
}
|
||||
|
||||
/*
|
||||
* Authentication callback: called on any valid connction request
|
||||
* Returns true, if authorized
|
||||
*/
|
||||
|
||||
bool auth_data_callback(const char* username, const char *password, struct espconn *pesp_conn){
|
||||
Serial.print("Connect from: ");
|
||||
Serial.print((String)pesp_conn->proto.tcp->remote_ip[0]+".");
|
||||
Serial.print((String)pesp_conn->proto.tcp->remote_ip[1]+".");
|
||||
Serial.print((String)pesp_conn->proto.tcp->remote_ip[2]+".");
|
||||
Serial.println((String)pesp_conn->proto.tcp->remote_ip[3]);
|
||||
|
||||
Serial.println("Username/Pasword: "+(String)username+"/"+(String)password);
|
||||
|
||||
/*
|
||||
* Implement this,if you want to have username/password authentication
|
||||
*/
|
||||
// if ((String)username == "root" && (String)password == "admin" ) {
|
||||
// return true;
|
||||
// }else{
|
||||
// return false;
|
||||
// }
|
||||
return true;
|
||||
}
|
||||
|
||||
void setup()
|
||||
|
@ -43,8 +69,7 @@ void setup()
|
|||
Serial.println();
|
||||
|
||||
// We start by connecting to a WiFi network
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
Serial.println("Connecting to "+(String)ssid);
|
||||
WiFi.begin(ssid, pass);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
|
@ -54,13 +79,13 @@ void setup()
|
|||
Serial.println("");
|
||||
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
Serial.println("IP address: "+WiFi.localIP().toString());
|
||||
|
||||
/*
|
||||
* Register the callback
|
||||
* Register the callbacks
|
||||
*/
|
||||
MQTT_server_onData(data_callback);
|
||||
MQTT_server_onAuth(auth_data_callback);
|
||||
|
||||
/*
|
||||
* Start the broker
|
||||
|
@ -88,4 +113,3 @@ void loop()
|
|||
// wait a second
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue