Tutorial: Using Blynk with the Raspberry Pi Smart Garden System in Python

Tutorial: Using Blynk with the Raspberry Pi Smart Garden System in Python

The Smart Garden System (or SGS for short) is an introductory, easy to build Raspberry Pi based environmental monitoring and plant watering system using advanced sensors to monitor the soil moisture, monitor the sunlight, watch the air quality and monitor temperature and humidity.

You can see the new product here.  While this product does not come with a Raspberry Pi, you can buy a fully loaded SD Card with all the Smart Garden System preinstalled here.

 

Quick Demo of the Prototype Raspberry Pi Smart Garden System

smart garden system

How to use Blynk With The Smart Garden System

Blynk is a digital dashboard for your iOS or Android device that allows you to easily build graphical interfaces by dragging and dropping widgets.  Blynk runs on iOS and Android apps to control Arduino, Raspberry Pi and the likes over the Internet.

It’s a digital dashboard where you can build a graphic interface for your project by simply dragging and dropping widgets.

 

Step by Step Install (for iPhones – Android is very similar)

Step 1) Install the Blynk app on your mobile phone

 

 

 

 

 

 

 

 

Step 2) Open the Blynk app and create an account

 

 

 

 

 

 

 

 

Step 3) Now click the button to scan a QR (see arrow)

 

 

 

 

 

 

 

 

Step 4) Scan the QR Below (this is SGS V002), you will then see the screen below the QR code

 

 

 

 

 

 

 

 

Step 5) Add Energy.   SGS V002 requires a total of 6900 Blynk energy.  You start your account with 2000, so you need to purchase 5000 Blynk Energy.  As of this writing, it will cost $4.99.

 

 

 

 

 

 

 

 

Step 6) Scan the QR code above again.   You will now see the SGS App on your screen.  You can always check the version by looking at the project description.

 

 

 

 

 

 

 

 

 

 

Step 7) Click in middle of the project to select the project.   Then click the indicated button to go to project settings.   Note:   You can always see the App Version number in the project setting (V002).   Now copy and paste or email yourself the authentication token (AUTH TOKEN) as we will be putting this in the SmartGardenSystem configuration file in the next section.

 

 

 

 

 

 

 

 

 

Note:  The one in the picture above WILL NOT WORK!  You need to generate your own as in Step 7.

You have completed the Blynk SGS App installation.

Installing the Blynk Authentication Codes into the Smart Garden System

The final step to connecting the Smart Garden System to the SGS App is to copy your Authentication Token (AUTH TOKEN above) to the Smart Garden System.
Step 1) using a terminal window, go to the SmartGardenSystem directory

cd ~/SDL_Pi_SmartGardenSystem

Step 2) Copy the config.py file to conflocal.py, if you haven’t already. (this keeps you from copying over your changes when you update the SmartGardenSystem)

cp config.py conflocal.py

Step 3) Open the conflocal.py file with your favorite editor

Step 4) Change the following lines:

############
# Blynk configuration
############

USEBLYNK = False
BLYNK_AUTH = 'xxxxx'
BLYNK_URL = 'http://blynk-cloud.com/'

Change False to True

Replace xxxxx with your authentication token from above.   Then it should look like this (but use YOUR authentication token – this one won’t work):

############
# Blynk configuration
############

USEBLYNK = True
BLYNK_AUTH = 'daff75da224b447b69ce2e5c0abe5312'
BLYNK_URL = 'http://blynk-cloud.com/'

Step 5)  You are now complete.  Start up your SGS software again.   Look at the App on your phone and after about 30 seconds, things should start propagating and you will start seeing cool data.

Using the Blynk Smart Garden System App

We will now go through a screen by screen discussion of the SGS App.   This discussion is for SGS V002. Once you have started the app in Blynk (by hitting the start button in the top right most corner of the app), you will start to see data after about 30 seconds.

Main Screen

 

 

 

 

 

 

 

 

 

  • – Top Left – Plant Moisture Dial – It cycles through all of your plants (up to 9 with the extender kits)
  • – Top Middle – Tank Water Level
  • – Top Right – Inside Temperature, Inside Humidity, Visible Sunlight in Lux
  • – Middle Chart – Displays Air Quality, Inside Temperature, Humidity and Visible Sunlight.  As you get more data you will be able to see more and more data on the screen.  The button on the right bottom of the chart switches to landscape mode to see even more data.

 

STATUS Window Section – Gives you the current state status of the Smart Garden System

 

 

 

 

 

 

 

 

Alarm Lights:

  • – MALRM – One or more Plant is showing low moisture levels (default under 60%)
  • – AQ ALRM – Indoor Air Quality is significantly impaired (default over 10000)
  • – T ALRM – Indoor Temperature too Low (below 5 degrees C)
  • – W EMPTY – Water Tank Empty
  • – MS FLT – One or more moisture sensor has faulted (default under 15%) – This is done so if a sensor fails, the system doesn’t keep watering.  If you start with really dry soil, water it a bit to begin to get the moisture level above 15%)
  • – LAST EVENT – Displays the type, time and date of the last major SGS event

Bottom Tabs – Navigation to the various screens of the SGS App

Plant Moisture Screen

This screen will show you the last moisture reading for all of the plants you have under control.  Note that the screen below shows a fully fleshed out SGS system with two four plant extenders.  Plant #8 is looking really dry!   Green indicates moist conditions, white indicates dry conditions and red indicates a Sensor Moisture Fault has been detected (default under 15%)

The terminal window at the bottom shows a running report of what the SGS system is currently doing.

 

 

 

 

 

 

 

 

 

Control Screen

This screen has the current set of manual controls supported by the Smart Garden System.  Expect to see more controls added in the future.
The top most button “DISPLAY RAINBOW” will change the 8 Pixel strip to displaying Rainbows instead of water level and plant #1 moisture.   There is about a 30 second delay between pushing the button  and the Rainbow starting or stopping.
Next, the “LED DISPLAY” will shut off the 8 Pixel Strip. There is about a 30 second delay between pushing the button  and the LEDs turning on or Off.

Using the “Select Plant”, you can select a plant to manually water.  If you don’t have any extenders, then only plant #1 will respond.  Telling the system to water an unknown plant will do nothing.   There is up to a 2 minute delay on the watering actually taking place.  The button will turn white and then will return to Blue when the watering has happened.  Then go back to the Plants screen to see the change!

How Does the Software on the Smart Garden System talk to Blynk?

We use the RESTful HTTP API provided by Blynk located here.  There isn’t a good native Python library available that seems to work with the latest version of Blynk.  If we find one, or write one, we will notate that here.

A code snippet below shows how we do this.  You can see the entire Blynk libraries that we wrote on GitHub.com. The Smart Garden System software uses the “requests” library to send HTTP.

 

def blynkEventUpdate():
    try:
        put_header={"Content-Type": "application/json"}
        val = state.Last_Event 
        put_body = json.dumps([val])
        if (DEBUGBLYNK):
          print "blynkEventUpdate:",val
        r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V10', data=put_body, headers=put_header)
        if (DEBUGBLYNK):
            print "blynkEventUpdate:POST:r.status_code:",r.status_code
        return 1
    except Exception as e:
        print "exception in blynkEventUpdate"
        print (e)
        return 0

 

Check out the forums on forum.switchdoc.com