Monday, September 7, 2015

FM Radio Broadcasting with HackRF and GNURadio

Here's a flow graph which can be used for broadcasting some content of a Wav  file over FM radio frequencies. The original flow graph is from here but it created a lot of missing block errors. So I had to edit it a little bit to make something my own which can be downloaded from here. Since it requires a Wav file, I generated it from a MP3 song file using a tool called Sox as follows.

sudo apt-get install sox

sudo apt-get install libsox-fmt-mp3

sox phillip-phillips-home.mp3 -b 16 phillip-phillips-home.wav

Here's how the flow graph looks in GNURadio Companion tool.


GQRX for Quick SDR Jobs

While GNURadio provides very sophisticated capabilities to implement transmitters and receivers using SDR hardware platforms, it is not necessary to create flow graphs and run them in order to view the radio spectrum. For any quick and dirty requirement to view the spectrum, the easiest way is to use GQRX, which is a receiver program based on GNURadio.

Here's some screenshots of GQRX tuned to various frequencies.












Jamming WiFi Channels with HackRF

We can easily capture signals going in a particular frequency using HackRF and also we can retransmit those data back to the air from the file. Here's how we use it to jam some wifi channels. To try this, first I checked what is the specific frequency channel, which is used by my laptop and the WiFi hotspot to communicated. We can find it out by using the following command.

iwlist wlan0 channel

It should list down all the available WiFi channels and at the end, the channel currently we are using. In my case, it was  channel 11 which operated in 2.462 GHz. Now it's time to try jamming. First we should record some data to a file. We can do it with the following command. The parameter -f specify the frequency we need to tune in to in Hz. Similarly the parameter -s specify the sampling rate which I have set to 20 MHz. Finally the parameter -l specify the LNA gain. Reading man files will provide more information about those stuff.

hackrf_transfer -r test.bin -f 2462000000 -s 20000000 -l 40

This command will run for a while and save data to the file test.bin and stop at some point. Still, the reason to stop in that way is mysterious to me. Anyway, now we have some captured data. We can transmit it back. Before doing so, open a new terminal and ping to some public IP address such as 8.8.8.8 so that we can continuously see the ICMP packets going and coming with round trip information. While having the ping command running in that terminal, run the following command from our original terminal. That -x parameter sets the Tx VGA gain.

hackrf_transfer -t test.bin -f 2462000000 -s 20000000 -x 47

During the time period of above transmission going on, we can see that the ping packets are getting disturbed. Either they take a longer round trip delay or completely become unable to be delivered.

Here's a reference I used,


Saturday, August 29, 2015

Processing data out of GNURadio

While working on software defined radio (SDR), I have always worked on GNURadio software which provide a graphical interface. I have to drag and drop various blocks to create flow graphs which perform the functionality I need. It's time to escape the barriers of GNURadio and do further thing out of it. Finally I found the way to do it. We can start with GNURadio flow graphs and transfer data from it to our own python scripts where we have the freedom to do whatever we want with our program codes. I found some good resources from where I learnt it and those things are mentioned under the references section.

First of all to try these things, we need to have installed some software. GNURadio should be there as obvious. Additionally we need a python plotting library which we will be using. Install it as follows.

sudo apt-get install python-matplotlib

Let's clearly understand what we are going to do now. When we run a flow graph created using GNURadio, it will acquire the data from wherever we specified and will process it inside. Resulting data can be visualized as different plots provided as blocks within the flow graph or sometimes we can save data in to a file. Every kind of processing I have done so far are within the flow graph. Now, what we are going to achieve is creating a flow graph which has some special blocks. Instead of running the flow graph straightforwardly, we just generate a python script using GNURadio that represent the flow graph. Then we write a our own python program which will use that GNURadio generated python script as a module to acquire data and then do further processing of data inside our own python script.

It's time for action. Open gnuradio-companion tool and create the following flow graph. It will acquire data from a HackRF device and apply a fast fourier transform (FFT) operation on the data stream. Finally it turns that FFT data in to message block which are ready to be sent out of the flow graph though a special block. I saved this flow graph as fft_data.grc and clicked on the 'generate flow graph' button to generate the python script which implements this flow graph is code. It generates the script called top_block.py automatically.


