Beruflich Dokumente
Kultur Dokumente
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
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
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
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
IF THE REQUEST IS ON -
WRITE HIGH TO PIN 7 ;
IF REQUEST IF OFF WRITE LOW TO
PIN 7
PUT THE FILES IN A FOLDER, AND WHEN YOU ARE IN THAT FOLDER RUN
./ngrok 3700
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:
2 ) next download node (this will download the latest stable package):
wget http://node-arm.herokuapp.com/node_latest_armhf.deb
node -v
Installing Node.js (Troubleshooting)
You might get an error like this
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
Solution 3:
Upgrade to debian Jessie
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)
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.
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)
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:
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
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
After that select Device Tree and disable the use of Device Tree.
Download and run the code
by Marco Schwartz. He has written some cool books also. You can use purchase them here:
https://www.openhomeautomation.net/products/
First create a folder using mkdir and then clone the repository in that folder.
mkdir pi-node-relay
cd pi-node-relay
Enter the folder where the project repository is cloned. Type in the terminal:
node app.js
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