diff options
Diffstat (limited to 'esp-ledstrip.ino')
| -rw-r--r-- | esp-ledstrip.ino | 197 |
1 files changed, 142 insertions, 55 deletions
diff --git a/esp-ledstrip.ino b/esp-ledstrip.ino index 1286b7d..3404f85 100644 --- a/esp-ledstrip.ino +++ b/esp-ledstrip.ino @@ -1,39 +1,83 @@ +#define WITH_HTTP +#define WITH_UDP +//#define WITH_MQTT +//#define WITH_OTA // flash size needs to be 2* sketch size :/ +//#define WITH_SERIAL // not used yet +#define SILENT_STARTUP + #include <ESP8266WiFi.h> #include <ESP8266mDNS.h> #include <WiFiClient.h> +#include <ArduinoJson.h> + +#ifdef WITH_OTA +#include <ArduinoOTA.h> +#endif +#ifdef WITH_HTTP #include <ESP8266WebServer.h> -#include <ESP8266mDNS.h> +#endif +#ifdef WITH_UDP #include <WiFiUdp.h> -#include <ArduinoOTA.h> +#endif +#ifdef WITH_MQTT +#include <PubSubClient.h> +#endif const char* ssid = ""; const char* password = ""; +#define ME "esplamp1" +#ifdef WITH_MQTT +const char* mqtt_server = "mqtt.example.net"; +#endif +#ifdef WITH_UDP +const int udpPort = 2391; +#endif +#ifdef WITH_MQTT +const char* connectTopic = "api/" ME "/connected"; +const char* outTopic = "api/" ME "/status/rgb"; +const char* inTopic = "api/" ME "/set/rgb"; +#endif + // R G B -const int leds[] = {2, 0, 3}; +const int leds[] = {2, 0, 3}; // !TX is inverted, not used here unsigned int state[] = {0, 0, 0}; unsigned char cstate[] = {0, 0, 0}; -const int udpPort = 2391; -#define LRED 0 -#define LGREEN 1 -#define LBLUE 2 -#define OFF 0 -#define ON 1 +DynamicJsonBuffer jsonBuffer; +char cJson[50]; + +#ifdef WITH_HTTP ESP8266WebServer server(80); +#endif +#ifdef WITH_UDP WiFiUDP RAWudp; - -#ifndef BSSID - #define BSSID NULL #endif +#ifdef WITH_MQTT +WiFiClient espClient; +PubSubClient mqtt(espClient); +#endif + +/* hardware access */ +void writeJson() { + JsonObject& root = jsonBuffer.createObject(); + root["red"] = state[0]; + root["green"] = state[1]; + root["blue"] = state[2]; + root.printTo(cJson, sizeof(cJson)); +} void updateLeds() { for(int i=0; i<3; i++) analogWrite(leds[i], state[i]); + writeJson(); +#ifdef WITH_MQTT + if (mqtt.connected()) mqtt.publish(outTopic, cJson, true); +#endif } +#ifdef WITH_HTTP +/* http API */ void handleRoot() { - char temp[400]; - if(server.hasArg("red")) state[0] = server.arg("red").toInt(); if(server.hasArg("green")) @@ -42,69 +86,112 @@ void handleRoot() { state[2] = server.arg("blue").toInt(); updateLeds(); - - snprintf(temp, 400, - " {'red': %d,\ - 'green': %d,\ - 'blue': %d}\ - ", - state[LRED], - state[LGREEN], - state[LBLUE] - ); - server.send(200, "text/html", temp); + server.send(200, "application/json", cJson); } +#endif +#ifdef WITH_MQTT +/* MQTT API */ +void handleMqtt(char* topic, byte* payload, unsigned int length) { + if (strcmp(topic, inTopic) == 0) { + JsonObject& root = jsonBuffer.parseObject((char*)payload); + if (!root.success()) return; + state[0] = root["red"]; + state[1] = root["green"]; + state[2] = root["blue"]; + updateLeds(); + } +} + +void connectMqtt() { + // connect(id, willTopic, willQoS, willRetain, willMessage) + if (mqtt.connect(ME, connectTopic, 2, true, "offline")) { + // topic, msg, retain + mqtt.publish(connectTopic, "online", true); + mqtt.subscribe(inTopic); + } +} +#endif + +#ifdef WITH_UDP +/* UDP API */ +void handleUdp() { + if (!RAWudp) + RAWudp.begin(udpPort); + if (!RAWudp) + return; + + int cb = RAWudp.parsePacket(); + if (cb) { + RAWudp.read(cstate, 3); + for(int i=0; i<3; i++) + state[i] = cstate[i] << 2; + updateLeds(); + } +} +#endif + +/* core code */ void setup() { +#ifdef WITH_SERIAL + Serial.begin(115200); +#endif WiFi.begin(ssid, password); - for(int i=0; i<3; i++) { + for(int i=0; i<3; i++) pinMode(leds[i], OUTPUT); - } - + while(WiFi.status() != WL_CONNECTED) { - updateLeds(); - delay(500); +#ifndef SILENT_STARTUP + // blink three times for(int i=0; i<3; i++) { - if(state[i] < 512) { + if(state[i] < 512) state[i] = 1023; - } else { + else state[i] = 0; - } } + updateLeds(); +#endif + delay(500); } - MDNS.begin("esplamp1", WiFi.localIP()); - WiFi.hostname("esplamp1"); + WiFi.hostname(ME); + MDNS.begin(ME, WiFi.localIP()); +#ifdef WITH_OTA + ArduinoOTA.setHostname(ME); + ArduinoOTA.begin(); +#endif + +#ifdef WITH_HTTP server.on("/", handleRoot); server.begin(); MDNS.addService("http", "tcp", 80); +#endif - ArduinoOTA.setHostname("esplamp1"); - ArduinoOTA.begin(); -} - -void handleUdp() { - if (!RAWudp) { - RAWudp.begin(udpPort); - } - if (!RAWudp) { - return; - } +#ifdef WITH_UDP + MDNS.addService("udp", "udp", udpPort); +#endif - int cb = RAWudp.parsePacket(); - if (cb) { - RAWudp.read(cstate, 3); - for(int i=0; i<3; i++) { - state[i] = cstate[i] << 2; - } - updateLeds(); - } +#ifdef WITH_MQTT + mqtt.setServer(mqtt_server, 1883); + mqtt.setCallback(handleMqtt); +#endif } void loop() { +#ifdef WITH_OTA + ArduinoOTA.handle(); +#endif +#ifdef WITH_HTTP server.handleClient(); +#endif +#ifdef WITH_UDP handleUdp(); - ArduinoOTA.handle(); +#endif +#ifdef WITH_MQTT + if (!mqtt.connected()) + connectMqtt(); + mqtt.loop(); +#endif } |