Once this is done,  we have a python script which will acquire data and provide us FFT data. Let's use it in a our own python script. For this purpose, create a new python program with the name fft_plotter.py and add the content shown below. It should be understandable that we have two functions there. First one draws the FFT for the considered frequencies while the second one draws the variation of signal power of a particular frequency over time. Depending on what we want to plot, we can uncomment the relevant line.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import grcconvert
import struct
grcconvert.main("top_block.py")
import top_block
import matplotlib.pyplot as plot
import os
from collections import deque

def plot_fft(with_this):

 #Asanka: trying to play with this
 os.system('clear')
 print "------------------------------"
 print "length       =", len(with_this)
 print "start point  =", with_this[0][0], "dB"
 print "center point =", with_this[len(with_this)/2][0], "dB"
 print "end point    =", with_this[len(with_this)-1][0], "dB"
 print "------------------------------"

 plot.clf()
 # it seems logpwrfft exchanges the first and second part of the FFT output, we correct it:
 plot.plot(with_this[len(with_this)/2:]+with_this[:len(with_this)/2]) 
 plot.draw()

def plot_power(data_array):

 os.system('clear')
 print '\n\n'
 print "signal strengh at the center:", data_array[len(data_array)/2][0],"dB"
 
 a1.appendleft(data_array[len(data_array)/2][0])
 datatoplot = a1.pop()
 line.set_ydata(a1)
 plot.draw()
 plot.pause(0.00001)


plot.ion()
tb=top_block.top_block()
tb.start()

#Asanka: experiment
a1 = deque([0]*100)
ax = plot.axes(xlim=(0, 100), ylim=(-100, 0))
ax.grid(True)
line, = plot.plot(a1)
plot.ylim([-150,0])
plot.show()

while True:
 fft=tb.msgq_out.delete_head().to_string() # this indeed blocks
 floats=[] 
 for i in range(0,len(fft),4):
  floats.append(struct.unpack_from('f',fft[i:i+4]))
 print "got",len(floats), "floats; FFT size is", tb.fft_size
 i=0
 while i<len(floats): # gnuradio might sometimes send multiple vectors at once
  pack=floats[i:i+tb.fft_size-1]
  #plot_fft(pack)
  plot_power(pack)
  i+=tb.fft_size
  

This python script and the gnuradio-companion generated python script should be there in the same directory. Finally we need one more python script before we can try running this code. It can be obtained from here and saved into the same directory. It's written by Andras Retzler and I got the code from an example he provided in github.

Now I think we are good to go. Connect HackRF device into a USB port of the computer and run our python script with root privileges.

sudo python fft_plotter.py

Depending on the function we call within our script (we cannot call both at the same time), we will be able to see two different plots as follows.




That's it for the moment!

References:



Thursday, July 23, 2015

Low Frequencies and Solder Fumes

Somewhere inside UoC
Photo credit: Chathura Suduwella
Due to the nature of our research works, it's not enough just to sit in front of a computer and write programs that do certain things. Our work makes us to sniff solder fumes most of the times and sometimes it makes us to move out of the lab and walk all over the university. Last few weeks were so much stressful due to various challenging tasks we were facing. It's not easy but I think I learned so much interesting new things during these hard times. Among various things I was facing, two notable experiences are worth reporting for my future reference.

As a further step of our elephant infrasonic research, our guys wanted to do several field experiments within the university premises. It requires them to take the infrasonic emitter and detector equipment and move all over the university premises which is not as easy as it sounds. While moving we have to take data samples from the infrasonic detector at various places and we need to take note of the GPS coordinates of the particular location. Sometimes our infrasonic emitter stops functioning or gets stuck and sometimes our equipment runs out of battery power. Facing all those troubles, we have to keep moving all over. Joining with Chathura aiya, Poshitha and Waruna, I had a nice time in the field missing the lunch and a work day. Besides the troubles of experiments, we were happy about the outcomes of it.

