Tag Archives: YouTube

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

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