raspberry

Linux startup script

Create script in /etc/init.d

sudo nano /etc/init.d/NameOfYourScript

The following is an example based on starting up the no-ip service [/usr/local/bin/noip], but change the name of the script and the command to start and stop it and it would work for any command.

#! /bin/sh
# /etc/init.d/noip### BEGIN INIT INFO
# Provides:          noip
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Simple script to start a program at boot
# Description:       A simple script from www.stuffaboutcode.comwhich will start / stop a program a boot / shutdown.
### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting noip"
# run application you want to start
/usr/local/bin/noip2
;;
stop)
echo "Stopping noip"
# kill application you want to stop
killall noip2
;;
*)
echo "Usage: /etc/init.d/noip {start|stop}"
exit 1
;;
esac

exit 0

Warning – its important you test your script first and make sure it doesn’t need a user to provide a response, press “y” or similar, because you may find it hangs the raspberry pi on boot waiting for a user (who’s not there) to do something!

Make script executable

sudo chmod 755 /etc/init.d/NameOfYourScript


Test starting the program

sudo /etc/init.d/NameOfYourScript start


Test stopping the program

sudo /etc/init.d/NameOfYourScript stop


Register script to be run at start-up
To register your script to be run at start-up and shutdown, run the following command:

sudo update-rc.d NameOfYourScript defaults

Note – The header at the start is to make the script LSB compliant and provides details about the start up script and you should only need to change the name.  If you want to know more about creating LSB scripts for managing services, see http://wiki.debian.org/LSBInitScripts

If you ever want to remove the script from start-up, run the following command:

sudo update-rc.d -f  NameOfYourScript remove

XFCE4 Vnc Linux

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
startxfce4 &

Control Arduino from Raspberry Pi with pyFirmata

Requirements
  1. Raspberry Pi with Raspbian OS installed in it
  2. Arduino Uno or any other Arduino board
  3. Arduino USB cable
  4. LED

 

Installing PyFirmata in Arduino using Raspberry Pi

To upload PyFirmata firmware in Arduino, we have to install Arduino IDE in Raspberry Pi. Follow these steps to install:

Step 1:- Connect the Raspberry Pi to the internet. Open command terminal and type the following command and hit enter

sudo apt-get -y install arduino python-serial mercurial

Wait for few minutes, it will take time. This command will install the Arduino IDE in your Raspberry Pi.

Step 2:- Now, we will install pyFirmata files using the given github:

git clone https://github.com/tino/pyFirmata

Then run the following command:

cd pyFirmata
sudo python setup.py install

Step 3:- We have installed all the required files and setups.

Now, connect your Arduino board with Raspberry Pi using USB cable and launch Arduino IDE by typing arduino in terminal window.

Step 4:- Then type lsusb command to check whether Arduino is connected with your raspberry pi.

In Arduino IDE, Go to tools and choose your board and Serial Port.

Step 5:- Upload the PyFirmata firmware on the Arduino by clicking File -> Examples ->  Firmata -> Standard Firmata and then click upload button . As shown below.

We have successfully installed the pyFirmata firmware in the Arduino board. Now, we can control our Arduino using Raspberry Pi.

For demonstration we will blink and fade an LED on the Arduino by writing python codes in Raspberry Pi.

 

 

Code Explanation

For coding part, you should read documentation of pyFirmata for better understanding. We will use pyFirmata functions to write our code. You can find pyFirmata documentation by following the link.

So let’s start writing the code

Open your favorite text editor on the Raspberry Pi and import pyFirmata library.

import pyfirmata

Define pin on the Arduino to connect the LED

led_pin = 9

Now, we have to write serial port name on which Arduino board is connected using pyfirmata.Arduino() function and then make an instance by assigning port in board variable.

board = pyfirmata.Arduino("/dev/ttyACM0")
print "Code is running”

In while loop, make led pin HIGH and low using board.digital[].write() function and give delay using board.pass_time() function.

while True:
    board.digital[led_pin].write(0)
    board.pass_time(1)        
    board.digital[led_pin].write(1)
    board.pass_time(1)

Our code is ready, save this code by putting .py extension to the file name.

Open command terminal and type python blink.py to run the code on the Arduino board. Make sure your Arduino board is connected with your Raspberry Pi board using USB cable.

Now, you can see Blinking LED on the Arduino board.

Complete code for blinking LED using pyFirmata is given at the end.

Fading LED on Arduino using pyFirmata

Now, we will write code for fading the LED to make you more familiar with the pyFirmata functions. This code is easy as previous one. You have to use two for loops, one for increase brightness and other for decrease brightness.

In this code, we have defined the pins in different way like led = board.get_pin(‘d:9:p’) where d means digital pin. This is function of pyFirmata library. Read the documentation for more details.

Complete code for Fading LED using pyFirmata is given at the end.

Now, you can add more sensors to your system and make it more cool, check our other Arduino projects and try building them using Raspberry pi and python script.

Code

Python code for LED blink:

import pyfirmata

led_pin = 9

board = pyfirmata.Arduino("/dev/ttyACM0")

while True:
board.digital[led_pin].write(0)
board.pass_time(1)
board.digital[led_pin].write(1)
board.pass_time(1)

Python code for Fading LED:

import time
import pyfirmata

delay = 0.3
brightness = 0

board = pyfirmata.Arduino("/dev/ttyACM0")

led = board.get_pin('d:9:p')
while True:
# increase
for i in range(0, 10):
brightness = brightness + 0.1
print "Setting brightness to %s" % brightness
led.write(brightness)
board.pass_time(delay)

# decrease
for i in range(0, 10):
print "Setting brightness to %s" % brightness
led.write(brightness)
brightness = brightness - 0.1
board.pass_time(delay)

Linux, Debian remove / stop service

To stop a service
sudo service servicename stop

To disable service autostart
sudo update-rc.d servicename disable

Raspberry turn off usb ports

download this

https://github.com/codazoda/hub-ctrl.c

You can control the power on a port using the following command. (I installed it in user bin directory)

sudo $HOME/bin/hub-ctrl -h 1 -P 2 -p 1

That says to control hub 3 (-h 3) port 1 (-P 1) and to turn the power off (-p 0). You can also use ”-p 1” to turn the power back on.