IoT Dashboards

Setting up an Account at PubNub

Set up a free account at, making sure to take note of the password. Next set up the MQTT info to connect up SunIOT.

1. Create a new App by clicking on "NEW APP +". Type in SunIOT in the "enter a new app" field and hit the "CREATE NEW APP +" button. You should see the screen shown in Figure 3.

2. Click on the SunIOT App page. Enter SunIOT in the Enter a New Keyset Name field and hit the CREATE NEW KEYSET + button (see Figure 4). Ignore the application add-ons for this page. They are for more advanced usage.

Figure 4: PubNub SunIOT app screen.

SunIOT Software

To start setting up the SunIOT software, go to GitHub and clone the SunIOT_PubHub repository.

$ git clone

Make sure to set up any additional libraries you need. Make sure to also install I2C. Further help with this is available on Adafruit's website [12].

$ sudo pip install setuptools --upgrade
$ sudo pip install apscheduler
$ sudo pip install pubnub

Open up the 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 you got from PubNub into the pub-c-xxxxxx and sub-c-xxxxxx configuration strings above.

The three sets of data you will publish will be :

  • SunIOT_Visible
  • SunIOT_IR
  • SunIOT_UVIndex
Figure 5: SunIOT ready for the IoT dashboard.

These will be published to the MQTT channel SunIOT_Sunlight, as shown in Listing 2.

Listing 2

01 def publish_callback(result, status):
02         print "status.is_error", status.is_error()
03         print "status.original_response", status.original_response
04         pass
05         # handle publish result, status always present, result if successful
06         # status.isError to see if error happened
08 def publishToPubNub():
09         vis = sensor.readVisible()
10         IR = sensor.readIR()
11         UV = sensor.readUV()
12         uvIndex = UV / 100.0
13         print(,Publishing Data to PubNub time: %s' %
14         print ,         Vis: , + str(vis)
15         print ,         IR: , + str(IR)
16         print ,         UV Index: , + str(uvIndex)
18         myMessage = { "SunIOT_Visible": vis, "SunIOT_IR": IR, "SunIOT_UVIndex": uvIndex }
19         pubnub.publish().channel(,SunIOT_Sunlight').message(myMessage).async(publish_callback)
21         blinkLED(3,0.200)
22         returnValue = []
23         returnValue.append(vis)
24         returnValue.append(IR)
25         returnValue.append(uvIndex)
26         return returnValue

Note that the myMessage JSON message above is a Python dictionary message, not a string. PubNub changed this in their recent upgrade to the 4.0 SDK. If you are using earlier examples, the string JSON (called a serialized JSON message) will not work.

Now start your program by running.

$ sudo python

If you get the error:

"ImportError: No module named Adafruit_PureIO.smbus"

Try the following:

$ git clone
$ cd Adafruit_Python_PureIO
$ sudo python install

If everything is hooked up right with PubHub, you should see something like what is shown in Listing 3.

Listing 3

PubHub Output

01 Publishing Data to PubHub time: 2016-11-05 20:18:50.614003
02                 Vis: 261
03                 IR: 253
04                 UV Index: 0.02
05 [1, u'Sent', u'14784023306510771']
06 Publishing Data to PubHub time: 2016-11-05 20:18:52.613497
07                 Vis: 261
08                 IR: 254
09                 UV Index: 0.02
10 [1, u'Sent', u'14784023326454960']

The SunIOT_PubNub software comes configured to send data every 2 seconds. If you don't want to blow through your free data limit quickly, change that to every 10 seconds in the apscheduler call in file:

# add the Update to PubNub
scheduler.add_job(publishToPubNub,,interval', seconds=10)

If you want to subscribe to your own data from PubNub, open another terminal window on the Pi, configure with the same keys above and start

$ sudo python
Figure 6: Creating a new Freeboard at

Buy this article as PDF

Express-Checkout as PDF

Pages: 5

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content