Introduction

To cooperate with our hardware SPARKbox, we construct and improved our program in 2021 to control LEDs. It can work in Raspberry Pi 4B and can be easily installed and be used, as we construct a user-friendly webpage.

To get most information of our hardware, please click here.

Preliminary Code

As constructing a illumination equipment, we firstly make a same one as our hardware in 2021, including the program that is used to control LED.

And the corresponding code is as followed:

# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
while True:
GPIO.setmode(GPIO.BOARD)
Lights=[37] #The list shows the connected pins
GPIO.setup(Lights,GPIO.OUT)
GPIO.output(Lights,GPIO.HIGH)
GPIO.output(Lights,GPIO.LOW)
time.sleep(2) #Fill in the duration in the brackets. Here it is 2s
GPIO.output(Lights,GPIO.HIGH)
Lights=[36]
GPIO.setup(Lights,GPIO.OUT)
GPIO.output(Lights,GPIO.HIGH)
GPIO.output(Lights,GPIO.LOW)
time.sleep(2)
GPIO.output(Lights,GPIO.HIGH)
GPIO.cleanup()
time.sleep(26)

Example of preliminary Python code for LED control

Our Improvement

Problem

Although the preliminary version is successfully used in 2021, this year we find several problems during experiments. Above all, most of experimenters have very little knowledge of Python. However, they always need to modify the Python script to change time for light and darkness, aiming to search the best experiment condition. This leads to a situation that they may touch the screen by mistake and change codes wrongly, resulting in failure while running program. (Figure 1)

Problem illustration

Figure 1.Problem with preliminary code interface

When this accident happens, we can only seek for specific member that is responsible for hardware. It is obviously inconvenient and she is unlikely to be available all the time.

Solution

We need a program that is more user-friendly as well as more robust. That is to say, we need a graphic interface, hiding core code in the back-end and developing a wieldy front-end webpage. (Figure 2)

Program concept

Figure 2.Concept of the user-friendly program

Practice and Results

For back-end, we still choose Python script, as its abundant libraries. We import RPi.GPIO library to control LED. However, before that, we use flask library to catch users' requirements from front-end.

For front-end, we choose HTML+CSS+JavaScript to construct a webpage, providing a graphic interface for users and a platform for inputting requirements (Figure 3). Meanwhile, it worth noting that we also add validity check, which can examine: 1. if inputs sufficient; 2. if inputs valid. (Figure 4)

Front-end web page

Figure 3.Front-end web page

The first input is the pin numbers of your LED, if you do not have knowledge of it, please open your Raspberry Pi and enter the command line interface (Hardware page - Manual of instructions -- step 1, 2, and 10)

Then write down:

gpio readall

or

pinout

and you can check the board pin numbering and search for the corresponding pin number you connect on the Raspberry Pi.

The Parameters users need to input is lighting time, darkness time, and either cycles or duration. If you input cycles and duration at the same time, it would priority take cycles as a valid parameter.

Front-end web page Front-end web page

Figure 4.Actual working effect of the program

Then, we guarantee that everyone can easily read this webpage and use it, controlling LED without additional practice. Because what user can touch is only a front webpage, it is absolutely no need to worry that the code would be change and report an error.

To view the complete code: GitLab Repository

Future Work

Now user just need run the Python program via Raspberry Pi, enter the webpage, input needed parameters and click the "Submit" button, and then LEDs will turn on and turn off automatically.

To test if this webpage really is user-friendly, we invite Dr. Jiawei Shao to try out our new program. (Figure 5) He recognized our development, and offer a further thought that we can really build a website, allowing everyone can visit it no matter where he is. No need for turn on the Raspberry Pi in laboratory, but just open your phone and enter the website, and then we can control LEDs even in dormitory, providing great spatial freedom and saving amount of time. (Figure 6) It sounds really wonderful, and we are continuing to develop this program in this direction.

Future conception

Figure 5.Interview with Dr. Jiawei Shao

Future conception

Figure 6.Our conception for future development

Code

Code