浏览代码

Rename custom variables

move FS write/read to wifimgr thumb
Etienne Landon 9 年之前
父节点
当前提交
19de0e35fd
共有 6 个文件被更改,包括 140 次插入286 次删除
  1. 10 85
      ESP_UKI.ino
  2. 0 141
      eeprom.h
  3. 3 11
      includes.h
  4. 6 7
      leds.h
  5. 3 1
      ota.h
  6. 118 41
      wifimgr.h

+ 10 - 85
ESP_UKI.ino

@@ -3,7 +3,6 @@
 
 
   TODO :  better function organizing
   TODO :  better function organizing
           add firmware number in webserver
           add firmware number in webserver
-          rework led system with tickers
           send ADC to default IP via udp, allow configuration
           send ADC to default IP via udp, allow configuration
 
 
          build onDemand config mode
          build onDemand config mode
@@ -11,88 +10,32 @@
 */
 */
 
 
 
 
-
-#include <FS.h>                   //this needs to be first, or it all crashes and burns...
-
-
-char mqtt_server[40];
-char mqtt_port[6] = "8080";
-char blynk_token[34] = "YOUR_BLYNK_TOKEN";
-
-bool shouldSaveConfig = false;
-
 #include "includes.h"  //headers and variables declaration
 #include "includes.h"  //headers and variables declaration
 
 
 
 
-
-
-
-
 /* UDP CONFIGURATION */
 /* UDP CONFIGURATION */
 int UKI_UDP_In_Port = 9000;  //udp port input for ESP
 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
