Category Archives: Linux

Using 1-Wire Sensors with Raspberry Pi

This post details how to read 1-Wire temperature probes using a Raspberry Pi and how to serve those values to the rest of the world. Links to the four-part YouTube video series are listed below.

Using 1-Wire Sensors with Raspberry Pi: Part 1, Part 2, Part 3, Part 4.

The following Python code reads my 1-Wire temperature sensors and writes them to a text file I can read from Apache. This code executes every minute on my Raspberry Pi server in the KE5PRL shack and not on the KE5PRL.com server which is in Utah.

import os
import datetime
#Many thanks to the original author of this code.

#return local time
now = datetime.datetime.now()

#return UTC time
utcnow = datetime.datetime.utcnow()

#Inside the shack.
# Open the file
Inside_tfile = open("/sys/bus/w1/devices/28-02161df565ee/w1_slave")

# Read it.
Inside_text = Inside_tfile.read()

# Close it.
Inside_tfile.close()
# Split the text with new lines (\n) and select the second line.
Inside_secondline = Inside_text.split("\n")[1]

# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
Inside_temperaturedata = Inside_secondline.split(" ")[9]

# The first two characters are "t=", so get rid of those and convert the temperature from a string $
Inside_temperature = float(Inside_temperaturedata[2:])

# Split the text with new lines (\n) and select the second line.
Inside_secondline = Inside_text.split("\n")[1]

# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
Inside_temperaturedata = Inside_secondline.split(" ")[9]

# The first two characters are "t=", so get rid of those and convert the temperature from a string $
Inside_temperature = float(Inside_temperaturedata[2:])

# Put the decimal point in the right place (in degrees C)
Inside_temperature = round(Inside_temperature / 1000, 1)

# Convert to F
Inside_Convert = round((Inside_temperature * 1.8) + 32, 1)

#Under the shack.
Under_tfile = open("/sys/bus/w1/devices/28-02161e0125ee/w1_slave")
Under_text = Under_tfile.read()
Under_tfile.close()
Under_secondline = Under_text.split("\n")[1]
Under_temperaturedata = Under_secondline.split(" ")[9]
Under_temperature = float(Under_temperaturedata[2:])
Under_temperature = round(Under_temperature / 1000, 1)
Under_Convert = round((Under_temperature * 1.8) + 32, 1)

#Outside the shack.
Outside_tfile = open("/sys/bus/w1/devices/28-02161de3afee/w1_slave")
Outside_text = Outside_tfile.read()
Outside_tfile.close()
Outside_secondline = Outside_text.split("\n")[1]
Outside_temperaturedata = Outside_secondline.split(" ")[9]
Outside_temperature = float(Outside_temperaturedata[2:])
Outside_temperature = round(Outside_temperature / 1000, 1)
Outside_Convert = round((Outside_temperature * 1.8) + 32, 1)

#GPU Temperature.
gpu1 = os.popen('vcgencmd measure_temp').readline()
gpu2 = gpu1.replace("temp=", "")#delete "temp="
gpu3a = gpu2.replace("'C", "")#delete "'C"
gpu3 = gpu3a.replace("\n","")#delete the return
gpu4 = float(gpu3)
gpu6 = round((gpu4 * 1.8) + 32, 1)

#CPU Temperature.
cpu1 = open('/sys/class/thermal/thermal_zone0/temp')
cpu2 = cpu1.read()
cpu3 = float(cpu2)
cpu4 = round((cpu3/1000 * 1.8) + 32, 1)
cpu5 = round((cpu3/1000),1)

#Open a file for writing
fo = open("/var/www/html/shackdata.txt", "w")

#Write the following pipe-delimited string.
#localtime|UTC time|Inside F|Inside C|Under F|Under C|Outside F|Outside C|GPU F|GPU C|CPU F|CPU C

fo.write( now.strftime("%H:%M %Y-%m-%d"))
fo.write("|")
fo.write( utcnow.strftime("%H:%M %Y-%m-%d"))
fo.write("|")
fo.write( str(Inside_Convert))#inside shack F
fo.write("|")
fo.write( str(Inside_temperature))#inside shack C
fo.write("|")
fo.write( str(Under_Convert) )#Under shack F
fo.write("|")
fo.write( str(Under_temperature) )#Under shack C
fo.write("|")
fo.write( str(Outside_Convert))#outside shack F
fo.write("|")
fo.write( str(Outside_temperature))#outside shack C
fo.write("|")
fo.write( str(gpu6) )#GPU F
fo.write("|")
fo.write( str(gpu3) )#GPU C
fo.write("|")
fo.write( str(cpu4) )#CPU F
fo.write("|")
fo.write( str(cpu5) )#CPU C

