Browse Source

lots of cleaning

better separation of config/functions by categories : web, led, ota,
eeprom
compiles, not tested on hardware
Etienne Landon 9 years ago
parent
commit
3dfba27b17
13 changed files with 539 additions and 467 deletions
  1. 9 9
      ESP_UKI.ino
  2. 141 0
      eeprom.h
  3. 0 195
      functions.h
  4. 68 221
      global.h
  5. 23 25
      includes.h
  6. 29 0
      leds.h
  7. 55 0
      ota.h
  8. 197 0
      web.h
  9. 6 6
      web_General.h
  10. 2 2
      web_Information.h
  11. 2 2
      web_NTPSettings.h
  12. 6 6
      web_NetworkConfiguration.h
  13. 1 1
      web_Root.h

+ 9 - 9
ESP_UKI.ino

@@ -2,22 +2,22 @@
     ESP_UKI
 
   TODO :  clean webserver
-          add uki configuration/information page (player number, ADC value, IP of main computer)
+          better function organizing
+          add firmware number in webserver
+          rework led system with tickers
           send ADC to default IP via udp, allow configuration
-          ajout numéro de firmware sur webserver
+         
 */
 	
 
 #include "includes.h"  //headers and variables declaration
 
-/* UKI udp configuration */
-
-int UKI_UDP_In_Port = 9000;  //udp port input for ESP
-IPAddress UKI_UDP_Master_IP(192, 168, 0, 41);  //default udp address to send to. Will automatically change to the ip sending something to udp in
- 
 
 void setup ( void ) {
-  startESP();
+  EEPROM.begin(512);
+  Serial.begin(115200);
+  Serial.println("Starting ES8266");
+  setupLeds();
   setupWifi();
   setupWebserver();
   setupOTA();
@@ -42,7 +42,7 @@ void loop ( void ) {
   
   loopWebserver();
   
-  loopHandles();
+  loopOTA();
 
   /*  UKI part	*/
   GSR_sensor = analogRead(A0);

+ 141 - 0
eeprom.h

@@ -0,0 +1,141 @@
+
+struct strConfig {
+  String ssid;
+  String password;
+  byte  IP[4];
+  byte  Netmask[4];
+  byte  Gateway[4];
+  boolean dhcp;
+  String ntpServerName;
+  long Update_Time_Via_NTP_Every;
+  long timezone;
+  boolean daylight;
+  String DeviceName;
+  boolean AutoTurnOff;
+  boolean AutoTurnOn;
+  byte TurnOffHour;
+  byte TurnOffMinute;
+  byte TurnOnHour;
+  byte TurnOnMinute;
+  byte LED_R;
+  byte LED_G;
+  byte LED_B;
+}   config;
+
+
+/*
+**
+** CONFIGURATION HANDLING
+**
+*/
+
+
+
+
+
+
+void WriteConfig()
+{
+
+  Serial.println("Writing Config");
+  EEPROM.write(0,'C');
+  EEPROM.write(1,'F');
+  EEPROM.write(2,'G');
+
+  EEPROM.write(16,config.dhcp);
+  EEPROM.write(17,config.daylight);
+  
+  EEPROMWritelong(18,config.Update_Time_Via_NTP_Every); // 4 Byte
+
+  EEPROMWritelong(22,config.timezone);  // 4 Byte
+
+
+  EEPROM.write(26,config.LED_R);
+  EEPROM.write(27,config.LED_G);
+  EEPROM.write(28,config.LED_B);
+
+  EEPROM.write(32,config.IP[0]);
+  EEPROM.write(33,config.IP[1]);
+  EEPROM.write(34,config.IP[2]);
+  EEPROM.write(35,config.IP[3]);
+
+  EEPROM.write(36,config.Netmask[0]);
+  EEPROM.write(37,config.Netmask[1]);
+  EEPROM.write(38,config.Netmask[2]);
+  EEPROM.write(39,config.Netmask[3]);
+
+  EEPROM.write(40,config.Gateway[0]);
+  EEPROM.write(41,config.Gateway[1]);
+  EEPROM.write(42,config.Gateway[2]);
+  EEPROM.write(43,config.Gateway[3]);
+
+
+  WriteStringToEEPROM(64,config.ssid);
+  WriteStringToEEPROM(96,config.password);
+  WriteStringToEEPROM(128,config.ntpServerName);
+
+  EEPROM.write(300,config.AutoTurnOn);
+  EEPROM.write(301,config.AutoTurnOff);
+  EEPROM.write(302,config.TurnOnHour);
+  EEPROM.write(303,config.TurnOnMinute);
+  EEPROM.write(304,config.TurnOffHour);
+  EEPROM.write(305,config.TurnOffMinute);
+  WriteStringToEEPROM(306,config.DeviceName);
+  
+
+
+  EEPROM.commit();
+}
+boolean ReadConfig()
+{
+
+  Serial.println("Reading Configuration");
+  if (EEPROM.read(0) == 'C' && EEPROM.read(1) == 'F'  && EEPROM.read(2) == 'G' )
+  {
+    Serial.println("Configurarion Found!");
+    config.dhcp =   EEPROM.read(16);
+
+    config.daylight = EEPROM.read(17);
+
+    config.Update_Time_Via_NTP_Every = EEPROMReadlong(18); // 4 Byte
+
+    config.timezone = EEPROMReadlong(22); // 4 Byte
+
+    config.LED_R = EEPROM.read(26);
+    config.LED_G = EEPROM.read(27);
+    config.LED_B = EEPROM.read(28);
+
+    config.IP[0] = EEPROM.read(32);
+    config.IP[1] = EEPROM.read(33);
+    config.IP[2] = EEPROM.read(34);
+    config.IP[3] = EEPROM.read(35);
+    config.Netmask[0] = EEPROM.read(36);
+    config.Netmask[1] = EEPROM.read(37);
+    config.Netmask[2] = EEPROM.read(38);
+    config.Netmask[3] = EEPROM.read(39);
+    config.Gateway[0] = EEPROM.read(40);
+    config.Gateway[1] = EEPROM.read(41);
+    config.Gateway[2] = EEPROM.read(42);
+    config.Gateway[3] = EEPROM.read(43);
+    config.ssid = ReadStringFromEEPROM(64);
+    config.password = ReadStringFromEEPROM(96);
+    config.ntpServerName = ReadStringFromEEPROM(128);
+    
+    
+    config.AutoTurnOn = EEPROM.read(300);
+    config.AutoTurnOff = EEPROM.read(301);
+    config.TurnOnHour = EEPROM.read(302);
+    config.TurnOnMinute = EEPROM.read(303);
+    config.TurnOffHour = EEPROM.read(304);
+    config.TurnOffMinute = EEPROM.read(305);
+    config.DeviceName= ReadStringFromEEPROM(306);
+    return true;
+    
+  }
+  else
+  {
+    Serial.println("Configurarion NOT FOUND!!!!");
+    return false;
+  }
+}
+

+ 0 - 195
functions.h

@@ -1,195 +0,0 @@
-
-
-void ledBlink (int Led, int blink_qty, int blink_time) {
-  for (int i = 0 ; i < blink_qty ; i++) {
-    digitalWrite(Led, LOW) ;
-    delay(blink_time);
-    digitalWrite(Led, HIGH);
-    delay(blink_time);
-  }
-}
-  
-  void startESP() {
-  EEPROM.begin(512);
-  Serial.begin(115200);
-  pinMode(Blue_Led, OUTPUT);
-  digitalWrite(Blue_Led, HIGH);
-  pinMode(Red_Led, OUTPUT);
-  digitalWrite(Red_Led, LOW);//red led on
-  delay(500);
-  Serial.println("Starting ES8266");
-}
-
-
-void setupWifi(){
-  if (!ReadConfig())  {
-    // DEFAULT CONFIG
-    config.ssid = "Freebox-6F7B3C";
-    config.password = "accessorem6-gignendi7-insultare!";
-    config.dhcp = true;
-    config.IP[0] = 192; config.IP[1] = 168; config.IP[2] = 0; config.IP[3] = 100;
-    config.Netmask[0] = 255; config.Netmask[1] = 255; config.Netmask[2] = 255; config.Netmask[3] = 0;
-    config.Gateway[0] = 192; config.Gateway[1] = 168; config.Gateway[2] = 0; config.Gateway[3] = 254;
-    config.ntpServerName = "0.de.pool.ntp.org";
-    config.Update_Time_Via_NTP_Every =  0;
-    config.timezone = -10;
-    config.daylight = true;
-    config.DeviceName = "UKI_ESP_default";
-    config.AutoTurnOff = false;
-    config.AutoTurnOn = false;
-    config.TurnOffHour = 0;
-    config.TurnOffMinute = 0;
-    config.TurnOnHour = 0;
-    config.TurnOnMinute = 0;
-    WriteConfig();
-    Serial.println("General config applied");
-  }
-
-
-  if (AdminEnabled)  {
-    WiFi.mode(WIFI_AP_STA);
-    //WiFi.softAP( ACCESS_POINT_NAME , ACCESS_POINT_PASSWORD);
-    WiFi.softAP( config.DeviceName.c_str() , ACCESS_POINT_PASSWORD);
-  }
-  else  { WiFi.mode(WIFI_STA); }
-  ConfigureWifi();
-  while (WiFi.status() != 3) {
-    Serial.println(WiFi.status());
-  }
-//    Serial.print(".");
-//    digitalWrite(Blue_Led, Blue_Led_State);
-//    Blue_Led_State = !Blue_Led_State;
-//    yield();
-//  }
-  Serial.println(WiFi.status());
-
-  /* A MODIFIER : pour le moment si AdminEnabled, lance pendant AdminTimeOut un AP en parallèle de la connection configurée
-   *  devrait être remplacé par  - tente de se connecter au wifi configuré, si ok on continue normalement
-   *                             - si n'arrive pas à se connecter au wifi configuré, passe en mode AP avec webserver pour reconfigurer le wifi / ou demarrer en mode AP si bouton utilisateur appuyé pendant démarragae
-   *                             - normalement redémarre après reconfiguration, donc ok
-   *                             - eventuellement ajouter dans loop un redemarrage en cas de perte de connection
-   */
-
-}
-
-void setupWebserver(){
-  server.on ( "/", []() {digitalWrite(Blue_Led, LOW); Serial.println("admin.html"); server.send ( 200, "text/html", web_AdminMainPage ); digitalWrite(Blue_Led, HIGH);  }  );
-  server.on ( "/admin.html", []() {digitalWrite(Blue_Led, LOW); Serial.println("admin.html"); server.send ( 200, "text/html", web_AdminMainPage ); digitalWrite(Blue_Led, HIGH);  }  );
-  server.on ( "/config.html", send_network_configuration_html );
-  server.on ( "/info.html", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("info.html"); server.send ( 200, "text/html", web_Information ); digitalWrite(Blue_Led, HIGH) ; }  );
-  server.on ( "/ntp.html", send_NTP_configuration_html  );
-  server.on ( "/general.html",  send_general_html );
-  server.on ( "/style.css", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("style.css"); server.send ( 200, "text/plain", web_Style_css ); digitalWrite(Blue_Led, HIGH) ; } );
-  server.on ( "/microajax.js", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("microajax.js"); server.send ( 200, "text/plain", web_microajax_js ); digitalWrite(Blue_Led, HIGH); } );
-  server.on ( "/admin/values", send_network_configuration_values_html );
-  server.on ( "/admin/connectionstate", send_connection_state_values_html );
-  server.on ( "/admin/infovalues", send_information_values_html );
-  server.on ( "/admin/ntpvalues", send_NTP_configuration_values_html );
-  server.on ( "/admin/generalvalues", send_general_configuration_values_html);
-  server.on ( "/admin/devicename",     send_devicename_value_html);
-
-  server.onNotFound ( []() {
-    Serial.println("Page Not Found");
-    server.send ( 400, "text/html", "Page not Found" );
-    ledBlink(Blue_Led, 10, 100);
-  }  );
-  server.begin();
-  Serial.println( "HTTP server started" );
-  tkSecond.attach(1, Second_Tick);
-  UDPNTPClient.begin(2390);  // Port for NTP receive
-}
-
-void loopWebserver(){
-  if (AdminEnabled)  {
-    if (AdminTimeOutCounter > AdminTimeOut)   {
-      AdminEnabled = false;
-      Serial.println("Admin Mode disabled!");
-      WiFi.mode(WIFI_STA);
-    }
-  }
-  if (config.Update_Time_Via_NTP_Every  > 0 )  {
-    if (cNTP_Update > 5 && firstStart)    {
-      NTPRefresh();
-      cNTP_Update = 0;
-      firstStart = false;
-    }
-    else if ( cNTP_Update > (config.Update_Time_Via_NTP_Every * 60) )    {
-      NTPRefresh();
-      cNTP_Update = 0;
-    }
-  }
-
-  if (DateTime.minute != Minute_Old)  {
-    Minute_Old = DateTime.minute;
-    if (config.AutoTurnOn)
-      if (DateTime.hour == config.TurnOnHour && DateTime.minute == config.TurnOnMinute)      {
-        Serial.println("SwitchON");
-      }
-    }
-
-    Minute_Old = DateTime.minute;
-    if (config.AutoTurnOff)    {
-      if (DateTime.hour == config.TurnOffHour && DateTime.minute == config.TurnOffMinute)      {
-        Serial.println("SwitchOff");
-      }
-    }
-  }
-
-
-void setupOTA(){
-  ArduinoOTA.onStart([]() {
-    Serial.println("Start");
-    digitalWrite(Red_Led, HIGH);
-    digitalWrite(Blue_Led, LOW);
-    delay(1000);
-    digitalWrite(Blue_Led, HIGH);
-    delay(500);
-  });
-  ArduinoOTA.onEnd([]() {
-    Serial.println("End");
-    digitalWrite(Blue_Led, HIGH);
-    delay(1000);
-    ledBlink(Blue_Led, 3, 100);
-
-  });
-  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
-    Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
-    digitalWrite(Blue_Led, Blue_Led_State);
-    Blue_Led_State = !Blue_Led_State;
-
-  });
-  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");
-    digitalWrite(Blue_Led, HIGH);
-  });
-  ArduinoOTA.begin();
-}
-
-void loopHandles(){
-  if (loop_counter == 10)  {
-    loop_counter = 0;
-    server.handleClient();
-    Red_Led_State = !Red_Led_State;
-    digitalWrite(Red_Led, Red_Led_State);
-    delay(10);
-    }
-  if (loop_counter == 5)  {
-    ArduinoOTA.handle();
-    delay(10);
-    }
-  loop_counter += 1;
-  if (Refresh)  {
-    Refresh = false;
-    ///Serial.println("Refreshing...");
-    //Serial.printf("FreeMem:%d %d:%d:%d %d.%d.%d \n",ESP.getFreeHeap() , DateTime.hour,DateTime.minute, DateTime.second, DateTime.year, DateTime.month, DateTime.day);
-  }
-
-}
-
-
-

