Rpi Projects

= Playing with GPIO for first time = Source: GPIO Examples gpio readall gpio mode 0 out gpio write 0 1 gpio write 0 0 nano ledtest.py import RPi.GPIO as GPIO import time
 * Install wiringPi.
 * Run following command to verify GPIOs:
 * Connect a LED & ~1000Ω resistor to GPIO.0(Pin 11) & 0V(Pin 6).
 * Boot Pi On.
 * Run the following commands:
 * LED should flash On & Off.
 * To automate this process, use the following python code:

GPIO.setmode(GPIO.BOARD) print "Setup Pin 11" GPIO.setup(11, GPIO.OUT)
 * 1) Use physical pin numbers
 * 1) Set up header pin 11 (GPIO17) as an input

var=1 print "Start loop" while var==1 : print "Set Output False" GPIO.output(11, False) time.sleep(1) print "Set Output True" GPIO.output(11, True) time.sleep(1) sudo python ledtest.py
 * Now run the following command to check:
 * LED should alternatively switch on & off.
 * Press Ctrl+C to stop this code.

= Control Raspberry Pi with IR Sensor = Source: AdaFruit, Weedpi


 * CHQB IR Receiver is used here. Other IR Receiver like TSOP38238, etc also work.
 * Connect Pins as follows:

sudo apt-get install lirc sudo modprobe lirc_rpi sudo kill $(pidof lircd) mode2 -d /dev/lirc0
 * Installing LIRC
 * Test the IR Reveiver:

If pressing remote key produces similar to the following output, the IR receiver is working correctly: space 9025951 pulse 286 pulse 2314773 pulse 2277668 space 446984 ...... ......


 * Edit /etc/modules file & add:

lirc_dev lirc_rpi gpio_in_pin=18

LIRCD_ARGS="--uinput"
 * Change your /etc/lirc/hardware.conf file to:
 * 1) /etc/lirc/hardware.conf
 * 2) Arguments which will be used when launching lircd
 * 1) Arguments which will be used when launching lircd
 * 1) Arguments which will be used when launching lircd


 * 1) Don't start lircmd even if there seems to be a good config file
 * 2) START_LIRCMD=false


 * 1) Don't start irexec, even if a good config file seems to exist.
 * 2) START_IREXEC=false

LOAD_MODULES=true
 * 1) Try to load appropriate kernel modules

DRIVER="default" DEVICE="/dev/lirc0" MODULES="lirc_rpi"
 * 1) Run "lircd --driver=help" for a list of supported drivers.
 * 1) usually /dev/lirc0 is the correct setting for systems using udev

LIRCD_CONF="" LIRCMD_CONF=""
 * 1) Default configuration files for your hardware if any


 * Restart lircd:

sudo /etc/init.d/lirc stop sudo /etc/init.d/lirc start

irrecord --list-namespace
 * List of allowed key names when programming the remote

irrecord -d /dev/lirc0 ~/lircd.conf
 * Follow the instructions to map the remote

sudo cp ~/lircd.conf /etc/lirc
 * Once complete copy the lircd.conf file to /etc/lirc directory

sudo /etc/init.d/lirc restart
 * Restart LIRC

irw
 * Test the new remote conf file

Following output should appear on pressing 1, 2 and 3 keys on the remote: 0000000000fd08f7 00 KEY_1 0000000000fd08f7 01 KEY_1 0000000000fd08f7 02 KEY_1 0000000000fd8877 00 KEY_2 0000000000fd8877 01 KEY_2 0000000000fd8877 02 KEY_2 0000000000fd48b7 00 KEY_3

sudo nano /etc/lirc/lircrc Paste the following lines: begin prog = irexec button = KEY_0 config = sudo halt repeat = 0 end
 * To assign actions when keys are pressed create a file named lircrc in the /etc/lirc directory

begin prog = irexec button = KEY_1 config = omxplayer http://192.69.212.61:8020/stream -o local --vol -2000 repeat = 0 end

begin prog = irexec button = KEY_5 config = sudo killall omxplayer.bin repeat = 0 end

“remote” is the name you will find in your lircd.conf file you created before. If you do not have multiple remotes you can skip this variable and it will use the default remote. “button” is the value that you assigned to button when you configure your remote usiong irrecord utility “config” is the executable (with its parameters) to be run when the button is pressed

sudo /etc/init.d/lirc stop sudo /etc/init.d/lirc start
 * Once you save this file stop and start the lircd deamon as follows

LED controlling using LIRC
Source Ozzmaker

#include 
 * Wire the circuit around a breadboard as shown in the diagram.
 * Install wiringPi.
 * Save the following code as "lirc-led.c":
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 
 * 6) include 

void flipLED (int led);

//The WiringPi pin numbers used by our LEDs
 * 1) define LED1 2
 * 2) define LED2 3
 * 3) define LED3 4
 * 4) define LED4 5
 * 5) define LED5 6


 * 1) define ON 1
 * 2) define OFF 0

