188 lines
4.5 KiB
C++
Raw Normal View History

2021-06-10 18:36:16 +02:00
#include <Arduino.h>
2021-06-10 20:58:48 +02:00
#include <Wire.h>
#include "PCA9685.h"
2021-06-10 18:36:16 +02:00
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "wifi_credentials.h"
2021-06-10 20:58:48 +02:00
#include "index.html.h"
2021-06-10 18:36:16 +02:00
const char* ssid = STASSID;
const char* password = STAPSK;
const int led = 13;
ESP8266WebServer server(80);
void handleRoot() {
digitalWrite(led, 1);
2021-06-10 20:58:48 +02:00
// char temp[sizeof(index_html)];
// snprintf(temp, sizeof(index_html), index_html);
server.send(200, "text/html", index_html);
2021-06-10 18:36:16 +02:00
digitalWrite(led, 0);
}
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() {
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());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
2021-06-10 20:06:58 +02:00
2021-06-10 20:19:25 +02:00
PCA9685 pwmController;
2021-06-10 20:06:58 +02:00
2021-06-10 18:36:16 +02:00
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
2021-06-10 20:06:58 +02:00
2021-06-10 20:19:25 +02:00
Wire.begin(); // Wire must be started first
Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
2021-06-10 18:36:16 +02:00
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
2021-06-10 18:47:55 +02:00
// setup OTA
ArduinoOTA.setPort(8266);
ArduinoOTA.setHostname("ESP_Treppenlicht");
ArduinoOTA.setPassword("admin");
2021-06-10 18:36:16 +02:00
// 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();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
setup_webserver();
2021-06-10 20:19:25 +02:00
pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line
2021-06-10 20:21:57 +02:00
pwmController.init(B000000); // Address pins A5-A0 set to B000000
pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz
2021-06-10 20:19:25 +02:00
pwmController.setChannelPWM(0, 128 << 4); // Set PWM to 128/255, but in 4096 land
Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4
2021-06-10 18:36:16 +02:00
}
2021-06-10 20:58:48 +02:00
unsigned long i = 0;
2021-06-10 20:06:58 +02:00
unsigned long a = 0;
unsigned long s = 0;
float ard_ota_time = 0;
float server_handle = 0;
2021-06-10 18:36:16 +02:00
void loop() {
2021-06-10 20:06:58 +02:00
a = micros();
2021-06-10 18:36:16 +02:00
ArduinoOTA.handle();
2021-06-10 20:06:58 +02:00
ard_ota_time += micros()-a;
2021-06-10 18:36:16 +02:00
2021-06-10 20:06:58 +02:00
s = micros();
2021-06-10 18:36:16 +02:00
server.handleClient();
2021-06-10 20:06:58 +02:00
server_handle += micros()-s;
if(++i >= 10000) {
Serial.print("Mittlere Laufzeit\tOTA: ");
Serial.print(ard_ota_time/10000.0);
Serial.print("µs\tHTTP: ");
Serial.print(server_handle/10000.0);
Serial.println("µs");
ard_ota_time = 0;
server_handle = 0;
i = 0;
}
2021-06-10 18:36:16 +02:00
}