333 lines
8.5 KiB
C++
333 lines
8.5 KiB
C++
#include <Arduino.h>
|
|
|
|
// debugging support via GDBStub over UART
|
|
#ifdef WITH_DEBUGGING_ON
|
|
#include <GDBStub.h>
|
|
#endif
|
|
|
|
#include "PCA9685.h"
|
|
extern "C" {
|
|
#include "user_interface.h"
|
|
}
|
|
|
|
#define ESP12_LED 2
|
|
#define NODEMCU_LED 16
|
|
|
|
// OTA & WEB
|
|
#include "wifi_credentials.h"
|
|
#include <ESP8266WiFi.h>
|
|
#include <WiFiUdp.h>
|
|
#include <ArduinoOTA.h>
|
|
|
|
#include "filesys.h"
|
|
#include <ESP8266WebServer.h>
|
|
#include <WiFiClient.h>
|
|
|
|
#include "index.html.gz.h"
|
|
#include "style.css.gz.h"
|
|
#include "favicon.png.gz.h"
|
|
// images are possible
|
|
// maybe check out FS <- SPIFFS
|
|
|
|
const char* ssid = STASSID;
|
|
const char* password = STAPSK;
|
|
|
|
os_timer_t timer1;
|
|
int timer_arg;
|
|
|
|
void setup_webserver();
|
|
void setup_ota();
|
|
void setup_pwm_pca9685();
|
|
void handleRootGz();
|
|
void handleCssGz();
|
|
void handleNotFound();
|
|
|
|
const int led = 13;
|
|
ESP8266WebServer server(80);
|
|
PCA9685 pwmController;
|
|
|
|
void handleRootGz() {
|
|
const char* dataType = "text/html";
|
|
server.sendHeader(F("Content-Encoding"), F("gzip"));
|
|
server.send(200, dataType, (const char*)index_html_gz, index_html_gz_len);
|
|
}
|
|
|
|
void handleCssGz() {
|
|
const char* dataType = "text/css";
|
|
server.sendHeader(F("Content-Encoding"), F("gzip"));
|
|
server.send(200, dataType, (const char*)style_css_gz, style_css_gz_len);
|
|
}
|
|
void handleFaviconGz() {
|
|
const char* dataType = "image/png";
|
|
server.sendHeader(F("Content-Encoding"), F("gzip"));
|
|
server.send(200, dataType, (const char*)favicon_png_gz, favicon_png_gz_len);
|
|
}
|
|
|
|
void handleNotFound() {
|
|
digitalWrite(led, 1);
|
|
String message = "File Not Found\n\n";
|
|
message += "URI: ";
|
|
message += server.uri();
|
|
message += "\nMethod: ";
|
|
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
|
message += "\nArguments: ";
|
|
message += server.args();
|
|
message += "\n";
|
|
|
|
for (uint8_t i = 0; i < server.args(); i++) {
|
|
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
|
}
|
|
|
|
server.send(404, "text/plain", message);
|
|
digitalWrite(led, 0);
|
|
}
|
|
|
|
|
|
void setup_webserver() {
|
|
server.on("/", handleRootGz);
|
|
server.on("/style.css", handleCssGz);
|
|
server.on("/favicon.png", handleFaviconGz);
|
|
// void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL );
|
|
|
|
server.serveStatic("/static", LittleFS, "/tt.html");
|
|
|
|
server.onNotFound(handleNotFound);
|
|
|
|
server.begin();
|
|
Serial.println("HTTP server started");
|
|
}
|
|
|
|
void setup_ota() {
|
|
ArduinoOTA.setPort(8266);
|
|
ArduinoOTA.setHostname("ESP_Treppenlicht");
|
|
ArduinoOTA.setPassword("admin");
|
|
// Password can be set with it's md5 value as well
|
|
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
|
|
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
|
|
|
|
ArduinoOTA.onStart([]() {
|
|
String type;
|
|
if (ArduinoOTA.getCommand() == U_FLASH) {
|
|
type = "sketch";
|
|
} else { // U_FS
|
|
type = "filesystem";
|
|
}
|
|
|
|
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
|
|
Serial.println("Start updating " + type);
|
|
});
|
|
ArduinoOTA.onEnd([]() {
|
|
Serial.println("\nEnd");
|
|
});
|
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
|
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
|
});
|
|
ArduinoOTA.onError([](ota_error_t error) {
|
|
Serial.printf("Error[%u]: ", error);
|
|
if (error == OTA_AUTH_ERROR) {
|
|
Serial.println("Auth Failed");
|
|
} else if (error == OTA_BEGIN_ERROR) {
|
|
Serial.println("Begin Failed");
|
|
} else if (error == OTA_CONNECT_ERROR) {
|
|
Serial.println("Connect Failed");
|
|
} else if (error == OTA_RECEIVE_ERROR) {
|
|
Serial.println("Receive Failed");
|
|
} else if (error == OTA_END_ERROR) {
|
|
Serial.println("End Failed");
|
|
}
|
|
});
|
|
ArduinoOTA.begin();
|
|
}
|
|
|
|
void setup_pwm_pca9685() {
|
|
pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
|
|
|
|
pwmController.init(B000000); // Address pins A5-A0 set to B000000
|
|
pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz
|
|
|
|
Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
|
|
}
|
|
|
|
|
|
uint32_t dimmtimer = 0;
|
|
uint8_t direction = 1;
|
|
uint8_t onoff = 1;
|
|
|
|
void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor);
|
|
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval, uint8_t factor);
|
|
|
|
uint8_t flag = 0;
|
|
void timer_callback(void *pArg)
|
|
{
|
|
*((int *) pArg) += 1;
|
|
|
|
flag = 1;
|
|
}
|
|
|
|
|
|
void setup() {
|
|
#ifdef WITH_DEBUGGING_ON
|
|
Serial.begin(460800);
|
|
gdbstub_init();
|
|
#else
|
|
Serial.begin(115200);
|
|
#endif
|
|
|
|
Serial.println(F("Booting ...."));
|
|
|
|
mount_fs();
|
|
//format_fs();
|
|
ls("/");
|
|
ls("tester/");
|
|
// readFile("/test.txt");
|
|
|
|
pinMode(NODEMCU_LED, OUTPUT);
|
|
pinMode(ESP12_LED, OUTPUT);
|
|
|
|
Wire.begin(); // Wire must be started first
|
|
Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
|
Serial.println("Connection Failed! Rebooting...");
|
|
delay(5000);
|
|
ESP.restart();
|
|
}
|
|
|
|
|
|
|
|
Serial.println("");
|
|
Serial.print("Connected to ");
|
|
Serial.println(ssid);
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
os_timer_setfn(&timer1, timer_callback, &timer_arg);
|
|
os_timer_arm(&timer1, 1000, true);
|
|
|
|
Serial.println("Ready");
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
setup_ota();
|
|
|
|
pinMode(led, OUTPUT);
|
|
digitalWrite(led, 0);
|
|
Serial.println("");
|
|
|
|
// Wait for connection
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
setup_webserver();
|
|
setup_pwm_pca9685();
|
|
}
|
|
|
|
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval, uint8_t factor){
|
|
static uint8_t lastled = 255;
|
|
static uint8_t current_pwm = 0;
|
|
|
|
if(led != lastled){
|
|
pwmController.setChannelPWM(led, startval);
|
|
lastled = led;
|
|
current_pwm = startval;
|
|
return 1;
|
|
}
|
|
|
|
if(current_pwm == stopval){
|
|
return 0;
|
|
}
|
|
|
|
else if(startval > stopval){
|
|
current_pwm -= 1;
|
|
}
|
|
else {
|
|
current_pwm += 1;
|
|
}
|
|
pwmController.setChannelPWM(led, current_pwm*factor);
|
|
return 1;
|
|
}
|
|
|
|
#define LEDCOUNT 16
|
|
|
|
void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor){
|
|
static int8_t led = 0;
|
|
static uint8_t brightness = 0;
|
|
static uint8_t lastbrightness = 0;
|
|
static uint8_t finish = 1;
|
|
static uint32_t status = 0;
|
|
uint32_t status_build = 0;
|
|
status_build |= direction << 16;
|
|
status_build |= onoff << 8;
|
|
status_build |= factor;
|
|
if(status_build != status){ // check if any parameter changed
|
|
finish = 0; // set state unfinished -> start action
|
|
if(direction) led = 0; // reset led counter depending of direction
|
|
else led = LEDCOUNT-1;
|
|
if(onoff){
|
|
brightness = 127; // set brightness value depending of on/off
|
|
lastbrightness = 0;
|
|
}
|
|
else{
|
|
brightness = 0;
|
|
lastbrightness = 127;
|
|
}
|
|
status = status_build; // set parameter memory
|
|
Serial.print("----Status Changed! onoff: ");
|
|
Serial.print(onoff);
|
|
Serial.print(" dir: ");
|
|
Serial.println(direction);
|
|
|
|
}
|
|
if(!finish){ // finish == 0 -> action pending
|
|
if(!softstart_led(led,lastbrightness, brightness, factor)){
|
|
Serial.print("one LED finished, new set led: ");
|
|
Serial.print(led);
|
|
Serial.print(" last: ");
|
|
Serial.print(lastbrightness);
|
|
Serial.print(" curr: ");
|
|
Serial.println(brightness);
|
|
if(direction){
|
|
led++;
|
|
if(led >= LEDCOUNT) {
|
|
finish = 1;
|
|
//lastbrightness = brightness;
|
|
}
|
|
}
|
|
else{
|
|
led--;
|
|
if(led < 0){
|
|
//lastbrightness = brightness;
|
|
finish = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
uint32_t _t=0;
|
|
#define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("us")
|
|
#define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); }
|
|
|
|
void loop() {
|
|
if(millis() - dimmtimer > 2){
|
|
//ledsequence(direction, onoff, 4);
|
|
dimmtimer = millis();
|
|
}
|
|
if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0;
|
|
if(millis() > 35000 && direction == 1){
|
|
onoff = 1;
|
|
direction = 0;
|
|
}
|
|
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
|
|
TIMEIF_US(server.handleClient(), 1000, "HTTP");
|
|
|
|
if(flag) {
|
|
flag = 0;
|
|
ledsequence(direction, onoff, 4);
|
|
Serial.printf("[%lu] interrupt\n\r", millis());
|
|
}
|
|
} |