Created
March 21, 2018 14:00
-
-
Save msraynsford/543d6d270cd3f5bc8a16fc4fa97b27f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
#include <DNSServer.h> | |
#include <FS.h> | |
#include <FastLED.h> | |
#include <PGMWrap.h> | |
#define HTTP_PORT 80 | |
#define DNS_PORT 53 | |
#define WS_PORT 2205 | |
#define NUMLEDS 50 | |
#define LED_PIN D1 | |
#define BRIGHT 2 | |
#define PAUSE 350 | |
DNSServer dnsServer; | |
ESP8266WebServer webServer(HTTP_PORT); | |
CRGB leds[NUMLEDS]; | |
uint8_t pos = 0; | |
uint8_t ledNo = 0; | |
// Start the mapping array at 1 because everyone knows a=1 b=2... | |
uint8_p MAP[] PROGMEM = {0, 24, 25, 26, 27, 28, 29, 30, 31, 40, 39, 38, 37, 36, 35, 34, 33, 32, 41, 42, 43, 44, 45, 46, 47, 48, 49 }; | |
char msg[50] = "help me"; | |
uint8_t msglen; | |
struct config_t{ | |
char ssid[17] = "Stranger Things"; | |
char password[17] = "password"; | |
} config; | |
const String dnsName = "stranger"; | |
void setup() { | |
Serial.begin(115200); | |
FastLED.addLeds<WS2812, LED_PIN>(leds, NUMLEDS); | |
Serial.printf("SSID: '%s'\n", config.ssid); | |
Serial.printf("Password: '%s'\n", config.password); | |
WiFi.mode(WIFI_AP); | |
WiFi.softAP(config.ssid, config.password); | |
SPIFFS.begin(); | |
// Create a simple homepage | |
webServer.on("/", serveMessage); | |
// Redirect all unknown traffic back to the homepage | |
webServer.onNotFound([](){ | |
webServer.sendHeader("Location", "http://" + dnsName + ".local/"); | |
webServer.send(303); | |
}); | |
MDNS.begin(dnsName.c_str()); | |
dnsServer.start(DNS_PORT, "*", WiFi.softAPIP()); | |
webServer.begin(); | |
msglen = strlen(msg); | |
} | |
void loop() { | |
dnsServer.processNextRequest(); | |
webServer.handleClient(); | |
EVERY_N_MILLISECONDS(PAUSE) { | |
uint8_t color; | |
if((pos % 2) && (pos <= (msglen * 2))) { | |
char letter = msg[pos/2]; | |
char mapped = mapChar(letter); | |
// Serial.printf("pos: %d, char: %c, num: %d\n", pos, letter, mapped); | |
if(mapped > 0) { | |
ledNo = MAP[mapped]; | |
color = (ledNo % 4); | |
switch(color) { | |
case 0: leds[ledNo] *= 10; break; | |
case 1: leds[ledNo] *= 10; break; | |
case 2: leds[ledNo] *= 10; break; | |
case 3: leds[ledNo] *= 10; break; | |
} | |
} | |
} | |
else { | |
for(int i=0; i<NUMLEDS; i += 1) { | |
color = (i % 4); | |
switch(color) { | |
case 0: leds[i] = CRGB(0, 0, BRIGHT); break; | |
case 1: leds[i] = CRGB(0, BRIGHT, 0); break; | |
case 2: leds[i] = CRGB(BRIGHT, 0, 0); break; | |
case 3: leds[i] = CRGB(BRIGHT, BRIGHT, 0); break; | |
} | |
} | |
} | |
FastLED.show(); | |
// Create a gap at the end of the message | |
pos += 1; | |
if(pos == (msglen * 2) + 4) { | |
pos = 0; | |
} | |
} | |
} | |
char mapChar(char letter) { | |
if((letter >= 0x61) && (letter <= 0x7A)) | |
letter -= 0x60; | |
else if((letter >= 0x41) && (letter <= 0x5A)) | |
letter -= 0x40; | |
else | |
letter = 0; | |
return letter; | |
} | |
void serveMessage() { | |
if(webServer.hasArg("msg")) { | |
webServer.arg("msg").toCharArray(msg, sizeof(msg)); | |
msglen = strlen(msg); | |
pos = 0; | |
} | |
//Serve a HTML page containing an input button | |
String htmlPage = "<form action='/'><input type='text' name='msg' value='$v' maxlength='49'><input type='submit' value='Update Message'></form>"; | |
htmlPage.replace("$v", msg); | |
webServer.send(200, "text/html", htmlPage); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment