Skip to content

Commit

Permalink
refactoring (WebServer): refactored main web root of application, red…
Browse files Browse the repository at this point in the history
…uces html requests to resources

- reduced default serial logging
- WifiWebServer needs a patch for building a working version see: khoih-prog/WiFiWebServer#3 (comment)
  • Loading branch information
Mario Lukas committed Dec 14, 2021
1 parent dc98c6c commit 210ba7a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 33 deletions.
25 changes: 20 additions & 5 deletions CO2-Ampel_Plus/CO2-Ampel_Plus.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ Button modeButton(BUTTON_PIN);
int wifi_reconnect_attemps = WIFI_RECONNECT_ATTEMPTS;

void setup() {



#if DEBUG_LOG > 0
while (!Serial) {
while (!Serial) {
; // wait for serial port to connect.
}
#endif
Expand Down Expand Up @@ -77,16 +80,20 @@ void setup() {
* Factory Reset when button is pressed while reset
*/
if (!config_is_initialized() || modeButton.isPressed()) {
#if DEBUG_LOG > 0
Serial.println("Loading factory defaults");
#endif
led_off();
led_set_color(LED_RED);
led_update();
delay(50);
config_set_factory_defaults();
led_off();
}
#if DEBUG_LOG > 0
Serial.println("Setup complete!");
Serial.println("------------------------");
#endif
}

void loop() {
Expand All @@ -100,18 +107,25 @@ void loop() {

switch (wifi_state) {
case WIFI_MODE_AP_INIT: // Create an Access Point
#if DEBUG_LOG > 0
Serial.println("Creating Access Point");
#endif
wifi_ap_create();
wifi_state = WIFI_MODE_AP_LISTEN;
#if DEBUG_LOG > 0
Serial.println("------------------------");
#endif
break;

case WIFI_MODE_WPA_CONNECT: // Connect to WiFi

device_config_t cfg = config_get_values();

#if DEBUG_LOG > 0
Serial.print("Connecting to SSID ");
Serial.print(cfg.wifi_ssid);
Serial.println(" Wifi");
#endif
if (strlen(cfg.wifi_ssid) != 0) {
if (wifi_wpa_connect() == WL_CONNECTED) {
wifi_state = WIFI_MODE_WPA_LISTEN;
Expand All @@ -122,21 +136,22 @@ void loop() {
Serial.println("No WiFi SSID Configured.");
wifi_state = WIFI_MODE_NOT_CONECTED;
}
#if DEBUG_LOG > 0
Serial.println("------------------------");
Serial.println("Start measurement");
Serial.println("");
#endif
break;
}



if (!wifi_is_connected()) {
wifi_state = WIFI_MODE_WPA_CONNECT;
}


mqtt_loop();

wifi_handle_client();
sensor_handler();
sensor_handle_brightness();
wifi_handle_client();

}
10 changes: 4 additions & 6 deletions CO2-Ampel_Plus/Config.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CONFIG_H
#define CONFIG_H

#define VERSION "v3.0.0"
#define VERSION "v3.0.1"

#define DEBUG_LOG 0 // 1 = Enable debug log

Expand Down Expand Up @@ -103,15 +103,13 @@
#define WIFI_MODE_NOT_CONECTED 87
#define WIFI_RECONNECT_ATTEMPTS 10

// Required for WiFiWebServer lib
#define USE_WIFI_NINA false
#define USE_WIFI101 true
#define DEBUG_WIFI_WEBSERVER_PORT Serial

// WIFI_LOGLEVEL below 4 causes EMPTY HTTP RESPONSE
// see: https://github.com/khoih-prog/WiFiWebServer/issues/3

#define DEBUG_WIFI_WEBSERVER_PORT Serial
#define _WIFI_LOGLEVEL_ 4
#define _WIFININA_LOGLEVEL_ 3
#define _WIFININA_LOGLEVEL_ 0

#define USE_WIFI_NINA false
#define USE_WIFI101 true
Expand Down
17 changes: 12 additions & 5 deletions CO2-Ampel_Plus/HTMLStatic.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ const char settings_header_html[] PROGMEM = R"=====(
<head>
<meta charset="UTF-8">
<title>CO2 Ampel Wifi Settings</title>
<link rel="stylesheet" href="styles.css">
<style>
)=====";


const char settings_middle_html[] PROGMEM = R"=====(
</style>
</head>
<body>
<div class="box">
<form class="box" action="/save" method="POST" name="loginForm">
<h1>Settings</h1>
)=====";



const char settings_footer_html[] PROGMEM = R"=====(
</div>
Expand Down Expand Up @@ -65,12 +72,13 @@ Password:<input type="password" name="login_password" placeholder="password"><br
/**
* Main HTML Page
*/
const char root_html[] PROGMEM = R"=====(
const char root_header_html[] PROGMEM = R"=====(
<head>
<meta charset="UTF-8">
<title>CO2 Ampel Wifi AP</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="ampel.css">
)=====";

const char root_footer_html[] PROGMEM = R"=====(
</head>
<div class="box"><h1>CO2 Ampel State</h1>
<span id="ampel" class="css-ampel %s"><span class="cssampelspan"></span></span>
Expand All @@ -79,5 +87,4 @@ const char root_html[] PROGMEM = R"=====(
<span id="state">Waiting for initial state...</span>
</div>
<body></body></html>
<script type="text/javascript" src="/scripts.js"></script>
)=====";
64 changes: 47 additions & 17 deletions CO2-Ampel_Plus/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,28 @@ bool isAuthenticated(){
if (server.hasHeader(F("Cookie"))){
String cookie = server.header(F("Cookie"));
if (cookie.indexOf(F("CO2SESSIONID=1")) != -1){
#if DEBUG_LOG > 0
Serial.println(F("Authentication Successful"));
#endif
return true;
}
}
return false;
}

void handleRoot(){
server.send(200, F("text/html"), root_html);
server.sendContent(root_header_html);

server.sendContent("<style>");
server.sendContent(css_styles_general);
server.sendContent(css_styles_ampel);
server.sendContent("</style>");

server.sendContent(root_footer_html);
server.sendContent("<script>");
server.sendContent(javascript);
server.sendContent("</script>");

}

void handleNotFound() {
Expand Down Expand Up @@ -153,6 +166,8 @@ void handleSettings(){
*/

server.sendContent(settings_header_html);
server.sendContent(css_styles_general);
server.sendContent(settings_middle_html);

server.sendContent(F("<label for=mqtt_broker_address>MQTT Broker IP</label>"));
server.sendContent(F("<input name=mqtt_broker_address placeholder='127.0.0.1' value='"));
Expand Down Expand Up @@ -316,14 +331,14 @@ void init_webserver_routes(int wifi_status){
server.on(F("/styles.css"), handleCSSGeneralFile);

if(wifi_status == WL_CONNECTED){
/*
server.on(F("/ampel.css"), handleCSSAmpelFile);
server.on(F("/scripts.js"), handleJavaScriptRequest);
server.on(F("/sensors.json"), handleJSONResponse);
/*
server.on(F("/settings"), handleSettings);
server.on(F("/login"), handleLogin);
server.on(F("/logout"), handleLogout);
*/
server.on(F("/sensors.json"), handleJSONResponse);
server.on(F("/"), handleRoot);
} else {
server.on(F("/"), handleSettings);
Expand Down Expand Up @@ -354,7 +369,9 @@ void wifi_ap_create() {
}

if (WiFi.status() == WL_NO_SHIELD) {
#if DEBUG_LOG > 0
Serial.println("WiFi shield not present");
#endif
// don't continue
while (true) {
led_failure(LED_COLOR_WIFI_FAILURE);
Expand All @@ -367,7 +384,9 @@ void wifi_ap_create() {
sprintf(ap_ssid, "%s %02X:%02X", WIFI_AP_SSID, wifi_mac[4], wifi_mac[5]);
wifi_status = WiFi.beginAP(ap_ssid, cfg.ap_password);
if (wifi_status != WL_AP_LISTENING) {
#if DEBUG_LOG > 0
Serial.println("Creating access point failed");
#endif
while (true) {
led_failure(LED_COLOR_WIFI_FAILURE);
}
Expand Down Expand Up @@ -395,7 +414,9 @@ int wifi_wpa_connect() {

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
#if DEBUG_LOG > 0
Serial.println("WiFi shield not present");
#endif
// don't continue
while (true) {
led_failure(LED_COLOR_WIFI_FAILURE);
Expand All @@ -411,13 +432,17 @@ int wifi_wpa_connect() {
}

if (WiFi.status() != WL_CONNECTED) {

#if DEBUG_LOG > 0
if (strlen(cfg.wifi_ssid) == 0) {
Serial.println("No SSID and Password set. Wifi connection failed");
} else {
Serial.print("Cennecting to ");
Serial.print(cfg.wifi_ssid);
Serial.println(" failed");
}
#endif

} else {
init_webserver_routes(wifi_status);
print_wifi_status();
Expand All @@ -430,6 +455,7 @@ int wifi_wpa_connect() {

void print_wifi_status() {
// print the SSID of the network you're attached to:
#if DEBUG_LOG > 0
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

Expand All @@ -443,9 +469,11 @@ void print_wifi_status() {
Serial.print("Signal strength (RSSI): ");
Serial.print(rssi);
Serial.println(" dBm");
#endif
}

void print_mac_address(byte mac[]) {
#if DEBUG_LOG > 0
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
Expand All @@ -456,28 +484,30 @@ void print_mac_address(byte mac[]) {
}
}
Serial.println();
#endif
}


void wifi_handle_client() {

// compare the previous status to the current status
if (wifi_status != WiFi.status()) {
// it has changed update the variable
wifi_status = WiFi.status();

if (wifi_status == WL_AP_CONNECTED) {
// a device has connected to the AP
Serial.println(F("Device connected to AP"));
} else {
// a device has disconnected from the AP, and we are back in listening
// mode
Serial.println(F("Device disconnected from AP"));
if (!ap_is_active()){
// compare the previous status to the current status
if (wifi_status != WiFi.status()) {
// it has changed update the variable
wifi_status = WiFi.status();
if (wifi_status == WL_AP_CONNECTED) {
// a device has connected to the AP
Serial.println(F("Device connected to AP"));
} else {
// a device has disconnected from the AP, and we are back in listening
// mode
Serial.println(F("Device disconnected from AP"));
}
}
}

server.handleClient();
delay(15);
delay(100);

// WiFiClient client = server.available(); // listen for incoming clients

Expand Down

0 comments on commit 210ba7a

Please sign in to comment.