Ever want your Raspberry pi project to have a script that interfaces with I2C to start automatically after a power cycle? Would you like the vnc session to start at power up as well?
This instructable will show how to make a perl script that interfaces with the I2C ports to autostart along with an VNC server so you can log in after every power cycle or use the PERL script without any manual commands. There are several ways to do this, the goal for this method was to do so without opening up the permissions on the I2C ports for everyone. This will help prevent accidental writes to the ports by scripts from other owners.
Step 1: Check Your Version of the Operating System for Compatiblity
Check your operating system version, this example is for the Debian, Weezy version. If you have this version you can verify by typing cat /etc/os-release as shown in the photo above. This may work for other versions as well, but hasn't been tested.
Make sure your device is configured to start with a desktop for the user, the default user is "pi". That is the assumption for the instruction.
Step 2: Check the Permissions on the IO Ports
Check the permissions on the I2C ports. The ports need to be set up as shown in the photo above. To check the permissions cd to /dev. Type ls -l | grep I2C. The permissions need to be set as shown in the photo. Change the permissions with chmod if they are not correct. There is a lot of literature on the net for chmod if you have not used this command before. We keep the restrictions as tight as possible as the part or you system can possibly be damage by incorrect I2C.
Step 3: Set Up the VNC Server
Install your vnc server, follow one the many instructions on the web. This will place a .vnc folder in your home directory. Modify your xstartup file in the .vnc folder to make the session look the way you want. Also start the vnc session a first time while you are logged in, as the first time it will ask you to set the password. To do this type "vncserver" when you are in your home directory. The password for the vnc session doesn't have to match the login for the user. If you fail to do this password step you will not be able to log in after the first power up. If you have completed this step there will be a passwd file in the .vnc folder in your home directory. Make sure to write down the password somewhere so you can get in later.
If for some reason you forget the password you can delete the passwd file in the .vnc directory and restart the vnc session it will ask you for a new password.
Step 4: Now for the Fun Autostart Part
For this project we will use the autostart folder for both the vnc session and later to start the Perl script that will access the I2C ports. Autostart scripts in this folder will be executed at power up. To find this folder type "cd ~/.config/autostart"
Once in this folder you can make a file to start the vnc server, call it "vnc.desktop"
There is an attached copy of this file in the instructions and a screen shot as well.
After this step you can power cycle the device and you will have a vnc session available that you can use to log into the part for the rest of the steps.
Step 5: Set Up Your Perl Script
There isn't a lot to this step, the permission work is all handled with the rest of the files. you can use any Perl script to for this part. Here is one that uses the I2C ports for an example. You can place any executable script here. Make sure to check the permissions as shown in the picture. Also this setup requires the script to be located in the following path /home/pi/one_button/sprinkler. See the screen capture attached
Step 6: Set Up Your Bash Script
The bash script is the secret to making all the permissions work for the script for access to the ports after the other changes are complete. Make sure to set the permissions for the bash shell must not be editable by all, and probably not by group. For this example to work the .bsh file will need to be located in /home/pi. The use of sudo gives permissions for the Perl script to access the I2C ports., without this there will problems with the Perl script accessing the ports even with the permissions set.
Step 7: Set Up the Autostart
You are almost ready to go now, the only step left is to add the .autostart file to make the script work. Just like the autostart file for the VNC session this file needs to be located in /home/pi/.config/autostart. See the inserts for examples.
This is it. So a quick summary
Check permissions on your I2C ports
place your perl script in a known place under the desired user, PI was used for this example
Place your .bsh file in /home/pi that points to the Perl script
Place your .autostart file in /home/pi/.config/autostart "assuming you are using PI as the user.
Now when you power cycle the machine the perl script will start and run, with access to the I2C ports.
As a reminder if you want to test you can type "sudo shutdown -r now" and wait patiently for the device to reboot.
to test if your script is running you can type
ps - elf | grep scriptname
See the attached example showing that one.pl is running
You now have a self starting raspberry pi that can control the I2C ports, and use a powerful language like perl to run the ports. There are many I2C chips that you can attach to the pi such as the MCP23017 for a parallel port expander. There are a2d, d2a converters, temperature sensors, pwm generators and many more