Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Truncating feeds?
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Truncating feeds?

by msk on Thu Mar 10, 2016 11:44 pm

IFTTT is setting one of my fields to the time of sunset every day as a date/timestamp like:
March 10, 2016 at 05:58PM

When my sketch gets that field, it truncates it to the first 19 characters of the string in the feed. Is it possible to get the entire contents of the feed?

Thanks-
--MSK

msk
 
Posts: 11
Joined: Tue Sep 10, 2013 9:57 pm

Re: Truncating feeds?

by franklin97355 on Fri Mar 11, 2016 2:33 am

Post your sketch and we will check.

franklin97355
 
Posts: 21498
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Truncating feeds?

by msk on Fri Mar 11, 2016 11:46 am

The meat of the code is from the example sketch. THANKS for looking.


Code: Select all | TOGGLE FULL SIZE

/***************************************************
  Adafruit MQTT Library ESP8266 Example

  Must use ESP8266 Arduino from:
    https://github.com/esp8266/Arduino

  Works great with Adafruit's Huzzah ESP board & Feather
  ----> https://www.adafruit.com/product/2471
  ----> https://www.adafruit.com/products/2821

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Tony DiCola for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 ****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "MSK2"
#define WLAN_PASS       "XXXXX"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "xxxxx"
#define AIO_KEY         "XXXXX"
#define LED 0

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Store the MQTT server, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM    = AIO_SERVER;
const char MQTT_USERNAME[] PROGMEM  = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM  = AIO_KEY;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);

/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);

// Setup a feed called 'sunrise' for subscribing to changes.
const char SRISE_FEED[] PROGMEM = AIO_USERNAME "/feeds/srise";
Adafruit_MQTT_Subscribe srise = Adafruit_MQTT_Subscribe(&mqtt, SRISE_FEED);

// Setup a feed called 'sunset' for subscribing to changes.
const char SSET_FEED[] PROGMEM = AIO_USERNAME "/feeds/sset";
Adafruit_MQTT_Subscribe sset = Adafruit_MQTT_Subscribe(&mqtt, SSET_FEED);

/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  delay(10);

  Serial.println(F("Adafruit MQTT demo"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());

  // Setup MQTT subscription for feeds.
  mqtt.subscribe(&srise);
  mqtt.subscribe(&sset);

}

uint32_t x=0;

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  // this is our 'wait for incoming subscription packets' busy subloop
  // try to spend your time here

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(1000))) {


    if (subscription == &srise) {
      Serial.println(F("Got SUNRISE: "));
      Serial.println((char *)srise.lastread);
    }
   
    if (subscription == &sset) {
      Serial.println(F("Got SUNSET: "));
      Serial.println((char *)sset.lastread);
    }
   
  }



  // Now we can publish stuff!
  Serial.print(F("\nSending photocell val "));
  Serial.print(x);
  Serial.print("...");
  if (! photocell.publish(x+=1)) {
    Serial.println(F("Failed"));
  } else {
    Serial.println(F("OK!"));
  }



  // ping the server to keep the mqtt connection alive
  // NOT required if you are publishing once every KEEPALIVE seconds
  /*
  if(! mqtt.ping()) {
    mqtt.disconnect();
  }
  */
 
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}


msk
 
Posts: 11
Joined: Tue Sep 10, 2013 9:57 pm

Re: Truncating feeds?

by marke3 on Sat Mar 12, 2016 6:34 pm

The library truncates to 19 characters (SUBSCRIPTIONDATALEN - 1)

Also, there's a limit to the size of the message. Since some people are using this library with small microcontrollers, we set the default to 20 bytes. If you want to, say, pass around twitter messages or chucks of binary data you'll want to expand this.
You can adjust it in Adafruit_MQTT.h
Copy Code
// how much data we save in a subscription object
// eg max-subscription-payload-size
#define SUBSCRIPTIONDATALEN 20

marke3
 
Posts: 200
Joined: Sat Feb 08, 2014 5:24 pm

Re: Truncating feeds?

by msk on Sun Mar 13, 2016 1:36 am

Awesome! Thanks.

I set SUBSCRIPTIONDATALEN to 100.

While I was digging around I also changed MAXSUBSCRIPTIONS to 8.'

msk
 
Posts: 11
Joined: Tue Sep 10, 2013 9:57 pm

Re: Truncating feeds?

by HowardP on Tue Apr 05, 2016 3:24 pm

Leads to the general question: what is the max size of an adafruit.io MQTT payload ?

(And, Whoa! Amazon's AWS is a whopping 128 k bytes (1) )

I'm guessing here, because Microcontrollers and sensors are mainly in mind, that max might be more like < 128 bytes ?
This seems to be so, from the adafruit_MQTT_library.h file (2) mentioned immediately above, we have:

Code: Select all | TOGGLE FULL SIZE
#define MAXBUFFERSIZE (125)

And, as mentioned
Code: Select all | TOGGLE FULL SIZE
#define SUBSCRIPTIONDATALEN 20

Which is certainly fine for small sensor stuff … BTW, it might be good to edit that header and mention the max size (or say it more clearly).

It's been a long time since I worked with MQ stuff on Big Irons; I am working on an remote sensor-based application that needs to send a payload of 512 bytes, but only twice per day ... so the message is big, but the bandwidth is minimal. Would it be difficult to daisy chain messages, say, by embedding a timestamp, generated by the device (sender) subscriber's code, not the MQTT gateway (IoT server), then have the application (recipient) subscriber's code reconstruct and merge the messages into one bigger one?

Thanks!
- Howard in Florida
(PS This is wonderful stuff you all have accomplished !)

__________________
REFs
(1) http://docs.aws.amazon.com/iot/latest/developerguide/iot-limits.html
(2) https://github.com/adafruit/Adafruit_MQTT_Library/blob/master/Adafruit_MQTT.h

HowardP
 
Posts: 91
Joined: Fri Apr 03, 2015 6:42 pm

Please be positive and constructive with your questions and comments.