MQTT-Simple IOT Sunlight Sensing Raspberry Pi Project- SunIOT – Part 4

SunIOT

MQTT- Simple IOT Sunlight Sensing Raspberry Pi Project- SunIOT – Part 4figure-1

PubNub Tutorial and Freeboard Tutorial

[Updated to PubNub 4.02 as of November 21, 2016]

Adding an IOT Dashboard with PubNub and Freeboard

We just have added a new product, a Grove Sunlight Sensor to our lineup and wanted to build a project using it and MQTT.   We are intending to addimg_8676 drivers for this sensing device to three of our product lines:  OurWeather, the GroveWeatherPi and the WXLink wireless data transmission products.  Since we are going to be using the Grove Sunlight Sensor so extensively, we thought we should build a quick example project.

SunIOT was thus born.   We will be building this project in four postings.  FIrst the description of the project (Part 1).  Secondly, we get the hardware up and connected , Python connected to both of the Grove devices (Part 2), Part 3 had us connect up to initialstate.com and then in our final Part 4, we  use MQTT to connect to a dashboard, freeboard.io (and pubnub.com).

[include-page id=”SunIOTLinks”]

What is SunIOT?

The overall design of the project is simple.   We use the Raspberry Pi to read  a sensor that measures sunlight and breaks the sunlight down into three components (UV, IR and Visible). We also use an LED to provide visual feedback that a sample is being taken (two blinks) and that the process is still running (one blink).

figure-2The general idea of SunIOT is to build a software platform to build more complex IOT sensors.   In this column and the next, we will be going through a complete, albeit simple, IOT design.   As well as providing a test bed for the new Grove Sunlight Sensor.

PubNub and Freeboard

We are now going to set up an IOT Dashboard using Pubnub and freeboard as shown in the block diagram below.

PubNub is a free (at the base level) MQTT broker.  It implements a publish/subscribe system for real time data streams.   It is a very impressive service and tools and can grow with you as you develop either more complex applications or even products.

freeboard.io is a free (at the base level) dashboard application.  You can build real-time, interactive dashboards and visualizations in minutes using the reasonably intuitive drag & drop interface.   It’s not quite as pretty as the initialstate dashboard in Part 3, but it has an amazing amount of functionality under the hood and is open source so you can build your own blocks and modules for it.

img_1188

Introduction to MQTT

MQTT is a publish-subscribe based “light weight” messaging protocol for use on top of the TCP/IP protocol, such as the WiFi packets that we are using in this project. It is designed for connections with remote locations where a “small code footprint” is required or the network bandwidth is limited.  The publish-subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message.

mqttorg-glowPublish–subscribe is a pattern where senders of messages, called publishers (in this case our ESP8266 is the publisher), don’t program the messages to be sent directly to  subscribers, but instead characterize message payloads into classes without the specific knowledge of which subscribers the messages are sent to.   Similarly, subscriberswill only receive messages that are of interest without specific knowledge of which publishers there are.  Mosquitto operates as the broker in this system and routes the published data to the appropriate subscribers.

You can think of MQTT as writing stories for a newspaper where you don’t know who will be subscribing to the article.

JSON Data Payload

JSON  is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is the primary data format logo-jsonused for asynchronous browser/server communication, largely replacing XML.  XML is a “heavier” protocol that is also hierarchical in nature, but with a great deal more redundancy that JSON.  Yes, there are class wars going on for people that advocate JSON over XML, but in todays world of higher speed communication, it rarely matters.  You can make the argument that the higher data density of JSON is a better choice for IOT applications.

Here is an example of the data packet we are using in the SunIOT code in JSON for the LightSwarm data payload:

{ 
      SunIOT_Visible: 260, 
      SunIOT_IR: 257, 
      SunIOT_UVIndex: 0.020000 
}

The foundation of PubNub is providing you the ability to integrate scalable, realtime data streams into your applications. Using the publish/subscribe paradigm, subscribers to a particular channel will receive any and all messages that are published to that channel. It doesn’t matter if there is one subscriber, 10 subscribers, 1000 subscribers or millions of subscribers, a published message will be delivered to all of those subscribers on that channel in less than ¼ second1.

 