+ 68 - 221
global.h

@@ -1,246 +1,93 @@
 #ifndef GLOBAL_H
 #define GLOBAL_H
 
-ESP8266WebServer server(80);									// The Webserver
-boolean firstStart = true;										// On firststart = true, NTP will try to get a valid time
-int AdminTimeOutCounter = 0;									// Counter for Disabling the AdminMode
-strDateTime DateTime;											// Global DateTime structure, will be refreshed every Second
-WiFiUDP UDPNTPClient;											// NTP Client
-unsigned long UnixTimestamp = 0;								// GLOBALTIME  ( Will be set by NTP)
-boolean Refresh = false; // For Main Loop, to refresh things like GPIO / WS2812
-int cNTP_Update = 0;											// Counter for Updating the time via NTP
-Ticker tkSecond;												// Second - Timer for Updating Datetime Structure
-boolean AdminEnabled = false;		// Enable Admin Mode for a given Time
-byte Minute_Old = 100;				// Helpvariable for checking, when a new Minute comes up (for Auto Turn On / Off)
 
 
+/* ACCESS POINT CONFIGURATION */
 #define ACCESS_POINT_NAME  "ESP"
 #define ACCESS_POINT_PASSWORD  "12345678"
-#define AdminTimeOut 60  // Defines the Time in Seconds, when the Admin-Mode will be diabled
-
-struct strConfig {
-	String ssid;
-	String password;
-	byte  IP[4];
-	byte  Netmask[4];
-	byte  Gateway[4];
-	boolean dhcp;
-	String ntpServerName;
-	long Update_Time_Via_NTP_Every;
-	long timezone;
-	boolean daylight;
-	String DeviceName;
-	boolean AutoTurnOff;
-	boolean AutoTurnOn;
-	byte TurnOffHour;
-	byte TurnOffMinute;
-	byte TurnOnHour;
-	byte TurnOnMinute;
-	byte LED_R;
-	byte LED_G;
-	byte LED_B;
-}   config;
-
-
-/*
-**
-** CONFIGURATION HANDLING
-**
-*/
-void ConfigureWifi()
-{
-	Serial.println("Configuring Wifi");
-	WiFi.begin (config.ssid.c_str(), config.password.c_str());
-	if (!config.dhcp)
-	{
-		WiFi.config(IPAddress(config.IP[0],config.IP[1],config.IP[2],config.IP[3] ),  IPAddress(config.Gateway[0],config.Gateway[1],config.Gateway[2],config.Gateway[3] ) , IPAddress(config.Netmask[0],config.Netmask[1],config.Netmask[2],config.Netmask[3] ));
-	}
-}
-
-void WriteConfig()
-{
+boolean AdminEnabled = false; // Enable Admin Mode for a given Time
+int AdminTimeOutCounter = 0;  // Counter for Disabling the AdminMode
+#define AdminTimeOut 60  // Defines the Time in Seconds, when the Admin-Mode will be disabled
 
-	Serial.println("Writing Config");
-	EEPROM.write(0,'C');
-	EEPROM.write(1,'F');
-	EEPROM.write(2,'G');
 
-	EEPROM.write(16,config.dhcp);
-	EEPROM.write(17,config.daylight);
-	
-	EEPROMWritelong(18,config.Update_Time_Via_NTP_Every); // 4 Byte
-
-	EEPROMWritelong(22,config.timezone);  // 4 Byte
 
+/* UDP CONFIGURATION */
+int UKI_UDP_In_Port = 9000;  //udp port input for ESP
+IPAddress UKI_UDP_Master_IP(192, 168, 0, 41);  //default udp address to send to. Will automatically change to the ip sending something to udp in
+ 
 
-	EEPROM.write(26,config.LED_R);
-	EEPROM.write(27,config.LED_G);
-	EEPROM.write(28,config.LED_B);
 
-	EEPROM.write(32,config.IP[0]);
-	EEPROM.write(33,config.IP[1]);
-	EEPROM.write(34,config.IP[2]);
-	EEPROM.write(35,config.IP[3]);
+int GSR_sensor;
+int loop_counter;
 
-	EEPROM.write(36,config.Netmask[0]);
-	EEPROM.write(37,config.Netmask[1]);
-	EEPROM.write(38,config.Netmask[2]);
-	EEPROM.write(39,config.Netmask[3]);
 
-	EEPROM.write(40,config.Gateway[0]);
-	EEPROM.write(41,config.Gateway[1]);
-	EEPROM.write(42,config.Gateway[2]);
-	EEPROM.write(43,config.Gateway[3]);
 
 
-	WriteStringToEEPROM(64,config.ssid);
-	WriteStringToEEPROM(96,config.password);
-	WriteStringToEEPROM(128,config.ntpServerName);
+void ConfigureWifi()
+{
+  Serial.println("Configuring Wifi");
+  WiFi.begin (config.ssid.c_str(), config.password.c_str());
+  if (!config.dhcp)
+  {
+    WiFi.config(IPAddress(config.IP[0],config.IP[1],config.IP[2],config.IP[3] ),  IPAddress(config.Gateway[0],config.Gateway[1],config.Gateway[2],config.Gateway[3] ) , IPAddress(config.Netmask[0],config.Netmask[1],config.Netmask[2],config.Netmask[3] ));
+  }
+}
 
-	EEPROM.write(300,config.AutoTurnOn);
-	EEPROM.write(301,config.AutoTurnOff);
-	EEPROM.write(302,config.TurnOnHour);
-	EEPROM.write(303,config.TurnOnMinute);
-	EEPROM.write(304,config.TurnOffHour);
-	EEPROM.write(305,config.TurnOffMinute);
-	WriteStringToEEPROM(306,config.DeviceName);
-	
 
 
-	EEPROM.commit();
-}
-boolean ReadConfig()
-{
+void setupWifi(){
+  if (!ReadConfig())  {
+    // DEFAULT CONFIG
+    config.ssid = "Freebox-6F7B3C";
+    config.password = "accessorem6-gignendi7-insultare!";
+    config.dhcp = true;
+    config.IP[0] = 192; config.IP[1] = 168; config.IP[2] = 0; config.IP[3] = 100;
+    config.Netmask[0] = 255; config.Netmask[1] = 255; config.Netmask[2] = 255; config.Netmask[3] = 0;
+    config.Gateway[0] = 192; config.Gateway[1] = 168; config.Gateway[2] = 0; config.Gateway[3] = 254;
+    config.ntpServerName = "0.de.pool.ntp.org";
+    config.Update_Time_Via_NTP_Every =  0;
+    config.timezone = -10;
+    config.daylight = true;
+    config.DeviceName = "UKI_ESP_default";
+    config.AutoTurnOff = false;
+    config.AutoTurnOn = false;
+    config.TurnOffHour = 0;
+    config.TurnOffMinute = 0;
+    config.TurnOnHour = 0;
+    config.TurnOnMinute = 0;
+    WriteConfig();
+    Serial.println("General config applied");
+  }
+
+
+  if (AdminEnabled)  {
+    WiFi.mode(WIFI_AP_STA);
+    //WiFi.softAP( ACCESS_POINT_NAME , ACCESS_POINT_PASSWORD);
+    WiFi.softAP( config.DeviceName.c_str() , ACCESS_POINT_PASSWORD);
+  }
+  else  { WiFi.mode(WIFI_STA); }
+  ConfigureWifi();
+  while (WiFi.status() != 3) {
+    Serial.println(WiFi.status());
+  }
+//    Serial.print(".");
+//    digitalWrite(Blue_Led, Blue_Led_State);
+//    Blue_Led_State = !Blue_Led_State;
+//    yield();
+//  }
+  Serial.println(WiFi.status());
+
+  /* A MODIFIER : pour le moment si AdminEnabled, lance pendant AdminTimeOut un AP en parallèle de la connection configurée
+   *  devrait être remplacé par  - tente de se connecter au wifi configuré, si ok on continue normalement
+   *                             - si n'arrive pas à se connecter au wifi configuré, passe en mode AP avec webserver pour reconfigurer le wifi / ou demarrer en mode AP si bouton utilisateur appuyé pendant démarragae
+   *                             - normalement redémarre après reconfiguration, donc ok
+   *                             - eventuellement ajouter dans loop un redemarrage en cas de perte de connection
+   */
 
-	Serial.println("Reading Configuration");
-	if (EEPROM.read(0) == 'C' && EEPROM.read(1) == 'F'  && EEPROM.read(2) == 'G' )
-	{
-		Serial.println("Configurarion Found!");
-		config.dhcp = 	EEPROM.read(16);
-
-		config.daylight = EEPROM.read(17);
-
-		config.Update_Time_Via_NTP_Every = EEPROMReadlong(18); // 4 Byte
-
-		config.timezone = EEPROMReadlong(22); // 4 Byte
-
-		config.LED_R = EEPROM.read(26);
-		config.LED_G = EEPROM.read(27);
-		config.LED_B = EEPROM.read(28);
-
-		config.IP[0] = EEPROM.read(32);
-		config.IP[1] = EEPROM.read(33);
-		config.IP[2] = EEPROM.read(34);
-		config.IP[3] = EEPROM.read(35);
-		config.Netmask[0] = EEPROM.read(36);
-		config.Netmask[1] = EEPROM.read(37);
-		config.Netmask[2] = EEPROM.read(38);
-		config.Netmask[3] = EEPROM.read(39);
-		config.Gateway[0] = EEPROM.read(40);
-		config.Gateway[1] = EEPROM.read(41);
-		config.Gateway[2] = EEPROM.read(42);
-		config.Gateway[3] = EEPROM.read(43);
-		config.ssid = ReadStringFromEEPROM(64);
-		config.password = ReadStringFromEEPROM(96);
-		config.ntpServerName = ReadStringFromEEPROM(128);
-		
-		
-		config.AutoTurnOn = EEPROM.read(300);
-		config.AutoTurnOff = EEPROM.read(301);
-		config.TurnOnHour = EEPROM.read(302);
-		config.TurnOnMinute = EEPROM.read(303);
-		config.TurnOffHour = EEPROM.read(304);
-		config.TurnOffMinute = EEPROM.read(305);
-		config.DeviceName= ReadStringFromEEPROM(306);
-		return true;
-		
-	}
-	else
-	{
-		Serial.println("Configurarion NOT FOUND!!!!");
-		return false;
-	}
 }
 
-/*
-**
-**  NTP 
-**
-*/
 
-const int NTP_PACKET_SIZE = 48; 
-byte packetBuffer[ NTP_PACKET_SIZE]; 
-void NTPRefresh()
-{
 
-	
-
-
-	if (WiFi.status() == WL_CONNECTED)
-	{
-		IPAddress timeServerIP; 
-		WiFi.hostByName(config.ntpServerName.c_str(), timeServerIP); 
-		//sendNTPpacket(timeServerIP); // send an NTP packet to a time server
-
-
-		Serial.println("sending NTP packet...");
-		memset(packetBuffer, 0, NTP_PACKET_SIZE);
-		packetBuffer[0] = 0b11100011;   // LI, Version, Mode
-		packetBuffer[1] = 0;     // Stratum, or type of clock
-		packetBuffer[2] = 6;     // Polling Interval
-		packetBuffer[3] = 0xEC;  // Peer Clock Precision
-		packetBuffer[12]  = 49;
-		packetBuffer[13]  = 0x4E;
-		packetBuffer[14]  = 49;
-		packetBuffer[15]  = 52;
-		UDPNTPClient.beginPacket(timeServerIP, 123); 
-		UDPNTPClient.write(packetBuffer, NTP_PACKET_SIZE);
-		UDPNTPClient.endPacket();
-
-
-		delay(1000);
-  
-		int cb = UDPNTPClient.parsePacket();
-		if (!cb) {
-			Serial.println("NTP no packet yet");
-		}
-		else 
-		{
-			Serial.print("NTP packet received, length=");
-			Serial.println(cb);
-			UDPNTPClient.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
-			unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
-			unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
-			unsigned long secsSince1900 = highWord << 16 | lowWord;
-			const unsigned long seventyYears = 2208988800UL;
-			unsigned long epoch = secsSince1900 - seventyYears;
-			UnixTimestamp = epoch;
-		}
-	}
-}
-
-void Second_Tick()
-{
-	strDateTime tempDateTime;
-	AdminTimeOutCounter++;
-	cNTP_Update++;
-	UnixTimestamp++;
-	ConvertUnixTimeStamp(UnixTimestamp +  (config.timezone *  360) , &tempDateTime);
-	if (config.daylight) // Sommerzeit beachten
-		if (summertime(tempDateTime.year,tempDateTime.month,tempDateTime.day,tempDateTime.hour,0))
-		{
-			ConvertUnixTimeStamp(UnixTimestamp +  (config.timezone *  360) + 3600, &DateTime);
-		}
-		else
-		{
-			DateTime = tempDateTime;
-		}
-	else
-	{
-			DateTime = tempDateTime;
-	}
-	Refresh = true;
-}
- 
 
-#endif
+#endif

+ 23 - 25
includes.h

@@ -1,43 +1,41 @@
 
-int GSR_sensor;
 
 
-const int Blue_Led = 2;
-bool Blue_Led_State ;
-
-const int Red_Led = 0;
-bool Red_Led_State ;
-
-int loop_counter;
-
 #include <ESP8266WiFi.h>
 #include <WiFiClient.h>
 #include <ESP8266WebServer.h>
 #include <Ticker.h>
 #include <EEPROM.h>
 #include <WiFiUdp.h>
-#include "helpers.h"
-#include "global.h"
-
 //OTA includes
 #include <ESP8266mDNS.h>
 #include <WiFiUdp.h>
 #include <ArduinoOTA.h>
 
+
+ESP8266WebServer server(80); 
+
 WiFiUDP UKI_UDP;
-/*
-  Include the HTML, STYLE and Script "Pages"
-*/
-#include "web_Root.h"
-#include "web_Admin.h"
-#include "web_Script.js.h"
-#include "web_Style.css.h"
-#include "web_NTPSettings.h"
-#include "web_Information.h"
-#include "web_General.h"
-#include "web_NetworkConfiguration.h"
-
-#include "functions.h"
+
+
+
+#include "helpers.h"
+
+#include "leds.h"
+#include "eeprom.h"
+#include "global.h"
+#include "web.h"
+
+
+
+
+#include "ota.h"
+
+
+
+
+
+
 
 
 

+ 29 - 0
leds.h

@@ -0,0 +1,29 @@
+/* LEDS  */
+const int Blue_Led = 2;
+bool Blue_Led_State ;
+Ticker tkBlue_Led ;
+int tkBlue_Led_Counter;
+
+const int Red_Led = 0;
+bool Red_Led_State ;
+Ticker tkRed_Led ;
+int tkRed_Led_Counter;
+
+
+void setupLeds() {
+  pinMode(Blue_Led, OUTPUT);
+  digitalWrite(Blue_Led, HIGH);
+  pinMode(Red_Led, OUTPUT);
+  digitalWrite(Red_Led, LOW);//red led on
+  
+}
+
+
+void ledBlink (int Led, int blink_qty, int blink_time) {
+  for (int i = 0 ; i < blink_qty ; i++) {
+    digitalWrite(Led, LOW) ;
+    delay(blink_time);
+    digitalWrite(Led, HIGH);
+    delay(blink_time);
+  }
+}

+ 55 - 0
ota.h

@@ -0,0 +1,55 @@
+void setupOTA(){
+  ArduinoOTA.onStart([]() {
+    Serial.println("Start");
+    digitalWrite(Red_Led, HIGH);
+    digitalWrite(Blue_Led, LOW);
+    delay(1000);
+    digitalWrite(Blue_Led, HIGH);
+    delay(500);
+  });
+  ArduinoOTA.onEnd([]() {
+    Serial.println("End");
+    digitalWrite(Blue_Led, HIGH);
+    delay(1000);
+    ledBlink(Blue_Led, 3, 100);
+
+  });
+  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
+    Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
+    digitalWrite(Blue_Led, Blue_Led_State);
+    Blue_Led_State = !Blue_Led_State;
+
+  });
+  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");
+    digitalWrite(Blue_Led, HIGH);
+  });
+  ArduinoOTA.begin();
+}
+
+void loopOTA(){
+  if (loop_counter == 10)  {
+    loop_counter = 0;
+    server.handleClient();
+    Red_Led_State = !Red_Led_State;
+    digitalWrite(Red_Led, Red_Led_State);
+    delay(10);
+    }
+  if (loop_counter == 5)  {
+    ArduinoOTA.handle();
+    delay(10);
+    }
+  loop_counter += 1;
+  if (Refresh)  {
+    Refresh = false;
+    ///Serial.println("Refreshing...");
+    //Serial.printf("FreeMem:%d %d:%d:%d %d.%d.%d \n",ESP.getFreeHeap() , DateTime.hour,DateTime.minute, DateTime.second, DateTime.year, DateTime.month, DateTime.day);
+  }
+
+}
+

