Previous posts:
In the Air Design Challenge - Pollen & Allergen Sensing
In the Air Design Challenge - Pollen & Allergen Sensing – Post 1 (Pollen Sensor)
In the Air Design Challenge - Pollen & Allergen Sensing – Post 2
Since I spent a lot of time on trying to get notifications from AirVantage when new values are published from BBB (in my case just a Paho MQTT client) I decided to dedicate a short post to AirVantage topic.
Wrong approach
At first, I was trying to subscribe to a topic on AirVantage MQTT and receive messages whenever BBB publishes new sensor values. I tried with a couple of different test clients and a couple of topics but no success. I tried using MQTT wildcards (# and +) but still - nothing arrived from AirVantage.
I read some AirVantage docs and find out that I could use REST API to read published data but I was under impression that I should only use MQTT (for some strange reason )
After a few hours of failed attempts I posted a question (Subscription to AirVantage MQTT topic) and soon I got a couple of replies. Some of them were from dlahay who was kind enough to answer all my questions and soon I got my app working…
Correct approach
The correct way to use AirVantage is the following:
- Use MQTT to publish new data messages like sensor readings (to topic<SERIAL>/messages/json) and subscribe to a topic (<SERIAL>/tasks/json) to receive commands and settings updates. Code sample written in C for publishing data is provided in my previous post. Check API documentation to see how commands can be sent to subscribed clients.
- On the remote clients (like mobile phones and computers) use REST API to get published data. For example, you can get historical data for the last 3 months.
REST API is well documented and can be found here: https://na.airvantage.net/develop/apiDocumentation/apiDocumentation?page=API+-+System+v1.html
The first thing you have to do to start using REST API is to obtain a valid Access Token. There are three ways to get this token and they are described in documentation too.
- Resource owner for really trusted application
- Authorization code for server-side application
- Implicit for client-side application
I chose the first one because it’s the easy to implement and fits my needs.
It’s as simple as making a HTTPS request to:
https://na.airvantage.net/api/oauth/token?grant_type=password&username=YOUR@EMAIL.COM&password=YOURPA$$WORD&client_id=CLIENT_ID&client_secret=CLIENT_SECRET
Where:
- YOUR@EMAIL.COM is the email you use to login to AirVantage
- YOURPA$$WORD is your AirVantage password
- CLIENT_ID is your API Client ID
- CLIENT_SECRET is the API Client Secret
You obviously need to have an API Client in order to fill in these parameters. If you don’t have one, you need to create it: go to AirVantage management console and there navigate to Develop -> API Clients and then click Create (Name is the only mandatory parameter). After you create a client you will have your Client ID and corresponding Secret Key.
In response to the above HTTPS request you will receive a JSON string which, among other values like access rights and token type, contains your Access Token that you can use to perform REST API calls. This token is valid for 1 day (86399s to be exact).
One more thing! Many REST API calls take system UID as a parameter, at least those that I require for my application. You can get your system's UID by clicking on System name in the Systems table under Inventory. Make sure not to click the system's serial in that table like I did the first time I tried to get the UID - this will take you to the linked Gateway instead.
I hope this will save time to others who are new to AirVantage or MQTT like I am.
Cheers,
Dragan
Top Comments