0

seems I have some issues in my code as I subscribe for specific mqtt topic and also read same value via serial and publish it to mqtt. It takes time for device connected to serial to change setting and I'am reading its state to quickly. How to optimize code that during subscribed topic value change serial read would be susspended or specific value data byte read ignored for some time. Main code parts:

    const char* mqtt_topic_base = "panasonic_heat_pump/sdc";
    const char* mqtt_set_quiet_mode_topic = 

"panasonic_heat_pump/SetQuietMode";
        byte inCheck = 0;

    #define MAXDATASIZE 256
    char data[MAXDATASIZE];
    int data_length = 0;

    // log message to sprintf to
    char log_msg[256];

    // mqtt topic to sprintf and then publish to
    char mqtt_topic[256];

    // mqtt
    WiFiClient mqtt_wifi_client;
    PubSubClient mqtt_client(mqtt_wifi_client);

    void send_command(byte* command, int length)
    {  


      int bytesSent = Serial.write(command, length);
      sprintf(log_msg, "sent bytes    : %d", bytesSent); log_message(log_msg);

      // wait until the serial buffer is filled with the replies
      delay(1000);

      // read the serial
      readSerial();

      sprintf(log_msg, "received size : %d", data_length); log_message(log_msg);
    }

    // Callback function that is called when a message has been pushed to one of your topics.
    void mqtt_callback(char* topic, byte* payload, unsigned int length) {
      char msg[length + 1];
      for (int i=0; i < length; i++) {
        msg[i] = (char)payload[i];
      }
      msg[length] = '\0';
    // set 0 for Off mode, set 1 for Quiet mode 1, set 2 for Quiet mode 2, set 3 for Quiet mode 3
        if (strcmp(topic, mqtt_set_quiet_mode_topic) == 0)
         {
        String set_quiet_mode_string(msg);
        int quiet_mode = (set_quiet_mode_string.toInt() + 1) * 8; 
        int checksum = 512 - (241 + 108 + 1 + 16 + quiet_mode);

        sprintf(log_msg, "set Quiet mode to %d", quiet_mode / 8 - 1); log_message(log_msg);
        byte command[] = {0xf1, 0x6c, 0x01, 0x10, 0x00, 0x00, 0x00, quiet_mode, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, checksum};
        send_command(command, sizeof(command));
         }

     if (strcmp(topic, mqtt_topic_base) == 0)
      {    
        log_message("Updating..");

        update_everything();
      }
    }

    void update_everything()
    {

      get_heatpump_data();

    }

    void get_heatpump_data() {  
        byte command[] = {0x71, 0x6c, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12};
        send_command(command, sizeof(command));

        int quiet_mode_state = (int)(data[7]);

        char* quiet_mode_state_string;
        if (quiet_mode_state == 73) {
          quiet_mode_state_string = "0";
        } else if (quiet_mode_state == 81) {
          quiet_mode_state_string = "1";
        } else if (quiet_mode_state == 89) {
          quiet_mode_state_string = "2";
        } else if (quiet_mode_state == 97) {
          quiet_mode_state_string = "3";
        } else if (quiet_mode_state == 73) {
          powerfull_mode_state_string = "OFF";
        } else if (quiet_mode_state == 74) {
          powerfull_mode_state_string = "30min";
        } else if (quiet_mode_state == 75) {
          powerfull_mode_state_string = "60min";
        } else if (quiet_mode_state == 76) {
          powerfull_mode_state_string = "90min";
        } else {
          quiet_mode_state_string = "Unknown"; 
        }
        sprintf(log_msg, "received quiet mode state : %d (%s)", quiet_mode_state, quiet_mode_state_string); log_message(log_msg);
        sprintf(log_msg, "powerfull mode state : %d (%s)", quiet_mode_state, powerfull_mode_state_string); log_message(log_msg);

        sprintf(mqtt_topic, "%s/%s", mqtt_topic_base, "quiet_mode_state"); mqtt_client.publish(mqtt_topic, quiet_mode_state_string);
        sprintf(mqtt_topic, "%s/%s", mqtt_topic_base, "powerfull_mode_state"); mqtt_client.publish(mqtt_topic, powerfull_mode_state_string);

    }

    void loop() {
      // Handle OTA first.
      ArduinoOTA.handle();

      if (!mqtt_client.connected())
      {
        mqtt_reconnect();
      }
      mqtt_client.loop();

      update_everything();
      delay(5000);
    }


    void readSerial()
    {
        if(Serial.available() > 0){

          log_message("serial available");
        Serial.readBytes(data, 203);
        while(Serial.available()){
          delay(2);
          Serial.read();
        }

      }
    }
  • set the used value from the published mqtt, not from the internal value. it fires an event anyway, right? this pattern vastly simplifies reconciling values, at the slight cost of model update delay time, which often isn't strictly required anyway. – dandavis Dec 11 '19 at 18:28
  • Any example how to do?? – Vaikis2006 Dec 11 '19 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.