+ 197 - 0
web.h

@@ -0,0 +1,197 @@
+               // The Webserver
+
+/* NTP things */
+boolean firstStart = true;                    // On firststart = true, NTP will try to get a valid time   
+strDateTime DateTime;                     // Global DateTime structure, will be refreshed every Second
+WiFiUDP UDPNTPClient;                     // NTP Client
+unsigned long UnixTimestamp = 0;                // GLOBALTIME  ( Will be set by NTP)
+boolean Refresh = false; // For Main Loop, to refresh things like GPIO / WS2812
+int cNTP_Update = 0;                      // Counter for Updating the time via NTP
+Ticker tkSecond;                        // Second - Timer for Updating Datetime Structure
+byte Minute_Old = 100;        // Helpvariable for checking, when a new Minute comes up (for Auto Turn On / Off)
+
+
+
+/*
+  Include the HTML, STYLE and Script "Pages"
+*/
+
+#include "web_Root.h"
+#include "web_Admin.h"
+#include "web_Script.js.h"
+#include "web_Style.css.h"
+#include "web_NTPSettings.h"
+#include "web_Information.h"
+#include "web_General.h"
+#include "web_NetworkConfiguration.h"
+
+
+
+/*
+**
+**  NTP 
+**
+*/
+
+const int NTP_PACKET_SIZE = 48; 
+byte packetBuffer[ NTP_PACKET_SIZE]; 
+void NTPRefresh()
+{
+
+  
+
+
+  if (WiFi.status() == WL_CONNECTED)
+  {
+    IPAddress timeServerIP; 
+    WiFi.hostByName(config.ntpServerName.c_str(), timeServerIP); 
+    //sendNTPpacket(timeServerIP); // send an NTP packet to a time server
+
+
+    Serial.println("sending NTP packet...");
+    memset(packetBuffer, 0, NTP_PACKET_SIZE);
+    packetBuffer[0] = 0b11100011;   // LI, Version, Mode
+    packetBuffer[1] = 0;     // Stratum, or type of clock
+    packetBuffer[2] = 6;     // Polling Interval
+    packetBuffer[3] = 0xEC;  // Peer Clock Precision
+    packetBuffer[12]  = 49;
+    packetBuffer[13]  = 0x4E;
+    packetBuffer[14]  = 49;
+    packetBuffer[15]  = 52;
+    UDPNTPClient.beginPacket(timeServerIP, 123); 
+    UDPNTPClient.write(packetBuffer, NTP_PACKET_SIZE);
+    UDPNTPClient.endPacket();
+
+
+    delay(1000);
+  
+    int cb = UDPNTPClient.parsePacket();
+    if (!cb) {
+      Serial.println("NTP no packet yet");
+    }
+    else 
+    {
+      Serial.print("NTP packet received, length=");
+      Serial.println(cb);
+      UDPNTPClient.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
+      unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
+      unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
+      unsigned long secsSince1900 = highWord << 16 | lowWord;
+      const unsigned long seventyYears = 2208988800UL;
+      unsigned long epoch = secsSince1900 - seventyYears;
+      UnixTimestamp = epoch;
+    }
+  }
+}
+
+void Second_Tick()
+{
+  strDateTime tempDateTime;
+  AdminTimeOutCounter++;
+  cNTP_Update++;
+  UnixTimestamp++;
+  ConvertUnixTimeStamp(UnixTimestamp +  (config.timezone *  360) , &tempDateTime);
+  if (config.daylight) // Sommerzeit beachten
+    if (summertime(tempDateTime.year,tempDateTime.month,tempDateTime.day,tempDateTime.hour,0))
+    {
+      ConvertUnixTimeStamp(UnixTimestamp +  (config.timezone *  360) + 3600, &DateTime);
+    }
+    else
+    {
+      DateTime = tempDateTime;
+    }
+  else
+  {
+      DateTime = tempDateTime;
+  }
+  Refresh = true;
+}
+
+
+
+
+
+void setupWebserver(){
+  server.on ( "/", []() {Serial.println("admin.html"); server.send ( 200, "text/html", web_AdminMainPage ); }  );
+  server.on ( "/admin.html", []() {Serial.println("admin.html"); server.send ( 200, "text/html", web_AdminMainPage ); }  );
+  server.on ( "/config.html", send_network_configuration_html );
+  server.on ( "/info.html", []() { Serial.println("info.html"); server.send ( 200, "text/html", web_Information ); }  );
+  server.on ( "/ntp.html", send_NTP_configuration_html  );
+  server.on ( "/general.html",  send_general_html );
+  server.on ( "/style.css", []() { Serial.println("style.css"); server.send ( 200, "text/plain", web_Style_css );} );
+  server.on ( "/microajax.js", []() { Serial.println("microajax.js"); server.send ( 200, "text/plain", web_microajax_js ); } );
+  server.on ( "/admin/values", send_network_configuration_values_html );
+  server.on ( "/admin/connectionstate", send_connection_state_values_html );
+  server.on ( "/admin/infovalues", send_information_values_html );
+  server.on ( "/admin/ntpvalues", send_NTP_configuration_values_html );
+  server.on ( "/admin/generalvalues", send_general_configuration_values_html);
+  server.on ( "/admin/devicename",     send_devicename_value_html);
+  /* old led management
+  server.on ( "/", []() {digitalWrite(Blue_Led, LOW); Serial.println("admin.html"); server.send ( 200, "text/html", web_AdminMainPage ); digitalWrite(Blue_Led, HIGH);  }  );
+  server.on ( "/admin.html", []() {digitalWrite(Blue_Led, LOW); Serial.println("admin.html"); server.send ( 200, "text/html", web_AdminMainPage ); digitalWrite(Blue_Led, HIGH);  }  );
+  server.on ( "/config.html", send_network_configuration_html );
+  server.on ( "/info.html", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("info.html"); server.send ( 200, "text/html", web_Information ); digitalWrite(Blue_Led, HIGH) ; }  );
+  server.on ( "/ntp.html", send_NTP_configuration_html  );
+  server.on ( "/general.html",  send_general_html );
+  server.on ( "/style.css", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("style.css"); server.send ( 200, "text/plain", web_Style_css ); digitalWrite(Blue_Led, HIGH) ; } );
+  server.on ( "/microajax.js", []() { digitalWrite(Blue_Led, LOW) ; Serial.println("microajax.js"); server.send ( 200, "text/plain", web_microajax_js ); digitalWrite(Blue_Led, HIGH); } );
+  server.on ( "/admin/values", send_network_configuration_values_html );
+  server.on ( "/admin/connectionstate", send_connection_state_values_html );
+  server.on ( "/admin/infovalues", send_information_values_html );
+  server.on ( "/admin/ntpvalues", send_NTP_configuration_values_html );
+  server.on ( "/admin/generalvalues", send_general_configuration_values_html);
+  server.on ( "/admin/devicename",     send_devicename_value_html);
+  */
+
+  server.onNotFound ( []() {
+    Serial.println("Page Not Found");
+    server.send ( 400, "text/html", "Page not Found" );
+//    ledBlink(Blue_Led, 10, 100);
+  }  );
+  server.begin();
+  Serial.println( "HTTP server started" );
+  tkSecond.attach(1, Second_Tick);
+  UDPNTPClient.begin(2390);  // Port for NTP receive
+}
+
+
+
+void loopWebserver(){
+  if (AdminEnabled)  {
+    if (AdminTimeOutCounter > AdminTimeOut)   {
+      AdminEnabled = false;
+      Serial.println("Admin Mode disabled!");
+      WiFi.mode(WIFI_STA);
+    }
+  }
+  if (config.Update_Time_Via_NTP_Every  > 0 )  {
+    if (cNTP_Update > 5 && firstStart)    {
+      NTPRefresh();
+      cNTP_Update = 0;
+      firstStart = false;
+    }
+    else if ( cNTP_Update > (config.Update_Time_Via_NTP_Every * 60) )    {
+      NTPRefresh();
+      cNTP_Update = 0;
+    }
+  }
+
+  if (DateTime.minute != Minute_Old)  {
+    Minute_Old = DateTime.minute;
+    if (config.AutoTurnOn)
+      if (DateTime.hour == config.TurnOnHour && DateTime.minute == config.TurnOnMinute)      {
+        Serial.println("SwitchON");
+      }
+    }
+
+    Minute_Old = DateTime.minute;
+    if (config.AutoTurnOff)    {
+      if (DateTime.hour == config.TurnOffHour && DateTime.minute == config.TurnOffMinute)      {
+        Serial.println("SwitchOff");
+      }
+    }
+  }
+
+
+ 
+