First three units out of the production line :)
Second move is from the hardware side. Moving ahead of breadboards, we finally managed to
implement a much more usable wireless mote. It has extension headers to connect a USB ASP programmer board to reprogram the MCU without removing it from the mote as it was a very troublesome thing in our earlier version. First I tried everything on a breadboard and then started moving things into the permanent mote. Chathura aiya provided a great assistance in this effort and his confidence and ideas helped so much. Without his guidance, these little things will not be as beautiful and handy as they are at the moment. He exactly knows how to put things in the right position in order to make it smaller and look good.

A lot more interesting work is going on these days. I will try to write a brief note if I found anything worth telling to my future self. For the moment, this is it.


Sunday, July 19, 2015

Serial communication between Arduino and Android

I wanted to establish communication between an Arduino Uno board and an Android device for a project work. Initially I tried usb-serial-for-android library and reached to some level. I was able to read some amount of bytes from the Arduino board. However due to some reason, my Android device disconnects from the Arduno serial connection after a while. In order to try that, I followed the steps mentioned here. It's sad that I spent a whole day trying to make it work but finally failed. Perhaps this library actually works but due to some mistake I made, it's still not ready to work. Anyway, I'm leaving it to try later some other day if I find time.

It took me some time to realize that there are other libraries available to achieve the same thing. Since I ran out of time, I didn't get a chance to try them from scratch but I decided to try some sample apps they provide. These guys in Physicaloid provides an interesting library and sample projects which actually worked for me. Their PhysicaloidLibrary includes sample project in this place and some of those are in google play already.

I installed their USB Serial Monitor Lite app in google play into my Galaxy S3 device which runs Android 4.4.4. Then I installed following simple Arduino sketch into an Arduino Uno board. Connecting the two devices with the help of a USB OTG cable showed me that Arduino board responds to the messages from Android phone by blinking the LED at pin 13 and also by sending reply data.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
static int counter = 0;
int led = 13;

void setup() {
        Serial.begin(115200);
        pinMode(led, OUTPUT);     
}

void loop() {
  
  digitalWrite(led, LOW);

  if (Serial.peek() != -1) {
          
          digitalWrite(led, HIGH);
        
        do {
                byte message = (byte) Serial.read();
                //Serial.print("Received: ");
                
                if(message == (byte) '1'){
                        Serial.println("1");
                } else {
                        Serial.println("-");
                }                                                         
        } while (Serial.peek() != -1);     
        
  }
  delay(1000);
}


I think I should look into the source code of this app which can be found here in order to adapt it into my work requirements.

Monday, July 6, 2015

Install GNURadio on Ubuntu 12.04 using install script

When using HackRF device, it is necessary to have GNURadio installed and running in our machine. Until today, what I used was a USB drive installed with a live linux image. This live linux image was pre-configured with GNURadio and other relevant tools. I have written a separate article about GNURadio based on this live linux image. This time, I'm installing GNURadio and relevant tools on my Ubuntu 12.04 system which will save me from requiring to restart the computer to go for a live linux image whenever I have to use HackRF.

Since the installation of GNURadio from source files is a big hassle, I found a wonderful installation script extremely helpful. GNURadio website has indicated it as a good way to install GNURadio and therefore, I decided to go with it. Here's the steps I followed to perform the installation using that script.

(1) Create a folder named 'src' somewhere in the home directory and move into it using terminal

mkdir src
cd src/

(2) Run the following command

wget http://www.sbrac.org/files/build-gnuradio && chmod a+x ./build-gnuradio && ./build-gnuradio

It takes a very long time to download source files and packages, compile them and configure all the necessary things.

(3) Add the following line to the ".bachrc" file in your home directory. This information was printed on the screen during the installation process of the previous step. That's why I followed it to set this path.

export PYTHONPATH=/usr/local/lib/python2.7/dist-packages

(4) Now plug-in HackRF device into a USB port and run following command.

sudo hackrf_info

An output much similar to the following should appear in the terminal then.

Found HackRF board 0:
Board ID Number: 2 (HackRF One)
Firmware Version: 2014.08.1
Part ID Number: 0xa000cb3c 0x00544f46
Serial Number: 0x00000000 0x00000000 0x14d463dc 0x2f6662e1

(5) Now we can start gnuradio companion (GRC) in order to try hackrf device using the following command.

sudo gnuradio-companion

That's it. After the GRC windowed showed up, we can start creating different flow graphs and run them with HackRF device.

Reference: