Wednesday, June 10, 2015

Home Automation Part 3 rpi static ip, On LED, and Arduino Code

Static IP

The worst thing that can happen to you is your ip address on your server changing every once in a while. When that happens you either need to change it back or change all of your devices that talk to it. So you will want to nip that in the but early and set a static ip address for your server that is never going to change. To do that you need to modify /etc/network/interfaces
sudo nano /etc/network/interfaces
In that file you will see either iface eth0 inet dhcp or iface eth0 inet manual. we need to change that to iface eth0 inet static
Now that we are not getting information from the router we need to set that information our selves. Copy and paste below into the file. X should be the "class" of your router. Class is the best name I got. Most routers use either 0 or 1. A should be the address you want your server to be. It has to be between 1 and 254 and cant be your gateway. Pick your poison. B is where you can access the router config. This is usually either 1 or 254
address 192.168.X.A
network 192.168.X.0
broadcast 192.168.X.255
gateway 192.168.X.B
While we at it we might as well change the hostname of the pi. it standard name is I believe raspberrypi. There are 2 files we need to modify. /etc/hosts and /etc/hostname
sudo nano /etc/hosts
You want to change the last line which starts with to whatever you want your hostname to be.
sudo nano /etc/hostname 
Change this file to the same as in the previous file.Then run the following commands to update the hostname and reboot the pi.
sudo /etc/init.d/
sudo reboot
Now make sure when you ssh back into the pi you use your new ip address


Now I get tired of having to guess when I can ssh back into my pi everytime I reboot it. So I created a small script that turns an LED on when I can ssh back in. 
# export GPIO pin 4 and set to output
echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction
echo 1 > /sys/class/gpio/gpio4/value

Basically you set a gpio to output, and set the pin to on or 1. In the above I use pin 4 since it had a ground right next to it. Then wire an LED to it with the proper polarity and run the script at start. To make ir run at start add sudo sh /location/of/script/  before exit 0
sudo nano /etc/rc.local

Now every time the pi restarts the LED will turn on when you can access it again. It also will turn off when the pi is completely shutdown letting you know when you can unplug it safely.  

Arduino Code

For testing purposes I have been using an arduino UNO with an Ethernet shield. The code so far has not been test on anything but that. What I have done with the code is everything that needs to be changed is changed in the variables at the top of the code. There are only 3 things that need to be changed. 1st is the devID. This is the ip address of the device as well as what topics it subscribes and publishes too. Second is IOcount. This is the number of buttons and outputs you have. For simplicity I have one button for each output. In the code the buttons will turn the output on when they have internet access or don't. The last item that needs to be changed is the mac address. I am planning on making that dependent on the devID as well that way it is one less item that needs to be changed. For outputs I am using the analog pins A0-5 and for inputs I am using 2,3,5-8. I am sure that my code could be improved upon and if you do let me know but here it is as of now. you will need to install the pubsubclient library and make sure it is installed properly, I had to move folders around due to the version number. Arduino Code Download. Now it is time to get the ESP8266-07 working.

No comments:

Post a Comment