timofurrer / w1thermsensor

A Python package and CLI tool to work with w1 temperature sensors like DS1822, DS18S20 & DS18B20 on the Raspberry Pi, Beagle Bone and other devices.
MIT License
493 stars 113 forks source link

Loading library on boot #3

Closed FriedCircuits closed 10 years ago

FriedCircuits commented 10 years ago

Hi,

I wanted to see if you have come across a problem when you start a python program that uses DS18B20 on boot.

I have a crontab that is set to on reboot to start my python program.

The problem is when it init the library it fails. But if I run it manually with sudo at the command line it works fine.

it fails on sensor=DS18B20()

I can't see the error when it runs from cron so I have no idea what is happening.

Any ideas?

Thanks.

Edit: Sometimes I get this File "/home/pi/piminer/ds18b20/init.py", line 87, in _get_sensor return path.join(DS18B20.BASE_DIRECTORY, DS18B20.SLAVE_PREFIX + self._id, DS18B20.SLAVE_FILE) TypeError: cannot concatenate 'str' and 'NoneType' objects

FriedCircuits commented 10 years ago

I was able to capture the error output when run as cron

Traceback (most recent call last): File "/home/pi/piminer/PiMiner.py", line 6, in from PiMinerDisplay import PiMinerDisplay File "/home/pi/piminer/PiMinerDisplay.py", line 3, in from PiMinerInfo import PiMinerInfo File "/home/pi/piminer/PiMinerInfo.py", line 17, in class PiMinerInfo: File "/home/pi/piminer/PiMinerInfo.py", line 43, in PiMinerInfo sensor = DS18B20() File "/home/pi/piminer/ds18b20/init.py", line 63, in init self._sensor = self._get_sensor() File "/home/pi/piminer/ds18b20/init.py", line 80, in _get_sensor sensors = self.get_available_sensors() File "/home/pi/piminer/ds18b20/init.py", line 47, in get_available_sensors for sensor in listdir(cls.BASE_DIRECTORY): OSError: [Errno 2] No such file or directory: '/sys/bus/w1/devices'

FriedCircuits commented 10 years ago

For future reference, seems like something to do with cron which can do odd things. So going to look for another way to start it on boot as root.

timofurrer commented 10 years ago

I never had such problems. To be honest, I've never tried to start DS18B20 from a cronjob. For your case a cronjob is anyway not the best way to go. Instead of cronjob use a init script. Place something like this in your /etc/init.d:

!/bin/sh

### BEGIN INIT INFO
# Provides: DAEMON_NAME
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: DESCRIPTION
# Description: DESCRIPTION
### END INIT INFO

DIR=/path/to/your/daemon
DAEMON=$DIR/daemonscript.py
DAEMON_NAME=MYDAEMON

# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=root

# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid

. /lib/lsb/init-functions

do_start () {
  log_daemon_msg "Starting system $DAEMON_NAME daemon"
  start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON
  log_end_msg $?
}
do_stop () {
  log_daemon_msg "Stopping system $DAEMON_NAME daemon"
  start-stop-daemon --stop --pidfile $PIDFILE --retry 10
  log_end_msg $?
}

case "$1" in
  start|stop)
    do_${1}
    ;;

  restart|reload|force-reload)
    do_stop
    do_start
    ;;

  status)
    status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
    ;;

  *)
    echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
    exit 1
  ;;
esac

exit 0

I hope I could help! :beers:

FriedCircuits commented 9 years ago

Just wanted to let you know the init scripts works great.