IoT Browser Control

This post is as an extension to my earlier post http://manmade2.com/raspberry-pi-sprinkler-system/.

The here presented code can generally be used to remotely control and configure any IoT (internet-of-things) device or even appliance, which is in the range of your home WiFi.

And the advantage of this method (to others…)…: You can just use your standard browser on any of your devices, to control and configure your IoT devices — no app installation needed – no special hardware needed…

Browser Graphical User Interface (GUI) for Sprinkler System

Requirements

Here I will be applying this to the sprinkler system:

The goal is to have a remote control for the sprinkler that you can call with any browser for example on your smartphone.

The hardware you need is described in my post http://manmade2.com/raspberry-pi-sprinkler-system/. Nothing changed there.

The changes are all on software side. The following picture is a sketch for the software inter-dependencies:

The Software

The central program is the Python Flask application ‘webForm’. This application calls the html template in the file ‘template/guiPage.html’, which is the website you can call in your browser – the control hub for your sprinkler system.

In detail:

    1. The file webForm contains following python code

      In above code in line 14 the secret key is necessary for the communication between the flask app webForm and the jinja code in the html template /template/guiPage.html.

      In the lines 16 and 17 the json file settings.json which contains a Python dictionary with the pin addresses and the watering times for the different sprinkler valves. Note: If you are planning to call this routine from a timer set in crontab, make sure you use absolute paths in these scripts (e.g. here ‘/home/pi/flaskSprinkler/settings.json’).
      This dictionary file is saved in the variable sprinkler.

      The class definition in line 19 to 23 defines the input fields where the user can input new watering times (in seconds).

      Then from line 25 to 102 comes the actual application to the html page in /template/guiPage.html:

      Line 28 checks if one of the buttons in web page have been pressed. In this case it is first checked if a “submitAll” has been sent from there. In that case it collects the new watering times the user put into the webform. If a field (e.g. Z0) is not empty then the old value in the Python dictionary sprinkler gets overwritten with this new value. The other values stay unchanged.

      Additionally e.g. Z0.isnumeric() makes sure that the new value is really numeric and not a typo containing other characters.

      In the lines 46 to 49 the sprinkler times are sent to the html file, for display in the web-browser. Again this only works if the SECRET_KEY was provided.

      In line 51 and 52 the modified set of the watering times in the dictionary sprinkler is saved back into the settings.json file (overwriting he old values).
      If “startAll” was pressed in the browser GUI (line 54), then the Python script /home/pi/flaskSprinkler/sprinklerMain.py is called. This call is performed using a Linux system call and sending the routine sprinklerMain.py in the “background” with the & symbol at the end of the call. This has the advantage that sprinklerMain.py can run its opening and shutting of valves independent of the webForm script. Now the user can terminate (or kill) the sprinklerMain.py routine by pressing  “stopAll” in the webForm (line 57). This calls in sprinklerMain.py the subroutine closeAllValves(), which first closes all valves and then kills itself (sprinklerMain.py).

      Line 61 calls openValve() with the valve number in the script sprinklerMain.py to activate this valve.

      Line 65 calls closeValve() with the valve number in the script sprinklerMain.py to deactivate this valve.
      Similar it can be done with any number of valves.
      In lines 99 to 102 the collected sprinkler values are transferred to the guiPage.html page in the template/ folder using the render_template() method.
      Line 105 to 107 contain the main function.

      This script should be made executable using following call:

      With this you actually don’t need the .py ending anymore and can call the script just webForm. The header #!/usr/bin/python links this script to python and you also don’t need to explicitely call python to run this. Now you can call this just with

    2. The json file settings.json contains the GPIO addresses of the Raspberry Pi that are connected to vales and the running times each valve is supposed to be opened for. The file is organized as a Python dictionary and each set of GPIO address/running times has a consecutive number (here “0” to “3”):

    3. The python script sprinklerMain.py contains the functions to control the GPIO ports of the Raspberry Pi, which in turn control the valves of the sprinkler system.

      The subroutine run() (line 44) activates in a loop one valve after the other, taking its information from the settings.json file. The actual GPIO control is formulated in the function openCloseValves().
      The function openValve() (line 27) allows just the opening of one valve.
      The function closeValve() (line 36) allows just the closing of one valve.
      The function closeAllValves() (line 56) first closes all valves (all GPIO ports of the Raspbery Pi), then kills its own script ( sprinklerMain.py).

      The actual GUI (graphical user interface) that is displayed in your browser is “designed” in the file guiPage.html. This file needs to be in the folder templates/.

      Line 5 to 10 define the background colors.

      Lines 19 to 67 define and design the text boxes and buttons of the GUI interface in the website.

      In lines 70 to 88 is an interactive jinja code snippet that combined with the flash() function used in webForm.py displays the new times the user typed in, combined with the non-changed values.

    Below picture shows the result of this html code when opened in a web-browser. The address you will have to use is the IP address of your Raspberry Pi in your home-network (something you an find out by typing e.g. ifconfig) followed by :5000. So the address you have to type could be something like http://192.168.1.101:5000 (yours might have a different IP address).

    Keeping it Running

    This code worked fine so far. However I wanted to make sure that the sprinkler stays activated and does its job regularly. For this I added three lines to crontab:

    The first line calls a function shutSprinkler.py that does basically the same as closeAllValves() in the script sprinklerMain.py besides that it does not “kill itself”. It just makes shure that at start all valves are closed.

    The second line calls every day at 10:15PM the script sprinklerMain.py which executes all sprinklers one after the other. sprinklerMain.py is sent also here in the background and can be “killed” by the web-interface webForm.py if the user wishes to.

    The third line calls a simple shell script that every five minutes checks if the web interface is active, i.e. is webForm.py is still running. This shell script looks like this:


    I hope this post gives you some ideas how to build and design an IoT system that you can control from any computer or cellphone using a simple browser.
    As usual please don’t hesitate to leave me a note below if you have comments or questions.