Linux Format

Capture and display

Les Pounder shows us that the Raspberry Pi can be used to garner the opinions of people in real time – ideal next time you’re on a show booth.

- Les Pounder is a freelance maker who works with organisati­ons such as the Raspberry Pi Foundation to promote maker skills. He blogs at www.bigl.es.

Les Pounder shows you that the Raspberry Pi can be used to visualise physical interactio­ns.

Have you ever been into a store and seen one of those “How did we do today?” machines. You know, the ones with green smiley faces and red unhappy faces. Those machines are used to gather informatio­n from shoppers in real time, and will hopefully lead to better consumer experience­s.

So that got us thinking: “Could we build one with a Raspberry Pi?” Turns out that the answer is yes, and we can generate lovely graphs using a Python library called matplotlib. Better still, this project is remarkably simple so let’s get started, shalll we?

Pinned down

The hardware build for this project consists of three buttons, connected to the GPIO at pins 17, 27 and 25. Then the other side of the buttons are connected to any of the available Ground (GND) pins on the GPIO. Take a look at the diagram ( farright) for the connection­s, and there’s also a high-resolution image in the download for this project.

Make all of the GPIO connection­s, then connect the keyboard, mouse, HDMI and finally power up the Raspberry Pi to the desktop, ensuring that your Pi is connected to the Internet.

Bitten by the Python

Our Raspberry Pi is already running the Raspbian operating system, but before we start writing any code we first need to ensure that we have all of the Python 3 libraries necessary for the project to succeed. The first of these is matplotlib, so let’s open a Terminal, the icon for which is in the top left of the screen. In the terminal type the following and then press Enter. sudo pip3 install matplotlib

Now the next software install is to ensure that we have the latest version of the numpy, a scientific computing library. In the same terminal enter this command. sudo pip3 install numpy --upgrade

Once installed the final step is two-fold. First we install a dependency for working with complex numerical data, while the second is a library used by matplotlib to render graphics to the screen. In the terminal type the following lines and press Enter at the end of each line. sudo apt install libatlas3-base sudo apt-get install python3-gi-cairo That’s all of the installati­on steps out of the way, so let’s start coding.

Coding the project

To code the project we shall use the Python 3 editor, found in the Programmin­g section of the main menu. When the applicatio­n opens, click File>New and then a new window will open. Immediatel­y click File>Save and name the file customer-survey.py, and remember to save often.

The first step in our Python code is to import the libraries that we shall use. The first is matplotlib’s graph plotting class, which we rename to plt to save typing the full command every time. Secondly we import the numpy library, then we import the time library, used to control the pace of the code. Finally, we import the Button class from GPIOZero, which are used for our voting buttons: import matplotlib.pyplot as plt import numpy as np import time from gpiozero import Button So now let’s tell Python where to find our buttons. Each button is connect to a specific GPIO pin, and to a

GND connection. When the button is not depressed, the GPIO pin is turned on (HIGH) but when we press the button, it connects the high GPIO pin to GND, which causes the pin to be turned off (LOW) and that causes a change of state used to trigger the code. Here we create three variables for our yes, no and stats buttons. yes = Button(17) no = Button(27) stats = Button(22)

We create two counters. These are used to keep a tally of the number of votes that both our happy shoppers and unhappy ones cast. To start each session we set the counters to zero. happy = 0 unhappy = 0

Now let’s print a message to the user, asking them to press a button. In reality this would be hidden from the customers, because they would have some lovely instructio­ns printed on to a purpose-made framework. print(“Did you enjoy the event today? Press Green for Yes, Red for No”)

The main part of the code is an infinite loop that will run a series of tests. The first test is to see if the yes button has been pressed. while True: if yes.is_pressed:

If this condition is True, then we increment the integer stored in the happy variable by one. Printing the value stored in the happy variable, we can see the value increase, and in order to concatenat­e the value with an identifier – a string that says “Happy” – we need to convert the value into a string using str() . Lastly, we use sleep to introduce a one-second delay, which prevents the button from registerin­g multiple presses. happy += 1 print(“Happy”,str(happy)) time.sleep(1) Next, we reuse the same chunk of code to check if our no button has been pressed. elif no.is_pressed: unhappy += 1 print(“Unhappy”,str(unhappy)) time.sleep(1)

The last test is to see if the “stats” button has been pressed. If that is True, then the code will create a bar chart based upon the data we have captured. So if the stats button has been pressed, we create a tuple to store the collected data. elif stats.is_pressed: objects = (‘Happy’, ‘Unhappy’) We then tell the graph how many topics (happy, unhappy) there are. y_pos = np.arange(len(objects)) Then we store the data in a list, ready for it to be plotted on to the graph. satisfacti­on = [happy,unhappy] plt.bar(y_pos, satisfacti­on, align=’center’, alpha=1) plt.xticks(y_pos, objects)

A graph needs labels, and our Y axis shows the number of customers, and the title of the graph is simply “Satisfacti­on”. plt.ylabel(‘No. of people’)

plt.title(‘Satisfacti­on’)

To save a copy of the graph, we instruct matplotlib to save the “figure”, this is what it calls visualisat­ions. To keep the graph filename relevant, we use the current time and date at the moment the button is pressed, and concatenat­e this to the file extension “.png” plt.savefig(str(time.ctime())+’.png’)

The last two lines of code will publish the graph to the screen, so that we can see the results, and then it pauses for one second, before the main loop checks for further input. plt.show() time.sleep(1)

That’s it. Save the code, and click Run>Run Module to start the code. Once you’re asked to press a button, do so. Create some test data and when ready press the Stats button to see the results. Now at your next event, LUG meeting, Raspberry Jam install this piece of kit and learn how well your event went.

 ??  ?? Our chart is relatively simple, and we can use it to collect multiple samples of data for both customer responses.
Our chart is relatively simple, and we can use it to collect multiple samples of data for both customer responses.
 ??  ??
 ??  ??

Newspapers in English

Newspapers from Australia