Jeremy Morgan

Mostly Coherent Ramblings of a Silicon Forest Software Developer

How to Restore Arch Linux Boot After Installing Windows

This week they released the RTM Version of Windows 10, and I decided to install it on my laptop. Most of my development is done on my desktop Mac at home, but the laptop is good for collaborating, coffee shop coding, etc. I decided Windows 10 and Visual Studio 2015 would be good for this. There’s only one issue: I also have Arch Linux on this laptop, and it’s dialed in very nice and I definitely don’t want to remove it.

I haven’t upgraded Windows on this machine since I put Windows 8.1 on it over a year ago, so I never had to deal with the issue of fixing grub. It is slightly different from other distributions, and there is no “boot repair” yet. I decided to Google a few things and figure out how to restore it but didn’t find a complete article on it, so I decided to write one. I installed Windows 10 on the machine without changing any of the partition data, and that’s crucial. After doing that it would only boot into Windows 10, so here’s what I did.

1. Download an Arch Linux ISO

Download a live ISO for Arch Linux here. If you’re using Windows, LiLi is a great free tool for creating bootable Linux USBs.

boot to this USB drive and you’ll be taken to a command prompt.

2. Restore Grub

This assumes of course that you were using GRUB to begin with, but here’s how to restore it.

Find the partition your linux is installed on:

1
cfdisk

You’ll see something like this:

How to restore GRUB in Arch Linux

In my case my Linux installation is at /dev/sda4. Note where yours is, then exit this application.

So mount your Linux partition:

1
mount -t ext4 /dev/sda4 /mnt

Then, change root with this command (specific to Arch Linux)

1
arch-chroot /mnt

and restore GRUB:

1
grub-install /dev/sda

And yes, it does detect Windows 10 properly.

After you reboot, you’ll see your familiar boot screen again:

How to restore GRUB in Arch Linux

And you can now boot into Linux (or Windows) again.

Configure Grub

Now if you’re like me (which you probably are if you’re reading this blog) you will be bothered that it incorrectly says “Windows 8” or whatever your previous version was. This is easy to fix by editing some entries in your Grub configuration:

Make a backup of your grub.cnf

1
sudo cp /boot/grub/grub.cfg /boot/grub/grub.cfg.8.1

Edit the file:

1
sudo nano /boot/grub/grub.cfg

Look for the Windows 8 or Windows 7 entry in the file:

How to restore GRUB in Arch Linux

and change it to whatever you want. Then reboot. Now it should look like this:

How to restore GRUB in Arch Linux

This is how I have mine set up. If you’d like to learn more about GRUB menu options click here.

Summary

This is how you can restore your dual boot options if you reinstall or upgrade Windows on a dual boot Arch Linux system. I have heard of people just reinstalling Linux afterword, and it’s a terrible idea unless you were going to change it anyway. It only takes a few minutes to fix.

A note about Arch Linux:

As I said in the article I have Arch Linux installed and it’s “dialed in” meaning I have a nice system compiled with only what I need, lean custom kernel, and my development environment is all set up. I wouldn’t advise an Arch install to everyone, but it has been fantastic for me personally. It runs very fast and reliably and it’s easy to update everything.

If you’re looking for a new disto, or just want to escape the Ubuntu crowd, you should give it a try. IT has a steep learning curve as you’ll need to know Linux really well, or be able to follow the directions explicitly, but if you do you’ll find a nice solid system that’s fast and lean.

Get Arch Linux Now

-Jeremy


Do you like articles like this?

I’m constantly hacking on stuff and writing about happenings in the programmer world. You can subscribe to my feed here, or you can get the programmer newsletter 100% spam free!

How to Monitor Room Temperature With a Raspberry Pi

Today I’m going to show you how to monitor ambient room temperature with a Raspberry Pi. We’re going to wire up a temperature sensor, gather temperature values and send them to Firebase. This could be the start of a weather station or other crazy experiments. In my case we’re going to check the temperature of a room to make sure our newborn puppies don’t get too hot or cold during the day.

