From 1b9c1fe4252f7e272da88aabafd143f172385fac Mon Sep 17 00:00:00 2001 From: martin-ger Date: Fri, 3 Aug 2018 10:38:27 +0200 Subject: [PATCH] Added authentication in the sample --- .../uMQTTBrokerSample/uMQTTBrokerSample.ino | 50 ++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/examples/uMQTTBrokerSample/uMQTTBrokerSample.ino b/examples/uMQTTBrokerSample/uMQTTBrokerSample.ino index 00b71ef..22afffe 100644 --- a/examples/uMQTTBrokerSample/uMQTTBrokerSample.ino +++ b/examples/uMQTTBrokerSample/uMQTTBrokerSample.ino @@ -8,18 +8,23 @@ #include #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); } -