0

Starting Service at Boot
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Starting Service at Boot

by wsquared58 on Sun Sep 08, 2019 3:22 pm

I've been trying to get my weather station program to start at boot via Systemd and they all are pretty much the same as far as on-line examples. This is what I came up with based on the various examples.

Code: Select all | TOGGLE FULL SIZE
[Unit]
Description=Weather Service
After=multi-user.target

[Service]
Type=simple
User=root
ExecStart=/usr/bin/python3 /home/pi/weather_station.py > /home/pi/weather.log 2>&1
Restart=always
RestartSec=20

[Install]
WantedBy=multi-user.target


My problem is that it doesn't appear to start (obviously) but when I look at it in systemctl what it shows is this next line.
Code: Select all | TOGGLE FULL SIZE
weather.service           loaded     activating     auto-restart     Weather Service 


Rather than saying active it just sits there forever

wsquared58
 
Posts: 124
Joined: Sun Jun 09, 2019 9:13 pm

Re: Starting Service at Boot

by jerryn on Sun Sep 08, 2019 3:46 pm

here is a configuration that has worked for me. perhaps the Type=idle makes difference.
Also did you
run
Code: Select all | TOGGLE FULL SIZE
sudo systemctl enable yourservice


Code: Select all | TOGGLE FULL SIZE
pi@gjnpi4desk:~ $ cat /lib/systemd/system/temperature.service
[Unit]
Description=Temperature Monitor
After=multi-user.target

[Service]
Type=idle
User=pi
ExecStart=/usr/bin/python3 /home/pi/projects/blinka/temperature_monitor/temperature_monitor.py
Restart=always

[Install]
WantedBy=multi-user.target

jerryn
 
Posts: 990
Joined: Sat Sep 14, 2013 9:05 am

Re: Starting Service at Boot

by wsquared58 on Sun Sep 08, 2019 5:56 pm

I solved one problem but am now confronted with another. This is related to the fact that I am sending this weather data to IO. I had a file location wrong, deleted it and started fresh and got over the initial failure. What is challenging is that the code runs fine but this is related to getting it to restart when no one is near by to manually restart the code.

I found a line to check the status of script and now I am getting the following:

