benmason
Posts: 23
Joined: Sat Jan 19, 2013 7:40 pm

Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Thu Feb 28, 2013 11:44 pm

Good evening all,

I have successfully coded a program in python to read temperature data from a BS18B20 1 wire sensor and log it in a daily email to give me an overview of temperature changes.

My next move is to make this program run at startup, however I have hit a stumbling block. I have to issue the following after each reboot to allow the program to read the sensor.

Code: Select all

sudo modprobe w1-gpio
sudo modprobe w1-therm
Is there a way this can be automatically issued on startup, allowing full automation of my project?

I have tried a few thing on google like adding the commands to the /etc/modules file but this did not work.

Many Thanks

Ben

saltydog
Posts: 25
Joined: Mon Dec 24, 2012 10:40 am

Re: Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Fri Mar 01, 2013 12:56 am

Doing either of the following works fine for me.

Code: Select all

sudo nano /etc/modules
Then adding:

Code: Select all

w1-gpio
w1-therm
Or adding these commands to the python code:

Code: Select all

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
Chris

User avatar
silverfox0786
Posts: 215
Joined: Mon Feb 04, 2013 10:32 pm
Location: Slough, UK
Contact: Website

Re: Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Fri Mar 01, 2013 12:57 am

add those commadn to your rc.local file

sudo nano rc.local

Code: Select all

#!/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.
sudo modprobe w1-gpio &
sudo modprobe w1-therm &
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
exit 0
or

Code: Select all

#!/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.
sudo modprobe w1-gpio &
sudo modprobe w1-therm
exit 0

repton
Posts: 91
Joined: Sat Mar 17, 2012 6:06 pm
Location: North Yorkshire, UK.
Contact: Website

Re: Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Fri Mar 01, 2013 7:19 pm

saltydog wrote:

Code: Select all

sudo nano /etc/modules
Then adding:

Code: Select all

w1-gpio
w1-therm
This is what I would consider the "correct" way of doing it although all the other options mentioned in this thread should also work.

Paul
UK Supplier of 1-Wire components, kits and modules:
http://www.sheepwalkelectronics.co.uk/

User avatar
silverfox0786
Posts: 215
Joined: Mon Feb 04, 2013 10:32 pm
Location: Slough, UK
Contact: Website

Re: Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Fri Mar 01, 2013 10:28 pm

repton wrote:
saltydog wrote:

Code: Select all

sudo nano /etc/modules
Then adding:

Code: Select all

w1-gpio
w1-therm
This is what I would consider the "correct" way of doing it although all the other options mentioned in this thread should also work.

Paul

if i recall correctly modules is user level and rc.local is global level

right?

Bright Sparks NZ
Posts: 20
Joined: Fri Dec 21, 2012 8:11 pm

Re: Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Tue Jun 10, 2014 8:25 pm

Thanks for the aboves. I am trying to get os.system('modprobe w1-gpio') / os.system('modprobe w1-therm') to work. I am trying to set up a few little demo py scripts along with photos so students can gain confidence with interfacing simple Low cost devices like great little DS18B20 temperature sensor. Like most stuff with Pi I find it very hard to get simple meaning and explanations for some commands and sort out (guesse) the environment and or user mode (pi or root) for some of them.
Put me straight here / help please:

1/. modprobe w1-gpio / modprobe w1-therm 'worked just fine from the cmd prompt and created the w1 folder and appropriate bits for the rest of the code to find. Of course this is all gone next time the Pi is started. Understood.

2/. os.system('modprobe w1-gpio') / os.system('modprobe w1-therm') run within the Raspi idle did nothing (no sys/bus/w1 etc folder created.

I know you can create (yet another tweak / autostart modification to things but I want to be able to provide a little / lab / workshop that allows a student to just 'load the code' then plug in a DS18B20 into the bus and get a result in the most direct way possible

3/. I have seen the glob thing that I read somewhere only works 'pre version z.y.z' so that will not be a lot of use and may still require more pre tinkering... = Syntax error anyway when the code struck the 'blob' in any case :)

I have to say that this sure helps fill in time and is way more complex than my other micro and weapon of choice: Gives a result with one line of code in two optional resolutions and has three lines of syntax and a short para explanation in the help section ;)

~ Andrew

8one6
Posts: 1
Joined: Fri Oct 31, 2014 4:32 pm

Re: Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Fri Oct 31, 2014 4:36 pm

