emeckle86
Posts: 5
Joined: Thu Jan 24, 2013 5:57 pm

Raspberry Pi+Arduino+Xbee

Thu Jan 24, 2013 10:21 pm

What I find to be quite a good combo together is the Raspberry Pi, an Arduino Mega, and a Xbee pro with Arduino shield. First off, download the Raspbian image or Xbian (XBMC for raspberry pi). The Xbian installer is very easy to install because it flashes the SD card with the image it downloads from the internet. When the installation to SD card is complete, pop it into the Raspberry Pi. Hook up the Pi to an HDMI capable TV and use a USB micro power cable to give it power. Grab an ethernet cable and connect the Pi to your router. You can either directly connect a keyboard to the Pi, or you can use Putty (Windows) or Terminal (Mac) to communicate with SSH to the Pi by grabbing the Pi's IP address (192.168.0.###). If using Xbian, it will tell you in System info. The default login in is username xbian and password Raspberry. Raspbian's default username is pi with password raspberry. Go to your router's configuration page (192.168.0.1) and login. Go to Gateway->Port Forwarding. Enable two ports to be forwarded. For both, put the local port to be forwarded that corresponds to Pi's IP address. For example, if Pi is at 192.168.0.100, then put 100 for both. Next you want to set one to Internal port 22 and one to internal port 80 and have the start and end ports the same (100,22,22,22; 100,80,80,80). Leave public interface port alone, and enable both. Also download WINSCP for FTP to your Raspberry Pi server you are about to create. If you are using Raspbian, you want to expand the root partition to fill the SD card, else you will quickly run out of room...the default partition when flashing an SD card is somewhere around 55 Mb for the Linux operating system. Xbian does the root partition expansion automatically when it first powers on. So the next step is to make Raspberry Pi a full fledged server. If using Xbian, use Putty to connect to your Pi (have to have ethernet cable connected). Get away from the setup box by clicking Return. This should bring up the command prompt, looking like xbian@192.168.0.100:~#. You can change the default password, do an update, and install any packages from here. Just Google "Raspberry Pi command prompt" to get an idea how. So, first install Apache webserver (sudo apt-get install apache2 php5 libapache2-mod-php5). Hit 'y' for yes, wait about 5 min and you should now have your very own webserver at 192.168.0.### from the URL bar), it should bring up the default page "It works!". You may have to restart the Apache server (sudo service apache2 restart). Change ownership of the folder where html files for your webserver are put (sudo chown -R xbian /var/www). Install a FTP service to make transfer of files possible from anywhere in the world (sudo apt-get install vsftpd). There are some things you want to change in the configuration file (sudo nano /etc/vsftpd.conf). Scroll down, delete the #(comment out symbol) infront of local_enable=YES and write_enable=YES. Scroll all the way to the end and typle in "force_dot_files=YES". Hit Ctrl+X,'y',Enter to save. Now you can use WINSCP to connect to your server, use SFTP to connect, default to port 22(I think). Go to Google and type in "what is my IP address" to get you external IP address to the world. Since you did port forwarding, you can access your webserver from anywhere; to test, turn off wi-fi on your phone and go to your ip address. It is different than the 192.168.0.###. It'd be like 74.##.#.###. By default, your browser loads the index.html file. You can create more html files, link to them, do whatever you want. Learn a little html programming for use later on. Practice making drop down menus cause this will really pay dividends in a bit. Create radio buttons, check boxes, text enter fields, play with headers, have left justified, center justified, right justified columns, basically just arrange things the way you want. Change background color, font size, button size, etc. Now comes the great part that makes the Raspberry Pi do truely useful things for you. You want to have Pi communicate through Serial interface to Arduino to do functions on the backside. Look up the pin layout for the Pi to see the transmit pin and the ground pin. Be careful, as Pi is at 3.3V logic level and Arduino's other than the Due are at 5V logic level. Transmitting from the Pi and receiving to the Arduino works just fine. Go back to the command prompt. Type in "sudo nano /etc/inittab". If using Xbian, go to the bottom where it says "TO:....ttyAMA0". Comment that line out using a # sign. Save and go back to the command prompt. You want to give yourself dialout access, ie serial output from programs. Type in "sudo usermod -a -G dialout xbian" and "sudo usermod -a -G dialout www-data". It should give you no problems, then sudo reboot. Google raspberry pi php serial class. You want your submit buttons affiliated with buttons, dropdown menus, etc to be a POST, which the php script reads and acts on to PUT the posted values onto the Serial transmit line with a selected baud rate, have correct parity, and stop bits. I would take a look at http://ictoblog.nl/raspberry-pi/arduino ... unications. You want to put the php_serial_class.php file into whatever folder in the www-data folder you want to try doing Pi->Arduino communication. Create a file.php for each html link or button you have. I can include examples that work if anyone wants them. So now that Raspberry Pi up and running and sending out data over Serial interface, now what? Have 2 or more Arduino's and 2 or more Xbee's? Then you are in luck. For one Arduino, connect its Serial1,Serial2,or Serial3 receive pin to Raspberry Pi's transmit pin. Everytime something goes out, the Arduino can read it in. Using a little programming and string comparison, you can use a little ingenuity to directly address what Arduino you want to do something. Have the one Arduino connected to the Pi have a Xbee shield that uses the main Serial port (UNO only has one so it is harder to debug). In your PHP script, you can predetermine which Arduino will be addressed. You can make an html page that can talk to infinite Arduino's, set any of the digital pins HIGH or LOW, analogWrite any of the 13 PWM pins to any value 0-255, digitalRead any of the digital pins, or analogRead any of the analog pins. Just do a "for" loop in the setup to iterate through all of them to initialize them as output. So how useful is this? Well, get a 2 channel relay from Ebay and a 3 foot power strip, a gang box, an outlet receptacle, some wire strippers, some thermostat wire (for carrying a digital signal), and some electrical tape from Home Depot. Cut the power strip cord as close to strip itself as possible, you just want the cord and prong. If you only have the cord itself for this project, you want a little extra wire for splicing purposes, otherwise get wire that is rated to 15-20A, or is pretty thick gauge. The 2 channel relay will require 4 signal wires (cut two pieces of 2 strand or one piece of 4 strand). These will go to any of the digital pins, plus ground, plus 5/3.33 V. Connect them using a female to female Dupont wire to connect to the Pi's male pins. Splice the Live Wire to go to 2 of the relay terminals. You will one of two types of relays. Either it needs a HIGH signal to close the normally open contact, or it will need a LOW(ground) signal to close the normally open contact. The type matters, any you might need to switch your logic in Arduino to account for this cause if you are trying to control a lamp and Arduino gets unplugged, the lamp might get turned on (or off). I recommend testing this all out before working with 120 VAC. But I leave that up to you. I have had good success with analog magnetic reed style relays. I had a friend who used solid state relays that literally melted when too high of a starting current passed through it. So now that you have an outlet box, you can take the first Arduino and pass the command onto infinite other Arduino's that have similar scripts that each look to see if itself is being addressed, run some string comparisons and act on them. You could quickly scale this to have every lamp or fan in your house controlled from your phone. If you need any example code or have any trouble, just ask me what you need. If you have a cool project in mind, let me know! Good luck and be safe with high voltage.

