bassplyr647
Posts: 2
Joined: Sat Mar 21, 2015 5:48 pm

systemd and i2c

Tue Aug 28, 2018 6:33 pm

In the past, I've had a Pi 3 running Jessie and created a service that would start after the i2c module loaded during boot, the service would run a small program to place a message, via i2c using wiringPI, to a character LCD. This has been working great, deployed on numerous Pi's etc..

I just received a Pi 3 B+. I've installed Stretch Lite and have the Pi starting from USB. I've created a systemd service that should start after i2c is active during boot (I've got i2c enabled and working), but I get the following error

Code: Select all

 systemctl status startmsg.service
startmsg.service - LCD Message
   Loaded: loaded (/lib/systemd/system/startmsg.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2018-08-28 11:08:51 CDT; 2h 16min ago
  Process: 88 ExecStart=/home/pi/startmsg (code=exited, status=1/FAILURE)
 Main PID: 88 (code=exited, status=1/FAILURE)

Aug 28 11:08:51 raspberrypi startmsg[88]: Unable to open I2C device: No such file or directory
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
The program startmsg works fine after the Pi has booted up.

This is the service I created:

Code: Select all

[Unit]
Description=LCD Message
DefaultDependencies=false
Requires=systemd-modules-load.service
After=systemd-modules.load.service
Before=sysvinit.target
ConditionPathExists=/sys/class/i2c-adapter

[Service]
Type=oneshot
ExecStart=/home/pi/startmsg
Restart=no

[Install]
WantedBy=sysinit.target
Any suggestions would be greatly appreciated.

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

Re: systemd and i2c

Tue Aug 28, 2018 9:31 pm

Do you have

Code: Select all

i2c-dev
in /etc/modules?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

bassplyr647
Posts: 2
Joined: Sat Mar 21, 2015 5:48 pm

Re: systemd and i2c

Thu Aug 30, 2018 10:42 am

Yes.

TimSmall
Posts: 5
Joined: Sat Jun 03, 2017 11:14 am

Re: systemd and i2c

Wed Nov 13, 2019 10:51 am

You can make systemd track the availability of the i2c devices by creating a file e.g.

Code: Select all

/etc/udev/rules.d/99-i2c.rules
which looks like this:

Code: Select all

SUBSYSTEM=="i2c-dev", TAG+="systemd"
After rebooting you can then verify this has worked with:

Code: Select all

systemctl --all --full -t device | grep i2c
You can then add one the resulting i2c .device units as a dependency in your own unit file e.g.

Code: Select all

cat /etc/systemd/system/mything.service
[Unit]
...
Requires=dev-i2c\x2d1.device
After=dev-i2c\x2d1.device
...

Return to “Troubleshooting”