#Close the opened file
fo.close()

The following PHP code reads the text file created above and displays it on KE5PRL.com. This code executes on the KE5PRL Web site server and not on my Raspberry Pi server.


Facebooktwittergoogle_plusredditpinterestlinkedinmail

Raspberry Pi PuTTY Network Error Connection Refused

Just a quick mention on an update to Raspberry Pi running Raspbian… I recently used apt-get to update and upgrade and found out a few hours later that I could not SSH into my RPi using PuTTY.  Internet searches revealed that it was a common problem but not a new one.  In other words my update/upgrade wasn’t the source of the problem – it’s a problem that happens regularly.  After a couple of hours of various tweaks I discovered that SSH was not enabled.  Here is how to do it… Continue reading Raspberry Pi PuTTY Network Error Connection Refused

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Tweet from a Raspberry Pi

Python can send tweets using the Tweepy library and after these few steps you should be able to use it tweet from your Raspberry Pi.  Step one – download Tweepy to your Raspberry Pi with three command line statements.  In a terminal window type each command and press return.

git clone https://github.com/tweepy/tweepy.git
cd tweepy
sudo python setup.py install

Once the library is installed, you can move to step two – getting the values for consumer_key, consumer_secret, access_token and access_token_secret.  It’s hard to explain this so just watch my video on YouTube: https://youtu.be/xEQYbT1gpxo

Step three – copy this code to your favorite  editor then save it a with a descriptive name like HelloWorld.py and do not forget the .py extension.

#Import the Tweepy library
import tweepy

#Consumer keys and access tokens, used for OAuth.  You will
#need your own values and they are much longer than these examples.  #Follow the instructions from my YouTube video (link above). 
consumer_key = '1xS'
consumer_secret = 'AxD'
access_token = '3x9'
access_token_secret = 'UxR'

#OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
api.update_status('Hello World from my Raspberry Pi!')

Navigate to the directory you saved the file in and type the following command line statement in a terminal window.

sudo python HelloWorld.py

You should have tweeted “Hello World from my Raspberry Pi”.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Why I Enjoy RTTY Contests

I enjoy RTTY contesting because it’s fast-paced and I can easily contact many stations from a wide geographical area in a single contest.  That’s not to say that I don’t enjoy a good ragchew but sometimes I just want to rack up the contacts.  As I write this I am calling CQ in the CQ WW DX RTTY Contest and have worked three European countries and a few stateside stations in the last five minutes. Continue reading Why I Enjoy RTTY Contests

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Get Raspberry Pi IP Address by Email at Boot

I like to use Remote Desktop to access my Raspberry Pi but that is difficult to do if you don’t know what its IP address is. My solution was to have the IP address emailed to me, read that email on my phone, then log in using Remote Desktop. The rc.local file is executed at boot so I edited it as shown below.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
rm /home/pi/ipAddress.txt
hostname -I > /home/pi/ipAddress.txt
ssmtp user@email.com < /home/pi/ipAddress.txt
exit 0

Explanation of the code…

  • rm /home/pi/ipAddress.txt deletes the file where I store the IP address so that I can make a new file with the latest IP address.
  • hostname -I > /home/pi/ipAddress.txt gets the current IP address and saves it to a file.
  • ssmtp user@email.com < /home/pi/ipAddress.txt emails the contents of the new file to me.

I also had to set up SSMTP to use my email host – in this example it is email.com
and the account holder is “user”. My edits to ssmtp.conf are shown below.

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com

mailhub=smtp.email.com:785
UseSTARTTLS=YES
AuthUser=user
AuthPass=password
# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=raspberrypi

# Are users allowed to set their own From: address?
# YES – Allow the user to specify their own From: address
# NO – Use the system generated From: address
#FromLineOverride=YES

I modified or set the mailhub, AuthUser, AuthPass and hostname settings.

Be careful with this file because it will have your account name and password in it. You may want to use an account other than your main one for that reason.

With both of these files (rc.local, ssmtp.conf) you will want to make back-up copies of before you edit them.

Facebooktwittergoogle_plusredditpinterestlinkedinmail