babbage
Posts: 123
Joined: Mon Aug 29, 2011 2:23 pm

Re: Raspberry Pi+Arduino+Xbee

Fri Jan 25, 2013 12:27 pm

That was a very difficult read :-(

I think some formatting may be in order :-)

User avatar
bugsy_malone 666
Posts: 292
Joined: Mon Dec 17, 2012 10:50 pm
Location: Basingstoke UK

Re: Raspberry Pi+Arduino+Xbee

Fri Jan 25, 2013 1:38 pm

babbage wrote:That was a very difficult read :-(

I think some formatting may be in order :-)
Agreed, I gave up reading after a few sentences.

I think it needs to be broken down into sections of the project and paragraphs to make it easier to follow.

bgirardot
Posts: 518
Joined: Wed Oct 10, 2012 6:20 am
Location: Switzerland

Re: Raspberry Pi+Arduino+Xbee

Sat Jan 26, 2013 1:49 pm

Here is a slightly formatted version of the original post:

What I find to be quite a good combo together is the Raspberry Pi, an Arduino Mega, and a Xbee pro with Arduino shield.

First off, download the Raspbian image or Xbian (XBMC for raspberry pi). The Xbian installer is very easy to install because it flashes the SD card with the image it downloads from the internet. When the installation to SD card is complete, pop it into the Raspberry Pi.

