Browse Source

erste Testversion

FloKra 6 years ago
parent
commit
8e4b0f3221

+ 24 - 17
src/WiFiSwitch/Buttonhandling.ino

@@ -1,9 +1,10 @@
-bool buttonCurrentState[3] = {HIGH,HIGH,HIGH};
-bool buttonLastState[3] = {HIGH,HIGH,HIGH};
-unsigned long buttonDownMillis[3] = {0,0,0};
-unsigned long buttonHoldTime[3] = {0,0,0};
-bool buttonFired[3] = {false,false,false};
-bool buttonHoldFired[3] = {false,false,false};
+bool buttonCurrentState[3] = {HIGH, HIGH, HIGH};
+bool buttonLastState[3] = {HIGH, HIGH, HIGH};
+unsigned long buttonDownMillis[3] = {0, 0, 0};
+unsigned long buttonHoldTime[3] = {0, 0, 0};
+bool buttonFired[3] = {false, false, false};
+bool buttonHoldFired[3] = {false, false, false};
+unsigned long lastButtonPress[3] = {0, 0, 0};
 
 int debounceTime = 120;
 
@@ -13,7 +14,7 @@ void buttonAction(byte relnr) {
 }
 
 void buttonHoldAction(byte btn) {
-  switch(btn) {
+  switch (btn) {
     case 0:
       mqttclient.publish(mqtt_topic_out_hold_1, mqtt_payload_out_hold_1);
       break;
@@ -27,7 +28,7 @@ void buttonHoldAction(byte btn) {
 }
 
 void checkButtonStates() {
-  for (int i=0; i < BUTTONS_COUNT; i++) {
+  for (int i = 0; i < BUTTONS_COUNT; i++) {
     checkButtonState(i);
   }
 }
@@ -44,11 +45,14 @@ void checkButtonState(byte btn) {
       buttonLastState[btn] = buttonCurrentState[btn];
       if ( buttonHoldTime[btn] > 750 && !buttonHoldFired[btn] ) {
         // button is held longer
-        Serial.print("Button ");
-        Serial.print(btn + 1);
-        Serial.println(" long pressed");
-        buttonHoldAction(btn);
-        buttonHoldFired[btn] = true;
+        if ( (millis() - lastButtonPress[btn]) > 700 ) { // mitigate double triggering
+          lastButtonPress[btn] = millis();
+          Serial.print("Button ");
+          Serial.print(btn + 1);
+          Serial.println(" long pressed");
+          buttonHoldAction(btn);
+          buttonHoldFired[btn] = true;
+        }
       }
     }
   }
@@ -56,10 +60,13 @@ void checkButtonState(byte btn) {
     if (buttonHoldTime[btn] > debounceTime) {          // entprellung
       buttonLastState[btn] = buttonCurrentState[btn];
       if (!buttonHoldFired[btn] && !buttonFired[btn]) {
-        Serial.print("Button ");
-        Serial.print(btn + 1);
-        Serial.println(" short pressed");
-        buttonAction(btn);
+        if ( (millis() - lastButtonPress[btn]) > 700 ) { // mitigate double triggering
+          lastButtonPress[btn] = millis();
+          Serial.print("Button ");
+          Serial.print(btn + 1);
+          Serial.println(" short pressed");
+          buttonAction(btn);
+        }
       }
       buttonFired[btn] = false;
       buttonHoldFired[btn] = false;

+ 22 - 16
src/WiFiSwitch/WiFiSwitch.ino

@@ -1,5 +1,7 @@
 
 // pre compiletime config
+#define SPIFFS_DBG
+#define SPIFFS_USE_MAGIC
 
 #define FIRMWARE_NAME "WiFi Switch"
 #define VERSION "0.1"
@@ -21,7 +23,7 @@
 #define BUTTON2_HOLD_PAYLOAD_OUT "TOGGLE"
 #define BUTTON3_HOLD_PAYLOAD_OUT "TOGGLE"
 #define DOMOTICZ_IN_TOPIC "domoticz/in"
-#define DOMOTICZ_OUT_TOPIC "domoticz/outTEST"
+#define DOMOTICZ_OUT_TOPIC "domoticz/out"
 #define DOMOTICZ_IDX_1 0
 #define DOMOTICZ_IDX_2 0
 #define DOMOTICZ_IDX_3 0
@@ -35,17 +37,17 @@
 // GPIO 15 has 10k pull-down, so could only be used as active-high input
 // GPIO 1 = TX and 3=RX, so could only be used when no UART is needed (using only TX for debug output and RX as GPIO3 is possible)
 // some GPIOs go high for some time at boot, so using only active-low logic is advisable, particularly for switching outputs
-#define RELAIS_COUNT 3
+#define RELAIS_COUNT 1
 #define PIN_RELAIS1 12
 #define PIN_RELAIS2 4
 #define PIN_RELAIS3 5
 
-#define BUTTONS_COUNT 3
+#define BUTTONS_COUNT 1
 #define PIN_BUTTON1 0
 #define PIN_BUTTON2 2
 #define PIN_BUTTON3 13
 
-#define RELAISONSTATE LOW
+#define RELAISONSTATE HIGH
 #define BUTTONONSTATE LOW
 
 
@@ -93,7 +95,7 @@ char mqtt_payload_out_hold_1[31];
 char mqtt_payload_out_hold_2[31];
 char mqtt_payload_out_hold_3[31];
 int domoticzIdx[3] = {DOMOTICZ_IDX_1, DOMOTICZ_IDX_2, DOMOTICZ_IDX_3}; // initially set to 0, must be defined in config
-char domoticz_out_topic[51];
+char domoticz_out_topic[55];
 
 int relais_impulse[3] = {RELAIS1_IMPULSE, RELAIS2_IMPULSE, RELAIS3_IMPULSE};
 
@@ -156,12 +158,6 @@ void setup() {
   Serial.println(COPYRIGHT);
   Serial.println("starting...");
 
-  Serial.println("Mounting FS...");
-  if (!SPIFFS.begin()) {
-    Serial.println("Failed to mount file system");
-    return;
-  }
-
   // set config default parameters
   strlcpy(deviceName, DEVICE_NAME, 31);
   strlcpy(mqtt_server, MQTT_SERVER, 41);
@@ -178,9 +174,15 @@ void setup() {
   Serial.println("default config values loaded..");
 
 
+  Serial.println("Mounting FS...");
+  if (!SPIFFS.begin()) {
+    Serial.println("Failed to mount file system");
+    return;
+  }
 
-  SPIFFS.begin();
 
+  //SPIFFS.format();
+  
   if (!SPIFFS.exists("/formatComplete.txt")) {
     Serial.println("Please wait 30 secs for SPIFFS to be formatted");
     SPIFFS.format();
@@ -192,6 +194,7 @@ void setup() {
     } else {
       f.println("Format Complete");
     }
+    f.close();
   } else {
     Serial.println("SPIFFS is formatted. Moving along...");
   }
@@ -199,8 +202,10 @@ void setup() {
 
 
 
-  //SPIFFS.format();
-  // load config from SPIFFS file if exists
+
+
+
+  //  // load config from SPIFFS file if exists
   if (!loadConfig()) {
     Serial.println("Failed to load config");
   } else {
@@ -213,10 +218,11 @@ void setup() {
     Serial.println("Config2 loaded");
   }
 
+
   // set relais pin modes
   for (int i = 0; i < RELAIS_COUNT; i++) {
     pinMode(relais_pins[i], OUTPUT);
-    digitalWrite(relais_pins[i], HIGH);
+    digitalWrite(relais_pins[i], !RELAISONSTATE);
   }
 
   // set button pin modes
@@ -291,6 +297,6 @@ void loop() {
 
   if (Serial.available()) serialEvent();
 
-  //delay(50); // save energy by sleeping
+  delay(50); // save energy by sleeping
 } //void loop
 

+ 63 - 32
src/WiFiSwitch/config.ino

@@ -14,6 +14,12 @@ bool setConfig(char* param, char* value) {
     strlcpy(deviceName, value, 31);
     //    deviceName[30] = '\0';
   }
+  else if ( strcmp(param, "httpUser") == 0 ) {
+    strlcpy(http_user, value, 31);
+  }
+  else if ( strcmp(param, "httpPass") == 0 ) {
+    strlcpy(http_pass, value, 31);
+  }
   else if ( strcmp(param, "usedRelais") == 0 ) {
     usedRelaisCount = atoi(value);
   }
@@ -173,47 +179,67 @@ bool loadConfig() { // loadConfig 1
       Serial.print("file size: ");
       Serial.println(size);
 
-      if (size > 800) {
+//      Serial.println("file content:");
+//
+//      while (configFile.available()) {
+//        //Lets read line by line from the file
+//        String line = configFile.readStringUntil('\n');
+//        Serial.println(line);
+//      }
+//      Serial.println();
+
+      if (size > 900) {
         Serial.println("Config file size is too large");
         return false;
       }
 
+      Serial.println("allocate buffer");
       // Allocate a buffer to store contents of the file.
       std::unique_ptr<char[]> buf(new char[size]);
 
+      Serial.println("read file bytes to buffer");
       // We don't use String here because ArduinoJson library requires the input
       // buffer to be mutable. If you don't use ArduinoJson, you may as well
       // use configFile.readString instead.
       configFile.readBytes(buf.get(), size);
 
-      StaticJsonBuffer<810> jsonBuffer;
+      Serial.println("create json buffer");
+
+      StaticJsonBuffer<800> jsonBuffer;
+
+      Serial.println("parse json data");
+
       JsonObject& json = jsonBuffer.parseObject(buf.get());
 
+      Serial.println("do her kumm ma a no");
+
       if (!json.success()) {
         Serial.println("Failed to parse config file");
         return false;
       }
 
-      strlcpy(deviceName, json["devName"], 31);
-      strlcpy(http_user, json["httpUser"], 31);
-      strlcpy(http_pass, json["httpPass"], 31);
-      strlcpy(mqtt_server, json["mqttHost"], 41);
-      mqtt_port = atoi(json["mqttPort"]);
-      strlcpy(mqtt_user, json["mqttUser"], 31);
-      strlcpy(mqtt_pass, json["mqttPass"], 31);
-      strlcpy(mqtt_topic_in, json["inTop"], 51);
-      strlcpy(mqtt_topic_out, json["outTop"], 51);
-      strlcpy(mqtt_willTopic, json["willTop"], 51);
-      mqtt_willQos = atoi(json["willQos"]);
-      if (atoi(json["willRet"]) == 1) mqtt_willRetain = true;
+      Serial.println("und da ?");
+      strlcpy(deviceName, json["devName"] | "", 31);
+      strlcpy(http_user, json["httpUser"] | "", 31);
+      strlcpy(http_pass, json["httpPass"] | "", 31);
+      strlcpy(mqtt_server, json["mqttHost"] | "", 41);
+      mqtt_port = atoi(json["mqttPort"] | "");
+      strlcpy(mqtt_user, json["mqttUser"] | "", 31);
+      strlcpy(mqtt_pass, json["mqttPass"] | "", 31);
+      strlcpy(mqtt_topic_in, json["inTop"] | "", 51);
+      strlcpy(mqtt_topic_out, json["outTop"] | "", 51);
+      strlcpy(mqtt_willTopic, json["willTop"] | "", 51);
+      mqtt_willQos = atoi(json["willQos"] | "0");
+      if (atoi(json["willRet"] | "") == 1) mqtt_willRetain = true;
       else mqtt_willRetain = false;
-      strlcpy(mqtt_willMsg, json["willMsg"], 31);
-      strlcpy(domoticz_out_topic, json["domoOutTop"], 51);
+      strlcpy(mqtt_willMsg, json["willMsg"] | "", 31);
+      strlcpy(domoticz_out_topic, json["domoOutTop"] | "", 51);
 
       Serial.println("Loaded config values:");
       printConfig();
       return true;
     }
+    configFile.close();
   }
   else {
     Serial.println("file /config.json file does not exist");
@@ -235,14 +261,19 @@ bool loadConfig2() {
       size_t size = configFile.size();
       Serial.print("file size: ");
       Serial.println(size);
-      if (size > 1195) {
+      if (size > 800) {
         Serial.println("Config file size is too large");
         return false;
       }
 
       // Allocate a buffer to store contents of the file.
+
+
+
       std::unique_ptr<char[]> buf(new char[size]);
 
+
+
       // We don't use String here because ArduinoJson library requires the input
       // buffer to be mutable. If you don't use ArduinoJson, you may as well
       // use configFile.readString instead.
@@ -256,20 +287,20 @@ bool loadConfig2() {
         return false;
       }
 
-      usedRelaisCount = atoi(json["usedRelais"]);
-      usedButtonsCount = atoi(json["usedButtons"]);
-      strlcpy(mqtt_topic_out_hold_1, json["outTop_hold1"], 51);
-      strlcpy(mqtt_topic_out_hold_2, json["outTop_hold2"], 51);
-      strlcpy(mqtt_topic_out_hold_3, json["outTop_hold3"], 51);
-      strlcpy(mqtt_payload_out_hold_1, json["outPld_hold1"], 31);
-      strlcpy(mqtt_payload_out_hold_2, json["outPld_hold2"], 31);
-      strlcpy(mqtt_payload_out_hold_3, json["outPld_hold3"], 31);
-      domoticzIdx[0] = atoi(json["domoIdx1"]);
-      domoticzIdx[1] = atoi(json["domoIdx2"]);
-      domoticzIdx[2] = atoi(json["domoIdx3"]);
-      relais_impulse[0] = atoi(json["impuls1"]);
-      relais_impulse[1] = atoi(json["impuls2"]);
-      relais_impulse[2] = atoi(json["impuls3"]);
+      usedRelaisCount = atoi(json["usedRelais"] | "");
+      usedButtonsCount = atoi(json["usedButtons"] | "");
+      strlcpy(mqtt_topic_out_hold_1, json["outTop_hold1"] | "", 51);
+      strlcpy(mqtt_topic_out_hold_2, json["outTop_hold2"] | "", 51);
+      strlcpy(mqtt_topic_out_hold_3, json["outTop_hold3"] | "", 51);
+      strlcpy(mqtt_payload_out_hold_1, json["outPld_hold1"] | "", 31);
+      strlcpy(mqtt_payload_out_hold_2, json["outPld_hold2"] | "", 31);
+      strlcpy(mqtt_payload_out_hold_3, json["outPld_hold3"] | "", 31);
+      domoticzIdx[0] = atoi(json["domoIdx1"] | "");
+      domoticzIdx[1] = atoi(json["domoIdx2"] | "");
+      domoticzIdx[2] = atoi(json["domoIdx3"] | "");
+      relais_impulse[0] = atoi(json["impuls1"] | "");
+      relais_impulse[1] = atoi(json["impuls2"] | "");
+      relais_impulse[2] = atoi(json["impuls3"] | "");
 
       Serial.println("Loaded config values:");
       printConfig();
@@ -300,6 +331,7 @@ bool saveConfig() { // safeConfig
   json["willMsg"] = mqtt_willMsg;
   json["inTop"] = mqtt_topic_in;
   json["outTop"] = mqtt_topic_out;
+  json["domoOutTop"] = domoticz_out_topic;
 
   File configFile = SPIFFS.open("/config.json", "w");
   if (!configFile) {
@@ -327,7 +359,6 @@ bool saveConfig2() { // safeConfig2
   json["domoIdx1"] = domoticzIdx[0];
   json["domoIdx2"] = domoticzIdx[1];
   json["domoIdx3"] = domoticzIdx[2];
-  json["domoOutTop"] = domoticz_out_topic;
   json["impuls1"] = relais_impulse[0];
   json["impuls2"] = relais_impulse[1];
   json["impuls3"] = relais_impulse[2];

+ 7 - 5
src/WiFiSwitch/httpServer.ino

@@ -21,7 +21,8 @@ static const char httpRoot[] PROGMEM =
   </h6>
   <br>
   <a href='/wifi.htm'>WiFi-Einstellungen</a><br>
-  <a href='/config'>Einstellungen</a><br>
+  <a href='/conf'>Basis-Einstellungen</a><br>
+  <a href='/conf2'>Erweiterte Einstellungen</a><br>
   <a href='/update'>Firmware Update</a>
   <script>
   function g(i) { return document.getElementById(i) };
@@ -83,6 +84,7 @@ static const char httpRoot[] PROGMEM =
 static const char httpConfPage[] PROGMEM =
   R"(<html><head><body>
 <h3>Configuration</h3>
+<a href='/'>Home</a><br>
 <input type='button' value='reload' onclick='return transmit()'/><br><br>
 <form id='form1' onsubmit='return transmit(this)'>
 Device Name: <input type='text' name='devName' id='devName'/><br>
@@ -184,6 +186,7 @@ Domoticz Out Topic *: <input type='text' name='domoOutTop' id='domoOutTop'/><br>
 static const char httpConf2Page[] PROGMEM =
   R"(<html><head><body>
 <h3>Configuration 2</h3>
+<a href='/'>Home</a><br>
 <input type='button' value='reload' onclick='return transmit()'/><br><br>
 <form id='form1' onsubmit='return transmit(this)'>
 used Relais: <input type='number' name='usedRelais' id='usedRelais'/><br>
@@ -289,8 +292,7 @@ void httpServerHandleNotFound() {
 }
 
 
-void httpServerInit() {
-  //handles commands from webpage, sends live data in JSON format
+void httpServerInit() {  
   httpServer.on("/api", []() {
     //Serial.println("httpServer.on /api");
     if (httpServer.hasArg("Btn1")) {
@@ -314,8 +316,8 @@ void httpServerInit() {
     JsonObject &json = jsonBuffer.createObject();
     json["ssid"] = WiFi.SSID();
     json["swState1"] = relais_state[0];
-    if (RELAIS_COUNT > 1) json["swState2"] = relais_state[1];
-    if (RELAIS_COUNT > 2) json["swState3"] = relais_state[2];
+    if (usedRelaisCount > 1) json["swState2"] = relais_state[1];
+    if (usedRelaisCount > 2) json["swState3"] = relais_state[2];
     json["devname"] = deviceName;
 
     char jsonchar[200];