Links2004 / arduinoMideaAC

hack job for decoding and sending Midea AC Serial commands
GNU Lesser General Public License v2.1
38 stars 8 forks source link

request_status() scrambling webserver data and other code #9

Open mathias172 opened 3 years ago

mathias172 commented 3 years ago

Hi, trying in English this time. Maybe this is interesting for someone. My connection works and I can send data to the AC. What I want to do is set up an easy webserver on an ESP8266 to control the AC. Sending control signals works with that but if I want to read status and/or config of the AC the whole program is going crazy. I tried with different webservers from ESP8266WiFi.h and this one with exact same result, what's really crazy for me.

My .ino is:

#include <mideaAC.h>
#include <ArduinoOTA.h>
#include <SPI.h>
#include <Wire.h>
#include <ESP8266WiFi.h>

#ifndef STASSID
#define STASSID "asdasd"
#define STAPSK  "asdasdasdasd"
#endif

acSerial s1;
char wiFiHostname[ ] = "esp24_splitklima";

const char* ssid = STASSID;
const char* password = STAPSK;
int innen, aussen, an, turbo, eco, soll, lamelle, modus, luefter;
unsigned long statusMillis = 31000;

// Webserver-Kram
WiFiServer server(80);

String header;
unsigned long currentTime = millis();
unsigned long previousTime = 0; 
const long timeoutTime = 20000;
// Webserver-Kram Ende

void ACevent(ac_status_t * status) {
        innen=status->ist;
        aussen=status->aussen;

        an=status->conf.on;
        turbo=status->conf.turbo;
        eco=status->conf.eco;
        soll=status->conf.soll;

        if(status->conf.lamelle!=acLamelleOff) {lamelle = 1; } else {lamelle=0; }

        if(status->conf.mode==acModeAuto) modus = 1;
        if(status->conf.mode==acModeCool) modus = 2;
        if(status->conf.mode==acModeDry) modus = 3;
        if(status->conf.mode==acModeHeat) modus = 4;
        if(status->conf.mode==acModeFan) modus = 5;

        if(status->conf.fan==acFAN1) luefter = 1;
        if(status->conf.fan==acFAN2) luefter = 2;
        if(status->conf.fan==acFAN3) luefter = 3;
        if(status->conf.fan==acFANA) luefter = 4;
}

void setup() {
  IPAddress ip(192, 168, 0, 99);
  IPAddress gateway(192, 168, 0, 1);
  IPAddress subnet(255, 255, 0, 0);
  IPAddress dns(192, 168, 0, 1);
  WiFi.config(ip, dns, gateway, subnet);  //}

  //WiFi.mode(WIFI_STA);
  WiFi.mode(WIFI_STA);
  WiFi.setPhyMode(WIFI_PHY_MODE_11B);
  WiFi.begin(ssid, password);
  wifi_station_set_hostname(wiFiHostname);

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    server.begin();
  }
  ArduinoOTA.setHostname(wiFiHostname);
  ArduinoOTA.setPassword(wiFiHostname);
  ArduinoOTA.begin();

  Serial.begin(9600);
  s1.begin((Stream *)&Serial, "Serial");
  s1.send_getSN();
  s1.onStatusEvent(ACevent);
}

void loop(){
  ArduinoOTA.handle();
  WiFiClient client = server.available();  

  s1.loop();
  unsigned long currentMillis = millis();
    if(currentMillis - statusMillis >= 5000) {
        statusMillis = currentMillis;
        s1.send_status(true, true);
        s1.request_status();
    }

  if (client) {
    String currentLine = "";
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) {
      currentTime = millis();         
      if (client.available()) {
        char c = client.read();
        header += c;
        if (c == '\n') {
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // send_conf_h(on, soll, fan, mode, lamelle, turbo, eco)
            if (header.indexOf("GET /an") >= 0) {
              s1.send_conf_h(true, 17, 1, acModeCool, false, false, false);
            } else if (header.indexOf("GET /aus") >= 0) {
              s1.send_conf_h(false, 19, 1, acModeCool, false, false, false);
            } else if (header.indexOf("GET /kuehlen") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /heizen") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /automatik") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /lueften") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /trocknen") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /lamellean") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /lamelleaus") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /luefterauto") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /luefter1") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /luefter2") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /luefter3") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /turboaus") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /turboan") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /ecoan") >= 0) {
              // irgendwas
            } else if (header.indexOf("GET /ecoaus") >= 0) {
              // irgendwas
            }

            client.println("innen:"+innen);
            client.println("aussen:"+aussen);
            client.println("an:"+an);
            client.println("turbo:"+turbo);
            client.println("eco:"+eco);
            client.println("soll:"+soll);
            client.println("lamelle:"+lamelle);
            client.println("modus:"+modus);
            client.println("luefter:"+luefter);

            break;
          } else { 
            currentLine = "";
          }
        } else if (c != '\r') {  
          currentLine += c;      
        }
      }
    }
    header = "";
    client.stop();
  }

}

Not only that there are no values put, to whole text is messed up. My output looks like this: "rbo: an: turbo: eco: s: lamelle: dus: uefter: " but only if s1.request_status(); is called. If not the text is put out normally.

Links2004 commented 3 years ago

Hi, the problem is that you use the add operation on a const char *, which effective is moving the pointer in to the memory around (not what you want).

the "some text" is no Arduino String its a const char *. try:

            client.println((String("innen:")+innen));
mathias172 commented 3 years ago

Interesting, thanks for help but I'm sorry, that was not the solution. Now my output is:

: eco: n: turbo: eco: ter: lamelle: s: ter: