The many ways of running a program at boot on Raspberry Pi

This is a collection of tutorials that explain in various ways how to run something at boot

Crontab:

http://www.dexterindustries.com/howto/raspberry-pi-tutorials/auto-run-python-programs-on-the-raspberry-pi/

rc.local:
https://www.raspberrypi.org/documentation/linux/usage/rc-local.md

Crontab 2 (with logs)

http://www.instructables.com/id/Raspberry-Pi-Launch-Python-script-on-startup/step4/Add-to-your-crontab/

boot.rc (and .bashrc)

http://www.akeric.com/blog/?p=1976

 

/etc/init.d for executable script files (also mentions the other approaches)

http://pi.bek.no/autostartProgramOnBoot/

Advertisements

Simple Raspberry Pi Shutdown wires

Pi with shutdown wires
Pi with shutdown wires

via Simple Raspberry Pi Shutdown Button.

Finally, I can turn off my Pi even after I turned off my laptop. As I always SSH in, I have to remember to shutdown the Pi before leaving, which is not always the case.  Simple 5 minute tutorial, and problem solved!
I didn’t solder anything onto my Pi. I simply used female to male wires, as I want to keep my case closed.

Clyde Is Eating Candy

(originally published for Fabule Fabrications, but as they closed down their Clyde website, I’m safekeeping it here. Unfortunately, it is not the final version.)