int main(int argc, char *argv[]) {       struct lirc_config *config;

//Timer for our buttons int buttonTimer = millis;

char *code; char *c;

//Initiate WiringPi and set WiringPi pins 4, 5 & 6 (GPIO 23, 24 & 25) to output. These are the pins the LEDs are connected to. if (wiringPiSetup == -1) exit (1) ;

pinMode (LED1, OUTPUT); pinMode (LED2, OUTPUT); pinMode (LED3, OUTPUT); pinMode (LED4, OUTPUT); pinMode (LED5, OUTPUT);

//Initiate LIRC. Exit on failure if(lirc_init("lirc",1)==-1) exit(EXIT_FAILURE);

//Read the default LIRC config at /etc/lirc/lircd.conf This is the config for your remote. if(lirc_readconfig(NULL,&config,NULL)==0) {               //Do stuff while LIRC socket is open  0=open  -1=closed. while(lirc_nextcode(&code)==0) {                       //If code = NULL, meaning nothing was returned from LIRC socket, //then skip lines below and start while loop again. if(code==NULL) continue;{ //Make sure there is a 400ms gap before detecting button presses. if (millis - buttonTimer > 400){ //Check to see if the string "KEY_1" appears anywhere within the string 'code'. if(strstr (code,"KEY_1")){ printf("MATCH on KEY_1\n"); flipLED(LED1); buttonTimer = millis; }                                       else if(strstr (code,"KEY_2")){ printf("MATCH on KEY_2\n"); flipLED(LED2); buttonTimer = millis; }                                       else if(strstr (code,"KEY_3")){ printf("MATCH on KEY_3\n"); flipLED(LED3); buttonTimer = millis; }                                       else if(strstr (code,"KEY_4")){ printf("MATCH on KEY_4\n"); flipLED(LED4); buttonTimer = millis; }                                       else if(strstr (code,"KEY_5")){ printf("MATCH on KEY_5\n"); flipLED(LED5); buttonTimer = millis; }                               }                        }                        //Need to free up code before the next loop free(code); }               //Frees the data structures associated with config. lirc_freeconfig(config); }       //lirc_deinit closes the connection to lircd and does some internal clean-up stuff. lirc_deinit; exit(EXIT_SUCCESS); } void flipLED (int led) {       //If LED is on, turn it off. Otherwise it is off, so thefore we need to turn it on. if(digitalRead(led)==ON) digitalWrite(led, OFF); else digitalWrite(led, ON); }

gcc -o lirc-led lirc-led.c -lwiringPi -llirc_client
 * Compile the above code with;

touch /etc/lirc/lircrc
 * Create an empty lirc file, if it already not exists:

sudo ./lirc-led
 * Execute it:

= PiScope = Source: Piscope Using Raspberry Pi as an Oscillator.

wget http://abyz.co.uk/rpi/pigpio/piscope.tar tar xvf piscope.tar cd PISCOPE make x86_64
 * Download executable at Laptop/Desktop:

wget abyz.co.uk/rpi/pigpio/pigpio.zip unzip pigpio.zip cd PIGPIO make make install
 * Install pigpio in Raspberry Pi:

sudo pigpiod
 * Launch the pigpio daemon On the Pi

export PIGPIO_ADDR=192.168.1.6 && ./piscope
 * On Desktop:

= Digital Clock using 7 Segment Display =

Source: Hackyourmind.org

I have used a 7 Segment 4 digit display commonly available in china made MP3 players for this project.


 * Rpi-hw Library Install:

sudo apt-get install g++-4.7 cmake sudo ln -fs /usr/bin/g++-4.7 /usr/bin/g++ git clone https://github.com/Wicker25/Rpi-hw cd Rpi-hw* cmake. -DCMAKE_INSTALL_PREFIX=/usr -DUSE_FREETYPE=OFF -DUSE_MAGICK=OFF make sudo make install

nano /home/pi/Clock/mytime.hpp
 * Create mytime.hpp file
 * 1) include
 * 2) include

int get_time {

time_t raw = time(0); struct tm *now = localtime( &raw );

return now->tm_min + 100 * now->tm_hour; }

nano /home/pi/Clock/timer.cpp // Include Rpi-hw headers
 * Create timer.cpp file
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 


 * 1) include "mytime.hpp" // Function `get_time` seen above

// Use Rpi-hw namespace using namespace rpihw;

int main( int argc, char *args[] ) {

// Create the display controller display::m7seg dev( 1, 4, 18, 15, 14, 0, 17, 23 );

iface::decoderOut enabler( { 25, 24, 22, 21 } ); dev.setDisplays( 4, enabler );

// Set the updating frequency (Hz) //dev.setFreq( 100.0 );

// Main loop for {

// Update the current time dev.set( get_time );

// Wait a second time::sleep(1); }

return 0; }

g++ `pkg-config --libs --cflags rpi-hw` -o timer timer.c
 * Compile:

sudo ./timer
 * Execute:

= Eyesight protection system = Source: Universal Raspberry Pi Remote & Ultrasonic Distance Measurement

This project is used to turn off TV when a child is sitting too close to the Television Screen.


 * Requirements(apart from Raspberry pi):
 * 1) IR LED
 * 2) HC-SR04 Ultrasonic Range Sensor
 * 3) NPN Transistor
 * 4) Resistor


 * Wiring up the circuit diagram:

