IoT Dashboards
Setting up an Account at PubNub
Set up a free account at pubnub.com, 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.
SunIOT Software
To start setting up the SunIOT software, go to GitHub and clone the SunIOT_PubHub repository.
$ git clone https://github.com/switchdoclabs/SunIOT_PubHub.git
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 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 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
These will be published to the MQTT channel SunIOT_Sunlight, as shown in Listing 2.
Listing 2
SunIOT_PubHub.py
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 07 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' % datetime.now()) 14 print , Vis: , + str(vis) 15 print , IR: , + str(IR) 16 print , UV Index: , + str(uvIndex) 17 18 myMessage = { "SunIOT_Visible": vis, "SunIOT_IR": IR, "SunIOT_UVIndex": uvIndex } 19 pubnub.publish().channel(,SunIOT_Sunlight').message(myMessage).async(publish_callback) 20 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 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 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 SunIOT_PubNub.py 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 listen.py with the same keys above and start listen.py.
$ sudo python listen.py
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 5
(incl. VAT)