Hook up the Pi to an HDMI capable TV and use a USB micro power cable to give it power.

Grab an ethernet cable and connect the Pi to your router.

You can either directly connect a keyboard to the Pi, or you can use Putty (Windows) or Terminal (Mac) to communicate with SSH to the Pi by grabbing the Pi's IP address (192.168.0.###).

If using Xbian, it will tell you in System info. The default login in is username xbian and password Raspberry. Raspbian's default username is pi with password raspberry.

Go to your router's configuration page (192.168.0.1) and login.

Go to Gateway->Port Forwarding. Enable two ports to be forwarded. For both, put the local port to be forwarded that corresponds to Pi's IP address. For example, if Pi is at 192.168.0.100, then put 100 for both.

Next you want to set one to Internal port 22 and one to internal port 80 and have the start and end ports the same (100,22,22,22; 100,80,80,80). Leave public interface port alone, and enable both.

Also download WINSCP for FTP to your Raspberry Pi server you are about to create.

If you are using Raspbian, you want to expand the root partition to fill the SD card, else you will quickly run out of room...the default partition when flashing an SD card is somewhere around 55 Mb for the Linux operating system. Xbian does the root partition expansion automatically when it first powers on.

So the next step is to make Raspberry Pi a full fledged server. If using Xbian, use Putty to connect to your Pi (have to have ethernet cable connected). Get away from the setup box by clicking Return. This should bring up the command prompt, looking like xbian@192.168.0.100:~#. You can change the default password, do an update, and install any packages from here. Just Google "Raspberry Pi command prompt" to get an idea how.

So, first install Apache webserver (sudo apt-get install apache2 php5 libapache2-mod-php5). Hit 'y' for yes, wait about 5 min and you should now have your very own webserver at 192.168.0.### from the URL bar), it should bring up the default page "It works!". You may have to restart the Apache server (sudo service apache2 restart). Change ownership of the folder where html files for your webserver are put (sudo chown -R xbian /var/www).

Install a FTP service to make transfer of files possible from anywhere in the world (sudo apt-get install vsftpd). There are some things you want to change in the configuration file (sudo nano /etc/vsftpd.conf). Scroll down, delete the #(comment out symbol) infront of local_enable=YES and write_enable=YES. Scroll all the way to the end and typle in "force_dot_files=YES". Hit Ctrl+X,'y',Enter to save. Now you can use WINSCP to connect to your server, use SFTP to connect, default to port 22(I think).

Go to Google and type in "what is my IP address" to get you external IP address to the world. Since you did port forwarding, you can access your webserver from anywhere; to test, turn off wi-fi on your phone and go to your ip address. It is different than the 192.168.0.###. It'd be like 74.##.#.###.

By default, your browser loads the index.html file. You can create more html files, link to them, do whatever you want. Learn a little html programming for use later on. Practice making drop down menus cause this will really pay dividends in a bit. Create radio buttons, check boxes, text enter fields, play with headers, have left justified, center justified, right justified columns, basically just arrange things the way you want. Change background color, font size, button size, etc.

