DEV Community

Dave
Dave

Posted on

Better NixCraft Raspberry Pi Temp Script

Back near Christmas last year, I purchased my first Raspberry Pi as a test to see if I could build a Minecraft Server for our family. It worked, but the 1GB version I purchased would only support one player at a time. It could support two if the household LAN had little other traffic.

This past weekend, I purchased the 4GB Raspberry Pi 4 in the hopes that would solve much of my problems. It did. I think we can now support the five total players that I care to admin.

However, lo and behold, it left me with this perfectly good Raspberry Pi 4 left over. What will I ever do now? You can bet your sweet bippy that I'm going to put it to work doing something.

In perusing Raspberry Pi's downloads, I discover that there is now an Ubuntu Server version just for my Pi. I have nothing against Raspbian. It serves our needs on the MinePi server well, but for my own uses, I'd rather use Ubuntu. I downloaded the image, got it stuck onto my microSD card and loaded up the Pi.

I had a little difficulty as I didn't have an HDMI monitor, nor a keyboard at home to use. I did manage it with a tweak to /etc/netplan (yes, dangerous move) and an Ethernet cable. It worked and I'll try to write that article later today, if time permits.

I am up early this morning (because I'm always up early) and decided to start the work of configuring my new Pi server. After getting aptitude loaded, I wanted to see about CPU temp. I'm not running this one with a fan or box just yet. It's just a test bed, but that means I need to watch the temperature closely.

I found out quickly that vcgencmd isn't in the repository and regular sensor apps probably wouldn't work. This is an ARM, not an Intel clone.

Did some digging and found a bash script on NixCraft that showed where the CPU temp is kept in the file system. They offered a small bash script to convert the stored integer into Celsius. I tried it out on my Pi and it worked. But...

I'm not a Celsius person. I'm a Fahrenheit person.

Not to mention, I discovered on the article that vcgendcmd gives you the GPU temp, not the CPU temp. I still have that one to figure out. But at least I have one of the pair. Considering they are on the same chip, probably shouldn't matter, but better safe than sorry.

In any case, with a little bash tweaking, I came up with the following script:

echo "Raspberry Pi CPU Temp"
cpu=$(</sys/class/thermal/thermal_zone0/temp)
celsius=$((cpu/1000))
echo "Temp in Celsius"
echo "$((cpu/1000)) C"
echo "Temp in Fahrenheit"
echo "$((celsius*9/5+32)) F"

It worked! My CPU, sitting vertical on the kitchen counter, is running a balmy 111 degF (44 degC). I also added a little extra wording so that I can read it well with my bad eyes.

So, use it if you like. I think I might change it into a C program or python program later, just for practice; maybe even a temperature logger and alarm?

I've included a link to the NixCraft article below as well as a link to Raspberry Pi's download page that has links to the OS's one can use on the Pi.

Happy Monday!

Link to NixCraft article

Link to Raspberry Pi downloads

Top comments (0)