WiFlasher/wiflash_esp32/wiflash_esp32.ino

243 lines
7.9 KiB
C++

/*
WiFlash_esp32 - Concept & electronics: Phil Abeyta
- System design & firmware: Kevin Matz
Uses a GPIO to control a lighting console over OSC, whilst also
controlling an LED strobe light with DMX data recieved over sACN.
Copyright (c) 2020 Kevin Matz (kevin.matz@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Arduino.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <SPIFFS.h>
#include <ArduinoOSC.h>
#include <ESPAsyncWebServer.h>
#include "src/wiflash_configure.h"
#include "src/wiflash_status.h"
#include "src/lib/libESTA/sacn/receiver-esp.h"
#include "src/lib/libESTA/fixture/EspStrobe.h"
#include "src/lib/libESTA/fixture/EspRgbStrip.h"
#include "src/lib/libESTA/fixture/EspRgbaStrip.h"
// ESP logging module
#include <esp_log.h>
static const char* TAG = "WiFlash";
//// device objects
EspStrobe * strobe;
EspRgbStrip * strip;
//EspRgbaStrip * strip;
auto *e131 = new SACN::EspReceiver();
auto *httpd = new AsyncWebServer(80);
//// Global button variables
bool pressed = false; // track button state
/*
Start the async web server
*/
void startHTTPD() {
httpd->on("/api/config", HTTP_GET, [](AsyncWebServerRequest * request) {
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/template/configuration.json", "application/json", false, config::getString);
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
});
httpd->on("/api/status", HTTP_GET, [](AsyncWebServerRequest * request) {
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/template/status.json", "application/json", false, status::getString);
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
});
httpd->on("/api/reboot", HTTP_POST, [](AsyncWebServerRequest * request) {
String url = "http://" + config::settings.hostname + ".local";
request->redirect(url.c_str());
httpd->end();
ESP.restart();
});
httpd->on("/api/set", HTTP_POST, [] (AsyncWebServerRequest * request) {
if (!request->hasParam("key", true) ||
!request->hasParam("value", true)) {
request->send(404, "text/plain", "Missing parameter.");
return;
}
String key = request->getParam("key", true)->value();
const String& val = request->getParam("value", true)->value();
key.toUpperCase();
if (val == config::getString(key)) {
// already at value
request->send(204);
} else if (config::set(key, val) > 0) {
// change accepted OK
request->send(205);
} else {
// change failed, return current value
String json = "{\"" + key + "\": \"" + config::getString(key) + "\"}";
request->send(403, "application/json", json);
}
});
httpd->serveStatic("/", SPIFFS, "/www/").setDefaultFile("index.html");
httpd->onNotFound([](AsyncWebServerRequest * request) {
request->send(404, "text/plain", "Not found");
});
httpd->begin();
}
/*
Arduino powerup
*/
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
while (!Serial); // wait for serial attach
ESP_LOGI(TAG, "Serial started.");
ESP_LOGI(TAG, "Loading Prefferences from NVM.");
config::loadConfig();
//// start WiFi:
// set IPv4
WiFi.mode(config::settings.mode);
WiFi.setHostname(config::settings.hostname.c_str());
IPAddress ip_, gateway_, subnet_;
if (!config::settings.dhcp) {
ip_.fromString(config::settings.ip);
gateway_.fromString(config::settings.gateway);
subnet_.fromString(config::settings.subnet);
}
switch (config::settings.mode) {
case WIFI_AP: {
ESP_LOGI(TAG, "Starting Wifi as Access Point on: %s", config::settings.ssid);
ESP_LOGI(TAG, "WiFi password is: %s", config::settings.pwd);
WiFi.softAPsetHostname(config::settings.hostname.c_str());
if (!config::settings.dhcp) {
WiFi.softAPConfig(ip_, gateway_, subnet_);
}
WiFi.softAP(config::settings.ssid.c_str(), config::settings.pwd.c_str());
}
break;
case WIFI_STA: {
ESP_LOGI(TAG, "Starting Wifi as a Client on: %s", config::settings.ssid);
if (!config::settings.dhcp) {
WiFi.config(ip_, gateway_, subnet_);
}
WiFi.begin(config::settings.ssid.c_str(), config::settings.pwd.c_str());
}
break;
}
if (config::settings.dhcp)
ESP_LOGI(TAG, "Getting IP address from DHCP.");
else
ESP_LOGI(TAG, "Setting static IP: %s", config::settings.ip);
ESP_LOGI(TAG, "Access the configuration web page at http://%s.local", config::settings.hostname);
ESP_LOGI(TAG, "Disabling Bluetooth.");
btStop();
ESP_LOGI(TAG, "Mounting SPI Flash FS.");
SPIFFS.begin();
ESP_LOGI(TAG, "Starting HTTP interface.");
startHTTPD();
ESP_LOGI(TAG, "Starting mDNS responder.");
if (MDNS.begin(config::settings.hostname.c_str())) {
MDNS.addService("http", "tcp", 80);
}
//// start fixtures hardware:
// battery monitor
pinMode(status::battery_sense, INPUT);
// button
if (config::settings.button_enable) {
ESP_LOGI(TAG, "Initializing OSC button");
pinMode(config::settings.button_pin, INPUT_PULLUP);
}
// strobe
if (config::settings.strobe_enable) {
strobe = new EspStrobe(config::settings.strobe_address);
ESP_LOGI(TAG, "Initializing DMX Strobe");
strobe->begin(config::settings.strobe_led_pin, 0);
e131->subscribe(config::settings.strobe_universe);
e131->universe(config::settings.strobe_universe)->onData(std::bind(&EspStrobe::recvData,
strobe, std::placeholders::_1));
}
// pixels
if (config::settings.strip_enable) {
strip = new EspRgbStrip(config::settings.strip_address,config::settings.strip_led_count);
// strip = new EspRgbaStrip(config::settings.strip_address,config::settings.strip_led_count);
ESP_LOGI(TAG, "Initializing Pixelmapped LEDs.");
strip->begin(config::settings.strip_data_pin);
e131->subscribe(config::settings.strip_universe);
e131->universe(config::settings.strip_universe)->onData(std::bind(&EspRgbStrip::recvData,
strip, std::placeholders::_1));
// e131->universe(config::settings.strip_universe)->onData(std::bind(&EspRgbaStrip::recvData,
// strip, std::placeholders::_1));
}
ESP_LOGV(TAG, "Setup complete.");
}
/*
Arduino process loop
*/
void loop() {
if (WiFi.getMode() == WIFI_STA &&
WiFi.status() != WL_CONNECTED) {
ESP_LOGV(TAG, "Waiting for WiFi...");
delay(1000);
return;
}
// Be a remote trigger
//
bool val = !digitalRead(config::settings.button_pin);
if (val != pressed && config::settings.button_enable) {
pressed = val;
if (pressed) {
ESP_LOGD(TAG, "Pressed");
OscWiFi.send(config::settings.osc_host, config::settings.osc_port,
config::settings.osc_pressed_addr, config::settings.osc_pressed_value);
} else {
ESP_LOGD(TAG, "Released");
OscWiFi.send(config::settings.osc_host, config::settings.osc_port,
config::settings.osc_released_addr, config::settings.osc_released_value);
}
}
}