Monday, January 4, 2016

Tutorial - How to set up simple home automation system - Part IV

Raspberry Pi - The Internet


On this page

Internet - Wifi

Your Pi will need access to the Internet. If you're connected to your router via the ethernet port, skip to the next section. Wifi people, read on.
Like most house guests, your Pi wants to know how to connect to your wifi. We also want your router to assign a fixed IP address to the Pi so we can always find it. There are two configuration files we need to edit for this. First:
sudo nano /etc/network/interfaces
The example below is for a network that uses the 192.168.1.x range. In the example, the Pi will have address 192.168.1.80 and the router is at 192.168.1.1. If your local network uses a different IP range, make sure to follow that instead.
auto lo
iface lo inet loopback
allow-hotplug wlan0
auto wlan0

iface wlan0 inet static
address 192.168.1.80
netmask 255.255.255.0
gateway 192.168.1.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
When you're done modifying the contents of the file you press CTRL+X to exit. The program will ask if you want to save so you press Y for Yes and Enter to confirm the file name.
Now in the second configuration file you enter your wifi details:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
This file might be empty. Add the text below and replace "wifiname" and "wifipassword" with your network's details.
network={
ssid="wifiname"
psk="wifipassword"
key_mgmt=WPA-PSK
}
If your wifi network is more complicated, you can find online guides by googling "wpa_supplicant raspberry pi". When you're done, again exit with CTRL+X, Y, Enter.
Now you can skip the ethernet section and go directly to Check Internet & Unplug.

Internet - Ethernet

If you're already connected over wifi, skip to the next section.
By default, your router will assign a random IP address to the Pi. We want to change this to a fixed address so you can always find your Pi. For this we need to edit the network configuration file:
sudo nano /etc/network/interfaces
The example below is for a network that uses the 192.168.1.x range. In the example, the Pi will have address 192.168.1.80 and the router is at 192.168.1.1. If your local network uses a different IP range, make sure to follow that instead.
auto lo
iface lo inet loopback
auto eth0

iface eth0 inet static
address 192.168.1.80  
netmask 255.255.255.0
gateway 192.168.1.1
When you're done modifying the contents of the file you press CTRL+X to exit. The program will ask if you want to save so you press Y for Yes and Enter to confirm the file name.

Check Internet & Unplug

After you change settings it's usually a good idea to reboot your Pi and see if she understood you:
sudo reboot
When it's back up you can ping Google to see if you have Internet access:
ping www.google.com
If Google is replying, you're good. Exit "ping" using CTRL+C.
If ping is timing out (and you can access it fine from your other computers), double-check for typos in the configuration files. If that doesn't help, find help online or use your google-fu for clues.
Now that the Pi has Internet access and a fixed IP address, we no longer need to be connected to it. Before you unplug you should shut down your Pi:
sudo halt
Wait about half a minute for it to finish shutdown and then unplug screen, keyboard and mouse. You'll no longer need them. Then unplug & replug the Pi's power to boot it back up.
From now on you'll connect to the Pi using MobaXterm on your computer, so you can open that program now. Give your Pi a minute to boot up and then start a session by clicking the session icon in the top left corner. Then enter you Pi's address and "pi" as the username.

Connecting to your Pi via MobaXterm.
After you're logged on, all that's left to finish setup is to update the software on your Pi to the latest versions. This is like running Windows Update, but for your Pi. First run update, then upgrade:
sudo apt-get update
sudo apt-get upgrade

Make A Backup

Once your Pi is done updating (and this might take a while, especially the second step) I strongly advise you to MAKE A BACKUP of the SD card.
Why? While playing around and testing things you might mess things up beyond repair. If you have a backup from this point in time, you'll save yourself a lot of frustration (and time) if you can go back to a point where you have a clean image all ready to go.
Before you can take out the SD card you need to shut down your Pi:
sudo halt
When it's powered down take out the SD card, plug it in your Windows computer and use Win32 Disk Imager to read from the SD card to an image file you save somewhere on your computer. Give it a file name that will tell you what it is, likeraspbian_clean_configured_usethiswhenyouscrewup_20150201.img.
Now you can put the SD card back in your Pi, boot it up, and not worry too much about making mistakes.


No comments:

Post a Comment