I'm trying to manually edit the w1_slave
file of a DS18B20 Sensor. Using nano
to edit the file, when I manually change the temperature and go to save and exit, I get 'permission denied'.
Is there anyway of doing this?
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It only takes a minute to sign up.
Sign up to join this communitySetting up the sensor: We need to load the drivers for the 1-wire comms and the temp sensor into the Pi kernel. Modprobe is a Linux program to add a loadable kernel into the Linux kernel. In your terminal enter:
1.)
sudo modprobe w1-gpio
2.)
sudo modprobe w1-therm
3.)Now change your working directory (cd) to:
cd /sys/bus/w1/devices/
4.)This is where the devices running on the 1-wire will be. So to find our newly created device just list the contents of the directory with ls.
ls
Now you should see something listed like
28-00000222fd25 w1_bus_master1
5.)This is the serial number for the device. To interrogate that device we need to go into its directory. Make sure you use the serial number of your own sensor!!
cd 28-00000222fd25
6.)The sensor writes to a file called w1_slave so if we just read that file we can now finally know what temperature it is. Enter:
cat w1_slave
Please read the driver documentation: https://owfs.org/index_php_page_ds18b20.html
There you will find that the temp value is read-only:
temperature
read-only, floating point
Measured temperature with 12 bit resolution.
but the alarm values is read/write
temphigh
read-write, integer
Shows or sets the lower limit for the high temperature alarm state.
templow
read-write, integer
Shows or sets the upper limit for the low temperature alarm state.
And if you read the datasheet
Quote: Byte 0 and byte 1 of the scratchpad contain the LSB and the MSB of the temperature register, respectively. These bytes are read-only.
Lets Set up getTemp.py
7.)Go back to the root directory and make a new directory (mkdir) called tempLog.
cd
8.)Make a new dir file
mkdir tempLog
9.)Go into the new directory and create a new python file in nano.
cd tempLog
10.)Edit the file
sudo nano getTempSQL.py
11.)Copy the code below taking care to use your own value for the sensor as well as the database.
#!/usr/bin/python2
import os
import time
import datetime
import glob
import MySQLdb
from time import strftime
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
temp_sensor = '/sys/bus/w1/devices/28-01161d4fdbee/w1_slave'
# Variables for MySQL
db = MySQLdb.connect(host="localhost", user="root",passwd="Password", db="temp_database")
cur = db.cursor()
def tempRead():
t = open(temp_sensor, 'r')
lines = t.readlines()
t.close()
temp_output = lines[1].find('t=')
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string)/1000.0
return round(temp_c,1)
while True:
temp = tempRead()
print temp
datetimeWrite = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
print datetimeWrite
sql = ("""INSERT INTO tempLog (datetime,temperature) VALUES (%s,%s)""",(datetimeWrite,temp))
try:
print "Writing to database..."
# Execute the SQL command
cur.execute(*sql)
# Commit your changes in the database
db.commit()
print "Write Complete"
except:
# Rollback in case there is any error
db.rollback()
print "Failed writing to database"
cur.close()
db.close()
break
Allow python script work autonomously, putting a temperature reading into the database every 5 minutes. To do this we will use Crontab, which is a handy unix tool to schedule jobs. A good explanation of what crontab is can be found here.
12.)To open Crontab enter the following in your terminal.
sudo crontab -e
13.)Inside the Crontab file enter the following at the bottom. This simply runs our Python script every 5 minutes. If you have used different file and folder names, adjust them accordingly.
/usr/bin/python2 /home/pi/readTempSQL.py > /home/pi/cronjoblog 2>&1
8.)Now lets check that the Python script actually entered data into the MySQL database. Log back into MySQL and USE the temp_database. We can then query the tempLog table:
mysql -u root -p
14.)Use the database with the following.
USE temp_database;
ls
with appropriate options. Or create a dummy file and then modify your code to read that file instead.