I would like to automatically start the python program using systemd in raspberry pie.

Asked 2 years ago, Updated 2 years ago, 87 views

I'm a beginner in programming.
Currently, I would like to run the python program as soon as I launch the raspberry pie.

I searched the internet and found out that the python program can be started at the same time as I start using systemd, so I created it, but no matter how many times I try, I get an error.

Please let me know the cause even if I review the systemed, python.

Raspberry pie power on/off is compatible with RPZ-PowerMGR.

systemd code /etc/systemd/system

 [Unit]
Description=test
Service
ExecStart=/opt/RasPiSample/bin/2.2.1_Photoresistor.py
Restart=always
Type = simple
Install
WantedBy=multi-user.target

Python Code

#!usr/bin/python 3.7.3
import RPi.GPIO as GPIO
import ADC0834
import datetime

error messages:

Enter a description of the image here

python raspberry-pi systemd

2022-09-30 19:47

2 Answers

Sorry for the delay.

As for systemd and Python, it worked fine as follows.

What I wanted to do was to read the signal lights on the machine using an optical sensor and investigate the utilization rate.

/etc/systemd/system/RasPiAuto.service

 [Unit]
Description=test

Service
ExecStart=/usr/bin/env python3/opt/RasPiSample/bin/2.2.1_Photoresistor.py

Restart = no
Type = simple

Install
WantedBy=multi-user.target

Python script

#!/usr/bin/env python3

import RPi.GPIO as GPIO
import ADC0834
import datetime
import csv
import time

LedPin = 22

def setup():
    globaled_val
    # Set the GPIO modes to BCM Numbering
    GPIO.setmode (GPIO.BCM)
    # Set all LedPin's mode to output and initial level to High (3.3v)
    GPIO.setup (LedPin, GPIO.OUT, initial=GPIO.HIGH)
    ADC0834.setup()
    # Set as pwm channel and frequency to 2KHz
    led_val = GPIO.PWM (LedPin, 2000)

    # Set all begin with value 0
    led_val.start(0)

def nowtime():
    now=datetime.datetime.now()
    nowtime='{0:%Y /%m /%d%H:%M:%S}'.format(now)


def save():
    with open('/home/pi/Desktop/cp_log.csv', 'a') asf:
        writer=csv.writer(f)
        writer.writerow ([datetime.datetime.now(), ADC0834.getResult()])

def destroy():
    # Stop all pwm channel
    led_val.stop()
    # Release resource
    GPIO.cleanup()

def loop():
    while True:
        analogVal=ADC0834.getResult()
        now=datetime.datetime.now()
        nowTime='{0:%Y /%m /%d%H:%M:%S}'.format(now)
        save()
        print(nowTime, end=')
        print('CP1=%d'%analogVal)
        led_val.ChangeDutyCycle (analogVal*100/255)
        time.sleep (0.975)

if__name__=='__main__':
    setup()
    try:
        loop()
    except KeyboardInterrupt: # When'Ctrl+C'is pressed, the program destroy() will be executed.
        destroy()


2022-09-30 19:47

The Python code in the question appears to be incorrect in the first line (#! line).
The first / seems to be missing, so please try adding this.

Original Code:

#!usr/bin/python 3.7.3

modified:

#!/usr/bin/python 3.7.3


2022-09-30 19:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.