Browse Source

reorganize main.cpp

tags/v0.1.0
Simon Schmidt 2 years ago
parent
commit
5443800b40
3 changed files with 56 additions and 36 deletions
  1. 11
    1
      .vscode/settings.json
  2. 1
    2
      README.md
  3. 44
    33
      src/main.cpp

+ 11
- 1
.vscode/settings.json View File

@@ -1,5 +1,15 @@
{
"files.associations": {
"random": "cpp"
"random": "cpp",
"array": "cpp",
"*.tcc": "cpp",
"deque": "cpp",
"list": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"string_view": "cpp",
"memory": "cpp",
"ranges": "cpp"
}
}

+ 1
- 2
README.md View File

@@ -17,8 +17,7 @@ https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html
```

##### platformio.ini
change IP of esp8266 accordingly
IP gets printed on Serial Monitor after first Upload
see templ_platformio_ini

```ini
[env:<BOARD>]

+ 44
- 33
src/main.cpp View File

@@ -1,17 +1,17 @@
#include <Arduino.h>
#include <Wire.h>

#include "wifi_credentials.h"
#include "PCA9685.h"

// Änderung
// OTA & WEB
#include "wifi_credentials.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#include <ESP8266WebServer.h>
#include <WiFiClient.h> // evtl weghauen ?
#include <WiFiClient.h>

#include "index.html.gz.h"
#include "style.css.gz.h"
@@ -21,8 +21,16 @@
const char* ssid = STASSID;
const char* password = STAPSK;

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";
@@ -36,7 +44,6 @@ void handleCssGz() {
server.send(200, dataType, (const char*)style_css_gz, style_css_gz_len);
}


void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\n\n";
@@ -83,26 +90,7 @@ void setup_webserver() {
Serial.println("HTTP server started");
}



PCA9685 pwmController;

void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

Wire.begin(); // Wire must be started first
Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz

while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}

// setup OTA
void setup_ota() {
ArduinoOTA.setPort(8266);
ArduinoOTA.setHostname("ESP_Treppenlicht");
ArduinoOTA.setPassword("admin");
@@ -142,21 +130,44 @@ void setup() {
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

setup_webserver();
}

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

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
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
}



void setup() {
Serial.begin(115200);
Serial.println(F("Booting ...."));

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("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

setup_ota();
setup_webserver();
setup_pwm_pca9685();
}

uint32_t t;
@@ -164,7 +175,7 @@ uint32_t t;
#define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); }

void loop() {
TIMEIF_US("OTA", ArduinoOTA.handle(), 20000);
TIMEIF_US("OTA", ArduinoOTA.handle(), 1000);
TIMEIF_US("HTTP", server.handleClient(), 20000);
TIMEIF_US("HTTP", server.handleClient(), 1000);
}

Loading…
Cancel
Save