+String UKI_UDP_IP_test = "192.168.1.1";
+IPAddress UKI_UDP_Master_IP(192, 168, 10, 100);  //default udp address to send to. Will automatically change to the ip sending something to udp in
 Ticker tkUKI;  // periodic send ADC to UDP
 Ticker tkUKI;  // periodic send ADC to UDP
 int GSR_sensor;
 int GSR_sensor;
 
 
 
 
 void setup ( void ) {
 void setup ( void ) {
   
   
-  //EEPROM.begin(512);
   Serial.begin(115200);
   Serial.begin(115200);
   Serial.println("Starting ESP8266");
   Serial.println("Starting ESP8266");
 
 
-  //clean FS, for testing
-  //SPIFFS.format();
-
-  //read configuration from FS json
-  Serial.println("mounting FS...");
-
-  if (SPIFFS.begin()) {
-    Serial.println("mounted file system");
-    if (SPIFFS.exists("/config.json")) {
-      //file exists, reading and loading
-      Serial.println("reading config file");
-      File configFile = SPIFFS.open("/config.json", "r");
-      if (configFile) {
-        Serial.println("opened config file");
-        size_t size = configFile.size();
-        // Allocate a buffer to store contents of the file.
-        std::unique_ptr<char[]> buf(new char[size]);
-
-        configFile.readBytes(buf.get(), size);
-        DynamicJsonBuffer jsonBuffer;
-        JsonObject& json = jsonBuffer.parseObject(buf.get());
-        json.printTo(Serial);
-        if (json.success()) {
-          Serial.println("\nparsed json");
-
-          strcpy(mqtt_server, json["mqtt_server"]);
-          strcpy(mqtt_port, json["mqtt_port"]);
-          strcpy(blynk_token, json["blynk_token"]);
-
-        } else {
-          Serial.println("failed to load json config");
-        }
-      }
-    }
-  } else {
-    Serial.println("failed to mount FS");
-  }
-  //end read
-  
-  setupWifi();
-
-  
-  
-  
   setupLeds();
   setupLeds();
+   
+  setupWifi();  
   
   
-  setupOTA();
+  //setupOTA();
   
   
   delay(200);
   delay(200);
   Serial.println("Ready");
   Serial.println("Ready");
   Serial.print("IP address: ");
   Serial.print("IP address: ");
-  //Serial.println(WiFi.localIP());
+  Serial.println(WiFi.localIP());
 
 
   //UKI sensor setup
   //UKI sensor setup
   UKI_UDP.begin(UKI_UDP_In_Port); 
   UKI_UDP.begin(UKI_UDP_In_Port); 
@@ -109,39 +52,21 @@ void setup ( void ) {
 
 
 
 
 void loop ( void ) {
 void loop ( void ) {
+  //Serial.println(WiFi.status());
   StartConfigAP();
   StartConfigAP();
-   //save the custom parameters to FS
-  if (shouldSaveConfig) {
-    Serial.println("saving config");
-    DynamicJsonBuffer jsonBuffer;
-    JsonObject& json = jsonBuffer.createObject();
-    json["mqtt_server"] = mqtt_server;
-    json["mqtt_port"] = mqtt_port;
-    json["blynk_token"] = blynk_token;
-
-    File configFile = SPIFFS.open("/config.json", "w");
-    if (!configFile) {
-      Serial.println("failed to open config file for writing");
-    }
-
-    json.printTo(Serial);
-    json.printTo(configFile);
-    configFile.close();
-    //end save
-  }
   
   
 
 
   /*  UKI part	*/
   /*  UKI part	*/
   GSR_sensor = analogRead(A0);
   GSR_sensor = analogRead(A0);
   //UKI_UDP.beginPacketMulticast((224, 1, 2, 3), 8000, WiFi.localIP());//
   //UKI_UDP.beginPacketMulticast((224, 1, 2, 3), 8000, WiFi.localIP());//
-  UKI_UDP.beginPacket(UKI_UDP_Master_IP, 8000);
-  UKI_UDP.print(config.DeviceName);
+  UKI_UDP.beginPacket(UKI_UDP_IP_test, 8000);
+  UKI_UDP.print("UKI");
   UKI_UDP.print(" ");
   UKI_UDP.print(" ");
   UKI_UDP.print(GSR_sensor);
   UKI_UDP.print(GSR_sensor);
   UKI_UDP.endPacket();
   UKI_UDP.endPacket();
   yield();
   yield();
   
   
-  delay(20);
+  delay(50);
   
   
   //Check udp in
   //Check udp in
   int packetSize = UKI_UDP.parsePacket();
   int packetSize = UKI_UDP.parsePacket();

+ 0 - 141
eeprom.h

@@ -1,141 +0,0 @@
-
-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;
-  }
-}
-

+ 3 - 11
includes.h

@@ -1,5 +1,6 @@
 
 
 
 
+#include <FS.h>                   //this needs to be first, or it all crashes and burns...
 
 
 #include <ESP8266WiFi.h>
 #include <ESP8266WiFi.h>
 //#include <WiFiClient.h>
 //#include <WiFiClient.h>
@@ -11,14 +12,11 @@
 
 
 #include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson
 #include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson
 
 
-
 #include <Ticker.h>
 #include <Ticker.h>
-#include <EEPROM.h>
 #include <WiFiUdp.h>
 #include <WiFiUdp.h>
 
 
 //OTA includes
 //OTA includes
 #include <ESP8266mDNS.h>
 #include <ESP8266mDNS.h>
-//#include <WiFiUdp.h>
 #include <ArduinoOTA.h>
 #include <ArduinoOTA.h>
 
 
 
 
@@ -27,16 +25,10 @@
 WiFiUDP UKI_UDP;
 WiFiUDP UKI_UDP;
 
 
 
 
-
 #include "leds.h"     //config and functions relative to leds
 #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 "helpers.h"  //some helpers functions
 #include "ota.h"      //config and functions relative to ota firmware updates
 #include "ota.h"      //config and functions relative to ota firmware updates
-
-#include "wifimgr.h"   //config and functions relative to wifi and access point configuration
+#include "wifimgr.h"   //config and functions relative to wifi and access point configuration and configuration permanent saving
 
 
 
 
 
 

+ 6 - 7
leds.h

@@ -49,16 +49,15 @@ void setupLeds() {
   pinMode(Blue_Led, OUTPUT);
   pinMode(Blue_Led, OUTPUT);
   digitalWrite(Blue_Led, Blue_Led_State);
   digitalWrite(Blue_Led, Blue_Led_State);
   pinMode(Red_Led, OUTPUT);
   pinMode(Red_Led, OUTPUT);
-  digitalWrite(Red_Led, Red_Led_State);//red led on
-  
+  digitalWrite(Red_Led, Red_Led_State);//red led on at boot
   delay(2000);
   delay(2000);
+  
   blueLedState(-1, 100);
   blueLedState(-1, 100);
-  redLedState(-1, 400);
-  delay (5000);
+  redLedState(-1, 100);
+  delay (500); // blink fast during 0.5 seconds
   
   
-  redLedState(1, 100);
-  blueLedState(0, 100);
-  delay(1000);
+  redLedState(0, 100);
+  blueLedState(0, 100); // then switch all leds off
   
   
 }
 }
 
 

+ 3 - 1
ota.h

@@ -1,8 +1,10 @@
 Ticker tkOTA;  // periodic check if OTA available
 Ticker tkOTA;  // periodic check if OTA available
 int OTA_check_ms = 100;
 int OTA_check_ms = 100;
+bool flag_OTA = false;
 
 
 void loopOTA(){
 void loopOTA(){
-  
+    //change to ticker flag system 
+    
     ArduinoOTA.handle();
     ArduinoOTA.handle();
     //Serial.println("OTA check");
     //Serial.println("OTA check");
 }
 }

+ 118 - 41
wifimgr.h

@@ -1,59 +1,132 @@
+//ticker flag for saving data
+bool flag_SaveConfig = false;
 
 
-//flag for saving data
-
-#define TRIGGER_PIN 12
+#define TRIGGER_PIN 12 //start onDemand config portal when set to LOW
 Ticker tkConfig ;
 Ticker tkConfig ;
-bool StartConfig = false;
+bool flag_ConfigPortal = false;
+
+char UKI_NAME[40];
+char UKI_UDP_PORT[6] = "9000";
+char UKI_UDP_IP[34] = "192.168.10.100";
 
 
 
 
 
 
-//callback notifying us of the need to save config
+//callback notifying the need to save config
 void saveConfigCallback () {
 void saveConfigCallback () {
   Serial.println("Should save config");
   Serial.println("Should save config");
-  shouldSaveConfig = true;
+  flag_SaveConfig = true;
 }
 }
 
 
 // Ticker flag to go to config mode
 // Ticker flag to go to config mode
-void ConfigAPMode () {
+void CheckTriggerPin () {
   Serial.println("Config check");
   Serial.println("Config check");
   if ( digitalRead(TRIGGER_PIN) == LOW) {
   if ( digitalRead(TRIGGER_PIN) == LOW) {
-    StartConfig = true;
+    flag_ConfigPortal = true;
+  }
+}
+
+void ReadConfig() {
+  
+  //read configuration from FS json
+  Serial.println("mounting FS...");
+
+  if (SPIFFS.begin()) {
+    Serial.println("mounted file system");
+    if (SPIFFS.exists("/config.json")) {
+      //file exists, reading and loading
+      Serial.println("reading config file");
+      File configFile = SPIFFS.open("/config.json", "r");
+      if (configFile) {
+        Serial.println("opened config file");
+        size_t size = configFile.size();
+        // Allocate a buffer to store contents of the file.
+        std::unique_ptr<char[]> buf(new char[size]);
+
+        configFile.readBytes(buf.get(), size);
+        DynamicJsonBuffer jsonBuffer;
+        JsonObject& json = jsonBuffer.parseObject(buf.get());
+        json.printTo(Serial);
+        if (json.success()) {
+          Serial.println("\nparsed json");
+
+          strcpy(UKI_NAME, json["UKI_NAME"]);
+          strcpy(UKI_UDP_PORT, json["UKI_UDP_PORT"]);
+          strcpy(UKI_UDP_IP, json["UKI_UDP_IP"]);
+
+        } else {
+          Serial.println("failed to load json config");
+        }
+      }
+    }
+  } else {
+    Serial.println("failed to mount FS");
   }
   }
+  //end read
 }
 }
 
 
-void StartConfigAP(){
+void WriteConfig() {
+
+  //save the custom parameters to FS
+    if (flag_SaveConfig) {
+      
+      flag_SaveConfig = false ;
+      Serial.println("saving config");
+      DynamicJsonBuffer jsonBuffer;
+      JsonObject& json = jsonBuffer.createObject();
+      json["UKI_NAME"] = UKI_NAME;
+      json["UKI_UDP_PORT"] = UKI_UDP_PORT;
+      json["UKI_UDP_IP"] = UKI_UDP_IP;
+
+      File configFile = SPIFFS.open("/config.json", "w");
+      if (!configFile) {
+        Serial.println("failed to open config file for writing");
+      }
+  
+      json.printTo(Serial);
+      json.printTo(configFile);
+      configFile.close();
+      //end save
+    }
+}
+
+void StartConfigAP(){  
+  /* stops all tickers, start config portal and waits for new configuration
+   *  if new connection, saves the new configuration values and goes back to loop()
+   add a check if disconnected ?
+   */
 
 
-  if (StartConfig) {
-    StartConfig = false;
+  if (flag_ConfigPortal) { //or disconnected from wifi
+    
+    flag_ConfigPortal = false; //reset flag
+    
     // detach all tickers (redLed, blueLed, OTA, wifimgr, UKI_UDP)
     // detach all tickers (redLed, blueLed, OTA, wifimgr, UKI_UDP)
     redLedState (1, 500);
     redLedState (1, 500);
     blueLedState (1,500);
     blueLedState (1,500);
     detachOTA();
     detachOTA();
     tkConfig.detach();
     tkConfig.detach();
-    delay (500);
+    delay (500); 
+
+    ReadConfig() ; //read config.json from FS
     
     
     //WiFiManager
     //WiFiManager
     
     
     // The extra parameters to be configured (can be either global or just in the setup)
     // The extra parameters to be configured (can be either global or just in the setup)
     // After connecting, parameter.getValue() will get you the configured value
     // After connecting, parameter.getValue() will get you the configured value
     // id/name placeholder/prompt default length
     // id/name placeholder/prompt default length
-    WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
-    WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5);
-    WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);
+    WiFiManagerParameter custom_UKI_NAME("name", "UKI NAME", UKI_NAME, 40);
+    WiFiManagerParameter custom_UKI_UDP_PORT("port", "UKI udp port", UKI_UDP_PORT, 5);
+    WiFiManagerParameter custom_UKI_UDP_IP("ip", "UKI udp IP", UKI_UDP_IP, 32);
   
   
     //Local intialization. Once its business is done, there is no need to keep it around
     //Local intialization. Once its business is done, there is no need to keep it around
     WiFiManager wifiManager;
     WiFiManager wifiManager;
-
-    //reset settings - for testing
-    //wifiManager.resetSettings();
-
-    //set config save notify callback
-    wifiManager.setSaveConfigCallback(saveConfigCallback);
+    
+    //wifiManager.resetSettings();//reset settings - for testing    
+    wifiManager.setSaveConfigCallback(saveConfigCallback);//set config save notify callback
 
 
     //add all your parameters here
     //add all your parameters here
-    wifiManager.addParameter(&custom_mqtt_server);
-    wifiManager.addParameter(&custom_mqtt_port);
-    wifiManager.addParameter(&custom_blynk_token);
+    wifiManager.addParameter(&custom_UKI_NAME);
+    wifiManager.addParameter(&custom_UKI_UDP_PORT);
+    wifiManager.addParameter(&custom_UKI_UDP_IP);
 
 
     //it starts an access point with the specified name
     //it starts an access point with the specified name
     //here  "AutoConnectAP"
     //here  "AutoConnectAP"
@@ -62,37 +135,41 @@ void StartConfigAP(){
     redLedState (0, 500);
     redLedState (0, 500);
     blueLedState (-1, 100);
     blueLedState (-1, 100);
     delay(1000);
     delay(1000);
-    if (!wifiManager.startConfigPortal("UKI_AP")) {
-      Serial.println("failed to connect and hit timeout");
-      delay(3000);
-      //reset and try again, or maybe put it to deep sleep
+    if (!wifiManager.startConfigPortal("ESP_UKI_AP")) {
+      Serial.println("failed to connect, restarting");
+      //reset and try again
+      redLedState (-1, 100);
+      blueLedState (-1, 100);
       ESP.reset();
       ESP.reset();
-      delay(5000);
+      delay(3000);
       }
       }
-  
 
 
     //if you get here you have connected to the WiFi
     //if you get here you have connected to the WiFi
     Serial.println("connected to UKI wifi");
     Serial.println("connected to UKI wifi");
     blueLedState(1,500);
     blueLedState(1,500);
 
 
     //read updated parameters
     //read updated parameters
-    strcpy(mqtt_server, custom_mqtt_server.getValue());
-    strcpy(mqtt_port, custom_mqtt_port.getValue());
-    strcpy(blynk_token, custom_blynk_token.getValue());
-
-    // attach() tickers again ?
+    strcpy(UKI_NAME, custom_UKI_NAME.getValue());
+    strcpy(UKI_UDP_PORT, custom_UKI_UDP_PORT.getValue());
+    strcpy(UKI_UDP_IP, custom_UKI_UDP_IP.getValue());
 
 
-    //save the custom parameters to FS
-//    if (shouldSaveConfig) {
-//      Serial.print("should save config");
-//      }
-    }
+    WriteConfig();
+    
+    // attach() tickers back now that it's working
+    
   }
   }
+}
 
 
 
 
 void setupWifi() {
 void setupWifi() {
   pinMode(TRIGGER_PIN, INPUT);
   pinMode(TRIGGER_PIN, INPUT);
-  tkConfig.attach(5, ConfigAPMode); // check TRIGGER_PIN state periodically
+  tkConfig.attach(5, CheckTriggerPin); // check TRIGGER_PIN state periodically
+  WiFi.begin();
+//  while(WiFi.status()<3) {
+//    Serial.print
+//clean FS, for testing
+  //SPIFFS.format();
+  //}
   
   
 }
 }