So Halloween came and went! Clyde went trick or treating this year! But what can Clyde do with all the treats he? Here’s one project that Clyde can get in. We will use gummy bears [ Wikipedia | http://en.wikipedia.org/wiki/Gummy_bear] to control the ambient light. So get into your candy stash, and find three gummy bears: red, green and blue. We’ll wait here patiently while you get the candy.

What? There are no blue gummy bears? You looked carefully everywhere and you couldn’t find a blue gummy bear? You’re right. Standard gummy bears don’t come in blue, and that’s a shame, isn’t it? Although there are some jellied candy out there that are blue, we’ll have to make do with the colourless Gummy Bear for this project. Or you can go and get blue squishable candy, that’s fine too. As for us, we chose a huge edible spider.

So we now have our three gummy bears: red, green and colourless. The goal of this tutorial is to be able to control the ambient light by squishing each bear. The red one will give us a red light, the green one a green light, and the colourless standing in for blue will give us blue. Should we squeeze two at a time, we’ll get the combination of the corresponding colours. The ambient light is activated as long as at least one gummy bear is squeezed. The basic behaviour is *not* that of a light switch, our gummy bears will not act as on/off switches. At least not yet.

I see you’re puzzled, and rightly so. How can we control some electronics with candy??? We’ll have to put some wires inside to make the bears “electron-able”, and the bears will act as pushbuttons. When squeezed, they will connect the wires inside them. When not squeezed, the wires will not connect. This is the basic behaviour of a pushbutton. In fact, this whole tutorial is based on the pushbutton found on the Arduino website: http://arduino.cc/en/Tutorial/Button If you’re unfamiliar with Arduino programming and/or basic electric circuits, may we suggest going through this Button tutorial first, as we will not be duplicating the information that’s already nicely explained over there. This tutorial is basically the same done three times.

Needed material

Here’s a list of what you will need:

  • three gummy bears of adequate colours.
  • three metal paper clips that you are willing to sacrifice, or six dressmaker pins.
  • three resistors (I used 330 ohms, because that’s what I had nearby)
  • 11 male to male jumper wires. Hopefully, three red, three green and three blue. The other two can be any colour. We used black for 5V and white for ground.
  • a little breadboard, as small as possible, so it can fit inside of Clyde

Diagrams:

We have included a diagram we created with Fritzing, a free simple electronics documentation software, to help demonstrate the connections. Clyde is not a Fritzing part, so we use a Leonardo in its place.


A black wire goes from the 5V pin to the breadboard, a white wire goes is for ground. As we want to use coloured wires for our gummy bears, we won’t use standard colours in this project. For each gummy bear, you need a wire that goes from 5V to inside the gummy bear, and another that goes from inside the gummy bear to its respective pin (10 for red, 12 for green and 13 for blue). You then need a wire from each pin to a small resistor; the other end of that resistor is linked to GND (ground). The schematic found at http://arduino.cc/en/Tutorial/Button can be very helpful in understanding the one gummy bear / pushbutton circuit. The explanations are also quite clear as to why a resistor is needed.

Schematics - one bear

Schematic for one gummy bear and Clyde

As gummy bears are absolutely not good conductors when it comes to electricity (nor cars, I assume), may I suggest you leave them out of the circuit till the very end. Just use the end of the wires that should be in the gummy bears as loose wires. We’re using low voltage, there’s no danger. Or you can use pushbuttons for now, if you have them. The gummy bears will only be inserted into the circuit once everything else is working to our satisfaction.
Schematics - Three Bears

The Clyde code

The code is pretty straightforward.
1. We first include the necessary headers to access Clyde’s libraries


#include <Wire.h>
#include <EEPROM.h>
#include <Clyde.h>
#include <SerialCommand.h>
#include <SoftwareSerial.h>
#include <MPR121.h>

2. We declare three constants for our pins that will read the gummy bears status:

const int rBUTTON = 10;
// this is where we will read the red Gummy Bear

const int gBUTTON = 12;
// this is the pin where we will read the green Gummy Bear

const int bBUTTON = 13;
// this is the pin where we will read the blue Gummy Bear

3. Then we run the setup() function once, which initializes everything for us:
the first two lines are needed to initialize Clyde
the last three lines initializes our gummy bear input devices

void setup() {
Wire.begin();
Clyde.begin();
// set the three Gummy Bears as input devices
pinMode(rBUTTON, INPUT);
pinMode(gBUTTON, INPUT);
pinMode(bBUTTON, INPUT);
}

4. The loop function is where we read our gummy bears. As the pins can only give us 0 (for ‘not squished’, and 1 (for ‘squished’), we need to multiply that by 255 in order to get a decent value for our colours. We then pass those three RGB values to the RGB function, and use that to call setAmbient().

void loop() {

/*
Read status of all three gummy bears.
If they’re squished, they will register as HIGH.
Otherwise they will be low.
Each gummy bear is independent of each other
*/
rval = digitalRead(rBUTTON)*255;
gval = digitalRead(gBUTTON)*255;
bval = digitalRead(bBUTTON)*255;

Clyde.setAmbient(RGB(rval,gval,bval));
}

Idea for more fun:
Instead of just multiplying by 255, you can do it incrementally, or even randomly, for a variety of effects.

Final code all in one place:

The raw Arduino code

This code does not use Clyde’s firmware and libraries, but only uses the Arduino functionalities. We will be talking to pins directly, and reading them directly. The ambient light is controlled by pins #5, #6 and #9, for the RGB components (R:5, G:6, B: 9)

CODE:

Random twitters can be informative

HUGE thanks to Phil Howard on Twitter.

1:
Find out which installed packages are wasting all your Pi disk space: dpkg-query -Wf ‘${Installed-Size}\t${Package}\n’ | sort -n

Tip: Don’t uninstall raspberrypi-bootloader to try and save space

2:
please remember to apt-get clean before complaining your sd card/disk is full esp. if you apt a lot

3:
And to trace big folders of crud from your home directory, use: “du -sh *” and disappear down the rabbit hole!

Adafruit Capacitive Hat – first approach

I got myself Adafruit’s new capacitive hat. This is my first foray into I2C (heck, I didn’t even know it required it!)

Adafruit says they’re working on a tutorial for their hat, but right now I get sent to the old tutorial which I follow dutifully. However when I try to run sudo python simpletest.py I get an File not found error in line if not cap.begin():.

Hmm. Puzzled…

Googling left and right points me to the fact that I2C is being used. This is a brand new Pi 2. Most likely I2C is not activated. So I run sudo raspi-config and I activate it (in the advanced options). I reboot, just to be certain.

Still no go.

A quick search on the Adafruit forums bring me to a thread where the user has a similar error, although not the same. In that thread, I discover a command to see if I2C is properly configured.

sudo i2cdetect -y 1
which should return
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- 5a -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Not my case. Nothing works. I just get an error which I forgot to write down. Back to googling. I find this ModMyPi tutorial: Loading I2C, SPI and 1-Wire drivers

nano /etc/modules
I add in
# SPI devices
spi-dev
# I2C devices
i2c-dev

and reboot. A call to lsmod shows me that I2C is up. I try the hat example again, and yeah! Thank you to all who took the time to write tutorials, so that little old ladies like me can have fun!