IOStash IoT Platform supports a wide range of connectivity options including MQTT, REST, Sockets and CoAP. It supports sending custom data to devices via simple POST requests which can be utilized to implement simple switches by sending ON or OFF conditions directly. These custom events can be subscribed via MQTT or Sockets. Raspberry Pi is an interesting platform. From NAS boxes to LED cubes, the versatility of the platform is limited by the imagination of the maker. This tutorial implements a simple connected smart bulb that utilizes the custom data publishing feature of IOStash. Aim of this tutorial is to show how easy it is to develop a connected application from scratch using IOStash and RPi.
Setting Up IOStash
Login to IOStash, go to Devices > Add New Device And fill in the details. Since We're dealing with a device with only one data point, which is the state of the switch, we need only one data point, 'state' - We can use this datapoint to post back the actions to IOStash after receiving custom data.
Once the device setup is complete, newly created device's page will be displayed
Note down the device ID of your new device. Grab the source code from the GitHub repo and update your credentials. The code is throughly commented and you'll find it easy to follow if you're already familiar with Python. Now, login to your Raspberry Pi and move the updated program to your RPi.
Install Paho MQTT Client Library for Python and run our tutorial program
sudo pip install paho-mqttsudo python iostash.py
You'll see the following output:
Connected, Reconnect -> 0Subscribed: 1
Now, our RPi has connected to IOStash and is listening for custom data via MQTT. Custom data events can be subscribed from the MQTT topic:
If we go through the code, we can see that we're expecting a msg field(Line number 37) in custom data, which will hold the command to turn on or off the bulb. The program will also write the state back to IOStash after toggling the GPIO pin.
Controlling The Bulb
We can now send our custom data containing the command to turn ON/OFF the bulb. We can use Postman Chrome App to send POST request to publish custom data. Data to be sent is to be included in the body of the request as form/url-encoded. The url to send POST request is:
Here, we've added msg field and set its value as 1. Clicking Send will publish the data to your Raspberry Pi and you'll see something like this on the console:
You'll also notice that the LED attached to the Breadboard will turn ON or OFF according to the value of msg field. If you open the device page, you'll see the graph jumping between high and low according to the msg field.
Its easy to write an Android or iOS app that can send POST requests to your device, making it possible to control your bulb via an App. Custom data publishing is a useful feature that can be utilized to do more.
Program source code
Did you replicate this project? Share it!I made one
Love this project? Think it could be improved? Tell us what you think!