diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2016-06-05 21:17:19 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2016-06-05 21:17:19 +0200 |
| commit | 6ab457d9d05944bfb8208a57dd43618b5532c6ea (patch) | |
| tree | 133a230c5290d95c30dd0e606147d7c6cd74b05e | |
| download | esp-ledstrip-6ab457d9d05944bfb8208a57dd43618b5532c6ea.tar.gz esp-ledstrip-6ab457d9d05944bfb8208a57dd43618b5532c6ea.zip | |
first working version
| -rw-r--r-- | esp-hyperion.ino | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/esp-hyperion.ino b/esp-hyperion.ino new file mode 100644 index 0000000..1286b7d --- /dev/null +++ b/esp-hyperion.ino @@ -0,0 +1,110 @@ +#include <ESP8266WiFi.h> +#include <ESP8266mDNS.h> +#include <WiFiClient.h> +#include <ESP8266WebServer.h> +#include <ESP8266mDNS.h> +#include <WiFiUdp.h> +#include <ArduinoOTA.h> + +const char* ssid = ""; +const char* password = ""; +// R G B +const int leds[] = {2, 0, 3}; +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 +ESP8266WebServer server(80); +WiFiUDP RAWudp; + +#ifndef BSSID + #define BSSID NULL +#endif + +void updateLeds() { + for(int i=0; i<3; i++) + analogWrite(leds[i], state[i]); +} + +void handleRoot() { + char temp[400]; + + if(server.hasArg("red")) + state[0] = server.arg("red").toInt(); + if(server.hasArg("green")) + state[1] = server.arg("green").toInt(); + if(server.hasArg("blue")) + 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); +} + +void setup() { + WiFi.begin(ssid, password); + + for(int i=0; i<3; i++) { + pinMode(leds[i], OUTPUT); + } + + while(WiFi.status() != WL_CONNECTED) { + updateLeds(); + delay(500); + for(int i=0; i<3; i++) { + if(state[i] < 512) { + state[i] = 1023; + } else { + state[i] = 0; + } + } + } + + MDNS.begin("esplamp1", WiFi.localIP()); + WiFi.hostname("esplamp1"); + + server.on("/", handleRoot); + server.begin(); + MDNS.addService("http", "tcp", 80); + + ArduinoOTA.setHostname("esplamp1"); + ArduinoOTA.begin(); +} + +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(); + } +} + +void loop() { + server.handleClient(); + handleUdp(); + ArduinoOTA.handle(); +} |
