Add: MQTT user / pass

This commit is contained in:
Tobias Blum 2017-08-06 21:36:30 +02:00
parent c160b910d9
commit 20941906e1
4 changed files with 13 additions and 8 deletions

View file

@ -31,10 +31,11 @@
PubSubClient mqtt_client(espClient); PubSubClient mqtt_client(espClient);
#endif #endif
// *************************************************************************** // ***************************************************************************
// Instanciate HTTP(80) / WebSockets(81) Server // Instanciate HTTP(80) / WebSockets(81) Server
// *************************************************************************** // ***************************************************************************
ESP8266WebServer server ( 80 ); ESP8266WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81); WebSocketsServer webSocket = WebSocketsServer(81);
@ -93,7 +94,6 @@ void configModeCallback (WiFiManager *myWiFiManager) {
} }
// *************************************************************************** // ***************************************************************************
// Include: Webserver // Include: Webserver
// *************************************************************************** // ***************************************************************************
@ -207,8 +207,8 @@ void setup() {
// Configure MQTT // Configure MQTT
// *************************************************************************** // ***************************************************************************
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, 32); String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, strlen(HOSTNAME) + 3);
String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, 32); String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, strlen(HOSTNAME) + 4);
mqtt_client.setServer(mqtt_server, 1883); mqtt_client.setServer(mqtt_server, 1883);
mqtt_client.setCallback(mqtt_callback); 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("/upload to upload the webpages first.");
DBG_OUTPUT_PORT.println(""); DBG_OUTPUT_PORT.println("");
// *************************************************************************** // ***************************************************************************
// Setup: WebSocket server // Setup: WebSocket server

View file

@ -17,12 +17,13 @@ int analogLevel = 100;
boolean timeToDip = false; boolean timeToDip = false;
int ledStates[NUMLEDS]; int ledStates[NUMLEDS];
void hsb2rgbAN1(uint16_t index, uint8_t sat, uint8_t bright, uint8_t myled) { 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/ // Source: https://blog.adafruit.com/2012/03/14/constant-brightness-hsb-to-rgb-algorithm/
uint8_t temp[5], n = (index >> 8) % 3; 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[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]); strip.setPixelColor(myled, temp[n + 2], temp[n + 1], temp[n]);
} }

View file

@ -12,8 +12,11 @@ const char HOSTNAME[] = "ESP8266_VORONOI"; // Friedly hostname
#define MQTT_MAX_PACKET_SIZE 256 #define MQTT_MAX_PACKET_SIZE 256
char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: <HOSTNAME>/in char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: <HOSTNAME>/in
char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: <HOSTNAME>/out char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: <HOSTNAME>/out
const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker
const char mqtt_clientid[] = "ESP8266Client"; // MQTT ClientID 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 #endif
// *************************************************************************** // ***************************************************************************

View file

@ -391,7 +391,7 @@ void checkForRequests() {
while (!mqtt_client.connected()) { while (!mqtt_client.connected()) {
DBG_OUTPUT_PORT.print("Attempting MQTT connection... "); DBG_OUTPUT_PORT.print("Attempting MQTT connection... ");
// Attempt to connect // Attempt to connect
if (mqtt_client.connect(mqtt_clientid)) { if (mqtt_client.connect(mqtt_clientid, mqtt_username, mqtt_password)) {
DBG_OUTPUT_PORT.println("connected!"); DBG_OUTPUT_PORT.println("connected!");
// Once connected, publish an announcement... // Once connected, publish an announcement...
char * message = new char[18 + strlen(HOSTNAME) + 1]; char * message = new char[18 + strlen(HOSTNAME) + 1];