Setting up an Account at PubNub

Setup a free account at pubnub.com.   Make sure you write down your password.   Now let’s setup the MQTT info to connect up SunIOT.

Step 1) Create a new App by clicking on “NEW APP +”..   Type in SunIOT in the “enter a new app” filed and hit the “CREATE NEW APP +” button.  You should see this:

screen-shot-2016-11-07-at-4-39-06-pm

Step 2) Click on the SunIOT App page.   The enter “SunIOT” in the “Enter a New Keyset Name” field and hit the “CREATE NEW KEYSET +” button.   That will get you a page that looks like this.   Don’t worry about the application add-ons for this page.  Those are all for more advanced (and cool) usage.   Note the keys below will not work.   They have all been disabled.  You need to set up your own.

screen-shot-2016-11-07-at-4-41-25-pm

Next, let’s set up the SunIOT_PubHub Python Software

Setting up the SunIOT Software (Updated for PubNub 4.02 November 2016)

Go to github.com and clone the SunIOT_PubHub repository.SunIOT

git clone https://github.com/switchdoclabs/SunIOT_PubHub.git

Set up the additional libraries needed.

Make sure you installed I2C as in this link:

https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c

 

sudo pip install setuptools --upgrade

sudo pip install apscheduler

sudo pip install pubnub

Open up the SunIOT_PubHub.py file and edit the following lines

# configuration
Pubnub_Publish_Key = "pub-c-xxxxxx"
Pubnub_Subscribe_Key = "sub-c-xxxxxx"

Placing the Publish Key and Subscribe Key into the configuration strings above.   It will then look something like this:img_3442

# configuration
Pubnub_Publish_Key = "pub-c-e77e14eb-2bb1-4f58-8ac7-cf4d75c46370"
Pubnub_Subscribe_Key = "sub-c-10e3de76-a54c-11e6-8bfd-0619f8945a4f"

Here are the three pieces of data we will be publishing:

– SunIOT_Visible
– SunIOT_IR
– SunIOT_UVIndex

And these will be published to the channel “SunIOT_Sunlight”.

   
def publish_callback(result, status):
        print "status.is_error", status.is_error()
        print "status.original_response", status.original_response
        pass
        # handle publish result, status always present, result if successful
        # status.isError to see if error happened


def publishToPubNub():

        vis = sensor.readVisible()
        IR = sensor.readIR()
        UV = sensor.readUV()
        uvIndex = UV / 100.0
        print('Publishing Data to PubNub time: %s' % datetime.now())
        print '         Vis:             ' + str(vis)
        print '         IR:              ' + str(IR)
        print '         UV Index:        ' + str(uvIndex)


        myMessage = '{ SunIOT_Visible: %d, SunIOT_IR: %d, SunIOT_UVIndex: %f }' % (vis, IR, uvIndex)
        pubnub.publish().channel('SunIOT_Sunlight').message(myMessage).async(publish_callback)     

Now start the SunIOT_PubHub program running.

sudo python SunIOT_PubHub.py

If you get the error:

"ImportError: No module named Adafruit_PureIO.smbus"

Try the following:

git clone https://github.com/adafruit/Adafruit_Python_PureIO.git
cd Adafruit_Python_PureIO
sudo python setup.py install

If everything is hooked up right with PubHub, you should see something like this:

Publishing Data to PubHub time: 2016-11-05 20:18:50.614003
		Vis:             261
		IR:              253
		UV Index:        0.02
[1, u'Sent', u'14784023306510771']
Publishing Data to PubHub time: 2016-11-05 20:18:52.613497
		Vis:             261
		IR:              254
		UV Index:        0.02
[1, u'Sent', u'14784023326454960']

 

If you want to subscribe to your own data from PubHub, open another terminal wind on the Pi, configure listen.py with the same keys above and start listen.py

sudo python listen.py

Setting up an Account at Freeboard

Now we have all the data that we want being published to the PubNub MQTT broker, now it is time to set up an account at freeboard.io.

Step 1) Set up your free account at freeboard.io

Step 2) Go to My Freeboards and create a new Freeboard called SunIOT.   Click on the name of SunIOT to begin building your IOT Dashboard.

