Browse Source

basic led tickers working

needs cleaning
Etienne Landon 9 years ago
parent
commit
15f60574b3
5 changed files with 109 additions and 103 deletions
  1. 28 15
      ESP_UKI.ino
  2. 1 65
      global.h
  3. 9 3
      includes.h
  4. 69 13
      leds.h
  5. 2 7
      ota.h

+ 28 - 15
ESP_UKI.ino

@@ -3,27 +3,37 @@
 
     largely base on http://www.john-lassen.de/index.php/projects/esp-8266-arduino-ide-webconfig
 
-  TODO :  clean webserver
-          better function organizing
+  TODO :  better function organizing
           add firmware number in webserver
           rework led system with tickers
           send ADC to default IP via udp, allow configuration
-         
+
+         build onDemand config mode
+         load/save parameters (fixed ip, uki_name, udp_port udp_ip
 */
-	
+
+
+  /* LEDS  */
+
+
 
 #include "includes.h"  //headers and variables declaration
 
 
+
+Ticker tkOTA;  // periodic check if OTA available
+Ticker tkUKI;  // periodic send ADC to UDP
+
+
+
 void setup ( void ) {
   
   EEPROM.begin(512);
   Serial.begin(115200);
-  Serial.println("Starting ES8266");
+  Serial.println("Starting ESP8266");
   setupLeds();
-  setupWifi();
-  //setupWebserver();
-  setupOTA();
+  //setupWifi();
+  //setupOTA();
   
   delay(200);
   Serial.println("Ready");
@@ -32,12 +42,15 @@ void setup ( void ) {
 
   //UKI sensor setup
   UKI_UDP.begin(UKI_UDP_In_Port); 
-  delay(1000);
-  digitalWrite(Red_Led, HIGH); //red led off
-  digitalWrite(Blue_Led, HIGH);
-  delay(1000);
-  ledBlink(Red_Led, 3, 100); //3 quick blink on red led as we start 
-  delay (1000);
+//  delay(1000);
+//  digitalWrite(Red_Led, HIGH); //red led off
+//  digitalWrite(Blue_Led, HIGH);
+//  delay(1000);
+//  ledBlink(Red_Led, 3, 100); //3 quick blink on red led as we start 
+//  delay (1000);
+  blueLedState(-1, 500);
+  
+  
 }
 
 
@@ -45,7 +58,7 @@ void loop ( void ) {
   loop_counter += 1;
   //loopWebserver();
   
-  loopOTA();
+  //loopOTA();
 
   /*  UKI part	*/
 //  GSR_sensor = analogRead(A0);

+ 1 - 65
global.h

@@ -23,17 +23,6 @@ int loop_counter;
 int counter;
 
 
-
-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 setupWifi() {
   
   //if no config in eeprom, start as default access point
@@ -46,7 +35,7 @@ void setupWifi() {
   //if config in eeprom, try and connect to wifi, if fail after timeout start access point with config.name SSID
   else {  
     WiFi.mode(WIFI_STA);
-    ConfigureWifi();
+   // ConfigureWifi();
     Serial.println("Connecting to configured wifi network");
     while (WiFi.status() != 3) {
       counter++;
@@ -68,58 +57,5 @@ void setupWifi() {
 }
 
 
-void setupWifi_old(){
-  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
-   */
-
-}
-
-
-
 
 #endif

+ 9 - 3
includes.h

@@ -2,8 +2,12 @@
 
 
 #include <ESP8266WiFi.h>
-#include <WiFiClient.h>
+//#include <WiFiClient.h>
+//#include <ESP8266WebServer.h>
+//web config portal
 #include <ESP8266WebServer.h>
+#include <DNSServer.h>
+#include <WiFiManager.h>
 
 #include <Ticker.h>
 #include <EEPROM.h>
@@ -21,12 +25,14 @@ WiFiUDP UKI_UDP;
 
 
 
-#include "helpers.h"  //some helpers functions
 #include "leds.h"     //config and functions relative to leds
+
+#include "helpers.h"  //some helpers functions
+
 #include "eeprom.h"   //config and functions relative to config permanent storage
 #include "global.h"   //config and functions relative to wifi and access point configuration
 //#include "web.h"      //config and functions relative to configuration web server
-#include "ota.h"      //config and functions relative to ota firmware updates
+//#include "ota.h"      //config and functions relative to ota firmware updates
 
 
 

+ 69 - 13
leds.h

@@ -1,29 +1,85 @@
-/* LEDS  */
 const int Blue_Led = 2;
-bool Blue_Led_State ;
+bool Blue_Led_State = 1 ;
 Ticker tkBlue_Led ;
 int tkBlue_Led_Counter;
 
 const int Red_Led = 0;
-bool Red_Led_State ;
+bool Red_Led_State = 0;
 Ticker tkRed_Led ;
 int tkRed_Led_Counter;
 
+/*Faire des fonctions simples à appeler depuis autres functions callbacks, les fonctions locales attachent et detachent les tkLeds
+
+fonctions extérieures appellent blueLedState (0 off, 1 on, -1 blink; time_ms)
+blueStateLed attache/détache les tickers sur fonction invert
+*/
+
+//void LedState ( int Led, int state) { // -1 inverts, 0 off, 1 on
+//  if (Led==Red_Led) {
+//    if (state == -1) { Red_Led_State = !Red_Led_State;}
+//    else {Red_Led_State = state ;}
+//  }
+//  if (Blue_Led)
+//  digitalWrite(Led, !Red_Led_State) ; // inverted, 1 is off and 0 is on
+//  Serial.println("Red Led "+(String)Red_Led_State);
+//}
+
+void blinkLed(int Led) {
+  if (Led==0) {Red_Led_State = !Red_Led_State; digitalWrite(Red_Led, !Red_Led_State) ;}
+  if (Led==1) {Blue_Led_State = !Blue_Led_State; digitalWrite(Blue_Led, !Blue_Led_State);}
+}
+
+void redLedState ( int state, int time_ms) { // -1 inverts, 0 off, 1 on
+  if (state == -1) {
+    tkRed_Led.attach_ms( time_ms, blinkLed, 0);
+    }
+  else {
+    tkRed_Led.detach();
+    Red_Led_State = state ;
+    digitalWrite(Red_Led, !Red_Led_State) ; // inverted, 1 is off and 0 is on
+    }
+  
+  Serial.println("Red Led "+(String)Red_Led_State);
+}
+
+void blueLedState ( int state, int time_ms) { // -1 inverts, 0 off, 1 on
+  if (state == -1) {
+    tkBlue_Led.attach_ms( time_ms, blinkLed, 1);
+    }
+  else {
+    tkBlue_Led.detach();
+    Blue_Led_State = state ;
+    digitalWrite(Blue_Led, !Blue_Led_State) ;
+    }
+   // inverted, 1 is off and 0 is on
+  Serial.println("Blue Led "+(String)Blue_Led_State);
+}
+
 
 void setupLeds() {
   pinMode(Blue_Led, OUTPUT);
-  digitalWrite(Blue_Led, HIGH);
+  digitalWrite(Blue_Led, Blue_Led_State);
   pinMode(Red_Led, OUTPUT);
-  digitalWrite(Red_Led, LOW);//red led on
+  digitalWrite(Red_Led, Red_Led_State);//red led on
+  
+  delay(2000);
+  blueLedState(-1, 100);
+  redLedState(-1, 400);
+  delay (5000);
+  
+  redLedState(1, 100);
+  blueLedState(0, 100);
+  delay(1000);
   
 }
 
 
-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 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);
+//  }
+//}

+ 2 - 7
ota.h

@@ -11,7 +11,7 @@ void setupOTA(){
     Serial.println("End");
     digitalWrite(Blue_Led, HIGH);
     delay(1000);
-    ledBlink(Blue_Led, 3, 100);
+    //ledBlink(Blue_Led, 3, 100);
 
   });
   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
@@ -39,12 +39,7 @@ void loopOTA(){
     ArduinoOTA.handle();
     delay(10);
     }
-  
-  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);
-  }
+
 
 }