Skip to main content
Improved formatting
Source Link
MatsK
  • 1.4k
  • 1
  • 11
  • 25

Scenario: ExistingScenario: Existing project: read soft serial then do http post New project:

New project: MQTT player from other (MrDIY / MrDIY Audio Notifier · GitLab)

Objective:Objective: Combine together (Result: successful )

Issue:Issue: When read serial do http post, after http.end(). MQTT client disconnect. It taks above 10 secs to reconnect.

The only way I found is just a workaround to add ESP.restart(); after each http post. But

But that's stupid. I have spend more than 10 hours to fix this issue. Any one can help. Apperaciate and great thanks.?

Eg code:

void loop() {
  if (!mqttClient.connected()) {
    mqttReconnect();
  }
  mqttClient.loop();

  if (mySerial.available() > 0)  //Checks is there any data in buffer
  {
    receive_data = String(receive_data) + String(char(mySerial.read()));
    //Serial.println(receive_data);

    if (String(receive_data).indexOf("lighton") >= 0) {
      Serial.println("lighton");
      receive_data = "";
      http.begin(client, switchon);
      http.addHeader("Content-Type", "application/json");
      http.addHeader("Authorization", auth);
      int httpCode = http.POST("{\"entity_id\": \"switch.sonoff_guest_room\"}");
      Serial.printf("[http] GET... code: %d\n", httpCode);
      http.end();
      //ESP.restart(); 
    }
}

Scenario: Existing project: read soft serial then do http post New project: MQTT player from other (MrDIY / MrDIY Audio Notifier · GitLab)

Objective: Combine together (Result: successful )

Issue: When read serial do http post, after http.end(). MQTT client disconnect. It taks above 10 secs to reconnect.

The only way I found is just a workaround to add ESP.restart(); after each http post. But that's stupid. I have spend more than 10 hours to fix this issue. Any one can help. Apperaciate and great thanks.

Eg code:

void loop() {
  if (!mqttClient.connected()) {
    mqttReconnect();
  }
  mqttClient.loop();

  if (mySerial.available() > 0)  //Checks is there any data in buffer
  {
    receive_data = String(receive_data) + String(char(mySerial.read()));
    //Serial.println(receive_data);

    if (String(receive_data).indexOf("lighton") >= 0) {
      Serial.println("lighton");
      receive_data = "";
      http.begin(client, switchon);
      http.addHeader("Content-Type", "application/json");
      http.addHeader("Authorization", auth);
      int httpCode = http.POST("{\"entity_id\": \"switch.sonoff_guest_room\"}");
      Serial.printf("[http] GET... code: %d\n", httpCode);
      http.end();
      //ESP.restart(); 
    }
}

Scenario: Existing project: read soft serial then do http post

New project: MQTT player from other (MrDIY / MrDIY Audio Notifier · GitLab)

Objective: Combine together (Result: successful )

Issue: When read serial do http post, after http.end(). MQTT client disconnect. It taks above 10 secs to reconnect.

The only way I found is just a workaround to add ESP.restart(); after each http post.

But that's stupid. I have spend more than 10 hours to fix this issue. Any one can help?

Eg code:

void loop() {
  if (!mqttClient.connected()) {
    mqttReconnect();
  }
  mqttClient.loop();

  if (mySerial.available() > 0)  //Checks is there any data in buffer
  {
    receive_data = String(receive_data) + String(char(mySerial.read()));
    //Serial.println(receive_data);

    if (String(receive_data).indexOf("lighton") >= 0) {
      Serial.println("lighton");
      receive_data = "";
      http.begin(client, switchon);
      http.addHeader("Content-Type", "application/json");
      http.addHeader("Authorization", auth);
      int httpCode = http.POST("{\"entity_id\": \"switch.sonoff_guest_room\"}");
      Serial.printf("[http] GET... code: %d\n", httpCode);
      http.end();
      //ESP.restart(); 
    }
}
Source Link
Rex
  • 11
  • 1

ESP8266 How to run MQTT Client and Http Client in one loop

Scenario: Existing project: read soft serial then do http post New project: MQTT player from other (MrDIY / MrDIY Audio Notifier · GitLab)

Objective: Combine together (Result: successful )

Issue: When read serial do http post, after http.end(). MQTT client disconnect. It taks above 10 secs to reconnect.

The only way I found is just a workaround to add ESP.restart(); after each http post. But that's stupid. I have spend more than 10 hours to fix this issue. Any one can help. Apperaciate and great thanks.

Eg code:

void loop() {
  if (!mqttClient.connected()) {
    mqttReconnect();
  }
  mqttClient.loop();

  if (mySerial.available() > 0)  //Checks is there any data in buffer
  {
    receive_data = String(receive_data) + String(char(mySerial.read()));
    //Serial.println(receive_data);

    if (String(receive_data).indexOf("lighton") >= 0) {
      Serial.println("lighton");
      receive_data = "";
      http.begin(client, switchon);
      http.addHeader("Content-Type", "application/json");
      http.addHeader("Authorization", auth);
      int httpCode = http.POST("{\"entity_id\": \"switch.sonoff_guest_room\"}");
      Serial.printf("[http] GET... code: %d\n", httpCode);
      http.end();
      //ESP.restart(); 
    }
}