Browse Source

reorganize main.cpp

tags/v0.1.0
Simon Schmidt 3 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

{ {
"files.associations": { "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

``` ```


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


```ini ```ini
[env:<BOARD>] [env:<BOARD>]

+ 44
- 33
src/main.cpp View File

#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>


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


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


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


#include "index.html.gz.h" #include "index.html.gz.h"
#include "style.css.gz.h" #include "style.css.gz.h"
const char* ssid = STASSID; const char* ssid = STASSID;
const char* password = STAPSK; const char* password = STAPSK;


void setup_webserver();
void setup_ota();
void setup_pwm_pca9685();
void handleRootGz();
void handleCssGz();
void handleNotFound();

const int led = 13; const int led = 13;
ESP8266WebServer server(80); ESP8266WebServer server(80);
PCA9685 pwmController;


void handleRootGz() { void handleRootGz() {
const char* dataType = "text/html"; const char* dataType = "text/html";
server.send(200, dataType, (const char*)style_css_gz, style_css_gz_len); server.send(200, dataType, (const char*)style_css_gz, style_css_gz_len);
} }



void handleNotFound() { void handleNotFound() {
digitalWrite(led, 1); digitalWrite(led, 1);
String message = "File Not Found\n\n"; String message = "File Not Found\n\n";
Serial.println("HTTP server started"); 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.setPort(8266);
ArduinoOTA.setHostname("ESP_Treppenlicht"); ArduinoOTA.setHostname("ESP_Treppenlicht");
ArduinoOTA.setPassword("admin"); ArduinoOTA.setPassword("admin");
} }
}); });
ArduinoOTA.begin(); 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.resetDevices(); // Software resets all PCA9685 devices on Wire line


pwmController.init(B000000); // Address pins A5-A0 set to B000000 pwmController.init(B000000); // Address pins A5-A0 set to B000000
pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz 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; uint32_t t;
#define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); } #define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); }


void loop() { 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