       }
       return dev_id;
 }
+String getHex(String did) {
+  String doubledid = did + did;
+  builder_md5.begin();
+  builder_md5.add(String(doubledid));
+  builder_md5.calculate();
+  return builder_md5.toString();
+}
 void batteryCheck(){
   float vdd = ESP.getVcc();
   if( vdd < VOLTAGE_THRESHOLD ){
    Serial.print("Curent voltage: "); Serial.println(String(vdd));   
   }
 }
-void setConfigParams(String repository_host, String repository_int_check){
-  Serial.println(String(repository_host));
-  Serial.println(String(repository_int_check));
+void checkConfig(){
+         SPIFFS.begin();
+       if (!SPIFFS.open("/formated.txt", "r")){
+             SPIFFS.format();
+             File format = SPIFFS.open("/formated.txt", "w+");
+             if (format) { 
+                     format.close();
+                     Serial.println("SPIFFS has been formated");
+                     File configFile = SPIFFS.open("/config.txt", "w+");
+                     if (configFile)  {Serial.println("config has been created"); }
+                         }
+            }
+}
+void setConfigParams(String repository_host, String repository_interval){
+      File configFile = SPIFFS.open("/config.txt", "w+");
+       if (configFile) {
+       StaticJsonBuffer<200> jsonBuffer;
+       JsonObject& data = jsonBuffer.createObject();
+          data["repository_host"] = repository_host;
+          data["repository_interval"] = repository_interval;
+       data.printTo(configFile);
+       configFile.close();
+       }
+}
+String getConfigParam(String name){
+       StaticJsonBuffer<200> jsonBuffer; 
+       SPIFFS.begin();
+       File config = SPIFFS.open("/config.txt", "r");
+     if (config) {
+          String line = config.readStringUntil('\n');
+          config.close();
+          JsonObject& jdata = jsonBuffer.parseObject(line);
+          String paramValue = jdata[name];
+          return paramValue; 
+          
+     }else{
+          return "";
+     }
+}
+void initVars(){
+ DID = getID(); 
+ DHEX = getHex(DID);
+                            
+  String rhost = getConfigParam("repository_host");
+  if(rhost != ""){ 
+    char *cstr = new char[rhost.length() + 1];
+    rhost.toCharArray(cstr, rhost.length() + 1); 
+ REPOSITORY_HOST = cstr;
+  }
+  String rint = getConfigParam("repository_interval");
+  if(rint != ""){ 
+ REPOSITORY_INTERVAL = rint.toInt()*60*1000;
+  } 
+}
+void setStaticIP(String ip, String gw, String sn){
+       File configFile = SPIFFS.open("/wificonfig.txt", "w+");
+       if (configFile) {
+       StaticJsonBuffer<200> jsonBuffer;
+       JsonObject& data = jsonBuffer.createObject();
+          data["ip"] = server->arg("ip");
+          data["gw"] = server->arg("gw");
+          data["sn"] = server->arg("sn");
+       data.printTo(configFile);
+       configFile.close();
+       }
 }
 void broadcast(){
    IPAddress broadcastIp = ~WiFi.subnetMask() | WiFi.gatewayIP();
    
    if(udp.beginPacket(broadcastIp, UPDPORT) == 1){
-    Serial.print(broadcastIp);
+    //Serial.print(broadcastIp);
    }
-   udp.write("IP: ");
    udp.print(WiFi.localIP());
    if(udp.endPacket() == 1){
-    Serial.println(" send endPacket");
+    //Serial.println(" send endPacket");
    }
-
    IPAddress bIp(255, 255, 255, 255);
    if(udp.beginPacket(bIp, UPDPORT) == 1){
-    Serial.print(bIp);
+   // Serial.print(bIp);
    }
-    udp.write("IP: ");
     udp.print(WiFi.localIP());
-    udp.print(WiFi.subnetMask());
    if(udp.endPacket() == 1){
-    Serial.println(" send endPacket");
-   }
-   
-      IPAddress bIp1(10, 0, 0, 185);
-   if(udp.beginPacket(bIp, UPDPORT) == 1){
-    Serial.print(bIp1);
-   }
-    udp.write("IP: ");
-    udp.print(WiFi.localIP());
-    udp.print(WiFi.subnetMask());
-   if(udp.endPacket() == 1){
-    Serial.println(" send endPacket");
-   }
-   
+   //Serial.println(" send endPacket");
+   }   
 }
 //------------- end main functions----------------------------------------  
 void setup() {

          strcat(msg, sval.c_str());
          server->send(200, "text/plain", msg);
  }
  void goFeedSlow(String sval) {
        int ival = sval.toInt();
        for(int a=0; a<ival; a++){
              for(int i=0; i<180; i++){

 * Flash button is convenient to use but if it is pressed it will stuff up the serial port device driver 
 * until the computer is rebooted on windows machines.
 */
const int TRIGGER_PIN = D3; // D3 on NodeMCU and WeMos.
const int LED_PIN = D7; //Diod pin
const int MOTOR_PIN = D5; //Motor pin
const String DEV_VERSION = "1.0";
const int POWERCHECK_INTERVAL = 600000; // 10 min Check battery volage
const float VOLTAGE_THRESHOLD = 2710; // voltage threshold , mv
const int BROADCAST_INTERVAL = 5000; // 5 sec broadcastinterval, milisecond
unsigned int UPDPORT = 28031;

int REPOSITORY_INTERVAL = 120000; // 2 min Repository interval check, milisecond
char* REPOSITORY_HOST = "http://345.kiev.ua/1.php"; //Repository  full adress for remote job
String DID = ""; //device ID
String DHEX = ""; //Device md5 hex id based

/*
 * Alternative trigger pin. Needs to be connected to a button to use this pin. It must be a momentary connection