Now comes the great part that makes the Raspberry Pi do truely useful things for you. You want to have Pi communicate through Serial interface to Arduino to do functions on the backside.

Look up the pin layout for the Pi to see the transmit pin and the ground pin. Be careful, as Pi is at 3.3V logic level and Arduino's other than the Due are at 5V logic level.

Transmitting from the Pi and receiving to the Arduino works just fine. Go back to the command prompt. Type in "sudo nano /etc/inittab". If using Xbian, go to the bottom where it says "TO:....ttyAMA0". Comment that line out using a # sign. Save and go back to the command prompt.

You want to give yourself dialout access, ie serial output from programs. Type in "sudo usermod -a -G dialout xbian" and "sudo usermod -a -G dialout www-data". It should give you no problems, then sudo reboot.

Google raspberry pi php serial class. You want your submit buttons affiliated with buttons, dropdown menus, etc to be a POST, which the php script reads and acts on to PUT the posted values onto the Serial transmit line with a selected baud rate, have correct parity, and stop bits.

I would take a look at http://ictoblog.nl/raspberry-pi/arduino ... unications.

You want to put the php_serial_class.php file into whatever folder in the www-data folder you want to try doing Pi->Arduino communication.

Create a file.php for each html link or button you have. I can include examples that work if anyone wants them.

So now that Raspberry Pi up and running and sending out data over Serial interface, now what?

Have 2 or more Arduino's and 2 or more Xbee's? Then you are in luck. For one Arduino, connect its Serial1,Serial2,or Serial3 receive pin to Raspberry Pi's transmit pin. Everytime something goes out, the Arduino can read it in.

Using a little programming and string comparison, you can use a little ingenuity to directly address what Arduino you want to do something.

Have the one Arduino connected to the Pi have a Xbee shield that uses the main Serial port (UNO only has one so it is harder to debug). In your PHP script, you can predetermine which Arduino will be addressed.

You can make an html page that can talk to infinite Arduino's, set any of the digital pins HIGH or LOW, analogWrite any of the 13 PWM pins to any value 0-255, digitalRead any of the digital pins, or analogRead any of the analog pins. Just do a "for" loop in the setup to iterate through all of them to initialize them as output.

So how useful is this? Well, get a 2 channel relay from Ebay and a 3 foot power strip, a gang box, an outlet receptacle, some wire strippers, some thermostat wire (for carrying a digital signal), and some electrical tape from Home Depot. Cut the power strip cord as close to strip itself as possible, you just want the cord and prong. If you only have the cord itself for this project, you want a little extra wire for splicing purposes, otherwise get wire that is rated to 15-20A, or is pretty thick gauge. The 2 channel relay will require 4 signal wires (cut two pieces of 2 strand or one piece of 4 strand). These will go to any of the digital pins, plus ground, plus 5/3.33 V. Connect them using a female to female Dupont wire to connect to the Pi's male pins. Splice the Live Wire to go to 2 of the relay terminals. You will one of two types of relays. Either it needs a HIGH signal to close the normally open contact, or it will need a LOW(ground) signal to close the normally open contact. The type matters, any you might need to switch your logic in Arduino to account for this cause if you are trying to control a lamp and Arduino gets unplugged, the lamp might get turned on (or off). I recommend testing this all out before working with 120 VAC. But I leave that up to you. I have had good success with analog magnetic reed style relays. I had a friend who used solid state relays that literally melted when too high of a starting current passed through it. So now that you have an outlet box, you can take the first Arduino and pass the command onto infinite other Arduino's that have similar scripts that each look to see if itself is being addressed, run some string comparisons and act on them. You could quickly scale this to have every lamp or fan in your house controlled from your phone.

If you need any example code or have any trouble, just ask me what you need.

If you have a cool project in mind, let me know!

Good luck and be safe with high voltage.

stephenzzz
Posts: 5
Joined: Sun Jan 27, 2013 5:13 pm

Re: Raspberry Pi+Arduino+Xbee

