byteLOG.

Hosting on a Pi!

24 Dec 2017 11:55pm

After almost 3 years of deciding on whether to get a Raspi or not, its finally here. Having worked with Arduinos I always imagined the Raspi as an elder brother to the Uno, just another microcontroller. Oh boy was I wrong!

The pros of an Uno? Easier to program, harder to break! The Uno is a nifty little microcontroller for beginners. Its best suited for working with sensors, actuators and other devices. It works well with most of the current computers and has a steep learning curve. (Join this thread if you want to argue on 'steep learning curve')

Coming to the Pi 3 B: its a full fledged SBC (single board computer) that comes with general purpose IO pins for connecting to components using some libraries. Even better thing about model 3 B is that it comes with onboard Wifi chip and BLE & Bluetooth 4.1 support. One better has to have his/her Linux skills and some Python brushed up before working with a Pi. More on the Pi 3 B specs.

"What should I do with it?" ¯\_(ツ)_/¯

I had worked on Arduinos before, but I had not carried my "Arduino-support-hardware" from India with me, all the servos, the wiring, lights, motors, sensors and what not, so a hardware project was out of scope. Now having known that the Pi is a full blown computer, can connect to the internet and can hence act as a server, I thought of hosting a website! This would also be my first foray into web dev! (probably another blog post for that)

All this would have been really simple had the setup been as easy going as for the Uno: Plug-n-Play. Now I miss the Uno :(

"'Headless Pi Setup'.. thats what you need to Google for!" (-_- )

Heads up, this post is just describing how I did things, feel free to use the almighty Google to tinker around and come up with better solutions which I know surely exist.

To start Google Chorme on my laptop, I hit the power button (if I ever accidently shut down my laptop), login to Windows (yeah, bite me! I do VMs) and open Chrome. So the Pi is also a computer: Which OS does it have? Wheres the screen?? Oh wait, where the hell is the power button!?!

Lets break it down.

What we will be doing here is called a Headless Setup. Who came up with this terminology? Who asumed the screen to be the head of a computer? Anyways..

What all do I really need to get the a server running?

  • A computer with a power source(I have the Pi)
  • A screen to look at.

    Who am i kidding? I know SSH now B) , without a screen ought to work.

  • An operating system
  • Internet connection
  • A way to connect to the Pi without connected any hardware.
  • A HTTP web server

Powering up the Pi

Why did I even bother, I still dont have an OS to run or any storage on the Pi nor a way to connect to it.

The Operating System

Raspberry Pi has a microSD card slot on the back side. This is where the SD card with your OS will reside and also most of the data. You can hook up an external storage device as well into one of the 4 USB ports, but thats only for extra storage; OS stays on the card.

First format a microSD card on your computer and write the desired OS image using something like Win32DiskImager (for Windows). I had a 32GB Class 10 that came with the Raspberry Pi.

Now this card has to be loaded with your favoutite Linux distro. The official Raspi website has a good selection of images to download. I went with Raspbian Stretch Lite which is based on Debian Stretch. (Will probably shift to Arch Linux in my next update). Writing an image to the formatted microSD card is pretty easy with Win32DiskImager.

Select the 'Image File' by navigating to the location where you extracted the downloaded image files and select the 'Device' that represents your microSD card (E:/ for me). Hit Write!

Once its done without errors you are done with the OS installation!

Internet Connection

If you are reading this, I assume you have an internet connection. What I meant here was connecting the Pi to the internet. Earlier versions of Raspberry Pi could connect to the internet only using ethernet. All hail the Pi 3 B with on board WiFi!

Now I remembered why I chose Raspbian as my OS: DHCPC daemon!

dhcpcd is a opensource Dynamic Host Configuration Protocol client daemon. IKR, too verbose,but equally important. It runs on Raspbian on boot and this is what helps connect to your WiFi. And we need to configure this serivce to connect to the Wifi on startup by using a file called wpa_supplicant.conf

Open the freshly cooked microSD card drive and add the following wpa_supplicant.conf file to the /boot/ directory (where you land when you open your microSD drive on Windows).

wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
 ssid="SSID_YOUR_RIDICULOUS_NETWORK_NAME"
 psk="YOUR_EVEN_MORE_RIDICULOUS_PASSWORD"
}

This part has set up the Pi to conneccted to your Wifi on boot.

Activating SSH for your Pi

SSH is disabled in RaspberryPi/Raspbian in the latest versions. To enable it, simply place an empty file called ssh (yes, without any extension) in the same place as the wpa_supplicant.conf file. Now you can SSH into the Pi from any computer on the same network on which the Pi is connected.

Power up the RaspberryPi!

What IP address do I SSH into?

Time to move to your Wifi-Router for the last part of the setup. Wait for a couple of minutes to give the Pi time to connect to the WiFi. Open the Web Management portal for your router. Open your routers IP address in any web browser. Its 192.168.0.1 for most of the new routers or you can have a look at the back of your router to find its address, also have a look for the username and password of the portal. For my TP-LINK router its the good old default admin and admin.

Open the DHCP tab (for TP-LINK routers) and check the DHCP Client List for a list of all the devices connected to your router. *fingers crossed* If all goes well, you should see your RaspberryPi in the list. Make a note of its IP address, thats your only 'headless' point of entry into the Pi. You probably wont be having a 'Permanent' Lease Time on your first connection, nor is it required. I'll cover that in my next post.

SSH into the Pi

Non Windows users can start with the terminal and Windows users may have to resort to Putty or even better (and my fav) Bash On Windows for SSH-ing. Use pi as the user and SSH into the Pi's IP address you found in your router. The default password is raspberry

$ ssh pi@192.168.0.101

Aaaaand we are done! Kind of.

There's still a lot of scope in changing raspi-configurations, changing password and creating users, update packages and a lot more customization. But the steps here are enough for a basic setup to get up and running with Pi.

The last step is setting up the actual HTTP server on the Pi. I'll take that up in the next one.

fin.