screen-shot-2016-11-09-at-8-26-27-am

The JSON for this dashboard is below. It is included in the GitHub repository. Being able to import dashboards is critical to being able to distribute kits and repositories for projects like this. You can either build your own dashboard following tutorial or copy and paste the following JSON into a file called “dashboard.json” and importing it from the Freeboard site. You will have to change your subscribe key. This one won’t work.

{"version":1,"allow_edit":true,"plugins":["/plugins/all"],"panes":[{"width":1,"row":{"3":1,"4":1,"5":1},"col":{"3":1,"4":2,"5":2},"col_width":1,"widgets":[{"type":"gauge_widget","settings":{"title":"Visible","value":"datasources[\"SunIOT\"][\"SunIOT_Visible\"]","min_value":0,"max_value":"1000"}}]},{"width":1,"row":{"3":1,"4":1,"5":1},"col":{"3":2,"4":3,"5":3},"col_width":1,"widgets":[{"type":"gauge_widget","settings":{"title":"InfraRed","value":"datasources[\"SunIOT\"][\"SunIOT_IR\"]","min_value":0,"max_value":"6000"}}]},{"width":1,"row":{"3":1,"4":1,"5":1},"col":{"3":3,"4":4,"5":4},"col_width":1,"widgets":[{"type":"gauge_widget","settings":{"title":"UV Index","value":"datasources[\"SunIOT\"][\"SunIOT_UVIndex\"]","min_value":0,"max_value":"10"}}]},{"width":1,"row":{"3":9,"4":9,"5":9},"col":{"3":2,"4":3,"5":3},"col_width":1,"widgets":[{"type":"sparkline_widget","settings":{"value":["datasources[\"SunIOT\"][\"SunIOT_Visible\"]"]}}]},{"width":1,"row":{"4":1},"col":{"4":1},"col_width":1,"widgets":[]}],"datasources":[{"name":"SunIOT","type":"plugin5376758af1776c1c2e000326","settings":{"subscribe_key":"sub-c-34a701c0-a6a1-11e6-85a3-02ee2ddab7fe","channel":"SunIOT_Sunlight"}}],"columns":5}

Step 3) Add a Datasource.   Click Add under DATASOURCES  In this case, the Datasource is our PubHub MQTT broker.   This is very important to get right.   Fill out the form as below, using your subscribe key from PubNub.

screen-shot-2016-11-07-at-5-21-27-pm

Step 4) Build your IOT Dashboard.  After completing Step 3, click add Pane, and then click the + in the new pane.   Select Gauge.  Fill out as shown below using the datasource and data value name as shown (SunIOT_Visible).

screen-shot-2016-11-07-at-5-21-43-pm

 

 

 

 

And for the spark line.

screen-shot-2016-11-07-at-5-21-43-pmscreen-shot-2016-11-09-at-8-26-27-am

 

Step 5) Repeat Step 4 for both of the other SunIOT data types (SunIOT_IR, SunIOT_UVIndex).   There are many other types of widgets  to experiment with.

Congratulations!  You now have your IOT device hooked up to an Internet Dashboard.   You can share the link with anyone.   If you want a private page, you need to get one of the inexpensive paid plans.

img_0690

 

Where to Go From Here?

We would suggest building some more complex IOT devices with more sensors.   Add some actuators (such as motors and fans).   The SunIOT project takes you all throughIOT Projects Book the basics of building an IOT project and is a great base on which to build.

APress/Springer-Daniel has published a new IOT Projects book on building IOT devices with the Raspberry Pi and ESP8266 written by our SwitchDoc Labs CTO, Dr. John Shovic.

This book is designed for entry-through-intermediate-level device designers who want to build their own Internet of Things (IoT) projects for prototyping and proof-of-concept purposes. Expert makers may also find interesting new approaches. Raspberry Pi IoT Projects contains the tools needed to build a prototype of your design, sense the environment, communicate with the Internet (over the Internet and Machine to Machine communications) and display the results.

[callout size=”col-12″ title=”Raspberry Pi IOT Projects” button_title=”Buy the Book” button_link=”https://amzn.to/2cAW2Zp”  button_size=”normal” button_rounded=”true” button_color=”red”]