Jump to content









Photo

Publishing a variable to data stream via MQTT

MQTT Arduino ESP8266

  • Please log in to reply
1 reply to this topic

#1 kahlkritz

kahlkritz

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 01 December 2017 - 08:34 AM

Good day all

 

I followed the tutorial to publish sample data from an Arduino to the Carriots data stream via a MQTT broker. My issue has for a while now been that I can't seem to publish any data other than the sample data used in the tutorial. I would like to publish a variable measured by my ESP8266 (basically an Arduino Wi-Fi module), but everything I try, text or value, does not get published, even though the feedback given on the serial console says it has been published.

 

I suspect it has something to do with the string length that I am trying to publish, since I can post a string value as long as it is the same amount of characters as the sample data. Anything with more characters or less characters does not get published. It seems like such a trivial issue but I have tried just about everything.

I've altered the frame_json string as well as the data_json string. I've also substituted headers and payloads used in the http examples and that also did not work.

 

Thanks in advance

 

 

 



#2 jpastor

jpastor

    Development leader

  • Administrators
  • 178 posts

Posted 26 December 2017 - 11:46 AM

Hi kahlkritz

If you share your code with us, maybe we can review it.
However, his problem seems related to the limitation that the "wifi.h" library has in the information to be sent (80 characters). To work properly, it is necessary to divide the string and send several strings

An example could be something like:

 

void sendStream()
{
  //const int httpPort = 80;
  if (client.connect(server, 80)) {   // If there's a successful connection
    Serial.println(F("connected"));

    // construct the data json
    String json = "{\"protocol\":\"v2\",\"device\":\"" + DEVICE + "\",\"at\":\"now\",";
    String json2 = "\"data\":{\"yourparameter\":\"" + String(val) + "\"}}";

    // Make an HTTP request
    client.println("POST /streams HTTP/1.1");
    client.println("Host: api.carriots.com");
    client.println("Accept: application/json");
    client.println("User-Agent: Arduino-Carriots");
    client.println("Content-Type: application/json");
    client.print("carriots.apikey: ");
    client.println(APIKEY);
    client.print("Content-Length: ");
    int thisLength = json.length()+json2.length();
    client.println(thisLength);
    client.println("Connection: close");
    client.println();
    client.print(json);
    client.println(json2);
  }

Hope it helps!







Also tagged with one or more of these keywords: MQTT, Arduino, ESP8266

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users