I just fought through this issue myself. Do you happen to be running your script using cron? If so it's important to remember that cron runs its jobs in an environment that's quite different (and much more spare) than the one active when "sudo"ing from the command line. In particular, I found that I needed to specify full paths for *everything*. In particular:

Code: Select all

os.system('/sbin/modprobe w1-gpio')
os.system('/sbin/modprobe w1-therm')
needed the full paths spelled out. Here is my full "temperature getting" class, which returns a dictionary of temps in different units:

Code: Select all

import os
import glob
import time

class TempReader(object):
    def __init__(self, temp_sensor_pin):
        os.system('/sbin/modprobe w1-gpio')
        os.system('/sbin/modprobe w1-therm')
 
        self.temp_sensor_pin = temp_sensor_pin
        self.base_dir  = '/sys/bus/w1/devices/'
        counter = 0
        incomplete = True
        while counter < 5 and incomplete:
            try:
                self.device_folder = glob.glob(
                    self.base_dir + 
                    str(self.temp_sensor_pin) +
                    '*')[0]
                incomplete = False
            except IndexError:
                counter += 1
                time.sleep(10)
        self.device_file = self.device_folder + '/w1_slave'

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

    def read_temp(self):
        lines = self.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 * 180.0/100.0) + 32.0
        tempdict = {
            'temp_c': temp_c,
            'temp_f': temp_f
        }
        return tempdict

nigratruo
Posts: 27
Joined: Tue Sep 24, 2013 2:20 am

Re: Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Sat Nov 22, 2014 4:51 am

Hi,

I was just wondering if there is a direct interface into w1-therm in python. I find the solution real ugly, requiring 10 lines of code to disassemble the things that go into that file in /sys/. "Everything is a file" does not seem to work out this well in this case and to needlessly complicate things.

I also wonder what happens when temparature turns 3 digits (which the sensor seems to be able to handle (max 150 c), but the module implementation does not (hard coding double digits).

Is there a way to directly access the temperature in Python instead of going through the file hierarchy in /sys/?

User avatar
DougieLawson
Posts: 30420
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Sat Nov 22, 2014 10:27 am

nigratruo wrote:Is there a way to directly access the temperature in Python instead of going through the file hierarchy in /sys/?
Unfortunately not. It's the way the hardware works and the way the kernel module presents the hardware to userspace programs. So you are stuck with the /sys virtual filesystem.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

sfrias
Posts: 2
Joined: Fri Jan 22, 2016 2:53 pm

Re: Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Fri Jan 22, 2016 3:00 pm

Hi all. I have a performance issue with the w1_bus_master1 process, which performs a process every 10 seconds, consuming at that moment an average of 30% of CPU time of my raspberry.
  I have searched and can not find any configuration file, and wanted to ask if you know any solution for that process to do every 5 minutes for example.

Regards.

Santiago. 8-)

sfrias
Posts: 2
Joined: Fri Jan 22, 2016 2:53 pm

Re: Adding Modprobe w1-gpio / Modprobe w1-therm to boot

Fri Jan 22, 2016 5:11 pm

Dependencies: w1_gpio->wire->cn

pi@proteus3rpi ~ $ lsmod
Module Size Used by
spi_bcm2835 7948 0
bcm2835_gpiomem 3703 0
w1_gpio 4295 0
wire 31227 1 w1_gpio
cn 5776 1 wire
uio_pdrv_genirq 3690 0
uio 10002 1 uio_pdrv_genirq
snd_bcm2835 22317 0
snd_pcm 92581 1 snd_bcm2835
snd_seq 61957 0
snd_seq_device 5130 1 snd_seq
snd_timer 23454 2 snd_pcm,snd_seq
snd 68161 5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device
ipv6 360374 26

pi@proteus3rpi ~ $ modinfo w1_gpio
filename: /lib/modules/4.1.13+/kernel/drivers/w1/masters/w1-gpio.ko
license: GPL
author: Ville Syrjala <syrjala@sci.fi>
description: GPIO w1 bus master driver
srcversion: 53ED8AC39FC409715A28D9A
alias: of:N*T*Cw1-gpio*
depends: wire
intree: Y
vermagic: 4.1.13+ preempt mod_unload modversions ARMv6
parm: pullup:Enable parasitic power (power on data) mode (int)
parm: extpullup:GPIO external pullup pin number (int)
parm: gpiopin:GPIO pin number (int)
pi@proteus3rpi ~ $ modinfo w1_gpio

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: Marwan00 and 23 guests