瀏覽代碼

Same than before with all files staged

Etienne Landon 9 年之前
父節點
當前提交
6ea7c48ab7
共有 6 個文件被更改,包括 483 次插入408 次删除
  1. 94 27
      ESP_UKI.ino
  2. 140 140
      eeprom.h
  3. 210 210
      helpers.h
  4. 7 4
      includes.h
  5. 1 10
      leds.h
  6. 31 17
      wifimgr.h

+ 94 - 27
ESP_UKI.ino

@@ -1,8 +1,6 @@
 /*
     ESP_UKI
 
-    largely base on http://www.john-lassen.de/index.php/projects/esp-8266-arduino-ide-webconfig
-
   TODO :  better function organizing
           add firmware number in webserver
           rework led system with tickers
@@ -13,9 +11,15 @@
 */
 
 
-  /* LEDS  */
 
+#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
 
@@ -23,11 +27,12 @@
 
 
 
+
 /* 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
 Ticker tkUKI;  // periodic send ADC to UDP
-//int GSR_sensor;
+int GSR_sensor;
 
 
 void setup ( void ) {
@@ -35,11 +40,54 @@ void setup ( void ) {
   //EEPROM.begin(512);
   Serial.begin(115200);
   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();
   
-  //setupOTA();
+  setupOTA();
   
   delay(200);
   Serial.println("Ready");
@@ -54,7 +102,7 @@ void setup ( void ) {
 //  delay(1000);
 //  ledBlink(Red_Led, 3, 100); //3 quick blink on red led as we start 
 //  delay (1000);
-  //blueLedState(-1, 500);
+  blueLedState(-1, 500);
   
   
 }
@@ -62,29 +110,48 @@ void setup ( void ) {
 
 void loop ( void ) {
   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	*/
-//  GSR_sensor = analogRead(A0);
-//  //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.print(" ");
-//  UKI_UDP.print(GSR_sensor);
-//  UKI_UDP.endPacket();
-//  //yield();
-//  
-//  delay(20);
-//  
-//  //Check udp in
-//  int packetSize = UKI_UDP.parsePacket();
-//  
-//  if(packetSize) {
-//    UKI_UDP_Master_IP = UKI_UDP.remoteIP();
-//    UKI_UDP.beginPacket(UKI_UDP_Master_IP, 8000);
-//    UKI_UDP.print("new master ip");
-//    UKI_UDP.endPacket();
-//  }
+  GSR_sensor = analogRead(A0);
+  //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.print(" ");
+  UKI_UDP.print(GSR_sensor);
+  UKI_UDP.endPacket();
+  yield();
+  
+  delay(20);
+  
+  //Check udp in
+  int packetSize = UKI_UDP.parsePacket();
+  
+  if(packetSize) {
+    UKI_UDP_Master_IP = UKI_UDP.remoteIP();
+    UKI_UDP.beginPacket(UKI_UDP_Master_IP, 8000);
+    UKI_UDP.print("new master ip");
+    UKI_UDP.endPacket();
+  }
 
 
 

+ 140 - 140
eeprom.h

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

+ 210 - 210
helpers.h

@@ -1,210 +1,210 @@
-//#ifndef HELPERS_H
-//#define HELPERS_H
-//
-//
-//
-//static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; 
-//#define LEAP_YEAR(Y) ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) )
-//
-//
-//struct  strDateTime
-//{
-//	byte hour;
-//	byte minute;
-//	byte second;
-//	int year;
-//	byte month;
-//	byte day;
-//	byte wday;
-//
-//} ;
-//
-//
-//
-////
-//// Summertime calculates the daylight saving for a given date.
-////
-//boolean summertime(int year, byte month, byte day, byte hour, byte tzHours)
-//// input parameters: "normal time" for year, month, day, hour and tzHours (0=UTC, 1=MEZ)
-//{
-// if (month<3 || month>10) return false; // keine Sommerzeit in Jan, Feb, Nov, Dez
-// if (month>3 && month<10) return true; // Sommerzeit in Apr, Mai, Jun, Jul, Aug, Sep
-// if (month==3 && (hour + 24 * day)>=(1 + tzHours + 24*(31 - (5 * year /4 + 4) % 7)) || month==10 && (hour + 24 * day)<(1 + tzHours + 24*(31 - (5 * year /4 + 1) % 7)))
-//   return true;
-// else
-//   return false;
-//}
-//
-////
-//// Check the Values is between 0-255
-////
-//boolean checkRange(String Value)
-//{
-//	 if (Value.toInt() < 0 || Value.toInt() > 255)
-//	 {
-//		 return false;
-//	 }
-//	 else
-//	 {
-//		 return true;
-//	 }
-//}
-//
-//
-//void WriteStringToEEPROM(int beginaddress, String string)
-//{
-//	char  charBuf[string.length()+1];
-//	string.toCharArray(charBuf, string.length()+1);
-//	for (int t=  0; t<sizeof(charBuf);t++)
-//	{
-//			EEPROM.write(beginaddress + t,charBuf[t]);
-//	}
-//}
-//String  ReadStringFromEEPROM(int beginaddress)
-//{
-//	byte counter=0;
-//	char rChar;
-//	String retString = "";
-//	while (1)
-//	{
-//		rChar = EEPROM.read(beginaddress + counter);
-//		if (rChar == 0) break;
-//		if (counter > 31) break;
-//		counter++;
-//		retString.concat(rChar);
-//
-//	}
-//	return retString;
-//}
-//void EEPROMWritelong(int address, long value)
-//      {
-//      byte four = (value & 0xFF);
-//      byte three = ((value >> 8) & 0xFF);
-//      byte two = ((value >> 16) & 0xFF);
-//      byte one = ((value >> 24) & 0xFF);
-//
-//      //Write the 4 bytes into the eeprom memory.
-//      EEPROM.write(address, four);
-//      EEPROM.write(address + 1, three);
-//      EEPROM.write(address + 2, two);
-//      EEPROM.write(address + 3, one);
-//      }
-//long EEPROMReadlong(long address)
-//      {
-//      //Read the 4 bytes from the eeprom memory.
-//      long four = EEPROM.read(address);
-//      long three = EEPROM.read(address + 1);
-//      long two = EEPROM.read(address + 2);
-//      long one = EEPROM.read(address + 3);
-//
-//      //Return the recomposed long by using bitshift.
-//      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
-//}
-//
-//void ConvertUnixTimeStamp( unsigned long TimeStamp, struct strDateTime* DateTime)
-//{
-//		uint8_t year;
-//	uint8_t month, monthLength;
-//	uint32_t time;
-//	unsigned long days;
-//	  time = (uint32_t)TimeStamp;
-//	  DateTime->second = time % 60;
-//	  time /= 60; // now it is minutes
-//	  DateTime->minute = time % 60;
-//	  time /= 60; // now it is hours
-//	  DateTime->hour = time % 24;
-//	  time /= 24; // now it is days
-//	  DateTime->wday = ((time + 4) % 7) + 1;  // Sunday is day 1 
-//  
-//	  year = 0;  
-//	days = 0;
-//	while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
-//		year++;
-//	}
-//	DateTime->year = year; // year is offset from 1970 
-//  
-//	  days -= LEAP_YEAR(year) ? 366 : 365;
-//	  time  -= days; // now it is days in this year, starting at 0
-//  
-//	  days=0;
-//	  month=0;
-//	  monthLength=0;
-//	  for (month=0; month<12; month++) {
-//		if (month==1) { // february
-//		  if (LEAP_YEAR(year)) {
-//			monthLength=29;
-//		  } else {
-//			monthLength=28;
-//		  }
-//		} else {
-//		  monthLength = monthDays[month];
-//		}
-//    
-//		if (time >= monthLength) {
-//		  time -= monthLength;
-//		} else {
-//			break;
-//		}
-//	  }
-//	  DateTime->month = month + 1;  // jan is month 1  
-//	  DateTime->day = time + 1;     // day of month
-//	  DateTime->year += 1970;
-//
-//	 
-//}
-//
-//
-//	
-//String GetMacAddress()
-//{
-//	uint8_t mac[6];
-//    char macStr[18] = {0};
-//	WiFi.macAddress(mac);
-//    sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0],  mac[1], mac[2], mac[3], mac[4], mac[5]);
-//    return  String(macStr);
-//}
-//
-//// convert a single hex digit character to its integer value (from https://code.google.com/p/avr-netino/)
-//unsigned char h2int(char c)
-//{
-//    if (c >= '0' && c <='9'){
-//        return((unsigned char)c - '0');
-//    }
-//    if (c >= 'a' && c <='f'){
-//        return((unsigned char)c - 'a' + 10);
-//    }
-//    if (c >= 'A' && c <='F'){
-//        return((unsigned char)c - 'A' + 10);
-//    }
-//    return(0);
-//}
-//
-//String urldecode(String input) // (based on https://code.google.com/p/avr-netino/)
-//{
-//	 char c;
-//	 String ret = "";
-//	 
-//	 for(byte t=0;t<input.length();t++)
-//	 {
-//		 c = input[t];
-//		 if (c == '+') c = ' ';
-//         if (c == '%') {
-//
-//
-//         t++;
-//         c = input[t];
-//         t++;
-//         c = (h2int(c) << 4) | h2int(input[t]);
-//		 }
-//		
-//		 ret.concat(c);
-//	 }
-//	 return ret;
-//  
-//}
-//
-//
-//
-//
-// 
-//#endif
+#ifndef HELPERS_H
+#define HELPERS_H
+
+
+
+static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; 
+#define LEAP_YEAR(Y) ( ((1970+Y)>0) && !((1970+Y)%4) && ( ((1970+Y)%100) || !((1970+Y)%400) ) )
+
+
+struct  strDateTime
+{
+	byte hour;
+	byte minute;
+	byte second;
+	int year;
+	byte month;
+	byte day;
+	byte wday;
+
+} ;
+
+
+
+//
+// Summertime calculates the daylight saving for a given date.
+//
+boolean summertime(int year, byte month, byte day, byte hour, byte tzHours)
+// input parameters: "normal time" for year, month, day, hour and tzHours (0=UTC, 1=MEZ)
+{
+ if (month<3 || month>10) return false; // keine Sommerzeit in Jan, Feb, Nov, Dez
+ if (month>3 && month<10) return true; // Sommerzeit in Apr, Mai, Jun, Jul, Aug, Sep
+ if (month==3 && (hour + 24 * day)>=(1 + tzHours + 24*(31 - (5 * year /4 + 4) % 7)) || month==10 && (hour + 24 * day)<(1 + tzHours + 24*(31 - (5 * year /4 + 1) % 7)))
+   return true;
+ else
+   return false;
+}
+
+//
+// Check the Values is between 0-255
+//
+boolean checkRange(String Value)
+{
+	 if (Value.toInt() < 0 || Value.toInt() > 255)
+	 {
+		 return false;
+	 }
+	 else
+	 {
+		 return true;
+	 }
+}
+
+
+void WriteStringToEEPROM(int beginaddress, String string)
+{
+	char  charBuf[string.length()+1];
+	string.toCharArray(charBuf, string.length()+1);
+	for (int t=  0; t<sizeof(charBuf);t++)
+	{
+			EEPROM.write(beginaddress + t,charBuf[t]);
+	}
+}
+String  ReadStringFromEEPROM(int beginaddress)
+{
+	byte counter=0;
+	char rChar;
+	String retString = "";
+	while (1)
+	{
+		rChar = EEPROM.read(beginaddress + counter);
+		if (rChar == 0) break;
+		if (counter > 31) break;
+		counter++;
+		retString.concat(rChar);
+
+	}
+	return retString;
+}
+void EEPROMWritelong(int address, long value)
+      {
+      byte four = (value & 0xFF);
+      byte three = ((value >> 8) & 0xFF);
+      byte two = ((value >> 16) & 0xFF);
+      byte one = ((value >> 24) & 0xFF);
+
+      //Write the 4 bytes into the eeprom memory.
+      EEPROM.write(address, four);
+      EEPROM.write(address + 1, three);
+      EEPROM.write(address + 2, two);
+      EEPROM.write(address + 3, one);
+      }
+long EEPROMReadlong(long address)
+      {
+      //Read the 4 bytes from the eeprom memory.
+      long four = EEPROM.read(address);
+      long three = EEPROM.read(address + 1);
+      long two = EEPROM.read(address + 2);
+      long one = EEPROM.read(address + 3);
+
+      //Return the recomposed long by using bitshift.
+      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
+}
+
+void ConvertUnixTimeStamp( unsigned long TimeStamp, struct strDateTime* DateTime)
+{
+		uint8_t year;
+	uint8_t month, monthLength;
+	uint32_t time;
+	unsigned long days;
+	  time = (uint32_t)TimeStamp;
+	  DateTime->second = time % 60;
+	  time /= 60; // now it is minutes
+	  DateTime->minute = time % 60;
+	  time /= 60; // now it is hours
+	  DateTime->hour = time % 24;
+	  time /= 24; // now it is days
+	  DateTime->wday = ((time + 4) % 7) + 1;  // Sunday is day 1 
+  
+	  year = 0;  
+	days = 0;
+	while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
+		year++;
+	}
+	DateTime->year = year; // year is offset from 1970 
+  
+	  days -= LEAP_YEAR(year) ? 366 : 365;
+	  time  -= days; // now it is days in this year, starting at 0
+  
+	  days=0;
+	  month=0;
+	  monthLength=0;
+	  for (month=0; month<12; month++) {
+		if (month==1) { // february
+		  if (LEAP_YEAR(year)) {
+			monthLength=29;
+		  } else {
+			monthLength=28;
+		  }
+		} else {
+		  monthLength = monthDays[month];
+		}
+    
+		if (time >= monthLength) {
+		  time -= monthLength;
+		} else {
+			break;
+		}
+	  }
+	  DateTime->month = month + 1;  // jan is month 1  
+	  DateTime->day = time + 1;     // day of month
+	  DateTime->year += 1970;
+
+	 
+}
+
+
+	
+String GetMacAddress()
+{
+	uint8_t mac[6];
+    char macStr[18] = {0};
+	WiFi.macAddress(mac);
+    sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0],  mac[1], mac[2], mac[3], mac[4], mac[5]);
+    return  String(macStr);
+}
+
+// convert a single hex digit character to its integer value (from https://code.google.com/p/avr-netino/)
+unsigned char h2int(char c)
+{
+    if (c >= '0' && c <='9'){
+        return((unsigned char)c - '0');
+    }
+    if (c >= 'a' && c <='f'){
+        return((unsigned char)c - 'a' + 10);
+    }
+    if (c >= 'A' && c <='F'){
+        return((unsigned char)c - 'A' + 10);
+    }
+    return(0);
+}
+
+String urldecode(String input) // (based on https://code.google.com/p/avr-netino/)
+{
+	 char c;
+	 String ret = "";
+	 
+	 for(byte t=0;t<input.length();t++)
+	 {
+		 c = input[t];
+		 if (c == '+') c = ' ';
+         if (c == '%') {
+
+
+         t++;
+         c = input[t];
+         t++;
+         c = (h2int(c) << 4) | h2int(input[t]);
+		 }
+		
+		 ret.concat(c);
+	 }
+	 return ret;
+  
+}
+
+
+
+
+ 
+#endif

