Archive for June 2014
Access Raspberry Pi via internet
In this post I will explain how to access Raspberry pi via Internet. I am trying out a home automation system using Pi and I wanted to access a node.js service running in my Raspberry pi. Also I may wanted to SSH to the pi to restart or configure it while I am away from home.
Configuring the Router
In my home Raspberry Pi is connected to a Netgear R6300 router. The router is then connected to a DSL modem. In these kind of setup we might be dealing with four different IP’s
-
External IP assigned to you by your ISP.
-
IP Address of the modem
-
IP Address of the Router assigned by the modem
-
IP Address of the Raspberry Pi assigned by Router.
Prerequisite
-
Credentials to login to the Modem configuration.
-
Credentials to login to the router configuration.
External IP: you can get your external ip by visiting whatismyip.com
IP Address of the Modem and Router: As I am using Netgear modem, I login into Netgear genie app. And from the Internet section I could see the IP Address assigned to my router as shown below.
As you could see the IP address assigned to my router is 192.168.1.2. Also you can see the Gateway IP address 192.168.1.1, that IP will be the IP address of your modem.
IP address of the Raspberry pi: SSH to Raspberry PI and issue ‘ifconfig’ command and you can see some details and check wlan0 as shown below. Line in bold shows the ip address of the Pi.
wlan0 Link encap:Ethernet HWaddr 04:a1:51:6a:fb:8d
inet addr:10.0.0.10 Bcast:10.0.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5543 errors:0 dropped:11 overruns:0 frame:0
TX packets:1799 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1200798 (1.1 MiB) TX bytes:192891 (188.3 KiB)
In my scenario I assigned a static IP to this Pi, other wise router’s DHCP server will assign ip and might change from time to time. So most router have configuration, to set static IP to the devices and will never change. At my house most of the devices like TV, Tabs and Raspberry PIs have static IP’s and the guest devices uses DHCP assigned IP’s.
Let’s configure SSH to the Pi from internet.
Router Port Forwarding
Excerpt from Genie help
Port forwarding is designed for FTP, Web server, or other server-based services. Once port forwarding is set up, requests from the Internet are forwarded to the proper server.
In the Genie app you can find Port forwarding in Advanced setup->Port forwarding/Port triggering, you might be able to find some thing similar in other modem’s configuration.
I clicked ‘Add Custom Service’ button and it will show the below screen to setup Port forwarding configuration. Here I am configuring SSH.
Here 22 is the port where SSH server in my PI is listening. Internal IP address is the IP address of the Raspberry Pi. Click apply and the changes will get saved.
We setup the port forwarding in the router. We need to do the similar configuration in Modem.
Modem Port Forwarding
Enter the IP address of the modem in the Chrome/IE, enter the user name and password if prompted.
Note: My DSL modem is pretty old one and may not be match with the interface of your modem configuration screen.
The port forwarding configuration in my Modem is located at Advanced Setup/NAT/Virtual servers. See the configuration below.
Here I setup the external port number as well as internal port number. You can have different external port number if you want, say for e.g. external port number 8090 can be mapped to internal port number 22. The server IP address should be the IP address of the router.
Check whether the port is opened by going to yougetsignal.com, if the port is still closed then you might need to do Port triggering in Modem as shown below.
We successfully configured SSH to Raspberry Pi from internet, you can check using a machine connected to a different network and connect to Pi using any SSH client like Putty. While connecting the IP address should be the external IP address and the port should be the one configured in Modem, here it’s 22.
Conclusion
The above approach will work, in case you want to open any port, I have a node.js service listening to 8090 in the same Raspberry pi. I did the above approach to access it via internet.
Leave your comments if you like this post or in case of any questions.
Pi Tracker–Code running in Raspberry Pi
In my last post you can see the Tracker in action. This post I will explain the code running in Raspberry pi that controls the Tracker. I use Wiring Pi to control the GPIO instead of the default RPI.GPIO. The intention of using Wiring pi is because Software PWM is easy to implement. Any way after the first run of the tracker I realized that my tracker is slow moving one and no need to control the speed, so I haven’t used Software PWM here.
Installing Wiring Pi for python
Command prompt type the below commands
-
sudo apt-get install python-dev python-pip
-
sudo pip install wiringpi2
Let’s check whether wiringPi installed correctly
sudo python
import wiringpi2
wiringpi2.piBoardRev()
if it returns a value then wiring Pi installed successfully.
Pi Tracker php code running in Raspberry pi
Motor.py
import wiringpi2 as io from time import sleep class Motor: def __init__(self, gpio_a, gpio_b): self.Gpio_a=gpio_a self.Gpio_b=gpio_b self.activeGpio=gpio_a def setup(self): io.wiringPiSetupGpio() io.pinMode(self.Gpio_a,1) io.pinMode(self.Gpio_b,1) def forward(self): self.stop() self.activeGpio=self.Gpio_a def reverse(self): self.stop() self.activeGpio=self.Gpio_b def acclerate(self,speed): io.digitalWrite(self.activeGpio,1)
#speed param is not used here.
def stop(self):
io.digitalWrite(self.Gpio_a,0)
io.digitalWrite(self.Gpio_b,0)
Engine.py
from Motor import Motor class Engine: motor_a=Motor(17,27) motor_b=Motor(23,24) def __init__(self): Engine.motor_a.setup() Engine.motor_b.setup() def steer(self, command): direction,speed=command.split(",") if(direction=="CLOSE"): self.stop() elif (direction=="U"): self.forward() elif (direction=="L"): self.left() elif(direction=="R"): self.right() else: self.reverse() def forward(self): Engine.motor_a.forward() Engine.motor_b.forward() self.acclerate() def reverse(self): Engine.motor_a.reverse() Engine.motor_b.reverse() self.acclerate() def right(self): Engine.motor_b.reverse() Engine.motor_a.forward() self.acclerate() def left(self): Engine.motor_a.reverse() Engine.motor_b.forward() self.acclerate() def acclerate(self): Engine.motor_a.acclerate(10) Engine.motor_b.acclerate(10) def stop(self): Engine.motor_a.stop() Engine.motor_b.stop() print "engine stopped"
trackerRemoteListener.py
import socket from Engine import Engine TCP_IP = '10.0.0.10' TCP_PORT =5005 BUFFER_SIZE = 100 engine= Engine() engine.stop() s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind((TCP_IP, TCP_PORT)) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.listen(1) conn, addr = s.accept() print 'Connection address:', addr while True: try: data = conn.recv(BUFFER_SIZE) print "received data: ",data engine.steer(data) if not data: engine.stop() conn,addr=s.accept() except KeyboardInterrupt: conn.close() engine.stop() print "connection closing" except Exception as err: conn.close() s.listen(1) conn,addr=s.accept() #s.listen(1) print "error occured" pass conn.close()
How it works
Motor.py class represent a motor in the Tracker. It accepts two parameters and it should be a valid GPIO pin to which the motor is connected. For e.g Motor_a is connected to GPIO 17 and 27. If we give write 1 to 17 and 0 to 27 then Motor_a will run forward, write 1 to 27 and 0 to 17 Motor_a run reverse.
Pi Tracker uses two motors to drive, Engine.py controls these two motors. As you can see in the code Engine.py creates two Motor instance. It also has functions to steer the tracker in different directions.
trackerRemoteListener.py is the socket program that listens to the command from external device to control the tracker. To control this tracker I used an Android tab that sends the command to this listener.
Hope the code is self explanatory, if not post your questions in the comment section.
Happy coding…
PI Tracker in action–video
In my last post I gave an overview of a toy I created using Raspberry Pi and the components used for it. After that some of my friends wanted to see the toy in action and here is the video.
Video Streaming
A new feature added to the tracker after the last post is the video streaming to the controlling device. In the modified tracker a USB camera is attached to the front of the tracker and connected to the Raspberry Pi. The RaspPi host a mjpg streamer and the video can be streamed to any device connected to the same network, including the Android device that controls the tracker. This helps my son to sit anywhere in the house and control the tracker.
Stay tuned for the next post with source code and more…
Edit: A new post added explaining the Source code running in Raspberry pi
PI TRACKER – Raspberry Pi based WiFi controlled toy
I was always fascinated about software and hardware interaction. But with my limited knowledge in electronics, I kept the hardware part aside and was working only in software. After having some hands on experience with my first Raspberry Pi, I decided to build a toy for my son. I purchased my second pi and some other components and this is what I built. Yes it looks ugly but worth playing with it :).
Last couple of days I was refreshing my basic electronic skills and virtually designing the software and hardware for the device. You can find so many similar RaspPi based toys and I am adding one more to it.
Yesterday evening I got the last piece of the puzzle, the rechargeable battery. After that I spent the next 5 hrs to code the application running in the vehicle and the Remote control app for my Android tab, remote control app is based on the Acclerometer of the device. To control the toy I decided to use WiFi and use my Android tab as a steering wheel. I will go more details into the software part in a different post.
Future addition
-
Obstacle detection and avoidance system
-
Video feed to the remote control device
Components
Rest of the post will go through the components used in the tracker and how to acquire it. If you are in Bangalore then you can get all these items from OM Electronics in SP road or you can buy online. I bought it from OM Electronics.
![]() |
Motor
Qty: 2 Features
|
![]() |
Chasis
Qty: 1 Features Powder coated Metal chasis for robots. Easy to mount the motors on place by using normal motor mount nut. It can either be used in skid steel configuration (4 motors) or differential configuration (2 rear wheels + 1 front castor wheel). The body contains perforated holes for easy mounting of various size circuit boards and other mechanical components. Length : 190mm Width : 105mm Height : 40mm |
![]() |
Track wheels
Qty: 4 Features
|
![]() |
Qty: 60 Features High quality Plastic Track Belt and Ribbit Specification :
|
![]() |
L293D Motor Driver controller
Qty: 1 Features H Bridge to control the motors individually |
![]() |
Jumber Wires
male to male jumber wires – 10 or more pcs male to female jumber wires – 10 or more pcs female to female jumber wires – 10 or more pcs |
![]() |
12V Lithium-Ion Rechargeable Battery
12v rechargable battery + charger Url: http://www.robosoftsystems.co.in/roboshop/index.php/12v-lithium-ion-rechargeable-battery.html |
Next part I will go through the software part of the system. Stay tuned.
My home entertainment network with NAS and Raspberry Pi
This post explains how different devices at my home connected via Wi-Fi and provide an easy way of streaming media contents. In this setup I created the playlists in one location (NAS) and all other devices can consume it.
Home network Topology
Router
All my devices are connected to Netgear’s gigabit router. Earlier I had a 10/100 Netgear router and I replaced it with 10/100/1000 router. If you are planning for a streaming network at home its worth investing in a very good gigabit router.
Network Attached Storage (NAS)
The integral component of my streaming network is the 4TB WD My Cloud. It host twonky DLNA server and iTunes server, any DLNA complaint devices can connect to it and stream audio or video. Some devices at my home is not DLNA complaint, like docking station and Home theatre. That’s where Raspberry PI will be helpful.
Raspberry Pi
I knew about Raspberry Pi since it launched but never used it until recently. I decided to use Raspberry Pi to make my non DLNA devices to play media from my NAS. There are so many tutorials available in the internet to setup a Raspberry Pi, using noobs installer and using Raspbian image, I use Raspbian image to setup.
Next is setting up my Pi to play media from a DLNA server, Stephen Phillips has a very good post explaining how to do it. Follow the steps and you will have a device that can connect to any non DLNA device to play media from a server. I then connect the Pi’s audio out to my Home theatre and could play audio from the NAS. Without raspberry pi I have to switch on my TV when ever I want to play audio, with this setup my TV can be switched off when I am listening to music, also it saves energy.
I can use my Android Tab as a remote to paly media using BubbleUPnP (paid and free) or from my Lumia 920 using AV Remote (paid and trial available) or a free app called myMediaHub. You can use any similar apps you want.