From 20941906e18a9deba4aafebed615a9dff1ce72b1 Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sun, 6 Aug 2017 21:36:30 +0200 Subject: [PATCH] Add: MQTT user / pass --- Arduino/McLighting/McLighting.ino | 9 +++++---- Arduino/McLighting/colormodes.h | 5 +++-- Arduino/McLighting/definitions.h | 5 ++++- Arduino/McLighting/request_handlers.h | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 567bcbe..76e504a 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -31,10 +31,11 @@ PubSubClient mqtt_client(espClient); #endif + // *************************************************************************** // Instanciate HTTP(80) / WebSockets(81) Server // *************************************************************************** -ESP8266WebServer server ( 80 ); +ESP8266WebServer server(80); WebSocketsServer webSocket = WebSocketsServer(81); @@ -93,7 +94,6 @@ void configModeCallback (WiFiManager *myWiFiManager) { } - // *************************************************************************** // Include: Webserver // *************************************************************************** @@ -207,8 +207,8 @@ void setup() { // Configure MQTT // *************************************************************************** #ifdef ENABLE_MQTT - String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, 32); - String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, 32); + String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, strlen(HOSTNAME) + 3); + String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, strlen(HOSTNAME) + 4); mqtt_client.setServer(mqtt_server, 1883); mqtt_client.setCallback(mqtt_callback); @@ -232,6 +232,7 @@ void setup() { DBG_OUTPUT_PORT.println("/upload to upload the webpages first."); DBG_OUTPUT_PORT.println(""); + // *************************************************************************** // Setup: WebSocket server diff --git a/Arduino/McLighting/colormodes.h b/Arduino/McLighting/colormodes.h index 33d7bf9..aa7c48b 100644 --- a/Arduino/McLighting/colormodes.h +++ b/Arduino/McLighting/colormodes.h @@ -17,12 +17,13 @@ int analogLevel = 100; boolean timeToDip = false; int ledStates[NUMLEDS]; + void hsb2rgbAN1(uint16_t index, uint8_t sat, uint8_t bright, uint8_t myled) { // Source: https://blog.adafruit.com/2012/03/14/constant-brightness-hsb-to-rgb-algorithm/ uint8_t temp[5], n = (index >> 8) % 3; - temp[0] = temp[3] = (uint8_t)(( (sat ^ 255) * bright) / 255); + temp[0] = temp[3] = (uint8_t)(( (sat ^ 255) * bright) / 255); temp[1] = temp[4] = (uint8_t)((((( (index & 255) * sat) / 255) + (sat ^ 255)) * bright) / 255); - temp[2] = (uint8_t)(((((((index & 255) ^ 255) * sat) / 255) + (sat ^ 255)) * bright) / 255); + temp[2] = (uint8_t)(((((((index & 255) ^ 255) * sat) / 255) + (sat ^ 255)) * bright) / 255); strip.setPixelColor(myled, temp[n + 2], temp[n + 1], temp[n]); } diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index fd0312e..6e3cc1f 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -12,8 +12,11 @@ const char HOSTNAME[] = "ESP8266_VORONOI"; // Friedly hostname #define MQTT_MAX_PACKET_SIZE 256 char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: /in char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: /out - const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker + const char mqtt_clientid[] = "ESP8266Client"; // MQTT ClientID + const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker + const char mqtt_username[] = ""; // MQTT Username + const char mqtt_password[] = ""; // MQTT Password #endif // *************************************************************************** diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 6bd5c03..f1061e4 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -391,7 +391,7 @@ void checkForRequests() { while (!mqtt_client.connected()) { DBG_OUTPUT_PORT.print("Attempting MQTT connection... "); // Attempt to connect - if (mqtt_client.connect(mqtt_clientid)) { + if (mqtt_client.connect(mqtt_clientid, mqtt_username, mqtt_password)) { DBG_OUTPUT_PORT.println("connected!"); // Once connected, publish an announcement... char * message = new char[18 + strlen(HOSTNAME) + 1];