Sie sind auf Seite 1von 37

THE OBJECTIVE IS TO TURN ON

THE LIGHTS IN A ROOM FROM


ANYWHERE (FROM YOUR OFFICE,
WHEN YOU ARE ON YOUR WAY
HOME ETC)
1. USE A RELAY TO TURN A SWITCH ON/OFF
A. CONNECT THE RELAY TO THE RASPBERRY PI
B. CONNECT THE RELAY TO THE POWER SUPPLY OF THE LIGHT / SWITCH
YOU WANT TO CONTROL

THIS PART IS EXACTLY THE SAME AS THE LAST PROJECT - WE JUST


NEED THE RELAY PORTIONS OF THE CIRCUIT, AND YOU CAN DROP THE SENSOR
PORTIONS

2. SET UP A WEB SERVER TO CONTROL THE RASPBERRY PI FROM ANYWHERE

3. WRITE SOME CODE THAT WILL FLIP THE SWITCH WE WANT TO CONTROL WHENEVER
IT RECEIVES A WEB REQUEST
WE WILL NEED TO USE A RELAY TO TURN THE LIGHT SWITCH
ON/OFF
REMEMBER THE RASPBERRY PI CAN ONLY PRODUCE A SMALL
OUTPUT CURRENT (3.3 V , 50 mA).
YOUR HOME POWER SUPPLY IS AT 220/110 V.
A RELAY IS AN ELECTROMAGNETIC SWITCH - IT IS OPERATED BY
A SMALL CURRENT AND CAN BE USED TO TURN A MUCH LARGER
CURRENT ON/OFF

IMAGINE A LEVER THAT CAN USE A SMALL AMOUNT OF


FORCE TO LIFT A LARGE OBJECT

CAUTION: BE EXTRA-CAREFUL IN THIS EXERCISE


WHEN YOU WORK WITH THE POWERSUPPLY
WHEN A LOW CURRENT FLOWS THROUGH THE ELECTROMAGNETIC
COIL IT MAGNETIZES THE METAL

HIGH VOLTAGE ON
MOVEABLE CONTACT
HIGH VOLTAGE OFF

ELECTROMAGNETIC COIL
WHEN A LOW CURRENT FLOWS THROUGH THE ELECTROMAGNETIC
COIL IT MAGNETIZES THE METAL

HIGH VOLTAGE ON
MOVEABLE CONTACT
HIGH VOLTAGE OFF

ELECTROMAGNETIC COIL
WHEN A LOW CURRENT FLOWS THROUGH THE ELECTROMAGNETIC
COIL IT MAGNETIZES THE METAL

HIGH VOLTAGE ON
MOVEABLE CONTACT
HIGH VOLTAGE OFF

ELECTROMAGNETIC COIL
ONE SMALL WRINKLE
THE RELAY IS ACTIVE LOW,
WE’LL USE AN NPN TRANSISTOR TO
CHANGE THE GPIO OUTPUT FROM HIGH TO WHICH MEANS THE SWITCH IS TRIGGERED
LOW WHEN YOU GIVE IT A LOW VOLTAGE (0V),
CONNECT C TO THE RELAY INPUT
CONNECT B TO GPIO OUTPUT
PIN 18

CAUTION: BE EXTRA-CAREFUL IN THIS EXERCISE CONNECT E TO GROUND


WHEN YOU WORK WITH THE POWERSUPPLY
A LITTLE SHOPPING TIP
INSTEAD OF BUYING A SINGLE CHANNEL
RELAY, BUY ONE LIKE THIS

THIS IS AN 8 CHANNEL 5 V RELAY


YOU CAN CONTROL 8 SWITCHES WITH IT ,
INSTEAD OF JUST 1 !
3. TO GIVE AN INPUT TO THE RELAY
1. CONNECT THE RELAY TO THE RASPBERRY PI
2. CONNECT THE RELAY TO YOUR LIGHT’S POWER SUPPLY

MAIN POWER SUPPLY RASPBERRY PI

CAUTION: BE EXTRA-CAREFUL IN THIS EXERCISE


