Introduction
RaspberryPi, the popular Open Source Hardware Board operating out of different Linux based flavors like Debian, Fedora, Archlinux ARM and Android, does indeed allow one to develop a GUI Application to support Real-Time display of Signal/Graph values.
This is a simple Application, that i completed recently, which can be used to plot Signals, Graphs and values that can be displayed via this Application, in Real-Time. The motivation for this Project came from the need to have a means to display sensor read values for Temperature, Pressure, Humidity, Distance via a GUI based application, the Sensor Module would be communicating with RaspberryPi via UART, SPI, I2C Communication Protocols. The values would be read by the RaspberryPi via UART, SPI or I2C Application Program and these values would be required to be processed and analyzed in Real Time, for which a Signal Plotting GUI Application would be good, if we are able to verify the processing that we are doing on these received values.
The Temperature and Humidity values are read via I2C Communication with RaspberryPi. These values are displayed via our Application on the Display. |
To get an understanding of the purpose further, consider an example,as shown above. Consider a Humidity Temperature Module connected to the Raspberry Pi, via I2C. Values of Temperature are read by the Raspberry Pi, on these values,we now do some signal processing operations and mathematical operations. These processed values now need to be displayed Real Time to verify and observe the behavior of our system, in Real Time. Via the Signal Plotting Application, we can display these values on the LCD and verify the behavior. Thus, it helps in great way to display Graphical data in Real time. The above figure shows the same behavior. A Humidity Temperature Sensor Module is interfaced with RaspberryPi via I2C, and the Temperature and Humidity Values are communicated to RaspberryPi. The Application Program running on RaspberryPi, receives the values and performs some processing and calculations on these values. These post processed values are input to the Signal Plotting Application, which renders the values graphically on a Plot on a display LCD attached to RaspberryPi.
The Setup of the System.
The Setup of our System. |
Our System as shown in the above Picture, consists of a RaspberryPi, Model B inside a Clear Acrylic Case. It is powered through a Battery Source, an Intex 6000mAh, (Model: IT-PB602) which comes at under Rs 1000,(16.67$), and gives two Outputs. An Output supply of 5Volt, 1Amp and 5 Volt, 2 Amp, out of which we are using 5 Volt, 1Amp safely to power our Board, for an excess of 5-6 hours, normal usage. (http://www.amazon.in/Intex-Power-Bank-6000-PB-602/dp/B00SN5TC8A/ref=sr_1_cc_2?s=aps&ie=UTF8&qid=1428149931&sr=1-2-catcorr&keywords=Intex+IT+6000Mah )
A 4 GB SD Card with Debian "Wheezy"(2013-02-09-wheezy-raspbian.img) Image flashed to it, using win32diskimager-v0.9-binary Program, which formats the SD Card and flashes the Image, .img to it.
A LCD Display is connected to RaspberryPi via RCA to RCA Composite Cable.
Since RaspberryPi, Model B comes with only two USB Ports, hence one USB port has been expanded to support additional 4 USB Ports, using an USB Hub, as shown as the White colored Hub. To the second, USB Port of Raspberry Pi, we connect our Wireless Wi-Fi Adapter, as discussed in the next section. To, the USB Hub, we connect our KeyBoard, and Mouse(not visible in the figure). This, completes the initial setup of our System.
Starting up - Setting the Wi-Fi up.
This is required before we start up with the Application.Our Application requires some Packages that are required in order to Build the Program or Install the Application. In this, section, we will see, how we can configure RaspberryPi to access the Internet Network, using Wi-Fi. Towards this purpose, we will consider EDIMAX, EW-7811UN Model. It supports WPS, WPA2, 802.1x Profiles and is a Wireless 802.11b/g/n nano USB Adapter. Here's the link for it:
http://www.amazon.in/Edimax-EW-7811Un-Wireless-Adapter-Wizard/dp/B003MTTJOY?tag=googinhydr18418-21&kpid=B003MTTJOY&tag=googinkenshoo-21&ascsubtag=5b7645a2-8fa3-2149-c3c0-000023431d81
Few features of Edimax EW-7811UN IEEE 802.11n USB - Wi-Fi Adapter EW-7811UN Network Adapters -
Wireless Features
1) Smallest USB wireless adapter for Windows XP,Vista 7/8, Mac,Linux,Raspberry Pi, Model A, B,
B+. Based on Realtek RTL 8192CU Driver, which comes as a part of "Wheezy" Debian Kernel
Source.So no need to build the Driver and insert it using insmod.
2) Supports 150 Mbps 802.11n Wireless data rate. Permits users to have the farthest range with the widest coverage.
3) Modulation OFDM: BPSK QPSK 16-QAM 64-QAM DSSS. Frequency Band 2.4GHz - 2.4835GHz. Antenna internal chip antenna. Channels (FCC) 2.4GHz : 1~11.
4) Standards IEEE 802.11n backward compatible with 802.11b g Wi-Fi Certified. Security 64/128-bit WEP Encryption and WPA-PSK WPA2-PSK security WPS compatible IEEE 802.1X .
5) Power Saving designed to support smart transmit power control and auto-idle state adjustment.
After, we plug in the Wireless Adapter in RaspberryPi. After startup of RaspberryPi, do a raspi-config.
#sudo raspi-config
Select the "Enable or Disable SSH server" Option, which will configure the '/etc/network/interfaces' File. |
So, on selecting the above option, will configure the Wi-Fi Mode,based on the Wi-Fi Auto Hotplug or Plug and play Device that we have connected with RaspberryPi. After this, we can see this if we open the file, '/etc/network/interfaces' in Leafpad or Nano.
/etc/network/interfaces File, showing the configuration for 'wlan0', after selecting "Enable or Disable SSH server" option from raspi-config |
After, this we launch our Wi-Fi Config Gui, to locate a Wi-Fi Hotspot. We have used our Android Smart Phone as a Portable Hot Spot, as shown below:
By turning ON, the above options and setting the security attributes,will setup our Android Smartphone as a Portable Hot spot. We have while doing the above project have not used any Security Profile, like WPAPSK/WPS et al and no Password has been set in this regard. We are using Push option to connect between the Target,the Android Smart phone and the Host, Raspberry Pi. Now,we see the WPA GUI Utility to scan and connect to our Portable Hot spot.
Starting the WPA GUI and select the WLAN0 profile as added in 'interfaces' file |
From the scanned Networks, we have selected our Smartphone portable hot spot one, hence we can see the SSID Name,that we had given,being mentioned here, Micromax HD A116. Next we will connect first from Raspberry Pi by using Connect button. Second,we will in our Smart Phone allow the above device to connect, by the Push option, and both will be connected, as shown below:
Connected to our Smart phone Wi-Fi Hotspot. No security authentication or attributes have been set for the Hot spot. |
This completes our setup of Wi-Fi on Raspberry Pi using Edimax EW-7811UN, Wireless Adapter. Next we will see what Packages would be required to be installed for the project.
Setting up the Packages.
Few packages are required to be installed before we can compile and build our Project, since our project relies heavily on the methods of these Packages.To install these packages we have used apt-get install method. Few of these packages will require the access through 'mirrordirector.raspbian.org', for which we need to add the following nameserver to enable them, in '/etc/resolv.conf'.
Add the above nameserver to enable installation of packages |
We have added the following nameserver, 8.8.8.8 and 4.4.4.4 in '/etc/resolv.conf' to be able to download the Gtk2.0 packages.After, this we will begin the installation of the packages. Start by installing the following:
#apt-get install libgtk2.0-dev
#apt-get install gtk+2.4
This will install the necessary packages. Next we will see how to build the Application and the sources.
Building and Setting up the sources.
Download the original source as a .zip from below:
https://drive.google.com/file/d/0B6Cu_2GN5atpcllNV0pXUlJvWXc/view?usp=sharing
After, extracting and copying the source on RaspberryPi, enter the directory, "glinegraph-cairo-0.3.4", and we find the configure file.
Run ./configure and configure the sources as per the Target Board,RaspberryPi, where we are trying to build the source.
#./configure
Since RTC is not connected to RaspberryPi at the moment, hence an error like the below, will be popped up:
Checking whether build environment is sane ... configure : error : newly created file is older than distributed files!
To resolve the above error, open the 'configure' file in some Text editor and comment the lines below in the configure file.
Comment the Lines as shown in the Left side of the Beyond Compare difference tool in 'configure' file. |
If the Packages mentioned in the section above were installed correctly, then the 'configure' will generate the Makefile successfully. Next errors if found while running the "./configure" script would be due to incorrect version of packages present the Raspbian distribution. Hence,try doing google for those package errors and do "apt-get install" to install the correct packages.After, we are done with generating the Makefile, we will start building our application.
A copy of configure script can be found below:
https://drive.google.com/file/d/0B6Cu_2GN5atpUWFaRXA1WE5TeGs/view?usp=sharing
We will start by commenting these few lines, in the Makefile:
Comment these above few lines in the Makefile of the Project. |
Commenting the above lines, will prevent the config script looping every time, and hence the make control will never come out of it and start the compilation of the sources. To prevent this, since we don't need the config script to run again, after we have already configured our sources on RaspberryPi, we have commented the above lines in Makefile. A copy of the Makefile, can be found shared below:
https://drive.google.com/file/d/0B6Cu_2GN5atpbldLYlUxM1h4aEk/view?usp=sharing
Next, enter the source directory, "glinegraph-cairo-0.3.4/src", and make similar change in the Makefile of the source too, as shown below:
Comment the above lines in the Makefile of the source too. |
After, this we will build the source by running make.Enter the "src" directory, "glinegraph-cairo-0.3.4/src", and run 'make'.
#make
The Build will take some time, and a binary, 'lgcairo' will be generated successfully,as shown below:
A Binary 'lgcairo' will be generated successfully inside the 'src' folder, via successful Make. |
Next, we will run our Binary, through ./lgcairo
#./lgcairo
This will start our Signal Plotting Application, as shown below:
Launching the GTK Line Graph Application on RaspberryPi |
After, sometime we will begin to see,a graph appearing in the below form.
The Default graph plotted after launching the Application. |
To delete, the Tool Tip Points and to get a smooth version corresponding to a Signal, make the following changes, as shown below, in the file "glinegraph-cairo-0.3.4/src/glg_cairo.c":
To remove the Tool Tip points and to get a smooth signal graph, make the changes as shown above in glg_cairo,c file. |
To get a smooth signal plot in Real Time, which is our aim in the first place, we will make the above changes in glg_cairo.c file, build the Sources again and launch the 'lgcairo' Application, to get a smooth signal graph. Something like this, as shown below, without any Tool Tip points.
After, making the changes, and building the project, Signal Plotting can be shown. |
In the next section, we will see a few examples and applications of our Signal Plotting Application.
Examples.
Let us consider this first simple example, for which we will try sending a known signal and see, if our Application running on the RaspberryPi is able to plot We have used an Arduino Board, Freeduino, which is generating a Ramp Signal values, from 0 to 79. These signal values are transmitted serially, using UART, at a Baud Rate of 115200.
#include
int i32StepVal=0;
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.write(i32StepVal);
i32StepVal +=1;
if (i32StepVal==80)
i32StepVal=0;
}
Open the Arduino IDE, and take down the above sketch program, which sets up a Serial Channel, at a Baud Rate of 115200. In the Loop section of the code, which is executed every time, in a continuity, we output the Ramp Values,every time the loop hits. We have attached the Arduino Code,"sketch_mar14a.pde" for reference,here:
https://drive.google.com/file/d/0B6Cu_2GN5atpWU1BS0pYaGw0d00/view?usp=sharing
Arduino Program to generate a Ramp Signal and transmit the Ramp Values. |
Build this above sketch, and flash it on the Freeduino Arduino Board.Power up the Arduino Board.
So, now we are transmitting the values, now we need to receive these transmitted values, process them and plot them Real Time through our Application. Our Freeduino Arduino Board comes with CP210X, USB to UART Bridge Driver, which can be used to realize a Virtual COM Port on RaspberryPi. This Virtual COM Port is detected when we plug the USB as 'ttyUSB0'.
Freeduino Arduino Board communicating with RaspberryPi, via USB UART Bridge Driver IC CP210x present on Freeduino Board. |
Here, as we can see, the white colored USB Hub, which is connected to RaspberryPi USB Port. The Mouse and the Keyboard are connected to this USB Hub. The Arduino's Type A USB Port is connected to this USB Hub and the connection is completed. Here, another fig. can be seen for the same purpose.
As, can be seen clearly the Arduino Board connected to the RaspberryPi via USB Hub. |
When we connect the Arduino with RaspberryPi, we can see, what log messages are output,via 'dmesg | tail ' command.
The Virtual COM Port detected is 'ttyUSB0'. |
Now,in the code we shall see, how we are receiving the transmitted values and processing them via our Signal Plotting Application. Firstly, we will be adding a few header files in the file 'lgcairo.c', corresponding to Termios Setup files, as shown in the figure below:
Add the required Termios Header files to setup the UART |
Include the necessary Header files in the lgcairo C source file. Create Global Termios structure variables, new_prt_settings and old. Next create the routine, OpenComport to handle the opening of the Comport, with the required Baud Rate, as shown below:
OpenComport to handle the Opening of the COM port. |
In the later part of the function, "OpenComport", we are taking care of the following things:
int comPortFd;
comPortFd = open(ttyPath, O_RDWR | O_NOCTTY);
if (comPortFd==-1)
return -1;
tcgetattr(comPortFd, &old);
memset(&new_port_settings, 0, sizeof(new_port_settings));
new_port_settings.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
new_port_settings.c_iflag = IGNPAR | ICRNL;
new_port_settings.c_oflag = 0;
new_port_settings.c_lflag = 0;
new_port_settings.c_cc[VMIN] = 1;
new_port_settings.c_cc[VTIME]= 0;
tcflush(comPortFd, TCIFLUSH);
error = tcsetattr(comPortFd, TCSANOW, &new_port_settings);
We are flushing the Old settings, and populating the new control structure, new_port_settings, configured with Flags, like 'B115200 | CS8 | CLOCAL | CREAD' and 'IGNPAR | ICRNL'.
Next, we write Methods, to read the received data, using 'PollComport' and write the transmitted data, using 'SendByte' and 'SendBuf'.
Methods to read the received Serial Data and write Transmitted Data to the COM Port. |
Lastly, in the main code function, "fn_add_series_data_values", we are reading these data point values using "PollComport", function. These values are added to the line-graph object, 'graph' in the function, "glg_line_graph_data_series_add_value".
Reading the Transmitted values via PollComport function. |
In the Main function, we will open the COM Port and set the Timer Handler function.
static gint gb_stop = TRUE;
static int error;
static struct termios new_port_settings, old;
int uart0_filestream = -1;
int main (int argc, char **argv)
{
..........
.................
uart0_filestream = OpenComport("/dev/ttyUSB0", 115200);
if (uart0_filestream == -1)
printf("Error - Unable to open UART.\n");
glg_line_graph_data_series_add (glg, "Volts", "red");
glg_line_graph_data_series_add (glg, "Battery", "blue");
.....
........
gtk_main ();
g_source_remove (ui_add_values );
CloseComport(uart0_filestream);
}
And here is how finally, the signal will look like on the Signal Plotting Application:
Signal Plot of the Ramp Signal sent by the Arduino Board. |
This completes, this example. Further examples will added in coming time, where i will try and showcase some interesting Applications of this Signal Plotting Application for Bio Medical Sensors and a lot more.
We will upload the Code of this Application shortly, and further queries can be posted in the comments section.
Thanks a Lot.
Rajiv.
Thanks for the tutorial !
ReplyDelete