Tuesday 12 January 2016

Controlling Your Raspberry Pi Remotely

After we had a running Raspberry Pi with operating system inside, it is time to control our Raspberry Pi remotely. Because we don't want to stuck in a boring place when we configure our Raspberry Pi, do we? So I would like to share about control our Raspberry Pi remotely and configure it everywhere we want as long it has same network between the Raspberry Pi and a controller.

To control our Raspberry Pi, our controller device (another computer or smartphone) must have a same network address with our Raspberry Pi or we can say it these devices must in same Local Area Network (LAN). Generally speaking, we can control our Raspberry Pi using SSH protocol (Port 22 in default).

So, what we can do to control our Raspberry Pi? In this idea, I would like to share my knowing about controlling our Raspberry Pi remotely.

1. The Components

For the needed component we just focus on the Raspberry Pi, because it is the main actor here. For the rest, we can set the configuration automatically. 
  1. Raspberry Pi that already have operating system inside
  2. Ethernet STRAIGHT cable configuration
  3. Wireless Access Point

2. The Topology

After all components is gathered, it is time to make our devices in the same network or on same local area network/ To do that, we must make our topology first. The common topology that is used for local network is Star Topology. The Star Topology happen when there is a hub/switch as a center of node  is connected to the rest of nodes (computer, laptop or smartphone) around. 


3. The Setting

The first thing to be set is the wireless access point. We must configure the IP of access point as a gateway and the distributed IP for the rest of the device. For the configuration, we can refer to the access point manual book. This step is to make the local IP so the devices (laptop, computer, smartphone and Raspberry Pi) is in the same network. In this case I use 192.168.0.0 as local network address.

After that, let's go through to our Raspberry pi configuration. For the Raspberry Pi configuration, I make it a static IP (I used 192.168.0.16) to be easy to remember and if the network accidentally reset by itself the configuration is still the same. Because if we make the configuration by default, when we reset the Raspberry Pi, there is a possibility that the IP will change to another IP address.

To configure the IP permanently, we must edit some lines in file /etc/network/interfaces. Don't forget to backup the file first before we edit it.

 sudo cp /etc/network/interfaces /etc/network/interfaces.bak

Then edit the file.

 sudo nano /etc/network/interfaces

Edit and add some lines focus on eth0 device.

 auto eth0
 allow-hotplug eth0
 iface eth0 inet static
       address 192.168.0.16
       gateway 192.168.0.1

Save the configuration and restart the service.

 sudo /etc/init.d/networking restart

After that, we must ensure that our Raspberry pi has the ssh service by type ssh on the terminal. But by default the Raspberry Pi already has this service. If our Raspberry doesn't have this service, it will appear an error message.

 -bash: ssh: command not found

Then we must install the package.

 sudo apt-get install ssh

Congratulation, with these configuration our Raspberry Pi is ready to be controlled remotely via terminal later.

But if we left it like these, we just can control our Raspberry Pi via terminal. There is another way to control the Raspberry Pi via desktop. To make it happen, we must install another service. The service we want to install is xrdp. This service allows us to control the Raspberry Pi via the Raspberry Pi desktop.

Install the xrdp package

 sudo apt-get install xrdp  

Now we can control our Raspberry Pi via it's desktop.

4. The Remote Tools

After we have set the Raspberry Pi so it can be controlled from other device, it is the time to set the controller device. Usually the controller device is always another computer (laptop or PC). But how about control our Raspberry Pi from our smartphone? In this idea I would like to share how to control our Raspberry Pi not only from our computer but also from our smartphone.

The important thing before we control our Raspberry Pi remotely is we must make sure that our controller device and the Raspberry Pi is in the same local Network. To make this happen, we just configure our controller device's network address to be the same as our Raspberry Pi's network address or we can just set our device to automatically obtain the IP address. So we don't have to define the IP address. It will be defined by the wireless access point. Because our access point will distribute the IP address automatically based on the number of active devices.

In this case I assume that we have already set the IP address for the controller device in the same local network as our Raspberry Pi.

Control Raspberry Pi from other computer

There are plenty of tools that is used to control our Raspberry Pi remotely. The common tool is called putty and we can get the tool from putty official site. Putty is free and open-source terminal emulator to control the remote device. Another tool that we can use is Remote Desktop Connection. Remote Desktop Connection is built-in program on Windows operating system. From Remote Desktop Connection we can control the Raspberry Pi from the Raspberry Pi desktop. In this idea I would like to try these tools.

Putty
Putty is a portable software. So once we download putty from it's official site, we don't have to install putty to our computer. Just double click on putty icon and it will appears putty window interface.

Input our Raspberry Pi IP address (192.168.0.16) and use port 22 as default post. Then click "Open".


Then we directly connected to our Raspberry Pi terminal and will be asked for username and password. Input our Raspberry Pi username and password.


