IOT APIs For Beginners -Working Notes

IOT-apis-iconToday almost all web services like Facebook,Twitter etc have APIs that enable developers to create applications that interact with the platform.

But What exactly is an API (Application Programming Interface)?

An API is an interface used by programs to access an application.

It allows a program to send commands to another programs and receive responses back from that application.

Basically a normal interface (user Interface) allows a person to use an application/service whereas an Application Interface allows a computer/program to use an application/service.

Many of the APIs today are Web API’s and are designed to use the HTTP protocol.

Many are also Rest (representational State Transfer) APIs .Rest is a design architecture and doesn’t really concern us as we will be using them and not designing them, however for more details see What is REST.

Although http based APIs are common on the web MQTT is becoming the main IOT protocol, and we will likely see a large growth of MQTT based APIs.

All of the main IOT providers like Amazon,Azure and IBM provide both http and MQTT APIs to access their services.

API Basics

Regardless of the protocol all APIs require Endpoints.

An API Endpoint is defined as the end of a communications channel. This is where an application sends requests to, and receives responses from.

In http endpoints are provided by urls and in MQTT by topics.

Using IOT APIs

Although IOT is still new their are many IOT APIs available however there aren’t any simple online testing facilities available for learning to use IOT APIs.

For this reason I am developing a very simple http and MQTT API on node red which will be available as a flow to download, and if there is enough Interest I will also make it available online as a service.

Introducing Our Home Made IOT API

Our API will be used for controlling three simulated IOT devices.

  • A switch
  • A light Bulb
  • A temperature and humidity sensor.

Commands available in the current version:

  • Switch – Get status,turn on/off,set timer on/off
  • Bulb – Get status,turn on/off,change colour
  • Sensor – Get status,turn on/off

Note: The flow is still development grateful that you report and bugs

Quick Usage Guide

The devices can be controlled using HTTP and MQTT.

For HTTP the complete url is

http://localhost:1880/myapi/switch1
http://localhost:1880/myapi/bulb1
http://localhost:1880/myapi/sensor1

You can control the devices using a get request which you can do with a web browser.

To turn the switch on/off use (partial urls):

myapi/switch1?state=off or myapi/switch1?state=on

See –Using HTTP based IOT APIs

For MQTT you can use the mosquitto_pub and sub tool or the Lens extension for chrome.

Use topics:
myapi/command/device and myapi/response/device
the commands uses JSON encoded data:

Example: to turn the switch on/off use:

mosquitto_pub -h 192.168.1.77 -t myapi/command/switch1 -m "{\"state\":\"on\"}"

The response should look like this:

mos-sub-mqtt-api-test

 

I am currently creating a detailed guide and will link it here shortly

Feedback

This is currently a work in progress and I would welcome any feedback to improve the tutorial and API demo flow.

Would You Prefer an Online version?

View Results

Loading ... Loading ...

Related Tutorials

 

Please rate? And use Comments to let me know more
[Total: 1    Average: 5/5]

Leave a Reply

Your email address will not be published. Required fields are marked *