Sun Jan 27, 2013 5:41 pm

bonjour.. Greetings

I got here by googling Raspberry and arduino... I think that your message is exactly on the way I am looking for.. except that there is some (maybe serious) software integration.

Following a trawl on Home automation devices, you get Z10, Zwave and Zigbee, as successive technologies... UPB, IR, Wifi. Given that Wifi is what is used by phones and most personal devices, have Home automation also on Wifi would seem the right way to go.. except that nowhere (and maybe I have not looked very hard, ) is this stated clearly... which means Zigbee I believe

So I am looking to have a seamless android app, controlling zigbee devices. Your post (I am guessing ) seems to imply that you have the RPi controlling wirelessly. I would use the http://phonegap.com HTML to Android. so please remind me the setup up, with just one Zigbee device..

so two questions:
1. I need WHAT hardware .. to transmit a signal to the zigbee device, arduino plus shield??? sorry I got a little bit lost.... in that bit of detail. sorry if you say it...

2. not sure if you are in the French speaking part of switzerland, but I found these devices http://docdif.fr.grpleg.com/general/leg ... Zigbee.pdf

would these work?

thanks for your comments...

stephenzzz
Posts: 5
Joined: Sun Jan 27, 2013 5:13 pm

Re: Raspberry Pi+Arduino+Xbee

Sun Jan 27, 2013 6:15 pm

just noticed (of course) that the legrand equipment is proprietary protocol, so do you know any open Zigbee products? home automation stuff, light, plugs, presence indicators.. etc, or is there a USB proprietary transmitter which can be addressed?

stephenzzz
Posts: 5
Joined: Sun Jan 27, 2013 5:13 pm

Re: Raspberry Pi+Arduino+Xbee

Sun Jan 27, 2013 6:16 pm

either way, any php code to talk to the RPi, arduino would be totally helpful

simonpb1
Posts: 25
Joined: Thu Sep 13, 2012 11:25 am

Re: Raspberry Pi+Arduino+Xbee

Sun Jan 27, 2013 6:59 pm

You will not get an IP device to talk to a zigbee device without some form of gateway. 6LoWPAN is the way to go it allows IP based devices such as smartphone, PC or any other devices connected to the LAN/WAN to send packets to wireless devices.

I am in the process of creating a gateway to do exactly this. The pi acts as a border router to intercept packets destined for a WPAN(Wireless Personal Area Network) and also to provide a control system using PHP/Mysql to control the devices on the PAN.
This will include Lights, Roller Blinds and whatever else needs to be controlled.
If you are interested drop me a private message.

emeckle86
Posts: 5
Joined: Thu Jan 24, 2013 5:57 pm

Re: Raspberry Pi+Arduino+Xbee

Mon Jan 28, 2013 3:05 am

stephenzzz,

I'll send you some files that allow you to control pins on the Arduino. Since I can not include files with the extension .PHP or .HTML, I have to Private Message you with them. Construct the appropriate PHP or HTML file. If anyone else wants them, let me know.

Edit the test.php or writetoself.php file to include your own IP address followed with slash marks as appropriate to denote what folder the file(s) are in. For example, if your ip address is 12.34.567.89 and say you create a folder in the www-data folder called "HelloWorld" and put the files in it, the PHP file should be edited to say 12.34.567.89/HelloWorld/test.html to give a loop back to the html file test.html when you hit "submit".

The file.html is a good example of having just one button on a page that is conveniently big enough on an iPhone that controls a single relay/lamp with just clicking HIGH/LOW. The writetoself.html allows you to digitalWrite any pin on the Arduino connected to the PI HIGH/LOW.

Also, I included the appropriate Arduino files for both the Arduino directly connected to the PI (Serial1 on a Mega or Due) and the file for a slave Arduino that receives the Serial transmissions via Xbee from the Mega connected to the Pi. Keep in mind that Xbee is best done at 9600 bps and is on Serial0 with a shield. I configured the PHP scripts to run at 115200 bps, so take that into account when serial reading from the Pi on Serial1. Don't forget to include the Serial.begin(9600/115200) in the Arduino setup.