List all of the commands that LIRC knows for 'samsung_tv' irsend LIST samsung_tv ""
 * Testing LIRC setup:

Send the KEY_POWER command once irsend SEND_ONCE samsung_tv KEY_POWER

#include 
 * Testing HC-SR04 Distance Sensor:
 * 1) !/usr/bin/python
 * R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
 * 1) ultrasonic_2.py
 * 2) Measure distance using an ultrasonic module
 * 3) in a loop.
 * 4) Author : Matt Hawkins
 * 5) Date   : 28/01/2013
 * 1) in a loop.
 * 2) Author : Matt Hawkins
 * 3) Date   : 28/01/2013
 * 1) Date   : 28/01/2013

import time import RPi.GPIO as GPIO
 * 1) Import required Python libraries
 * 1) Import required Python libraries


 * 1) Define some functions
 * 1) Define some functions

def measure: # This function measures a distance GPIO.output(GPIO_TRIGGER, True) time.sleep(0.00001) GPIO.output(GPIO_TRIGGER, False) start = time.time

while GPIO.input(GPIO_ECHO)==0: start = time.time

while GPIO.input(GPIO_ECHO)==1: stop = time.time

elapsed = stop-start distance = (elapsed * 34300)/2

return distance

def measure_average: # This function takes 3 measurements and # returns the average. distance1=measure time.sleep(0.1) distance2=measure time.sleep(0.1) distance3=measure distance = distance1 + distance2 + distance3 distance = distance / 3 return distance


 * 1) Main Script
 * 1) Main Script

GPIO.setmode(GPIO.BCM)
 * 1) Use BCM GPIO references
 * 2) instead of physical pin numbers

GPIO_TRIGGER = 23 GPIO_ECHO   = 24
 * 1) Define GPIO to use on Pi

print "Ultrasonic Measurement"

GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger GPIO.setup(GPIO_ECHO,GPIO.IN)     # Echo
 * 1) Set pins as output and input

GPIO.output(GPIO_TRIGGER, False)
 * 1) Set trigger to False (Low)

try:
 * 1) Wrap main content in a try block so we can
 * 2) catch the user pressing CTRL-C and run the
 * 3) GPIO cleanup function. This will also prevent
 * 4) the user seeing lots of unnecessary error
 * 5) messages.

while True:

distance = measure_average print "Distance : %.1f" % distance time.sleep(1)

except KeyboardInterrupt: # User pressed CTRL-C # Reset GPIO settings GPIO.cleanup Run the code as: sudo python ultrasonic_2.py


 * Final script for the project:

= Pi On-Off Shutdown Button = Source: element14.com, howchoo.com, howchoo.com




 * We will create a Python Script to turn off the Pi when GPIO 3 state is changed to Down by shorting it to Ground Pin(5).
 * These Two pins (5 & 6) can be again momentarily shorted to wake the Raspberry Pi from the halted state.
 * Recent changes to the bootcode.bin file will now allow the Raspberry Pi to be started by momentarily connecting pins 5 & 6.
 * Be careful as power pins are located right next to these pins and you could permanently damage your Raspberry Pi if you accidentally short a pin to the 5v power pin.

mkdir scripts cd scripts touch shutdown_pi.py  nano shutdown_pi.py
 * Creating a Python Script


 * Paste the following

sudo python shutdown_pi.py
 * Test the Script

sudo nano /etc/rc.local sudo python3 /home/pi/scripts/shutdown_pi.py &
 * Adding it to startup

Newer versions of Raspbian Jessie have the GPIO serial port disabled by default: sudo nano /boot/config.txt enable_uart=1
 * Enable the GPIO serial port to enable Status LED Support

= Pi Console Access =

Source elinux.org


 * Use USB To RS232 TTL Adapter such as this one found on ebay.in


 * Connect the Apapter to the Rpi GPIO's as below

ls -l /dev/ttyUSB0
 * You should be a member of the dialout group to access this port:

id
 * To find out if you are a member of group dialout:

sudo usermod -a -G dialout username
 * If you do not see dialout listed, add yourself:

sudo nano /boot/config.txt enable_uart=1
 * For Raspberry pi 3 you need to enable UART:

sudo screen /dev/ttyUSB0 115200 To exit GNU screen, type Control-A k.
 * Now run GNU Screen:

minicom -b 115200 -o -D Port_Name You can exit Minicom with Control-A x
 * Alternatively you can use Minicom:

= Thingspeak Temperature Logger =

Source: iotleague.com

= Thingspeak Resources Logger =

Crontab: 0 * * * * sudo python3 /home/pi/thingspeak.py @reboot sudo python3 /home/pi/thingspeak.py

= Blynk =

Virtual Pin read random Data
Source github.com

nano blynk_vread.py
 * Create a project in Blynk App.
 * Copy Auth Code received via email.
 * Drag a Value Display Widget in Blynk.
 * Set the Pin to Virtual Pin 11.
 * Change the Read Rate from PUSH to 5 Seconds.
 * Go to Raspberry Pi & create a file:


 * Paste the below code & change the Auth Code: