Awesome
CAN-bus-to-Arduino-to-Raspberry-Pi-Python-Keyboard-Press
This is a part of my car project. Basically I want to be able to use the media buttons on my car's steering wheel and trigger stuff on my computer. More exact a Raspberry pi running Android Auto (Open Auto). Goal is to get a button for volume up, volume down, media play/pause and a trigger for voice commands.
Dataflow
- A Arduino that listens on the CAN-bus. Filters the data and sends only the steering wheel actions out on the serial port.
- The python script listens for the data and makes a keypress on the computer for each invidual steering wheel action.
- Woops that list was really short, the flow is basically really easy.
Components Needed - My Setup
- A Arduino Uno (Original, ATmega328p)
- Sparkfun CAN-bus Shield (Uses MCP2515, MCP2551)
- A Rasberry Pi running Raspbian (RPi 3 B+, RASPBIAN STRETCH WITH DESKTOP 2018-06-27 4.14)
- A Opel Astra H 2005, with MS-CAN, 95kbps, CAN-H Pin3, CAN-L Pin11
How to run
Arduino Side
Normal Arduino stuff, except some modded library files that needs to be replaced. Follow there instructions to get everything installed:
- Install the MCP-can library in the Arduino IDE.
- Copy the files in the lib-files/ into the downloaded library's folder, replacing the files that are there. (These are a bit modified)
- Open
canbus-reciver/canbus-reciver.ino
from this repo and changes the filter ID's for your cars component. The default is the onces for my steeringwheel. - Upload the included sketch in this repository to your Arduino.
Computer Side
This python script uses a kernal module named uinput to press keyboard keys in linux. Follow there instructions to install everything:
- Install Raspbian on your Raspberry Pi (Raspbian Download)
- Run
pip install python-uinput
in the terminal to install a needed library. - Run
pip install pyserial
in the terminal to install another needed library. - Run
pip install regex
in the terminal to install one more needed library. - Open
canbus-actions.py
and change it to your likings. The default is to listen for my steering wheel buttons and press coresponding keys. - To make sure that the needed uinput kernal module is loaded on every boot, run
sudo echo "uinput" >> /etc/modules
in the terminal. This should make the module start up on every boot. If you instead, only want to start it once, typemodprobe uinput
in the terminal. - Make the python script run at startup by typing
sudo echo "sudo python /path/to/canbus-actions.py &" >> /etc/rc.local
in the terminal. This should make the script start up on every boot. Or you can usesudo python ./path/to/canbus-actions.py
to runt it once. - Restart your pi and it should boot up and start everything.
TODO:
- Make the traffic bidirectional so that the python script can request the Arduino to send out data on the CAN-bus when something is triggered on the computer.
Special Thanks and Inspiration
- Info about my Opel Astra H's CAN-Bus from vauxhallownersnetwork.co.uk
- Help with the code from chukonu's github repository and his YouTube video