Congratulation buddy, now we can control our Raspberry Pi from the Terminal.

Remote Desktop Connection
Remote Desktop Connection is Windows built-in program that can control the host computer remotely from host's computer desktop. On Mac, there is a same tool like CoRD. But in this idea I just describe how to control our Raspberry Pi using Remote Desktop Connection. Because I use Wndows.

Open the Remote Desktop Connection in Window -> All Programs -> Accessories -> Remote Desktop Connection.

Input our Raspberry Pi's IP address and click "Connect".


And it will direct you to xrdp login window. Input our Raspberry Pi username and password.


Then we go trough our Raspberry Pi desktop.


Great job fellas, now we can control our Raspberry Pi from Raspberry Pi desktop.

Control Raspberry Pi from our smartphone

Another way to control our Raspberry Pi is from our smartphone especially for android. When I tried to search an app to do remote connection like putty, there are a lot of apps that similar to each other. But just one app that have my attention. The app is JuiceSSH. I choose this app because I think it is easy to use. We can download this app to our smartphone from google apps store freely.

After we download the JuiceSSH app from google apps store and open the app on our smartphone.


Make a new connection and input our Raspberry Pi username followed by it's IP address (pi@192.168.0.16)


Then we will be asked for our Raspberry Pi password.


Input the password of our Raspberry Pi and we directly connected to our Raspberry Pi Terminal


TADA!!! No we can control our Raspberry Pi from our smartphone.

That's all the sharing idea. I hope this will be useful for you. Thanks for your attention.



Sunday 10 January 2016

Raspberry Pi: Setting Your Display

After we already run our Raspberry Pi with Raspbian "Wheezy" operating system at the first time as described on previous idea, it seems that something wrong with the output display on our monitor/TV. The display is overlap with our monitor/TV.



So, in this idea I would like to share how to set and fix the overlap display. 

The default resolution that the Raspberry Pi has is 1280 x 720. Sometimes, some monitors/TVs doesn't support this resolution. So we must set the display to fit the output display on our monitor/TV. To set the display, we just edit 4 lines in /boot/config.txt.

Here we go for the step to fix the display problems.

Step 1: Login to the Raspbian "Wheezy" operating system

Before we go through, as always, we must login to the Raspbian "Wheezy" operating system. Enter your username and password. If you didn't change the password, it will be username: pi and password: raspberry.

 raspberry login: pi
 Password: raspberry

Step 2: Backup the original file

Remember: For now and next time before we edit some files, we must back the file first. So, if there is something happen after we edit the file, we can revert it back to original default setting.

 sudo cp /boot/config.txt /boot/config.txt.bak

Step 3: Edit the file

After we have the back up file, it is time to edit the file.

 sudo nano /boot/config.txt

To fix the overlap display we just edit 4 lines only. Find the overscan_ lines and bash it out.

 overscan_left=40
 overscan_right=50
 overscan_top=40
 overscan_botton=15

Edit the value as you like and save it.

Step 4: Feel the difference

After the edit step is done, it is the time to feel the difference by reboot our system.

 sudo reboot  

And see what happen to the output display on our monitor/TV


If the display still overlap with the monitor/TV, we can just edit the overscan value and reboot it again.


Raspberry Pi: First Run

Raspberry Pi, or people call it Raspi, is a low cost mini computer. With the Raspberry Pi, people enable to explore computing and learn how to program in languages like Scratch and Python. It is like a normal computer, but a mini one. It is capable of doing everything like the normal PC can do, from browsing, and playing high definition video.

In this idea, I would like to share about the Raspberry Pi first run. Raspberry Pi is like a normal motherboard PC, but mini, without operating system and using SD card to store the operating system. So after we have a Raspberry Pi, we must install operating system into the SD card so we can do whatever we want like a normal PC.

The Components

To do the Raspberry Pi first run, we need several components:
  1. Raspberry Pi (I used Model B rev2)
  2. 5V phone charger
  3. TV/Monitor (with cable to connect it to Raspberry Pi, I used RCA cable)
  4. Keyboard 
  5. SD Card (min. 2GB)
  6. Computer with SD card reader
  7. Some accessories like (LAN cable, internet connection, etc)

Selecting Raspberry Pi Operating System

Since the Raspberry Pi use ARM processor, it cannot run the same operating system like our computer. Instead it must run one of the operating systems that have been optimized and ported to the Raspberry Pi processor and hardware architecture. There are plenty of the Raspberry Pi operating system like Raspbian, Arch Linux, RetroPi, ect.

In this idea, I used Raspbian "Wheezy" operating system. This operating system is the standard operating system to run on the Raspberry Pi and has very good Integration with the Raspberry Pi's hardware. We can get the operating system at Raspbian official site.

Supporting Tools

To get this idea or our Raspberry Pi run smoothly for it's first run, we need some programs as supporting tools to transfer the image to SD SDFormatter and Win32DiskImager.