WHEN YOU WORK WITH THE POWERSUPPLY
3. TO GIVE AN INPUT TO THE RELAY
RASPBERRY PI
1a. CONNECT THE RELAY’S POWER TO
RASPBERRY PI’S GPIO 1 (IF ITS A 3V RELAY)
MAIN POWER SUPPLY OR GPIO 2 (IF ITS A 5V RELAY
1b. CONNECT THE RELAY INPUT TO GPIO 18
(WHICH YOU WILL CONFIGURE AS AN
OUTPUT) - THIS WILL BE HIGH WHEN YOU
GET THE RIGHT READING FROM THE LIGHT
AND DISTANCE SENSORS, (IN CASE OF AN
ACTIVE LOW RELAY CONVERT TO LOW WITH
THE NPN TRANSISTOR AS THE MEDIATOR)
1c. CONNECT THE RELAY
GROUND TO GPIO PIN 6
CAUTION: BE EXTRA-CAREFUL IN THIS EXERCISE
WHEN YOU WORK WITH THE POWERSUPPLY
TO GIVE AN INPUT TO THE RELAY EACH RELAY HAS 3 PINS A, C, B
MAIN POWER SUPPLY
2b. CONNECT B TO THE GROUND WIRE OF
YOUR SWITCH
RASPBERRY PI

2c. CONNECT C TO THE WIRE THAT TURNS B


YOUR LIGHT ON/OFF C
A

2a. CONNECT A TO THE POWER SUPPLY WIRE


OF THE SWITCH

CAUTION: BE EXTRA-CAREFUL IN THIS EXERCISE


WHEN YOU WORK WITH THE POWERSUPPLY
WE WILL DO THIS USING A PIECE OF CODE WRITTEN IN NODE.JS
A KIND SOUL HAS WRITTEN IT AND SHARED IT ONLINE - WE WILL CREDIT HIM AND
USE IT IN OUR PROJECT (THANKS MARCO SCHWARTZ)

THE RASPBERRY PI COMMUNITY IS AMAZING THAT WAY.

THERE ARE SO MANY PROJECTS THAT YOU CAN BE INSPIRED BY. YOU CAN BORROW
THESE IDEAS OR COMBINE MULTIPLE IDEAS TO CREATE COOL STUFF.

AND THE NEXT TIME YOU DO SOMETHING COOL, YOU CAN GIVE BACK BY SHARING IT
SET UP A WEB SERVER TO CONTROL THE A SERVER IS A PIECE OF SOFTWARE THAT
RASPBERRY PI FROM ANYWHERE LISTENS TO REQUESTS ON THE NETWORK THAT
THE COMPUTER IS ON AND CAN RESPOND TO
THEM
THE BROWSER SENDS A
THE COMPUTER REQUEST TO THE URL
THAT MAKES CLIENT SERVER
THE REQUESTS THE SERVER WILL SEND
BACK AN HTML PAGE

YOU WILL GET A URL THAT YOU CAN HIT FROM


ANYWHERE - IT WILL SEND A REQUEST TO THE
SERVER WHICH IN TURN WILL RUN THE PROGRAM
TO TURN ON THE LIGHTS
1. INSTALL NODE.JS ON RASPBERRY PI AND A MODULE FOR GPIO PIN CONTROL
INSTALL NODE.JS (THIS MIGHT TAKE SOME TROUBLESHOOTING - PLEASE CHECK THE
ATTACHED GUIDE)
2. ENABLE ACCESS TO THE RASPBERRY PI GPIO PINS
A. DOWNLOAD THE PACKAGE GPIO-ADMIN FROM GITHUB
B. INSTALL THE PACKAGE AND ADD PI USER TO GPIO GROUP
C. DISABLE YOUR RASPBERRY PI’S DEVICE TREE (FROM THE CONFIGURATION MENU)

3. GET THE CODE FOR SETTING UP A SERVER FROM GITHUB AND RUN IT
A. GITHUB IS AN AWESOME WEBSITE FOR SOFTWARE COLLABORATION WHERE PEOPLE PUT
UP CODE THAT THEY’VE WRITTEN FOR FREE !
B. INSTALL THE GIT-CORE PACKAGE AND USE IT TO DOWNLOAD THE CODE FROM GITHUB
AND RUN IT
THE CODE REPOSITORY FIRST THERE IS A BUNCH
FROM GITHUB WILL HAVE OF BOILERPLATE THAT
MULTIPLE FILES. THE SETS UP THE WEBSERVER
MOST IMPORTANT ONE
IS THE ONE CALLED
APP.JS WHICH LOOKS LIKE
THIS
FIRST THERE IS A BUNCH
THE CODE REPOSITORY OF BOILERPLATE THAT
FROM GITHUB WILL HAVE SETS UP THE WEBSERVER
MULTIPLE FILES. THE
MOST IMPORTANT ONE
IS THE ONE CALLED MAKE A NOTE OF THIS PORT,
APP.JS WHICH LOOKS LIKE THIS WILL BE PART OF THE URL
THIS TO ACCESS THE RASPBERRY PI
THIS IS WHAT THE HTML PAGE / WEB APP WILL LOOK LIKE

3B AFTER RUNNING THE CODE YOU CAN ACCESS THE SWITCH FROM ANYWHERE IN YOUR HOME
USING THIS URL <YOUR PI’S IP ADDRESS>:3700
FIRST THERE IS A BUNCH
THE CODE REPOSITORY OF BOILERPLATE THAT
FROM GITHUB WILL HAVE SETS UP THE WEBSERVER
MULTIPLE FILES. THE
MOST IMPORTANT ONE
IS THE ONE CALLED MAKE A NOTE OF THIS PORT,
APP.JS WHICH LOOKS LIKE THIS WILL BE PART OF THE URL
THIS TO ACCESS THE RASPBERRY PI
THE CODE REPOSITORY FIRST THERE IS A BUNCH
FROM GITHUB WILL HAVE OF BOILERPLATE THAT
MULTIPLE FILES. THE SETS UP THE WEBSERVER
MOST IMPORTANT ONE
IS THE ONE CALLED
APP.JS WHICH LOOKS LIKE
THIS

THIS IS THE CRUCIAL


PIECE, IT IS THE CODE
THAT WILL FLIP THE
SWITCH - SENDING AN
INPUT TO THE RELAY
REQUEST WILL BE A URL - IT IS
DIFFERENT FOR THE ON AND OFF
BUTTONS
CHECK WHETHER YOU
RECEIVED AN ON OR AN OFF
REQUEST

IF THE REQUEST IS ON -
WRITE HIGH TO PIN 7 ;
IF REQUEST IF OFF WRITE LOW TO
PIN 7

YOU CAN CUSTOMIZE THIS BIT OF


CODE IF YOU WANT IT TO DO
SOMETHING DIFFERENT
4. USE NGROK TO ENABLE ACCESS TO THE SERVER FROM ANYWHERE
THE CODE TO SET UP THE SERVER, MAKES THE SERVER ACCESSIBLE ON A LOCAL NETWORK.
USE NGROK TO CREATE A TUNNEL - THAT WILL ALLOW THE SERVER TO BE ACCESSED
FROM ANYWHERE!

DOWNLOAD NGROK FROM https://ngrok.com/

PUT THE FILES IN A FOLDER, AND WHEN YOU ARE IN THAT FOLDER RUN
./ngrok 3700

THIS IS THE URL YOU CAN ACCESS FROM ANYWHERE IN


THE WORLD !!
Installing Node.js
Node.js is an open-source, cross-platform runtime environment for developing server-side web applications. Node.js applications are written in
JavaScript and can be run within the Node.js runtime on OS X, Microsoft Windows, Linux,FreeBSD, NonStop, IBM AIX, IBM System z and IBM i.

Node.js has its own package manager. npm is the package manager for javascript.

1) Start by making sure that your Pi is up to date by entering the following in the terminal:

