Experiment: ESP32 HTTP vs MQTT

ESP32 menghantar data ke cloud melalui internet protocol sama ada melalui http atau mqtt. Saya mahu menguji perbezaan keupayaan dan kepantasan ESP32 menghantar data melalui kedua-dua medium ini.

Bagi menghantar data secara http (request), ESP32 menunggu sehingga menerima data semula (response). Jumlah masa http bagi proses request dan response ini diambil kira. Bagi menghantar data secara mqtt (publish), NodeRED akan menerima dan menghantar semula. Data selepas proses itu, ESP32 terima (subcribe). Jumlah masa mqtt bagi proses publish dan subcribe ini diambil kira.

Jumlah masa yang diperlukan bagi menghantar data melalui

  • http = 1,000 – 5,000ms
  • mqtt = 500 – 800ms

Kesimpulannya, saya simpulkan penghantaran data melalui MQTT lebih cepat berbanding HTTP bagi ESP32. Oleh itu, penggunaan MQTT akan meningkatkan prestasi projek.

Sebahagian source code http


  if (millis() - timer > 5000)
  {
    timer2 = millis();
    url = "https://docs.google.com/forms/d/e/xxxxxxxxxx/formResponse?usp=pp_url";
    url += "&entry.1501868945=" + String(altitude);
    url += "&entry.1668911952=" + String(pressure);
    url += "&entry.776237686=" + String(temperature);
    url += "&entry.767963295=" + String(humidity);
    http.begin(url);
    httpCode = http.GET();
    if (httpCode == 200)
    {
      doc.clear();
      doc["altitude"] = String(altitude);
      doc["pressure"] = String(pressure);
      doc["temperature"] = String(temperature);
      doc["humidity"] = String(humidity);
      message = "";
      serializeJson(doc, message);
      Serial.println(message);
      Serial.println("Time taken: " + String(millis() - timer2) + "ms");
      Serial.println("Time interval: " + String(millis() - timer3) + "ms");
      Serial.println();
      timer3 = millis();
    }
    timer = millis();
  }

Sebahagian source code mqtt


  if (millis() - timer > 5000)
  {
    timer2 = millis();
    doc.clear();
    doc["altitude"] = String(altitude);
    doc["pressure"] = String(pressure);
    doc["temperature"] = String(temperature);
    doc["humidity"] = String(humidity);
    message = "";
    serializeJson(doc, message);
    Serial.println(message);
    client.publish(topicOut.c_str(), message.c_str());
    timer = millis();
  }

void callback(char* topic, byte* payload, unsigned int length)
{
  temp = "";
  for (i = 0; i < length; i++) temp += (char)payload[i];
  Serial.println("Time taken: " + String(millis() - timer2) + "ms");
  Serial.println("Time interval: " + String(millis() - timer3) + "ms");
  Serial.println();
  timer3 = millis();
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.