Chat freely about anything...

User avatar
By juan3211
#61903 Thanks a lot @torntrousers

I will re-write my sketch. But also, with QOS in MQTT broker of "0" you can ensure that the information is received, isn't it ? Or am I wrong again ?

Another thing will be MQTT with QOS of 1 or 2.

What do you think ?

Regards

NOTE: Thanks a lot for WIFI.begin's stuff. In this sketch it is not necessary as I only want to check the difference between two protocols.

torntrousers wrote:A couple of comments on that sketch -

On the HTTP side it doesn't wait to receive the http response, it does the "client.print(String("GET ")..." and then client.stop(). I'm a bit surprised thats even working and the sent data is actually getting stored in ThingSpeak. If the connection is closed before ThingSpeak gets to sending the HTTP 200 OK response then the ThingSpeak sever may well just discard the request if it hasn't processed it yet.

Another thing, not related to this HTTP/MQTT comparison, its always doing WiFi.begin(ssid, password), I've heard (though not tested) that that is slower than just letting the ESP auto connect to the previously used SSID, which is why you often see code doing:

if (strcmp (WiFi.SSID().c_str(), ssid) != 0) {
WiFi.begin(ssid, password);
}

HTH.

EDIT: Hope thats not sounding negative, I think its interesting what you're doing, just pointing out its not timing the complete http request-response.
User avatar
By GengusKahn
#61913 Hi there, I have been using this, I have a twitter version running for over a week (So Far) on an 18650 LiPo...

I have not included my sensor setup to allow you to customise.....
Code: Select all#include "ESP8266WiFi.h"


    char ssid[] = "YourSSID";     //  your network SSID (name)
    char pass[] = "YourPASSWD";   //  your network password
    WiFiClient  client;
float pfHum   = 44.9;
float pfTemp  = 22.9;
float pfHum1  = 44.9;
float pfTemp1 = 22.9;
float pfVcC   = 3.49;
   
const char* hostts = "api.thingspeak.com";
const char* thingspeak_key = "YourWriteKey";

void setup() {
      WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
  }
}

void loop() {
 // Use WiFiClient class to create Thingspeak Post
  WiFiClient client;
  if (!client.connect(hostts, 80)) {
    return;
  }
  //Construct the post...
  String url = "/update?key=";
  url += thingspeak_key;
  url += "&field1=";
  url += pfTemp;
  url += "&field2=";
  url += pfHum;
  url += "&field3=";
  url += pfTemp1;
  url += "&field4=";
  url += pfHum1; 
  url += "&field5=";
  url += pfVcC/1000;
 
// This will send the Thingspeak Post request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + hostts + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(50);
  client.stop();
ESP.deepSleep(3600000000, WAKE_RF_DEFAULT);  // Sleep for 60 Minutes
}


User avatar
By tomte76
#61926 Acutally my own experience is, that MQTT has a big advantage if you want to control things. As you always need to poll using HTTP(S) you always have a delay. Also the implementation of HTTPS seems to be more mature then TLS with MQTT. I still have some issues to get MQTT/TLS runnning smoothly. So for all my sensors which don't need a back-channel I use HTTPS with TLS v1.2 to send data to an InfluxDB. Then using Grafana and Kapacitor to visualize data or trigger actions. My battery-powered sensors last at least a month with a 1000 mAh Lio-Ion battery taken from a mobile phone (15 min deepsleep interval). Then they can be recharged using a micro usb power supply.

If I need a backchannel, then I prefer MQTT as it is near realtime. But it does not make sense to have these devices battery powered as they need to be "always on" to trigger actions in time. So if you want to switch e.g. a light bulp you won't wait for some time until it wakes up and does something :D To stick both together I use my own code in Perl oder python or Node-Red for "visual development" ;)