rgbled/productionMode.cpp

349 lines
6.5 KiB
C++
Raw Normal View History

2019-04-05 19:59:07 +02:00
/*
* productionMode.cpp
*
* Created on: Apr 05, 2019
* Author: wn
*/
#include "defines.h"
#define MQTT_MAX_PACKET_SIZE 256
#include <stdio.h>
#include <stdint.h>
2019-04-06 22:49:23 +02:00
#include <string.h>
2019-04-05 19:59:07 +02:00
#include <stdbool.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <PubSubClient.h>
2019-04-05 21:40:26 +02:00
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
#include <FastLED.h>
2019-04-05 19:59:07 +02:00
#include "configuration.h"
2019-04-10 11:15:12 +02:00
#define MAX_TOKENS 5
2019-04-05 19:59:07 +02:00
void callback(char* topic, byte* payload, unsigned int length);
WiFiClientSecure espClient;
PubSubClient client(espClient);
2019-04-05 21:40:26 +02:00
CRGB leds[NUM_OF_LEDs];
2019-04-05 19:59:07 +02:00
void setup_wifi() {
delay(10);
WiFi.mode(WIFI_STA);
// We start by connecting to a WiFi network
#ifdef DEBUG
Serial.println();
Serial.print("Connecting to ");
Serial.println(configBlock.wifiSsid);
#endif
WiFi.begin(configBlock.wifiSsid, configBlock.wifiKey);
while (WiFi.status() != WL_CONNECTED) {
delay(50);
#ifdef DEBUG
Serial.print(".");
#endif
}
Serial.println("!");
#ifdef DEBUG
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
#endif
}
2019-04-06 22:49:23 +02:00
static CRGB evaluationColorWord(char* cmd) {
uint8_t red = 0, green = 0, blue = 0;
if ((! strcmp(cmd, "on")) || (! strcmp(cmd, "white"))) {
2019-04-06 22:49:23 +02:00
red = 255;
green = 255;
blue = 255;
} else if (! strcmp(cmd, "off")) {
red = 0;
green = 0;
blue = 0;
} else if (! strcmp(cmd, "red")) {
red = 255;
green = 0;
blue = 0;
} else if (! strcmp(cmd, "green")) {
red = 0;
green = 255;
blue = 0;
} else if (! strcmp(cmd, "blue")) {
red = 0;
green = 0;
blue = 255;
} else if (! strcmp(cmd, "purple")) {
red = 255;
green = 0;
blue = 255;
} else if (! strcmp(cmd, "yellow")) {
red = 255;
green = 255;
blue = 0;
}
CRGB res;
res.r = red;
res.g = green;
res.b = blue;
return res;
}
2019-04-05 19:59:07 +02:00
void callback(char* topic, byte* payload, unsigned int length) {
const uint8_t BUFSIZE = 128;
if ((length + 1) >= BUFSIZE) { // 1 for terminating NUL
2019-04-05 19:59:07 +02:00
#ifdef DEBUG
Serial.println("Received message too long, ignore it");
2019-04-05 19:59:07 +02:00
#endif
} else {
char buffer[BUFSIZE];
memcpy(buffer, payload, length);
*(buffer + length) = 0;
2019-04-05 19:59:07 +02:00
#ifdef DEBUG
Serial.print("Received message: ");
Serial.print(length);
Serial.print(" ");
Serial.print(topic);
Serial.print(" ");
Serial.println(buffer);
2019-04-05 19:59:07 +02:00
#endif
2019-04-05 21:40:26 +02:00
char *inbuf = buffer;
char *tk = NULL;
uint8_t tokenCnt = 0;
char *tokens[MAX_TOKENS];
do {
if (tokenCnt >= MAX_TOKENS) {
break;
}
tk = strtok(inbuf, " ");
inbuf = NULL;
tokens[tokenCnt] = tk;
if (tk) {
tokenCnt++;
Serial.print("TokenCnt: ");
Serial.print(tokenCnt);
Serial.println();
}
} while (tk);
for (uint8_t i = 0; i < tokenCnt; i++) {
Serial.print("Token ");
Serial.print(i);
Serial.print(": ");
Serial.println(tokens[i]);
}
2019-04-10 11:15:12 +02:00
if (! strcmp(topic, configBlock.mqttTopicCommand)) {
int32_t n, b;
CRGB pseudoColors;
switch (tokenCnt) {
case 1:
// set brightness
b = strtol(tokens[0], NULL, 10);
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) {
leds[i].r = b;
leds[i].g = b;
leds[i].b = b;
}
FastLED.show();
break;
case 2:
// set brightness for one LED
n = strtol(tokens[0], NULL, 10);
b = strtol(tokens[1], NULL, 10);
if (n >= 0 && n < NUM_OF_LEDs) {
leds[n] = b;
FastLED.show();
}
break;
}
2019-04-10 11:15:12 +02:00
} else if (! strcmp(topic, configBlock.mqttTopicColorCommand)) {
int32_t n, red, green, blue;
CRGB colors;
switch (tokenCnt) {
case 1:
// on, off, color word for all LEDs
colors = evaluationColorWord(tokens[0]);
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) {
leds[i] = colors;
}
FastLED.show();
break;
case 2:
// token0 = LED number, token1 = color
n = strtol(tokens[0], NULL, 10);
if (n >= 0 && n < NUM_OF_LEDs) {
colors = evaluationColorWord(tokens[1]);
leds[n] = colors;
FastLED.show();
}
break;
case 3:
// token0 = red, token1 = green, token2 = blue
red = strtol(tokens[0], NULL, 10);
green = strtol(tokens[1], NULL, 10);
blue = strtol(tokens[2], NULL, 10);
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) {
leds[i].r = red;
leds[i].g = green;
leds[i].b = blue;
}
FastLED.show();
break;
case 4:
// token0 = LED number, token1 = red, token2 = green, token3 = blue
n = strtol(tokens[0], NULL, 10);
if (n >= 0 && n < NUM_OF_LEDs) {
red = strtol(tokens[1], NULL, 10);
green = strtol(tokens[2], NULL, 10);
blue = strtol(tokens[3], NULL, 10);
leds[n].r = red;
leds[n].g = green;
leds[n].b = blue;
FastLED.show();
}
break;
}
}
}
2019-04-05 19:59:07 +02:00
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
#ifdef DEBUG
Serial.print("Attempting MQTT connection...");
#endif
// Attempt to connect
//char clientId[128];
//snprintf(clientId, 127, "esp%s", WiFi.macAddress().c_str());
if (client.connect(configBlock.mqttClientId, configBlock.mqttUser, configBlock.mqttPass)) {
#ifdef DEBUG
Serial.println("connected");
#endif
client.setCallback(callback);
// Once connected, publish an announcement...
client.publish(configBlock.mqttDebugTopic, "hello world");
client.publish(configBlock.mqttDebugTopic, WiFi.localIP().toString().c_str());
2019-04-10 11:15:12 +02:00
client.subscribe(configBlock.mqttTopicColorCommand);
2019-04-05 21:40:26 +02:00
client.subscribe(configBlock.mqttTopicCommand);
2019-04-05 19:59:07 +02:00
} else {
#ifdef DEBUG
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
#endif
// Wait 5 seconds before retrying
delay(5000);
}
}
}
bool mqtt_connect() {
bool reconnected = false;
if (!client.connected()) {
reconnect();
reconnected = true;
}
client.loop();
return reconnected;
}
void setupProduction() {
setup_wifi();
client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
2019-04-05 21:40:26 +02:00
FastLED.addLeds<NEOPIXEL, PIXEL_PIN>(leds, NUM_OF_LEDs);
2019-04-05 19:59:07 +02:00
}
void loopProduction() {
bool reconnected = mqtt_connect();
static uint32_t reconnectTime = 0;
if (reconnected) {
reconnectTime = millis();
}
}
/*
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
#include "RgbLed.h"
#include <FastLED.h>
CRGB leds[2];
void setup() {
FastLED.addLeds<NEOPIXEL, 1>(leds, 2);
}
void loop() {
leds[0] = CRGB::Red;
leds[1] = CRGB::Red;
FastLED.show();
delay(500);
leds[0] = CRGB::Green;
leds[1] = CRGB::Green;
FastLED.show();
delay(500);
leds[0] = CRGB::Blue;
leds[1] = CRGB::Blue;
FastLED.show();
delay(500);
leds[0].r = 0;
leds[0].g = 0;
leds[0].b = 255;
leds[1].r = 255;
leds[1].g = 255;
leds[1].b = 255;
FastLED.show();
delay(500);
delay(2000);
}
*/
2019-04-05 19:59:07 +02:00