Getting Started

Once we have the formatter and imager. Insert the SD card into our computer card reader. Firstly, we format the SD card using SDFormatter. Actually we can use the default formatter in our computer, but sometime it doesn't format the SD card clearly.


After we format our SD card, then we extract the downloaded Raspbian "Wheezy" to get the image file. Because when we downloaded the file, it was the zip format.

Next, Run the Win32DiskImager and select our Raspbian "Wheezy" image file. Then select the SD card device drive (in this case my SD card on drive H:). Click "Write" and wait until the writing process is done.


While waiting, we can connect the Raspberry Pi to USB keyboard, mouse, monitor or TV.

First Boot

Once we have Raspbian "Wheezy" in our SD card, plug it into the Raspberry Pi's SD slot.



Connect the 5V phone charger to the Raspberry Pi and the we will see the boot sequence on our TV/Monitor (if not, check the connection between the Raspberry pi with the Monitor/TV). After the first boot, the Raspberry Pi will show the setup mode.


If this doesn't happen we can manually enter the setup mode while type the following command.

 sudo raspi-config  

In this setup we want to set:
  • Expand the Filesystem
  • Our new password (default username: pi and password: raspberry but it is optional)
  • Set locale and time zone
  • Enable Desktop boot (optionally)
After that, we can now reboot the Raspberry Pi and enter Raspbian "Wheezy" operating system with default username and password (if we didn't change the password) and we can configure the Raspberry Pi in the way we want to.

Reliable Sources












Saturday 19 December 2015

Arduino I2C Tutorial

As I have explained my understanding about I2C Communication before, how to deal and make a communication between microcontroller and I2C device. Here I would like to share a little tutorial about Arduino I2C.

Today, there are literally thousands of devices using I2C interface. Many application like RTC, digital compass, temperature and so on using I2C. It is because, I2C provide simplicity in wiring and communication. Even within 2 pins, SDA and SCL, we can control with up to 128 devices as slaves. Here, Arduino gives us an easy way to communicate with these I2C devices.

On Arduino boards, depends on Arduino type, there are two pins out that are used for I2C. For Arduino UNO, I2C pins out using Analog pin 4 (A4) as SDA and Analog pin 5 (A5) as SCL. But Arduino Mega, This board use another pin as I2C pins, pin 20 as SDA and pin 21 as SCL. So this will deferent too with other Arduino board.


Arduino Board I2C pins
UNO, Ethernet A4(SDA), A5(SCL)
Mega 20(SDA), 21(SCL)
Leonardo 2(SDA), 3(SCL)
Due 20(SDA), 21(SCL), SDA1, SCL1

To communicate with I2C devices, Arduino has spoiled us with it's library. With this Arduino's library, we can communicate easily with all I2C devices. The Arduino's library that allow us to communicate with I2C devices called Wire.h. In this library, there are some functions to help us to make an I2C communication.

In this opportunity, I would like to make a tutorial how to use these functions. In this library, I separate the functions into 4 sections: Initiating, Transmitting, Receiving, and Responding in Slave Mode. 

Initiating

This section is to initiate the Wire.h library and to define our Arduino as a master or a slave.

Wire.begin()
Initiating the Arduino in a master mode, with this function we can control the data transfer when connecting with other I2C devices.

Wire.begin(address)
Initiating the Arduino in a slave mode, filled with 7-bit slave address. We can define our I2C unique address here. For more detail about I2C addressing you can find it here.

Transmitting

This section is to communicate (starting, writing, and stopping) between master and slave. Used at master side.

Wire.beginTransmission(address)
Begin a transmission to I2C slave device with device's unique address.

Wire.write(data)
Write data from I2C slave device in response to a master's request, or queue bytes for transmission from master to slave. Place it between beginTransmission() and endTransmission().

Wire.endTransmission()
End a transmission to I2C slave device, and transmit the queue data.

Receiving

This section is to receive the buffered bytes of data from slave to master.

Wire.requestFrom(address, count)
Request "count" of data from I2C slave device at "address". Used at master side

Wire.available()
Return the number of bytes available to be retrieved by read(). This function should be called after function requestFrom() at master side or inside onReceive() handler at slave side.

Wire.read()
Read bytes of data that was transmitted from slave to master after asking for request in requestFrom() function or transmitted from master to slave.

Responding in Slave Mode

This section is only used at I2C slave device to communicate with master device (if we want to make our other Arduino act as a slave).

Wire.onReceive(handler)
Register a function to be called when a slave device receives a transmission from master or when master sends data. handler is the function to be called when the slave receives data, this should take a single int parameter and return nothing.

Wire.onRequest(handler)
Register a function to be called when a slave device sends a transmission to master or when master request for data. handler is the function to be called, takes no parameters and return nothing.

Reliable Sources