Showing posts with label Raspberry Pi. Show all posts
Showing posts with label Raspberry Pi. Show all posts

Friday, July 29, 2016

Using Scratch to control Raspberry Pi GPIO

In the previous post, I have built my first circuit and able to control the LED light using the gpio command:
http://pandaeatsbamboo.blogspot.hk/2016/07/the-hello-world-circuit-with-raspberry.html

Similar control can be done via Scratch, this is the official tutorial:
https://www.raspberrypi.org/blog/a-new-version-of-scratch-for-raspberry-pi-now-with-added-gpio/

This is a super simple Scratch script to illustrate this:


In this simple script, when you click the flag icon, it will set the gpio pin 17 (BCM) to 1 to switch on the LED.  When you press the spacebar on your keyboard, it will set the gpio pin 17 to 0 to switch off the LED.  With this you can incorporate the GPIO control into your own scratch script.  Give it a try!

Tuesday, July 26, 2016

The "Hello World" circuit with Raspberry - Single LED

Probably the "Hello World" of building a simple circuit with Raspberry is a single LED circuit.  So I decided to give it a try.  Not difficult frankly speaking, just to get all the ingredients together and there are plenty of goods tutorial if you spend a minute to google.

This is the official URL that you can first check it out:
https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/

The circuit that I've used is exactly the same as the one you can find from the above URL, except that the "switch" is not a physical one, I used the gpio command to control it.


This is how it looks physically.  I've connected the GPIO pin to my breadboard using the female-to-male jumper cable.  Physical pin 11 is used for +ve and physical pin 6 is the ground pin.


Check out this link about breadboard.  An other things about building a circuit.


Some holes (usually the top and bottom rows) are connected in row and some (usually the middle) are in column.  

I've followed this link and used a 330 ohm resistor.  Check your LED spec to decide the correct resistor.  My LED works fine in a 11mA current circuit so 330 ohm resistor is good for my case.

Also check out the above link for LED.  The LED should have 2 legs, the longer leg is +ve and shorter leg is -ve.

One thing that I've spent quite some time is the pinout.  The BCM and physical board pin numbering is a bit confusing.  Say for my case, I've used physical pin 11, and in BCM numbering it is pin 17.  It will spend you hours to troubleshoot if you don't get it right.

You can use the gpio readall command to print out all its number convention and other details.




Another thing is, your physical GPIO pin 11 will be providing 3.3V power and you have to change its mode to OUT.  You can do it with the gpio command below:



gpio -1 means you are referring to the physical pinout numbering.

gpio -g means you are referring to the BCM pinout numbering.



This is a python script to switch on then off the LED light instead of using the gpio command.  You can find a similar script from the link provided above.  The only difference here is I am using GPIO.BOARD which means I'm referring to the physical pin number 11.


If you want to use the BCM numbering, then you can setmode to GPIO.BCM

Hope it will save you some time from troubleshooting layer 1 problem and to build up your first circuit with Raspberry!


Thursday, July 14, 2016

Raspberry Pi Cam + Cisco Spark - Motion Detection Demo

In the previous post, I've setup a Raspberry Pi Surveillance camera with Live streaming capability, and it can also save a video file when motion is detected.

http://pandaeatsbamboo.blogspot.com/2016/07/live-streaming-video-from-raspberry-pi.html

Spark integration is done too so that when motion is detected, a Spark message is posted with video files.  This is how it works:



Live streaming video from the Raspberry Pi with Camera v2

Just got my Raspberry Pi Camera v2, cannot wait to unbox and play with it.


I didn't get a good case to fit in the camera module, but it works.  :)


First of all you can start by taking a picture to make sure your camera is working fine:

raspistill -o image.jpg

To capture your first video, you can use the command raspivid:

raspivid -o video.h264

The video is in raw h.264 format.  It is fine to be played back by my omxplayer on the Raspberry, however if you want most of the other video players to play it, it will be better to convert the file into mp4.

sudo apt-get install gpac
MP4Box -fps 30 -add video.h264 video.mp4

For some application such as "motion", the motion detection and video streaming applications, it needs to recognised the video device as /dev/video0.  It is not the case by default for the Pi Camera.

To enable that, you need to run:

sudo modprobe bcm2835-v4l2

You can also put this command in /etc/rc.local so that the next reboot it will run automatically.

Motion is an open source motion detection and surveillance software which can help you to convert your Raspberry Pi into a home surveillance camera.  To start with, you can get the motion package:

sudo apt-get install motion

A few parameters under motion.conf that you can change, such as:

daemon on

which makes the motion run in background when you start the motion server.

You can also change the resolution in the conf file:
width 1280
height 720

change the following parameter to off so that you can access from host other than localhost:
stream_localhost off
webcontrol_localhost off

You can now access the stream via http::8081


And you will get a bunch of files in your home directory, make sure you have enough storage, if not probably you might need to do it off box to a NAS or whatever it is.  These files are created when motion is detected.



These are the links that I've used to aid my setup, listing all here for your reference:
http://www.instructables.com/id/Raspberry-Pi-as-low-cost-HD-surveillance-camera/?ALLSTEPS
http://www.makeuseof.com/tag/raspberry-pi-camera-module/
http://www.raspberrypi-spy.co.uk/2013/05/capturing-hd-video-with-the-pi-camera-module/

Friday, July 8, 2016

Raspberry Pi 3 + Cisco Spark - My home presence check

Just got my Raspberry Pi 3 a few days ago.  I know there are loads of projects that I can do with that, before knowing what I actually want to do, I want to try something quick and simple to keep the box warm.  I've an python scripts based on the post here:

http://www.instructables.com/id/Raspberry-Pi-Bluetooth-InOut-Board-or-Whos-Hom/

I modified it slightly to send a message to a Spark room when the Raspberry Pi detects my iPhone bluetooth mac address:

#!/usr/bin/python

import bluetooth
import time
import pyCiscoSpark

print "Home Attendance"

accesstoken="Bearer "
roomid=""

while True:
        currenttime=time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
        print "Checking " + currenttime

        result = bluetooth.lookup_name('ff:ff:ff:ff:ff:ff', timeout=5)
        if (result != None):
                print "Danny: in"
                resp_dict = pyCiscoSpark.post_message(accesstoken,roomid,"BT: Danny is at home now - " + currenttime)
                #print (resp_dict)
        else:
                print "Danny: out"
                resp_dict = pyCiscoSpark.post_message(accesstoken,roomid,"BT: Danny is NOT at home now - " + currenttime)

        time.sleep(60)

Replace ff:ff:ff:ff:ff:ff with your phone Bluetooth mac address.  I've also used the pyCiscoSpark.py in Github for posting a message to the Spark room:

https://github.com/brbester/pyCiscoSpark

So this is how it looks like when the script is running:



Spark room: