0

Json, MQTT Adafruit.io
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Json, MQTT Adafruit.io

by cweiss on Sun Mar 17, 2019 2:25 am

Hi there!

I have successfully subscribed and published via MQTT using the Adafruit MQTT Library ESP8266 Example with the Arduino IDE. My next task is to publish my data in the form of json. I have a user interface that has 10 different counters (for simplicity lets say each block of data has 10 counters, counting days, hours, minutes etc elapsed) and for each block of data I want to publish those values (twice per day).

How can I make a json object that outputs the data like the Adafruit IO Location Example?

2019/03/16 11:09pmlocation24639.87143,-78.12575
2019/03/16 11:09pmlocation24739.86143,-78.10575
2019/03/16 11:09pmlocation24839.85143,-78.08575
2019/03/16 11:09pmlocation24939.84143,-78.06575
2019/03/16 11:10pmlocation25039.83143,-78.04575

I am pretty hung up reading the api and these forums, and would appreciate any help.

Cheers!!

Corey

cweiss
 
Posts: 6
Joined: Sat Oct 20, 2018 4:01 pm

Re: Json, MQTT Adafruit.io

by brubell on Mon Mar 18, 2019 10:42 am

Location data is stored as metadata with each data point in the feed (the value).

So, you would want to get the feed itself, and then parse out the data which you need. If you want the last recorded value of the feed, use the lastData API operation: https://io.adafruit.com/api/docs/#operation/lastData

The response will look something like this, and returned as JSON:

Code: Select all | TOGGLE FULL SIZE
{
  "id": "string",
  "value": "string",
  "feed_id": 0,
  "group_id": 0,
  "expiration": "string",
  "lat": 0,
  "lon": 0,
  "ele": 0,
  "completed_at": "string",
  "created_at": "string",
  "updated_at": "string",
  "created_epoch": 0
}


The values you are interested in are lat, lon, ele, and value.

brubell
 
Posts: 471
Joined: Fri Jul 17, 2015 10:33 pm

Re: Json, MQTT Adafruit.io

by cweiss on Mon Apr 22, 2019 12:11 pm

Hi Brubell,


sorry for the delayed reply, I was busy finishing finals. I also realize I wasn't clear with my question! I'm looking to make a json object that has 10 data points in it so when I use the lastData operation, the program sends the data and the output in adafruit.io is:

2019/03/16 counter1: 5 counter2: 18 counter3: 324 ... counter10=987
2019/03/17 counter1: 1 counter2: 181 counter3: 123 ... counter10=234

It seems from the documentation that only use the variables id, value, lat, lon ele, created_at etc. Is there a way to use variables such as counter1, counter2, counter3?

Is there any other way to send several data points in one batch?


Thanks again, I'm pretty new to this

cweiss
 
Posts: 6
Joined: Sat Oct 20, 2018 4:01 pm

Re: Json, MQTT Adafruit.io

by brubell on Thu Apr 25, 2019 10:01 am

It seems from the documentation that only use the variables id, value, lat, lon ele, created_at etc. Is there a way to use variables such as counter1, counter2, counter3?

You can't rename the data point ID, it's generated by Adafruit IO.

Is there any other way to send several data points in one batch?

Check out the batch data endpoint:
https://io.adafruit.com/api/docs/#opera ... CreateData

brubell
 
Posts: 471
Joined: Fri Jul 17, 2015 10:33 pm

Re: Json, MQTT Adafruit.io

by abachman on Thu Apr 25, 2019 11:01 am

Hi cweiss!


The value field will store and forward any string value shorter than 1KB (while feed history turned on, 100KB while feed history is turned off). Any data you can encode as a string--JSON, base64 encoded binary, numbers, etc.--can be stored.

If your counters are always in the same order, you could start with space separated numbers, which may be easier to encode/decode in your programs. I'm using a similar scheme to send multiple values to a single feed. The data visualized on this page: http://micavibe.com/sound is sound levels recorded at 100ms intervals and sent in batches of 30 values every 3 seconds. This page is an 8x6 grid (48 space separated ints) of motion capture data published every 2 seconds: http://micavibe.com/motion. You can see the actual data stream here: http://micavibe.com/about.

Adafruit IO won't do anything special with it when it's displaying on the IO website, but as long as you control the encoding end and the decoding end, you can store and retrieve any format you like. The only limit is that everything has to go in the value field, data points can only have one value and it's name is "value" :|


- Adam B.

abachman
 
Posts: 319
Joined: Mon Feb 01, 2010 12:48 pm

Re: Json, MQTT Adafruit.io

by cweiss on Tue Apr 30, 2019 5:20 pm

Hi abachman!

This is exactly what I was looking for!

cheers,

Corey

cweiss
 
Posts: 6
Joined: Sat Oct 20, 2018 4:01 pm

Please be positive and constructive with your questions and comments.