Initial commit
This commit is contained in:
commit
32ba413380
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.pio/
|
||||||
|
credentials.h
|
22
platformio.ini
Normal file
22
platformio.ini
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
; PlatformIO Project Configuration File
|
||||||
|
;
|
||||||
|
; Build options: build flags, source filter
|
||||||
|
; Upload options: custom upload port, speed and extra flags
|
||||||
|
; Library options: dependencies, extra library storages
|
||||||
|
; Advanced options: extra scripting
|
||||||
|
;
|
||||||
|
; Please visit documentation for the other options and examples
|
||||||
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
|
[env]
|
||||||
|
platform = espressif8266
|
||||||
|
framework = arduino
|
||||||
|
monitor_speed = 115200
|
||||||
|
upload_speed = 500000
|
||||||
|
|
||||||
|
# Serial1 (sign data) maps to D4 by default
|
||||||
|
[env:d1_mini]
|
||||||
|
board = d1_mini
|
||||||
|
|
||||||
|
[env:nodemcuv2]
|
||||||
|
board = nodemcuv2
|
8
src/credentials.h.example
Normal file
8
src/credentials.h.example
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace credentials {
|
||||||
|
const String ssid = "my SSID";
|
||||||
|
const String psk = "my PSK";
|
||||||
|
|
||||||
|
const String mdnsHostname = "led-marquee-sign";
|
||||||
|
}
|
52
src/main.cpp
Normal file
52
src/main.cpp
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#include <ESP8266WiFiMulti.h>
|
||||||
|
#include <ESP8266mDNS.h>
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
|
||||||
|
#include "credentials.h"
|
||||||
|
#include "webserver.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
static ESP8266WiFiMulti wifiMulti;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// serial console, for use via USB (also exposed to TXD0/RXD0 GPIOs)
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
// transmit-only UART port, used for communication with the LED sign
|
||||||
|
// the BAUD rate was deducued from the communication sent to the USB 12V UART adapter by the available Windows
|
||||||
|
// software
|
||||||
|
Serial1.begin(9600);
|
||||||
|
|
||||||
|
// configure WiFi and connect to the network
|
||||||
|
wifiMulti.addAP(credentials::ssid.c_str(), credentials::psk.c_str());
|
||||||
|
Serial.println("Connecting to Wi-Fi network...");
|
||||||
|
while (wifiMulti.run() != WL_CONNECTED) {
|
||||||
|
delay(1000);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
Serial.print("Connected to ");
|
||||||
|
Serial.print(WiFi.SSID());
|
||||||
|
Serial.print(", IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
// might be handy to have the IP written on the sign
|
||||||
|
sendTextToSign(WiFi.localIP().toString());
|
||||||
|
|
||||||
|
// publish this service using mDNS so that it's easy to find it in the LAN
|
||||||
|
if (MDNS.begin(credentials::mdnsHostname)) {
|
||||||
|
Serial.println("mDNS started up successfully");
|
||||||
|
} else {
|
||||||
|
Serial.println("Error: failed to start mDNS");
|
||||||
|
}
|
||||||
|
|
||||||
|
// configure HTTP endpoints
|
||||||
|
configureWebServer();
|
||||||
|
|
||||||
|
// don't forget to start the webserver!
|
||||||
|
webServer.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
webServer.handleClient();
|
||||||
|
}
|
18
src/util.h
Normal file
18
src/util.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
enum Brightness {
|
||||||
|
Normal,
|
||||||
|
Bright,
|
||||||
|
};
|
||||||
|
|
||||||
|
void sendTextToSign(String text, Brightness brightness = Bright) {
|
||||||
|
// debugging
|
||||||
|
Serial.print("Sending text \"");
|
||||||
|
Serial.print(text);
|
||||||
|
Serial.println("\"");
|
||||||
|
|
||||||
|
// TODO: find out how the brightness attribute in the LED sign is encoded in the UART signal
|
||||||
|
Serial1.print("f01A\\s");
|
||||||
|
Serial1.print(text);
|
||||||
|
Serial1.print("\r\r\r");
|
||||||
|
}
|
68
src/webserver.h
Normal file
68
src/webserver.h
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
static ESP8266WebServer webServer(80);
|
||||||
|
|
||||||
|
// use an anonymous namespace for the module's "private" methods
|
||||||
|
// this prevents the methods from ending up in the global namespace
|
||||||
|
namespace {
|
||||||
|
void handleNotFound() {
|
||||||
|
webServer.send(404, "text/plain", "urm... nope");
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleIndex() {
|
||||||
|
String htmlText = R"(
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>LED Marquee Sign Controller</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>LED Marquee Sign Controller</h1>
|
||||||
|
<form action="/" method="post">
|
||||||
|
<fieldset>
|
||||||
|
<div>
|
||||||
|
<label for="text">Text to send:</label>
|
||||||
|
<input type="text" name="text">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label for="bright">Bright mode</label>
|
||||||
|
<input type="checkbox" name="bright">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<button>Send</button>
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
)";
|
||||||
|
webServer.send(200, "text/html", htmlText);
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleFormSend() {
|
||||||
|
sendTextToSign("HELLO FORM");
|
||||||
|
|
||||||
|
// redirect back to root page
|
||||||
|
webServer.sendHeader("Location", "/");
|
||||||
|
webServer.send(303);
|
||||||
|
}
|
||||||
|
|
||||||
|
// RESTful API endpoint -- returns a proper status code rather than a redirect
|
||||||
|
void handleApiSend() {
|
||||||
|
sendTextToSign("HELLO API");
|
||||||
|
webServer.send(200, "text/plain", "ok");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void configureWebServer() {
|
||||||
|
webServer.onNotFound(handleNotFound);
|
||||||
|
|
||||||
|
webServer.on("/", HTTPMethod::HTTP_GET, handleIndex);
|
||||||
|
webServer.on("/", HTTPMethod::HTTP_POST, handleFormSend);
|
||||||
|
webServer.on("/send", HTTPMethod::HTTP_POST, handleApiSend);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user