Startup

At this point, your WeatherPi Station should be ready. There is one more thing to do here. We can set our Python script to start when the Raspberry Pi Zero W board is booting. To do this, we need to install a package named dos2unix that actually converts DOS style line endings into something that is Unix friendly. Apart from this, you can use crontab (install it if you don't have it) or edit your /etc/init.d/ to add some lines and make your script start with the other Raspberry Pi Zero W built-in services. To install the package mentioned previously, we have to run the following code:

sudo apt-get install dos2unix  

Next, we have to set up our Python script as a service, so we can start it at the Raspberry Pi Zero W boot. To do this, we will create a new script that will allow us to start/stop it and handle the Python script as we want. As always, open your favorite editor and start creating a new script. We will use vim as our editor:

sudo vim /etc/init.d/weatherstation  

It is time to write some code here. Start by writing the following code:

#!/bin/bash
### BEGIN INIT INFO
# Provides: weatherstation
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stops the weatherstation
# Description: Start/stops the weatherstation
### END INIT INFO

DIR=/home/pi
DAEMON=$DIR/weather_script.py
DAEMON_NAME=weatherstation

DAEMON_USER=root

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

After writing all this code to an editor, you can save the file and exit. To ensure that everything runs smoothly, you need to run dos2unix on the file that you just created. To do this, you need to run the following code:

sudo dos2unix /etc/init.d/weatherstation  

With this command, you ensure that the file is written correctly and everything is OK. Now, you need to change the permissions in your Python script. If you don't do this, your bash script will fail to work. To do this and change your permissions, run the following command:

sudo chmod 755 /home/pi/weather_script.py  

The next step is to modify and change the permissions of your weather station bash script. You have to give execution permissions, and you can easily do this with the chmod command:

sudo chmod +x /etc/init.d/weatherstation  

Create a symbolic link between your bash script and the rc.d folders:

sudo update-rc.d weatherstation defaults  

Now, start your Python script:

sudo service weatherstation start  

At this point, you should be ready. Your weather station service should automatically start on boot. You can check this by rebooting your Raspberry Pi Zero W board. Furthermore, you can interact with your weather station with some more commands; for example, you can start a service with the following code:

sudo service weatherstation start  

Alternatively, if, for any reason, you want to stop a running service, you can simply run the following command:

sudo service weather station stop  

Now, there are two more commands to go. The next one actually kills the process and starts it again. So, to reload the weather station, just run the following command:

sudo service weatherstation reload  

Finally, to get the current status of the weather station, run the following command:

sudo service weather station status  

This way, you should be able to find whether it is running or it has stopped.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.46.141