To do a check to see if you are having anything going out the serial port of the pi, go into the PHP script files (writetoself or test.php) and delete the little PHP script that loops it back. If when you hit submit on the HTML page and you are directed to the resulting page with a bunch of errors saying unable to establish serial communication, set the baud rate, etc...then you have not properly given yourself "dialout" access. This allows any PHP script to access the serial port. Make sure you restart the Pi following a "sudo usermod -a -G dialout xbian" or "sudo usermod -a -G dialout www-data" command. Reperform the test without the PHP loopback script. The errors should go away, then you can copy the loopback script into position and you won't have to do anything more.

I have another slightly altered Arduino file for doing all basic functions, to include analogWrite, but conceptually it is similar. Just make an "echo" variable in the PHP script following a POST and send it over Serial, run some String manipulation to find what values you are looking for.

emeckle86
Posts: 5
Joined: Thu Jan 24, 2013 5:57 pm

Re: Raspberry Pi+Arduino+Xbee

Mon Jan 28, 2013 4:12 am

The following is a parts list for controlling stuff via Web interface on any internet enabled device:
(*Note, ebay links are subject to removal after auction or buy-it-now ends, but just be aware that most of the stuff can be bought on eBay from China at a fraction of the cost it would in RadioShack) . (If time is not an issue, an Arduino Mega from Radioshack=$65+tax; Arduino Mega from China=$22)

1 Raspberry Pi:
http://downloads.element14.com/raspberr ... aspi-group

2 or more Xbee Pros:
http://www.ebay.com/itm/XBee-Pro-Series ... 257a3e100c

2 or more Xbee Pro Shields:
http://www.ebay.com/itm/New-SainSmart-X ... 3a6e8ae505

1 Ethernet Cable

1 Arduino Mega or Due:
http://www.ebay.com/itm/MEGA-2560-R3-AT ... 2a26c854cc

1 or more Arduino Uno or better:
http://www.ebay.com/itm/UNO-R3-ATMEGA-3 ... 3ccb78770d

1 or more two channel relays:
http://www.ebay.com/itm/5V-2-Channel-Re ... 5aecadc194

1 or more gang box:
http://www.homedepot.com/h_d1/N-5yc1v/R ... QX3Sr80WSo

1 or more outlet receptacle:
http://www.homedepot.com/h_d1/N-25ecodZ ... QX3qr80WSo

1 or more gang box wall plate:
http://www.homedepot.com/h_d1/N-5yc1v/R ... QX4Gb80WSo

1 power strip (just want the wire and prong, so any scrap appliance would work):
http://www.homedepot.com/h_d1/N-5yc1v/R ... QX33L80WSo

Thermostat wire, phone wire, intercom wire, or phone wire (Small gauge, flexible):
http://www.homedepot.com/h_d1/N-5yc1v/R ... QX4ab80WSo

Dupont wire (female-female or female-male; just to make connection from Pi to Arduino and relay to Arduino....female-female would be could for the thermostat wire to relay pins) :
http://www.miniinthebox.com/dupont-wire ... 78879.html

Electrical Tape
Wire Strippers( or just use a knife or scissors)
Maybe some solder and a soldering iron

emeckle86
Posts: 5
Joined: Thu Jan 24, 2013 5:57 pm

Re: Raspberry Pi+Arduino+Xbee

Fri Mar 22, 2013 5:06 pm

XBianInstaller->1.0Alpha5 to SD
sudo apt-get update
sudo apt-get install apache2 php5 libapache2-mod-php5
sudo chown -R xbian /var/www
sudo nano /etc/inittab.....=> # in front of bottom line TO:23,save
sudo usermod -a -G dialout xbian
sudo usermod -a -G dialout www-data

Return to “General discussion”