Nuffnang Ads

Sunday, 16 November 2014

Raspberry Pi with DS18B20 temperature sensor (part 1)

1. Hardware needed in this tutorial:-
     a) raspberry pi,
     b) DS18B20 sensor,
     c) 4.7k Ohm resistor
     d) some wires
     e) breadboard (optional)

2. Connect DS18B20 sensor to PIN7 (GPIO 4), the connection actually same as this post.

3. To read the data from sensor, some of the modules need to be loaded first. Run the following command:-
  modprobe w1-gpio
  modprobe w1-therm

4. Check whether raspberry pi can detect DS18B20 or not. Access to the /sys/bus/w1/devices. Then, run "ls" command. If the sensor is detected by raspberry pi, there will be an output start with 28....

5. Use "cat" command to display the temperature. The full command will be "cat /sys/bus/w1/devices/28*/w1_slave. See 't' value. In picture below, t=28437 means current temperature is 28.437 degree celcius.

Connect raspberry pi to a password protected AP (WPA2)

1. This tutorial use TL-WN725n wifi dongle attach with raspberry pi. Follow this link for driver installation.
2. Attach the TL-WN725n wifi dongle to raspberry pi. Open raspberry pi terminal, install wpa-supplicant and wireless-tools

3. Edit /etc/wpa_supplicant/wpa_supplicant.conf. For WPA2 encryption, edit as following (note: SSID and password depends on your AP):-

4. Edit /etc/network/interfaces. Make sure wireless interface is set to dhcp so it can obtain IP address from AP's DHCP server.

5. Run /etc/init.d/networking restart or reboot. Run ifconfig to make sure wireless interface got an IP address.
"wlan0" get an IP address from AP's DHCP server

Tuesday, 4 November 2014

Installing Avahi (Zeroconf) for raspberry pi

If raspberry pi get an IP from the DHCP server at the home modem/router, the IP address may change after we reboot the router or raspberry pi itself. So next time if we want to access raspberry pi using ssh through IP address, we have to use "nmap" to scan the new IP address. We can overcome this difficulties by installing avahi (zeroconf) at the raspberry pi.

1. At the raspberry pi terminal, simply run "apt-get update & apt-get install libnss-mdns".

2. After the installation complete, run "ps -A | grep avahi" command to make sure the avahi-daemon is running

3. Now open command prompt from another computer that connected to the same home router, and try to ping to raspberry pi hostname. If you are not sure your raspberry hostname, use "cat /etc/hostname". By default the name should be "raspberrypi". The ping command should be "ping raspberrypi.local" add .local after the username. If your machine is Windows, follow the step 3.1.

To retrieve the raspberry pi's hostname

Now we can ping to raspberry pi even without knowing the its IP address

3.1 In Windows by default it doesn't support Zeroconf service. We can install Apple software, iTunes that comes together with Bonjour service (based on Zeroconf). But if you just want to install Bonjour service, follow the following steps:-

   a) Download iTunes.
   b) Right click and open with WinRAR

   c) Double click on "Bonjour.msi"

   d) Install Bonjour. Once the installation finished, right click on "Computer" >> Manage >>                      Services >> Start Bonjour service.

   e) Now we should be able to ping or ssh to the raspberry pi from Windows machine. 

4. To use modify the username for raspberry pi simply use "hostname <new hostname>"

5. You can also simply ssh to the raspberry pi using its hostname. Dont forget to add .local after the hostname. See example below.

Wednesday, 24 September 2014

Installing Open-WRT in TP-Link MR3020

1. Connect Ethernet cable form TL-MR3020 to the computer. Your computer will get an IP in subnet.
2. Download the Open-WRT firmware from here.
3. From computer access at web browser, go to "System Tools" >> "Firmware Upgrade" >> "Choose File" >> choose the downloaded Open-WRT firmware at Step 2. Click "Upgrade".

4. After a few minutes, TL-MR3020 will reboot. After reboot, the IP address of the TL-MR3020 will be changed to But you cannot access the at web browser since Luci package is not installed yet.

5. Use putty and access using Telnet.

6. Set the password using "passwd" command.

7. To get the WiFi internet access, edit /etc/config/wireless as show below. Make sure "option disabled 1" is commented, and fill the correct SSID, encryption, key based on your WiFi.

8. Edit /etc/config/network. See interface 'wan' as shown below. Make sure option proto 'dhcp' so TL-MR3020 can obtain IP address from the WiFi hotspot. Save and exit. Run "wifi" command.

9. Run "ifconfig wlan0". Make sure wlan0 has IP address.

10. Try to ping to

11. To install web GUI, we need to install Luci. Run "opkg update", "opkg install luci", "opkg install luci-theme-openwrt"

12. Run /etc/init.d/uhttpd start.

13. Now we should be able to access the TL-MR3020 through web GUI.

14. Done


Tuesday, 16 September 2014

16x2 LCD Hello World (using C + WiringPi)

Thanks to WiringPi who made the GPIO porting become simple. This entry will show the "hello world" LCD tutorial in C using WiringPi.

Basic info about 16x2 LCD:-

16x2 LCD