What you’ll need:

  • Raspberry Pi (I am using a Raspberry Pi 2, but you don’t have to)
  • Breadboard
  • Temperature Sensor
  • A Firebase account (optional)

Notice: I have updated the source code for this article and built an ASP.Net Web API for this project. Watch them on GitHub to get notified when it’s updated.

Hooking up the Hardware

I ordered the DS18B20 Waterproof Digital temperature sensor + extras from Adafruit ( Get it here ) which is an excellent value at $10.

Monitor room temperature with Raspberry Pi

Out of the box the wires are stripped, but since I’m putting this in a bread board, I decided to solder them. I quickly learned my last roll of solder is rather poor quality, but it will work:

Monitor room temperature with Raspberry Pi

I then wired it up to the breadboard. There are plenty of diagrams online for wiring this up, I hooked

  • Red: +3v
  • Yellow: GPIO 4
  • Blue: Ground

Monitor room temperature with Raspberry Pi

The kit comes with a 4.7k resistor which you place between the red and yellow. The breadboard I use is from the Canakit for Raspberry Pi. This is connected to a Raspberry Pi 2, although a Raspberry Pi B would work just the same.

Testing the Sensor

The support for the DS18B20 on the Pi makes this part really trivial. First you need to edit /boot/config.txt:

1
sudo nano /boot/config.txt

and add the following line:

1
dtoverlay=w1-gpio

Monitor room temperature with Raspberry Pi

Then save and reboot the Pi.

To test the functionality, load in the kernel modules:

1
2
sudo modprobe w1-gpio
sudo modprobe w1-therm

Run the following to find your device:

1
2
cd /sys/bus/w1/devices
ls -la

This will show you the directory your device is in, based on your serial number. It should start with 28x:

Monitor room temperature with Raspberry Pi

cd into that directory, then type:

1
cat w1_slave

and it should look like this:

Monitor room temperature with Raspberry Pi

Look for crc=da YES. The YES means it’s working, and the t=n is the celcius*1000. This verifies the sensor is functioning.

Writing the Temp Gathering Script.

For this step, you will be creating a Python script to read the sensor. You can use Nano on the Pi, or use an IDE on your desktop and copy the file up to the Pi. I used PyCharm in Linux to write the script then copied it up to my Pi with each iteration with the following command:

1
scp readtemp.py pi@192.168.1.12:/home/pi

This can be a little more fluid when developing.

The initial file we’ll create is some code lifted from the Adafruit tutorials:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f

while True:
    print(read_temp())
    time.sleep(1)

This is a super simple script that loads in the kernel modules and reads the device. The function read_temp() opens the device and reads it by parsing through the lines and spitting out Celsius and Fahrenheit values. Then at the bottom we have a loop to do this every second.

1
python readtemp.py

Your output should look like this:

Monitor room temperature with Raspberry Pi

This is great, but we can’t do much with it. At this point you could just write these values to a file and parse them out later. But we’re going to store these values in a datastore.

In readtemp.py, let’s make some changes.

Remove the loop:

1
2
3
while True:
  print(read_temp())
  time.sleep(1)

and replace it with this:

1
print str(calendar.timegm(time.gmtime())) + ',' + str(read_temp())

Then add the following lines to the top:

1
2
import calendar
import time

Run the file again and you should see this:

Monitor room temperature with Raspberry Pi

We’re getting closer! Here is what the final script should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import glob
import subprocess
import calendar
import time
import urllib2
import json

#initialize
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

#device
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

# Opens raw device, code changed to reflect issue in Raspian
def read_temp_raw():
    catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out,err = catdata.communicate()
    out_decode = out.decode('utf-8')
    lines = out_decode.split('\n')
    return lines

# Reads temperature, outputs farenhiet
def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_f

## Please add some authentication or a secret here!

url = 'https://yourfirebaseapp.firebaseio.com/readings.json'

postdata = {
    'date': str(calendar.timegm(time.gmtime())),
    'temp': str(read_temp())
}

req = urllib2.Request(url)
req.add_header('Content-Type','application/json')
data = json.dumps(postdata)