Code: Select all | TOGGLE FULL SIZE
 weather.service - Weather Launcher
   Loaded: loaded (/lib/systemd/system/weather.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2019-09-08 17:36:39 EDT; 41s ago
  Process: 676 ExecStart=/usr/bin/python3 /home/pi/weather_station.py (code=exited, status=1/FAILURE)
 Main PID: 676 (code=exited, status=1/FAILURE)

Sep 08 17:36:38 raspberrypi systemd[1]: Started Weather Launcher.
Sep 08 17:36:39 raspberrypi python3[676]: Traceback (most recent call last):
Sep 08 17:36:39 raspberrypi python3[676]:   File "/home/pi/weather_station.py", line 48, in <module>
Sep 08 17:36:39 raspberrypi python3[676]:     from Adafruit_IO import Client, Feed, RequestError
Sep 08 17:36:39 raspberrypi python3[676]: ModuleNotFoundError: No module named 'Adafruit_IO'
Sep 08 17:36:39 raspberrypi systemd[1]: weather.service: Main process exited, code=exited, status=1/FAILURE
Sep 08 17:36:39 raspberrypi systemd[1]: weather.service: Failed with result 'exit-code'.


Line 48 is he offender but 47 is here also to provide some context.
This is all stuff that I just cut and pasted from a weather tutorial by BRubell

Code: Select all | TOGGLE FULL SIZE
Line 47# import Adafruit IO REST client
Line 48 from Adafruit_IO import Client, Feed, RequestError

wsquared58
 
Posts: 124
Joined: Sun Jun 09, 2019 9:13 pm

Re: Starting Service at Boot

by wsquared58 on Tue Sep 10, 2019 10:14 am

Can someone tell me what it means when this error is posted and if there is a way to resolve.
" No module named 'Adafruit_IO' "

wsquared58
 
Posts: 124
Joined: Sun Jun 09, 2019 9:13 pm

Re: Starting Service at Boot

by jerryn on Tue Sep 10, 2019 11:03 am

It looks like python is not finding the module -- If you installed it using pip or pip3 as a the user pi then if you are running the service as root, it may not find it. Do you need to run as root -- you can change the User setting to pi and see if that works.

jerryn
 
Posts: 990
Joined: Sat Sep 14, 2013 9:05 am

Re: Starting Service at Boot

by wsquared58 on Wed Sep 11, 2019 8:21 am

That did not work. I had gotten rid of User=root prior to your response.
Im curious. Are you using REST client or MQTT to interface with IO?

This may be a dead end but trying to sort out where files go when one initially installs Adafruit_IO via pip3 to see if that might be a source of the problem.
Don't understand why the code runs fine, sends data to IO just fine if I just launch the program via either Terminal window or MU but it won't fly as a service. Like the service needs to be told where to go find the REST client or something to that effect, but that is not an issue when manually starting as xxx.py vs (xxx.py).service

wsquared58
 
Posts: 124
Joined: Sun Jun 09, 2019 9:13 pm

Re: Starting Service at Boot

by jerryn on Wed Sep 11, 2019 9:23 am

I am using the REST client.

on my RPi the pip3 files are kept in
Code: Select all | TOGGLE FULL SIZE
pi@gjnpi4desk:~ $ ls .local/lib/python3.7/site-packages/
adafruit_blinka                                   adafruit_io-2.3.2.dist-info              busio.py                          RPi
Adafruit_Blinka-2.4.0.dist-info                   adafruit_platformdetect                  cryptoauthlib                     RPi.GPIO-0.7.0.dist-info
adafruit_bme280.py                                Adafruit_PlatformDetect-1.2.1.dist-info  cryptoauthlib-20190517.dist-info  rpi_ws281x
adafruit_bus_device                               Adafruit_PureIO                          digitalio.py                      rpi_ws281x-4.2.1.dist-info
adafruit_circuitpython_bme280-2.3.1.dist-info     Adafruit_PureIO-0.2.3.dist-info          ez_setup.py                       _rpi_ws281x.cpython-37m-arm-linux-gnueabihf.so
adafruit_circuitpython_busdevice-4.0.0.dist-info  adafruit_rfm69.py                        microcontroller                   spidev-3.4.dist-info
adafruit_circuitpython_rfm69-1.2.5.dist-info      adafruit_rfm9x.py                        micropython.py                    spidev.cpython-37m-arm-linux-gnueabihf.so
adafruit_circuitpython_rfm9x-1.1.7.dist-info      adafruit_sgp30.py                        neopixel_write.py                 sysv_ipc-1.0.0.dist-info
adafruit_circuitpython_sgp30-2.0.3.dist-info      adafruit_si7021.py                       paho                              sysv_ipc.cpython-37m-arm-linux-gnueabihf.so
adafruit_circuitpython_si7021-3.1.4.dist-info     adafruit_tinylora                        paho_mqtt-1.4.0.dist-info
adafruit_circuitpython_tinylora-2.0.0.dist-info   bitbangio.py                             pulseio.py
Adafruit_IO                                       board.py                                 __pycache__
pi@gjnpi4desk:~ $

jerryn
 
Posts: 990
Joined: Sat Sep 14, 2013 9:05 am

Re: Starting Service at Boot

by jerryn on Wed Sep 11, 2019 9:34 am

just for completeness -- here is how I installed my service:

Code: Select all | TOGGLE FULL SIZE
cp my.service  /lib/systemd/system
sudo chmod 644 /lib/systemd/system/my.service
sudo systemctl daemon-reload
sudo systemctl enable my.service

jerryn
 
Posts: 990
Joined: Sat Sep 14, 2013 9:05 am

Re: Starting Service at Boot

by wsquared58 on Wed Sep 11, 2019 8:09 pm

Hopefully this is a simple question.
I ran the ls .local/yadayadayada and it returned everything that is in your file and then some but what are the different color codes of the text?
All the xxx.py are white but there are other line items that are blue and some that are green. What is that about?

It would seem that there is still some conflict that is preventing IO from running in the weather.service script but just now got to where I could take a first look

wsquared58
 
Posts: 124
Joined: Sun Jun 09, 2019 9:13 pm

Re: Starting Service at Boot

by wsquared58 on Thu Sep 12, 2019 4:01 pm

definitely tied to IO end of things. commented that all out and service cranked right up.
Having major trouble finding any info on REST client.

wsquared58
 
Posts: 124
Joined: Sun Jun 09, 2019 9:13 pm

Re: Starting Service at Boot

by wsquared58 on Sun Sep 15, 2019 5:58 am

board.jpg
board.jpg (81.3 KiB) Viewed 33 times
I just yesterday cleaned up my code and reordered it for readability and understanding. It continues to run from the REPL but now I have a new fault relating to "import board" but which I think is the same problem afflicting the Adafruit IO module not found fault as well

Code: Select all | TOGGLE FULL SIZE
# Import standard python modules
import time
import math
import sys
#import Adafruit Blinka
import board
import busio
import RPi.GPIO as GPIO


If you look at the screen grab jpg you can see that the items "time, math, sys," show <module 'math' built in> for example yet board doesn't reflect that. I don't know where these modules are supposed to reside but I brought everything in via tutorial and I thought they all came as a bulk package in the Adafruit CircuitPython library so they should all be there in any case which has me baffled.

As a side note to that, where do I find which version I'm using to download the appropriate file?

wsquared58
 
Posts: 124
Joined: Sun Jun 09, 2019 9:13 pm

Re: Starting Service at Boot

by wsquared58 on Sun Sep 15, 2019 7:34 pm

VICTORY!!!

I feel like Tom Hanks in Cast Away when he made fire for the first time.

wsquared58
 
Posts: 124
Joined: Sun Jun 09, 2019 9:13 pm

Re: Starting Service at Boot

by wsquared58 on Mon Sep 16, 2019 7:33 am

Not sure how it happened but some of the busio and board issues were because somehow they were located in an older version of python location as best I can figure based on reading the mu debugger and systemctl status files.
Deleted all the old stuff, deleted adafruit-blinka, deleted all the python 2.7 files reinstalled everything and modified the /user/bin/python file to usr/bin/python3.7 and it fired right up.

I think some of the files on Github would benefit from having in the README file something in big bold lettering stating which versions of Python, Micropython, CircuitPython they work with or are for.

I saw some item relating to a Github IO installation file stating something to the effect of "this works best in python 2.7??, create the appropriate folder"

wsquared58
 
Posts: 124
Joined: Sun Jun 09, 2019 9:13 pm

Please be positive and constructive with your questions and comments.