reorder main, add ota.h

This commit is contained in:
2021-06-23 16:49:53 +02:00
parent 51bdd6210e
commit b3b11d0818
8 changed files with 1395 additions and 229 deletions
+38 -86
View File
@@ -1,40 +1,58 @@
#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"
}
#define ESP12_LED 2
#define NODEMCU_LED 16
// OTA & WEB
#include "wifi_credentials.h"
#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;
void setup_pwm_pca9685();
// WIFI
const char* ssid = STASSID;
const char* password = STAPSK;
os_timer_t timer1;
int timer_arg;
// PWM
uint32_t dimmtimer = 0;
uint8_t direction = 1;
uint8_t onoff = 1;
void setup_ota();
void setup_pwm_pca9685();
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);
void etup_pwm_pca9685();
// HTTP
void handleNotFound();
const int led = 13;
HTTPServer httpServer(80, "/");
// ===============================================
void timer_callback(void *pArg)
{
*((uint8_t *) pArg) = 1;
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
@@ -52,53 +70,6 @@ void handleNotFound() {
httpServer.send(404, "text/plain", message);
}
PCA9685 pwmController;
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
@@ -109,22 +80,6 @@ void setup_pwm_pca9685() {
}
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);
@@ -135,7 +90,6 @@ void setup() {
Serial.println(F("Booting ...."));
pinMode(NODEMCU_LED, OUTPUT);
pinMode(ESP12_LED, OUTPUT);
@@ -151,22 +105,20 @@ void setup() {
ESP.restart();
}
pinMode(led, OUTPUT);
digitalWrite(led, 0);
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_setfn(&timer1, timer_callback, &timer_flag);
os_timer_arm(&timer1, 1, true);
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
setup_ota();
ota_setup();
httpServer.start();
httpServer.onNotFound(handleNotFound);
@@ -275,8 +227,8 @@ void loop() {
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
if(flag) {
flag = 0;
if(timer_flag) {
timer_flag = 0;
ledsequence(direction, onoff, 4);
//Serial.printf("[%lu] interrupt\n\r", millis());
}