16x2 LCD pinout:-
1   = VSS (GND)
2   = VDD (VCC,5V)
3   = VE (connect with variable resistor)
4   = RS, Register Select
5   = RW, Read/Write
6   = E, Enable
7   = Data0, D0
8   = Data1, D1
9   = Data2, D2
10 = Data3, D3
11 = Data4, D4
12 = Data5, D5
13 = Data6, D6
14 = Data7, D7
15 = Backlight Anode (VCC)
16 = Backlight Cathode (GND)

For 16x2 LCD, usually  requires 8 data lines to provide data to Bits 0-7. However it also can enable “4 bit” mode which allows you to send data in parallel of 4 bits. This approach can reduces the number of GPIO connections when connection with Raspberry Pi.

1. You can found plenty of site that show the LCD to GPIO wiring. This tutorial will use 4 bit mode. The wiring can follow shown as below:-

2. After done the wiring, make sure you already installed WiringPi, if not, please refer to the previous post. Run "gpio readall" command to see the WiringPi pins.

GPIO mapping with WiringPi
Pin 12 = wPi 1 = D7
Pin 16 = wPi 4 = D6
Pin 18 = wPi 5 = D5
Pin 22 = wPi 6 = D4
Pin 24 = wPi 10 = E
Pin 26 = wPi 11 = RS

3. Place the code below to display "HELLO WORLD" text on the top line of the LCD. Make sure the WiringPi pinout and LCD pinout are correct. Refer to the 2nd step for the pin mapping.

#vi lcd-test.c
in lcd-test.c,
#include <wiringPi.h>           //WiringPi headers
#include <lcd.h>                //LCD headers from WiringPi
#include <stdio.h>              //Needed for the printf function below

//Pin numbers below are the WiringPi pin numbers

#define LCD_RS  11               //Register select pin
#define LCD_E   10               //Enable Pin
#define LCD_D4  6               //Data pin 4
#define LCD_D5  5               //Data pin 5
#define LCD_D6  4               //Data pin 6
#define LCD_D7  1               //Data pin 7

int main()
    int lcd;                //Handle for LCD
    wiringPiSetup();        //Initialise WiringPi

    //Initialise LCD(int rows, int cols, int bits, int rs, int enable, int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
    if (lcd = lcdInit (2, 16,4, LCD_RS, LCD_E ,LCD_D4 , LCD_D5, LCD_D6,LCD_D7,0,0,0,0)){
            printf ("lcd init failed! \n");
            return -1 ;
    lcdPosition(lcd,0,0);  //Position cursor on the first line in the first column
    lcdPuts(lcd, "HELLO WORLD");  //Print the text on the LCD at the current cursor postion
    getchar();                      //Wait for key press

4. Compile lcd-test.c using

5. Run using

6. "HELLO WORLD" text will be displayed at LCD


Wednesday, 10 September 2014

Raspberry Pi GPIO - LED on/off (wiring Pi + php)

This entry is the third entry for the GPIO on Raspberry pi. Assuming readers already installed the wiringPi, just follow the below step to make LED can be controllable using web GUI.

1) Type the following command to install the necessary packages at the Raspberry pi. Make sure your Raspberry pi has internet connection.

sudo apt-get update
sudo apt-get install apache2 php5 libapache2-mod-php5

2) Now, from your computer,, open the web browser and type the IP address of your Raspberry pi. You will get the "It works!" page. This means the apache and php successfully installed in your Raspberry pi.

3)Now cd to /var/www and edit the index.html to index.php.

4) Edit the index.php as the following to create two simple button (on,off) that will link to execution of the gpio commands. The command "system("")" is used to execute the linux command

    if (isset($_POST['button']))
         system("gpio mode 0 out");
        system("gpio write 7 1");
    else if (isset($_POST['btn']))
        system("gpio write 7 0");
    <form method="post">
        <button name="button">On</button>
        <button name="btn">Off</button>

5) Save index.php and exit. Open the web browser any machine that connected to the same router and enter the Raspberry Pi's IP address. You will get the page similar to this:-

Click "On"/"Off: to turn on/off the LED

LED on

LED off

6) Done


Raspberry Pi GPIO - LED blinking (wiring Pi + bash script)

This entry is same as the previous post except, this time using the WiringPi method.

I assume that, the LED already connected to the GPIO as shown in here.

1) Open the Raspberry pi terminal, run the following commands:-

sudo apt-get install git-core
sudo apt-get update
sudo apt-get upgrade
git clone git://
cd wiringPi

2) To verify wiring pi, run the following command:-
gpio -v

3) In wiringPi, GPIO4 => GPIO.7. First make the pin as output pin. The command is:
"gpio mode 7 out". Verify the GPIO.7 pin mode using "gpio readall"
GPIO.7 mode = OUT

4) To turn on LED, use "gpio write 7 1". We also can verify the GPIO status using gpio readall command.
gpio readall output when LED off

gpio readall output when LED on

5) Create a new bash (.sh) file

6) I wrote a simple bash script to make the LED blinking 100 times for every seconds.

7) Save the .sh file and make it executable by using chmod +x

8) Run the bash script
LED is blinking 

9) Done