+ 6 - 6
web_General.h

@@ -69,17 +69,17 @@ function load(e,t,n){if("js"==t){var a=document.createElement("script");a.src=e,
 // Functions for this Page
 void send_devicename_value_html()
 {
-	digitalWrite(Blue_Led, LOW);
+//	digitalWrite(Blue_Led, LOW);
 	String values ="";
 	values += "devicename|" + (String) config.DeviceName + "|div\n";
 	server.send ( 200, "text/plain", values);
 	Serial.println(__FUNCTION__); 
-	digitalWrite(Blue_Led, HIGH);
+//	digitalWrite(Blue_Led, HIGH);
 }
 
 void send_general_html()
 {
-	digitalWrite(Blue_Led, LOW);
+//	digitalWrite(Blue_Led, LOW);
 	if (server.args() > 0 )  // Save Settings
 	{
 		config.AutoTurnOn = false;
@@ -100,12 +100,12 @@ void send_general_html()
 	server.send ( 200, "text/html", web_AdminGeneralSettings ); 
 	Serial.println(__FUNCTION__); 
 	
-	digitalWrite(Blue_Led, HIGH);
+//	digitalWrite(Blue_Led, HIGH);
 }
 
 void send_general_configuration_values_html()
 {
-  digitalWrite(Blue_Led, LOW);
+//  digitalWrite(Blue_Led, LOW);
 	String values ="";
 	values += "devicename|" +  (String)  config.DeviceName +  "|input\n";
 	values += "tonhour|" +  (String)  config.TurnOnHour +  "|input\n";
@@ -116,5 +116,5 @@ void send_general_configuration_values_html()
 	values += "tonenabled|" +  (String) (config.AutoTurnOn ? "checked" : "") + "|chk\n";
 	server.send ( 200, "text/plain", values);
 	Serial.println(__FUNCTION__); 
-  digitalWrite(Blue_Led, HIGH);
+//  digitalWrite(Blue_Led, HIGH);
 }

+ 2 - 2
web_Information.h

@@ -56,7 +56,7 @@ function load(e,t,n){if("js"==t){var a=document.createElement("script");a.src=e,
 
 void send_information_values_html ()
 {
-  digitalWrite(Blue_Led, LOW);
+//  digitalWrite(Blue_Led, LOW);
 	String values ="";
 
 	values += "x_ssid|" + (String)WiFi.SSID() +  "|div\n";
@@ -67,7 +67,7 @@ void send_information_values_html ()
 	values += "x_ntp|" +  (String) DateTime.hour + ":" + (String) + DateTime.minute +  ":"  + (String)  DateTime.second + " " + (String)   DateTime.year + "-" + (String)  DateTime.month + "-" + (String)  DateTime.day +  "|div\n";
 	server.send ( 200, "text/plain", values);
 	Serial.println(__FUNCTION__); 
-  digitalWrite(Blue_Led, HIGH);
+//  digitalWrite(Blue_Led, HIGH);
 }
 
 

+ 2 - 2
web_NTPSettings.h

@@ -74,7 +74,7 @@ function load(e,t,n){if("js"==t){var a=document.createElement("script");a.src=e,
 void send_NTP_configuration_html()
 {
 	
-	digitalWrite(Blue_Led, LOW);
+//	digitalWrite(Blue_Led, LOW);
 	if (server.args() > 0 )  // Save Settings
 	{
 		config.daylight = false;
@@ -90,7 +90,7 @@ void send_NTP_configuration_html()
 	}
 	server.send ( 200, "text/html", web_NTPConfiguration ); 
 	Serial.println(__FUNCTION__); 
-	digitalWrite(Blue_Led, HIGH);
+//	digitalWrite(Blue_Led, HIGH);
 }
 
 

+ 6 - 6
web_NetworkConfiguration.h

@@ -75,7 +75,7 @@ Please Wait....Configuring and Restarting.
 
 void send_network_configuration_html()
 {
-	digitalWrite(Blue_Led, LOW);
+//	digitalWrite(Blue_Led, LOW);
 	if (server.args() > 0 )  // Save Settings
 	{
 		String temp = "";
@@ -108,7 +108,7 @@ void send_network_configuration_html()
 		server.send ( 200, "text/html", web_NetworkConfiguration ); 
 	}
 	Serial.println(__FUNCTION__); 
-  digitalWrite(Blue_Led, HIGH);
+//  digitalWrite(Blue_Led, HIGH);
 }
 
 
@@ -119,7 +119,7 @@ void send_network_configuration_html()
 
 void send_network_configuration_values_html()
 {
-  digitalWrite(Blue_Led, LOW);
+  //digitalWrite(Blue_Led, LOW);
 	String values ="";
 
 	values += "ssid|" + (String) config.ssid + "|input\n";
@@ -139,7 +139,7 @@ void send_network_configuration_values_html()
 	values += "dhcp|" +  (String) (config.dhcp ? "checked" : "") + "|chk\n";
 	server.send ( 200, "text/plain", values);
 	Serial.println(__FUNCTION__); 
-	digitalWrite(Blue_Led, HIGH);
+	//digitalWrite(Blue_Led, HIGH);
 }
 
 
@@ -149,7 +149,7 @@ void send_network_configuration_values_html()
 
 void send_connection_state_values_html()
 {
-  digitalWrite(Blue_Led, LOW);
+  //digitalWrite(Blue_Led, LOW);
 	String state = "N/A";
 	String Networks = "";
 	if (WiFi.status() == 0) state = "Idle";
@@ -202,5 +202,5 @@ void send_connection_state_values_html()
 	values += "networks|" +  Networks + "|div\n";
 	server.send ( 200, "text/plain", values);
 	Serial.println(__FUNCTION__); 
-	digitalWrite(Blue_Led, HIGH);
+	//digitalWrite(Blue_Led, HIGH);
 }

+ 1 - 1
web_Root.h

@@ -19,7 +19,7 @@ const char web_Root[] PROGMEM = R"=====(
 
 void sendRootPage()
 {        
-    digitalWrite(Blue_Led, LOW);
+//    digitalWrite(Blue_Led, LOW);
     if (server.args() > 0 )  // Are there any POST/GET Fields ? 
     {
        for ( uint8_t i = 0; i < server.args(); i++ ) {  // Iterate through the fields