Simple IOT Sunlight Sensing Raspberry Pi Project- SunIOT – Part 3

Simple IOT Sunlight Sensing Raspberry Pi Project- SunIOT – Part 3

Adding an IOT Dashboard

We just have added a new product, a Grove Sunlight Sensor to our lineup and wanted to build a project using it.   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 will have us connect up to initialstate.com and then in Part 4, we will 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).

The 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.

InitialState Dashboard

There are many internet dashboards and IOT data servers around today.   We chose initialstate.com to do first. InitialState has a free version, but it is limited to 25,000 streamed events per month and so we ran out in about 3+ hours (4 streaming events every 2 seconds = 7,200 events per hour – we hit the limit in 3.5 hours and got a pleasant (it was actually nice!) email telling us we were out of events until the start of the next calendar month.    We had assumed (very mistakenly) that all of our data being sent was one event.   Each stream call turns out to count as an event.   Hindsight says we should have realized that.   See our short review on initialstate.com below.

screen-shot-2016-10-29-at-1-33-15-pm

Installing InitialState

Installing Raspberry Pi support for InitialState is pretty straightforward.   As is the software for connecting your SunIOT device to InitialState.

To Install

If you have followed the tutorial in our previous postings, you have already done the first two steps

Installing I2C on your Raspberry Pi

Make sure you installed I2C as in this link:

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

When you have done the tutorial type the following into your Raspberry Pi:

sudo i2cdetect -y 1

If everything is good, then you will see this:

pi@RPi3-62:~/SunIOT $ i2cdetect -y 1

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

0x60 is the Sunlight Sensor.

Installing apscheduler

The Advanced Python Scheduler (APScheduler) is a light but powerful in-process task scheduler that lets you schedule functions (or any other python callables) to be executed at times of your choosing.

We have used the APScheduler software for a number of projects in the past, such as Project Curacao [ref:  Raspberry Pi Geek article on Project Curacao], SunRover [ref: Raspberry Pi Geek Article on SunRover software], the upcoming Project Curacao 2 project (we revisit wind power and the Caribbean with a 2nd generation Project Curacao) as well as other SwitchDoc Labs projects for customers.   It’s a great package.

sudo pip install setuptools --upgrade

sudo pip install apscheduler

Installing InitalState Library

sudo pip install ISStreamer

Setting up Initial State

Very straight forward.    Go to initialstate.com and sign up for a free account.   When you have done that go and set up your first bucket.

To work with the SunIOT_InitialState software, you will need to gather your Bucket Key and Access Key from settings under the bucket you just created.

screen-shot-2016-10-29-at-12-42-20-pm

You can name the bucket anything you want.  Note that the keys you see in the picture will not work.  They have been deleted for obvious reasons.

Next, to get things working create at least one tile.   Make it a summary type and fill it out as in the picture below.

screen-shot-2016-10-29-at-12-44-26-pm

The three Signal Keys that we have defined in the SunIOT software are:

  •     –  SunIOT_Visible
  •      – SunIOT_IR
  •      – SunIOT_UVIndex

Now you are ready to configure the software with your initial state account information.

SunIOT_InitialState Software

In Part 2 of this series we go through the basic SunIOT software, so we will just focus on the new part of the software.SunIOT

You can download the entire software by doing:

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

There are two parts of interest in this code that were not in the previous version.   First, the setup of the keys and initializing the InititalState connection.

from ISStreamer.Streamer import Streamer

# configuration - you must change these!
InitialState_Bucket_Name = "SunIOT_InitialState"
InitialState_Bucket_Key = "XXXXXXXXXXXX"
InitialState_Access_Key = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"

streamer = Streamer(bucket_name= InitalState_Bucket_Name bucket_key=InitialState_Bucket_Key, access_key=InitialState_Access_Key)

And the second part is the routine used to send it to the InitialState dashboard that you set up above.

def reportToInitialState():

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


        streamer.log("SunIOT_Visible", vis)
        streamer.log("SunIOT_IR", IR)
        streamer.log("SunIOT_UVIndex", uvIndex)
        streamer.log("SunIOT_Status", "SwitchDoc Labs\nSunIOT")
        blinkLED(3,0.200)

        returnValue = []
        returnValue.append(vis)
        returnValue.append(IR)
        returnValue.append(uvIndex)
        return returnValue

Note that the Signal Keys that we mentioned above are in the code above in the streamer.log lines of the program.

The final line of interest is where we schedule the reportToInitialState call. Note this is where we sent information way too fast to the InitialState dashboard. Every two seconds made the 25,000 events happen very quickly (3.5 hours). You may want to change the to every 30 or 60 seconds.

        # add the Update to Initial State
        scheduler.add_job(reportToInitialState, 'interval', seconds=2)

Looking at the Results

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

 

 

We ran the software until we ran out of events. It looked pretty good up on the site.

screen-shot-2016-10-29-at-10-23-20-am

Our Opinion on InitialState

The software setup for InitialState was very easy. It came together in a matter of 30 minutes and we had our dashboard working. There are some great tiles, and a whole level of sophistication underneath the dashboard (waves, triggers, SmartThings integrations and lots more).   Really good stuff to explore.  We have just starting looking at all of these features.   The tiles and dashboard look good and can be embedded in webpages.  The great news is it NEVER bombed.   Adafruit.io and the IBM BlueMix (our BlueMix tutorial is here.) all bombed at one time or another.  Below is an example of the waves output.   Very nice.

wavesexample

We had three issues with using InitialState for our dashboard.  We have an email into the company about two of these issues and we will update this page when we hear back.

25,000 Event Limitation in Free Version – Seems light.  maybe 50,000 would be better.

No way to save Buckets and share the code – Makes it hard to share structures for projects and articles. (Update Oct 31, 2016:  Jamie at intialstate.com:  This ability is planned for a future release.   No due date.)

No way to load images into Tiles – This is a bummer for adding logos and webcam images, plus graphs generated by MatPlotLib and the Raspberry Pi. (Update Oct 31, 2016:  Jamie at initialstate.com: They are discussing but do not have schedule.)

 

Coming Next

In our final installment on SunIOT we will be adding a dashboard that uses MQTT.  First we sent the data to pubhub.com and then hook up freeboard.io for the display.   A bit more complex than InitialState, but lots of flexibility and open source goodness.   We have also written an MQTT tutorial for the Raspberry Pi here.

img_0691