+ 7 - 4
includes.h

@@ -9,6 +9,9 @@
 #include <DNSServer.h>
 #include <WiFiManager.h>
 
+#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson
+
+
 #include <Ticker.h>
 #include <EEPROM.h>
 #include <WiFiUdp.h>
@@ -25,13 +28,13 @@ 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 "helpers.h"  //some helpers functions
 
-//#include "eeprom.h"   //config and functions relative to config permanent storage
+#include "eeprom.h"   //config and functions relative to config permanent storage
 
-//#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
 

+ 1 - 10
leds.h

@@ -17,6 +17,7 @@ void blinkLed(int Led) {
   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);
@@ -61,13 +62,3 @@ void setupLeds() {
   
 }
 
-
-
-//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);
-//  }
-//}

+ 31 - 17
wifimgr.h

@@ -1,6 +1,6 @@
 
 //flag for saving data
-bool shouldSaveConfig = false;
+
 #define TRIGGER_PIN 12
 Ticker tkConfig ;
 bool StartConfig = false;
@@ -13,6 +13,7 @@ void saveConfigCallback () {
   shouldSaveConfig = true;
 }
 
+// Ticker flag to go to config mode
 void ConfigAPMode () {
   Serial.println("Config check");
   if ( digitalRead(TRIGGER_PIN) == LOW) {
@@ -23,14 +24,22 @@ void ConfigAPMode () {
 void StartConfigAP(){
 
   if (StartConfig) {
+    StartConfig = false;
     // detach all tickers (redLed, blueLed, OTA, wifimgr, UKI_UDP)
-//    redLedState (1, 500);
-//    blueLedState (1,500);
-//    detachOTA();
+    redLedState (1, 500);
+    blueLedState (1,500);
+    detachOTA();
     tkConfig.detach();
     delay (500);
     
     //WiFiManager
+    
+    // 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
+    // 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);
   
     //Local intialization. Once its business is done, there is no need to keep it around
     WiFiManager wifiManager;
@@ -38,22 +47,20 @@ void StartConfigAP(){
     //reset settings - for testing
     //wifiManager.resetSettings();
 
-    //sets timeout until configuration portal gets turned off
-    //useful to make it all retry or go to sleep
-    //in seconds
-    //wifiManager.setTimeout(120);
-
     //set config save notify callback
     wifiManager.setSaveConfigCallback(saveConfigCallback);
 
+    //add all your parameters here
+    wifiManager.addParameter(&custom_mqtt_server);
+    wifiManager.addParameter(&custom_mqtt_port);
+    wifiManager.addParameter(&custom_blynk_token);
+
     //it starts an access point with the specified name
     //here  "AutoConnectAP"
     //and goes into a blocking loop awaiting configuration
 
-    //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1
-    //WiFi.mode(WIFI_STA);
-    //redLedState (0, 500);
-    //blueLedState (-1, 100);
+    redLedState (0, 500);
+    blueLedState (-1, 100);
     delay(1000);
     if (!wifiManager.startConfigPortal("UKI_AP")) {
       Serial.println("failed to connect and hit timeout");
@@ -66,12 +73,19 @@ void StartConfigAP(){
 
     //if you get here you have connected to the WiFi
     Serial.println("connected to UKI wifi");
-   // blueLedState(1,500);
+    blueLedState(1,500);
+
+    //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 ?
 
     //save the custom parameters to FS
-    if (shouldSaveConfig) {
-      Serial.print("should save config");
-      }
+//    if (shouldSaveConfig) {
+//      Serial.print("should save config");
+//      }
     }
   }