Thursday, June 27, 2013

Using rtimer in Contiki for more accurate timing

In contiki programs, the usage of rtimer is important when we need a real-time functionality. While having different kinds of timers such as ctimer and etimer, the most accurate timing can be achieved by using rtimer. Recently had such a requirement where my program should have very accurate timing. So, I learned about rtimer and used it in my program. For the sake of remembering it, I'm writing down a simple program which use rtimer.

The program shown below is a modified version of the hellow-world program which is in /contiki-2.6/examples/hello-world directory. Therefore I can run it just by issuing the following command in terminal inside that directory.

make TARGET=cooja hello-world

1:  #include "contiki.h"  
2:  #include <stdio.h>  
3:  #include "sys/rtimer.h"  
4:  #define     PERIOD_T     5*RTIMER_SECOND  
5:    
6:  static struct rtimer my_timer;  
7:    
8:  PROCESS(hello_world_process, "Hello world process");  
9:  AUTOSTART_PROCESSES(&hello_world_process);  
10:    
11:  // the function which gets called each time the rtimer triggers  
12:  static char periodic_rtimer(struct rtimer *rt, void* ptr){  
13:    uint8_t ret;  
14:    rtimer_clock_t time_now = RTIMER_NOW();  
15:    
16:    printf("Hello from rtimer!!!\n");  
17:    
18:    ret = rtimer_set(&my_timer, time_now + PERIOD_T, 1,   
19:          (void (*)(struct rtimer *, void *))periodic_rtimer, NULL);  
20:    if(ret){  
21:     printf("Error Timer: %u\n", ret);  
22:    }  
23:    return 1;  
24:  }  
25:    
26:  PROCESS_THREAD(hello_world_process, ev, data)  
27:  {  
28:   PROCESS_BEGIN();  
29:    
30:   printf("Starting the application...\n");  
31:    
32:   periodic_rtimer(&my_timer, NULL);  
33:    
34:   while(1){             
35:    PROCESS_YIELD();  
36:   }  
37:   PROCESS_END();  
38:  }  

Our program will keep printing Hello from rtimer!!! as the output.

Saturday, June 15, 2013

Bluetooth Programming On Linux



Sometimes we need to write programs that run on a PC and communicate with an external bluetooth capable device such as a smartphone. In such cases we need some good programming library which provide the necessary capabilities to access bluetooth hardware components in our PC easily. While looking for a good programming library for this task I came across a library called Bluez. It seems it has been widely used for Bluetooth programming in linux based environments. So, I decided to take a look at it.

I tried it on a Ubuntu 12.04 system connected with a USB Bluetooth adaptor. First thing is to install the necessary packages. Give following commands to install those packages.

sudo apt-get install bluez
sudo apt-get install python-bluez

After these library packages completes the installation, we will first check whether our hardware setup works. Connect the bluetooth adaptor to the USB port and turn On bluetooth. I had some problems with the default bluetooth manager that comes with Ubuntu 12.04 as I mentioned in my previous post. Therefore I use Blueman bluetooth manager to turn on/off and do anything with bluetooth on my PC.

After turning bluetooth ON, first I checked whether every thing's fine by pairing my PC with a smartphone and sending and receiving some files. Then it's time to check our Bluez libray. Put the following code in the text editor and save as bluez_test.py somewhere in the file system.

1:  import bluetooth  
2:  target_name = "SHV-E210K"  
3:  target_address = None  
4:  nearby_devices = bluetooth.discover_devices()  
5:  for bdaddr in nearby_devices:  
6:    print bluetooth.lookup_name( bdaddr )  
7:    if target_name == bluetooth.lookup_name( bdaddr ):  
8:      target_address = bdaddr  
9:      break  
10:  if target_address is not None:  
11:    print "found target bluetooth device with address ", target_address  
12:  else:  
13:    print "could not find target bluetooth device nearby"  

Please note that the target name variable is set to the name of the external bluetooth device we are going to connect. "SHV-E210K" was the name of my smartphone for bluetooth connection. According to the program, it print the names of available Bluetooth devices around and check whether our target device is available.

Now go to the location of the file from terminal and issue following command to run the python program. 

sudo python bluez_test.py













If every thing is fine, it should print that target device is found just like the one shown above. That means simply our Bluez library is capable of accessing the Bluetooth adaptor and using it. Following link will be a good reference for learning to use Bluez library.

Reference:

Enjoy with Bluetooth  programming on Linux! :)

Wednesday, June 12, 2013

Problem of sending files from Android smartphones to Ubuntu 12.04 via Bluetooth

Recently I wanted to check a USB bluetooth adaptor on a Ubuntu 12.04 system. I plugged the adapter to machine and paired it with a Galaxy S3 smart-phone. Sending files from Ubuntu machine to smart-phone works fine with the default bluetooth application that comes with Ubuntu 12.04.  However when sending files from the phone to the machine, a failure message is shown in phone and Ubuntu system does not notice any incoming file from the paired device.

I searched in web and found that many people have faced this problem. I tried the solution suggested in this link [http://askubuntu.com/questions/211006/unable-to-transfer-file-via-bluetooth-from-android-phone]. According to that, instead of using default bluetooth application, I installed Blueman Bluetooth Manager from software center. Now, sending files from Ubuntu to Android and Android to Ubuntu works fine.