sudo apt-get update && sudo apt-get upgrade

2 ) next download node (this will download the latest stable package):

wget http://node-arm.herokuapp.com/node_latest_armhf.deb

3) then install the package:

sudo dpkg -i node_latest_armhf.deb

To check whether you have installed the right version,

node -v
Installing Node.js (Troubleshooting)
You might get an error like this

pi@raspberrypi ~ $ node -v node: /usr/lib/arm-linux-gnueabihf/libstdc++.so.6: version `GLIBCXX_3.4.20' not found


(required by node) node: /lib/arm-linux-gnueabihf/libc.so.6: version `GLIBC_2.16' not found (required by node)

Solution 1: Edit your path variable

https://gist.github.com/ghalusa/5485982

wget http://nodejs.org/dist/v0.10.5/node-v0.10.5-linux-arm-pi.tar.gz
cd /usr/local
sudo tar xzvf ~/node-v0.10.5-linux-arm-pi.tar.gz --strip=1

vi ~/.bash_aliases
export PATH=$PATH:/usr/local/bin/node
bash
Installing Node.js (Troubleshooting)

Solution 2: http://stackoverflow.com/questions/32563173/installing-node-js-on-raspberry-pi-2

Run these commands in the terminal:

sudo apt-get update


sudo apt-get install gcc-4.8 g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 20
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50
Installing Node.js (Troubleshooting)

Solution 3:
Upgrade to debian Jessie

Replace all instances of "wheezy" in /etc/apt/sources.list

sudo apt-get update sudo apt-get dist-upgrade

After this you download the Node.js using command line as before.
Installing Node.js (gpio package)
Use npm to install new packages for gpio

sudo npm install -g pi-gpio express # g here stands for globally, pi-gpio is a package name

If you see any error like this (not exactly like this but similar):

node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: Cannot find module
'faye' at Function._resolveFilename (module.js:334:11) at Function._load (module.js:279:25) at Module.require
(module.js:357:17) at require (module.js:368:17) at Object.<anonymous> (/home/dave/src/server/app.js:2:12) at
Module._compile (module.js:432:26) at Object..js (module.js:450:10) at Module.load (module.js:351:31) at
Function._load (module.js:310:12) at Array.0 (module.js:470:10)

It means some package is missing (in this case module.js)

You need to install that package using the same command as above
Get access to GPIO Pins:
GPIO pins can only accessed through sudo command (root access). We need to secure them before we open
them up on internet:

1) We will need to install the gpio-admin package. GPIO admin is a command-line tool to export or unexport
the Raspberry Pi's GPIO pins and allow only the user to control then. It basically allows any user to
access GPIO pins. (When you will see the code, you will understand)

2) Add user pi to gpio group (GPIO admin package create this group)

Newer version of Raspberry Pi - Raspberry Pi 2 Model B needs some further steps to get access to GPIO pins.

1) Disable the device tree.


Installing git-core
2) Make manual changes to the file - src/gpio-admin.c sudo apt-get install git-core # installs git
sudo apt-get update # to update apt-cache
Get access to GPIO Pins:
Installing gpio-admin :

First step was to install the gpio-admin package. Clone the git repository for the gpis-admin package.
(If you have not installed git-core - do that first as explained before)

git clone git://github.com/quick2wire/quick2wire-gpio-admin.git


cd quick2wire-gpio-admin

If you are not using the new Raspberry Pi 2 Model B, you can install the gpio-admin package.
make
sudo make install
Get access to GPIO Pins:
Installing gpio-admin :

However, if you are using the new Raspberry Pi 2 Model B, you have to make some changes to this file src/
gpio-admin.c:

nano src/gpio-admin.c # you are already in quick2wire-gpio-admin

You have to edit 1 line. In the picture below, you will replace line 30 with -ve sign with +ve sign. Essentially
changing “/sys/devices/virtual” to “/sys/class”
Get access to GPIO Pins:
Installing gpio-admin :

Once you have done the changes, save them and exit the file.Now install the package as before:

make

sudo make install


Get access to GPIO Pins:

Add the user to gpio group:

sudo adduser pi gpio


Get access to GPIO Pins:
Disabling Device Tree

Device Tree - Device Tree (DT) is a description of the hardware in a system. It should include the name of the
base CPU, its memory configuration and any peripherals (internal and external). A DT should not be used to
describe the software, although by listing the hardware modules it does usually cause driver modules to be
loaded. It helps to remember that DTs are supposed to be OS-neutral, so anything which is Linux-specific
probably shouldn't be there.

Raspberry Pi's latest kernels and firmware, including Raspbian and NOOBS releases, now by default use Device
Tree (DT) to manage some resource allocation and module loading.

The main impact of using Device Tree is to change from everything on, relying on module blacklisting to manage
contention, to everything off unless requested by the DTB.
Get access to GPIO Pins:
Disabling Device Tree

Go to Raspi-Config and select Advanced Options (2nd last).


Get access to GPIO Pins:
Disabling Device Tree

After that select Device Tree and disable the use of Device Tree.
Download and run the code

This project is an explanation of https://www.openhomeautomation.net/control-a-relay-from-anywhere-using-


the-raspberry-pi/

by Marco Schwartz. He has written some cool books also. You can use purchase them here:

https://www.openhomeautomation.net/products/

This code is written in Node.Js , Javascript, HTML and CSS.

You can do some minor tweaks in it as per your needs


Download and run the code

Download the GitHub repository of the project on your Raspberry Pi

First create a folder using mkdir and then clone the repository in that folder.

mkdir pi-node-relay

cd pi-node-relay

git clone https://github.com/openhomeautomation/pi-node-relay


Download and run the code
Run the app and test on local network

Enter the folder where the project repository is cloned. Type in the terminal:

node app.js

You should see the following message:

Listening on port 3700

You can now go to the web browser of your Pi, and type:

localhost:3700/interface

or go to any other computer that is on the local network / wifi / and type

ip-address-of-raspberry-pi:3700/interface (for example: 192.168.0.107:3700/interface)

Das könnte Ihnen auch gefallen