This commit is contained in:
2021-06-23 16:57:11 +02:00
24 changed files with 1857 additions and 223 deletions
+130
View File
@@ -0,0 +1,130 @@
#include "filesys.h"
// listDir("/");
// deleteFile("/hello.txt");
// writeFile("/hello.txt", "Hello ");
// appendFile("/hello.txt", "World!\n\r");
// readFile("/hello.txt");
// listDir("/");
FSInfo fsinfo;
bool mount_fs() {
if(!LittleFS.begin()) {
Serial.println("[ERROR] LittleFS.info(), reset ...");
return false;
}
if(!LittleFS.info(fsinfo)) {
Serial.println("[ERROR] LittleFS.info(), reset ...");
return false;
}
printf("Filesystem opened:\n\r");
printf("\ttotalBytes:\t%d\n\r", fsinfo.totalBytes);
printf("\tusedBytes:\t%d\n\r", fsinfo.usedBytes);
printf("\tblockSize:\t%d\n\r", fsinfo.blockSize);
printf("\tpageSize:\t%d\n\r", fsinfo.pageSize);
printf("\tmaxOpenFiles:\t%d\n\r", fsinfo.maxOpenFiles);
printf("\tmaxPathLength:\t%d\n\r", fsinfo.maxPathLength);
printf("\n\r");
return true;
}
bool format_fs() {
printf("Formatting FS ! \n\r");
return LittleFS.format();
}
void ls(const char * dirname) {
Serial.printf("ls -l %s\n\r", dirname);
Dir root = LittleFS.openDir(dirname);
while (root.next()) {
File file = root.openFile("r");
time_t lw = file.getLastWrite();
struct tm * tmstruct = localtime(&lw);
Serial.printf("%8d %02d %02d %02d:%02d %s\n\r",
file.size(),
tmstruct->tm_mon + 1,
tmstruct->tm_mday,
tmstruct->tm_hour,
tmstruct->tm_min,
root.fileName().c_str());
file.close();
}
Serial.println();
}
void readFile(const char * path) {
Serial.printf("Reading file: %s\n\r", path);
File file = LittleFS.open(path, "r");
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while (file.available()) {
Serial.write(file.read());
}
file.close();
}
void writeFile(const char * path, const char * message) {
Serial.printf("Writing file: %s\n\r", path);
File file = LittleFS.open(path, "w");
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
delay(2000); // Make sure the CREATE and LASTWRITE times are different
file.close();
}
void appendFile(const char * path, const char * message) {
Serial.printf("Appending to file: %s\n\r", path);
File file = LittleFS.open(path, "a");
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
void renameFile(const char * path1, const char * path2) {
Serial.printf("Renaming file %s to %s\n\r", path1, path2);
if (LittleFS.rename(path1, path2)) {
Serial.println("File renamed");
} else {
Serial.println("Rename failed");
}
}
void deleteFile(const char * path) {
Serial.printf("Deleting file: %s\n\r", path);
if (LittleFS.remove(path)) {
Serial.println("File deleted");
} else {
Serial.println("Delete failed");
}
}
+17
View File
@@ -0,0 +1,17 @@
#include "httpserver.h"
bool HTTPServer::addRootFileHandler() {
// //experimental, see doku.md
// server.serveStatic("/compressed", LittleFS, "/compressed.html.gzip");
this->serveStatic("", LittleFS, "/index.html");
this->serveStatic("/", LittleFS, "/index.html");
this->serveStatic("/#", LittleFS, "/index.html");
this->serveStatic("/style.css", LittleFS, "/style.css");
this->serveStatic("/input.js", LittleFS, "/input.js");
this->serveStatic("/favicon.png", LittleFS, "/favicon.png");
return true;
}
+51 -135
View File
@@ -1,25 +1,38 @@
#include <Arduino.h>
// debugging support via GDBStub over UART
#ifdef WITH_DEBUGGING_ON
#include <GDBStub.h>
#include <GDBStub.h> // debugging support via GDBStub over UART
#endif
#include "PCA9685.h"
extern "C" {
#include "user_interface.h"
extern "C" {
#include "user_interface.h"
}
// OTA & WEB
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "ota.h"
#include "wifi_credentials.h"
#include "httpserver.h"
// BOARD
#define ESP12_LED 2
#define NODEMCU_LED 16
// PWM
os_timer_t timer1;
uint8_t timer_flag = 0;
PCA9685 pwmController;
os_timer_t Timer1; // Verwaltungsstruktur des Timers
void setup_pwm_pca9685();
int c;
bool toggle = false;
uint32_t m=0;
// WIFI
const char* ssid = STASSID;
const char* password = STAPSK;
// PWM
uint32_t dimmtimer = 0;
uint16_t time_per_stair = 500; // global parameter: dimmtime per stair [ms]
uint8_t direction = 1;
@@ -40,138 +53,28 @@ void timerCallback(void *pArg)
// m = micros();
}
// OTA & WEB
#include "wifi_credentials.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.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;
void setup_webserver();
void setup_ota();
void setup_pwm_pca9685();
void handleRootGz();
void handleCssGz();
// HTTP
void handleNotFound();
HTTPServer httpServer(80, "/");
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 += httpServer.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += httpServer.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
for (uint8_t i = 0; i < httpServer.args(); i++) {
message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup_webserver() {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRootGz);
server.on("/style.css", handleCssGz);
server.on("/favicon.png", handleFaviconGz);
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();
httpServer.send(404, "text/plain", message);
}
void setup_pwm_pca9685() {
@@ -183,6 +86,7 @@ void setup_pwm_pca9685() {
Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
}
void setup() {
#ifdef WITH_DEBUGGING_ON
Serial.begin(460800);
@@ -208,17 +112,27 @@ void setup() {
ESP.restart();
}
os_timer_setfn(&Timer1, timerCallback, &c);
os_timer_arm(&Timer1, 20, true);
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
os_timer_setfn(&timer1, timerCallback, &timer_flag);
os_timer_arm(&timer1, 20, true);
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
setup_ota();
setup_webserver();
ota_setup();
httpServer.start();
httpServer.onNotFound(handleNotFound);
Serial.println("HTTP server started !");
setup_pwm_pca9685();
Serial.println("PCA9685 connected !");
}
uint32_t t;
@@ -319,6 +233,9 @@ void ledsequence(uint8_t direction, uint8_t onoff){
}
}
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() {
@@ -326,10 +243,9 @@ void loop() {
if(millis() > 25000 && onoff == 1 && direction == 1) onoff = 0;
if(millis() > 45000 && direction == 1){
onoff = 1;
direction = 0;
direction = 0;
}
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
TIMEIF_US("OTA", ArduinoOTA.handle(), 1000);
TIMEIF_US("HTTP", server.handleClient(), 1000);
}