response = urllib2.urlopen(req,data)

Pushing the results to Firebase

This is an optional step, but here we are going to push the results up to Firebase. You can create an account for free, and I would encourage you to check it out. The URL will be the url of the project you create and the name of a JSON file to use for storage. For example:

https://mycooltemperatureapp.firebaseio.com/readings.json

You can replace with Firebase with any API call at this point, and it’s easy to make a POST in Python:

Add the following includes:

1
2
import urllib2
import json

Then add the following to the bottom:

1
2
3
4
5
6
7
8
9
10
11
12
url = 'https://mycooltemperatureapp.firebaseio.com/readings.json'
postdata = {
    'date': str(calendar.timegm(time.gmtime())),
    'temp': str(read_temp())
}

req = urllib2.Request(url)
req.add_header('Content-Type','application/json')
data = json.dumps(postdata)

#you don't have to do anything with response
response = urllib2.urlopen(req,data)

Now run the file a few times, and open up your Firebase console, and you should see something that looks like this:

Monitor room temperature with Raspberry Pi

Congrats! you’re now storing data. You can export this to a CSV or create a JavaScript application to display it. I’ll be covering what we do with this data in a future tutorial.

NOTE: You really should add some authentication to your Firebase database if you care about the data.

Create a CRON Job to Automatically gather data

There’s two ways we can run this script automatically. We can add the loop back in, and just run the Python script and keep it running. You can use something like nohup to run it as a service but that’s a little shaky. I’d rather just run the script once a minute with a CRON job. To do this, type in

1
crontab -e

to run the cron editor. Add an entry like this:

1
* * * * * /usr/bin/python /home/pi/readtemp.py

Monitor room temperature with Raspberry Pi

And save the file. CRON will automatically update and start running this command every minute (or whatever you set it to).

This will ensure that your script is run at regular intervals. You can set it to run every minute, hourly, daily or whatever you’d like. Here’s some more information about scheduling cron jobs.

Summary

I hope this tutorial has helped show you how to monitor ambient air temperature with a Raspberry Pi. If you purchase this same sensor it’s waterproof which means there is much more you can do with it. You can also hook up multiple sensors to the Pi, separated by serial numbers.

There are tons of neat things you can do with this information, and I encourage you to experiment. In a future tutorial we’ll take some of this data I’ve gathered and do something with it. Thanks for reading.

You can download the source code for this article at GitHub.


Do you like my Rasbperry Pi Tutorials?

I’m constantly hacking on stuff with the Raspberry Pi. If you want to get notified every time I post a new article subscribe to my Raspberry Pi feed here, or you can Sign up for the Raspberry Pi Newsletter spam free!

Two Awesome Tools You Need Right Now

I have recently been using a couple of tools every day, in both my professional and and personal work and feel like sharing them here today. These tools will help you tremendously as a developer and they just happen to be free.

Brief Introduction to REST

Perhaps you’ve seen the term REST being thrown around lately and have been a little curious what it’s all about. If that’s the case, or you know a little about it but never used it, this article is for you. Today I’m going to give a small overview of what REST services are, and why it’s awesome.

How to Install ASP.Net 5 on Ubuntu Linux

One of the primary goals of the ASP.Net 5 (codenamed vNext) project is cross platform development, and it’s become easier than ever to develop ASP.Net applications on Linux and OSX. Today we’re going to take a look at how to do this with Ubuntu Linux server.

Open Graph Tags Are Terribly Important

Today I want to talk about something that’s pretty important for your blog, even if it’s a tech blog. It’s Open Graph tags, and it’s a fairly easy way to get more people to your site, and get it noticed. I haven’t done an SEO or Marketing type article on this site in two years, but I think this is pretty fitting for anyone working on websites. Is your company doing this? They should be.

How to Blink an LED on a Raspberry Pi 2

The blinking LED is the “hello world” of the maker community, and today I’ll show you how easy it is to do with the Raspberry Pi 2 (or Model B)! We’re going to use Python and WiringPi for this project.