0

Community,

A team of friends just built, with an Arduino UNO, a system where one can open a door lock from the internet. The Arduino UNO has an Ethernet Module (ENC28J60). When the command prompts, the Arduino will let a solenoid receive 5v, which will open it and, therefore, open the door.

The novel thing is, this door-opening-arduino connects via the Internet to a central server, a webpage that 'centralizes' the door-opening process. So, in a certain URL, we'll get a screen which goes:

example of the webpage which centralizes door-opening actions

All these doors are in remote locations, connected to very different routers. Some of these routers will go 192.168.1.1, whereas others will go with the 0.1 ending.

So far, my friend has hard-coded the internal IP address 192.168.1.143 for identifying the Arduino, so, he's given up on DHCP. Afterwards, he's chosen port 6560 to be the open port and connect the door to the Internet. (VPN is not a option).

So the public IP address and port 6560 are our way in into the door. This info is shared with his webpage, where commands can be given: 'open it'.

But with these hard-coding of the 1.143 IP and the router's different default addresses, we've had trouble. You know what I mean: sometimes, the whole code must be changed for our Arduino to connect to the router. Not even close to 'plug-n-play'.

My questions seems natural: how can we ensure the Arduino connects each time to the router, without any problems? Most devices just connect: an IP camera in these routers will connect, and will never care which IP address the router has given it. If the router resets, things will be fine, and connection will restart.

So, why not DHCP? As far as my understanding goes, a problem my friend mentioned that would happen if he chose to use DHCP would be the communication between his Arduino and his online server: 'how can I know which IP I should speak with in order to open the door? By choosing the .143, I know which would should be'. Another problem comes in with port forwarding. Apparently, by choosing the .143 and then using port 6560, we ensure that we can access via the Internet, because there's a standarized procedure: 'please open port 6560 for this IP'.

This is a bit cumbersome. Most devices just don't work this way. We want to be able to just plug it in, let our Arduino and the router get any IP, and by just opening port 6560, we're in. Pretty simple.

How can we ensure that:

  • Our Arduino just connects to the router, and
  • that communication between our Arduino and the centralized server just flows?

Another friend suggested configuring each router so that a device with a certain MAC address (our Arduino) gets this IP* and this port forwarding. But what if the default address is 192.168.0.1 (instead of 1.1), for example? wouldn't we be facing a problem?

We're seeking a kind of plug-n-play thing, like most access control systems work. Hardware's ok, it's connection the thing that bugs us.

Any suggestions, advice, or plain responses are really welcome. I think we're just a step away from our commit, which is creating a reliable connection for these doors. It may be just a line of code, or a config trick. Thank you for your time.

(you'll soon notice we're not that savvy on routing and switching, so If we got our premises completely wrong, don't hesitate to correct us)

1
  • 2
    I'd forgo the port forwarding and ip-fixing, and just have the Arduino poll the central server, to see if there's any action that needs to take place. Alternatively use something like pagekite.
    – Gerben
    Feb 4, 2017 at 20:30

2 Answers 2

1

Your problem is two-fold, and neither are easy for an Arduino to overcome.

First you have the problem that the router's internet-facing IP address is (unless you have a special arrangement with your ISP) liable to change at any time. You need to keep your sever up to date at all times with what that IP address is, and the only real way of doing that is for the router, or something behind the router, to connect with your server so it can update its IP address.

Secondly you have the port forwarding. The router has to know where to forward the port to. That means that either the IP address of the Arduino has to be static, or the Arduino has to somehow tell the router to do the port forwarding. The simplest method is just to allocate a static IP address as you have done. The alternative is to use UPnP to instruct the router "I need you to forward this port through to me" - assuming the router understands UPnP. The hardest part of that though is getting the Arduino to speak UPnP. I am not aware of any UPnP libraries for the Arduino.

So keep the static IP address for the Arduino, it is simplest. One thing to note with manually allocating an IP address though is that it is vitally important you allocate it outside the IP address range of the DHCP server. Otherwise it is possible that the DHCP server could then allocate the IP address to someone else, and you'd then get a conflict. Most routers allow you to choose the range of IP addresses so you may want to go in and check that the DHCP server isn't able to clobber the IP address you have chosen.

To help solve the first problem some routers provide a "DDNS" (Dynamic DNS) facility, whereby they will periodically connect to a DDNS service to update a DNS record with the public IP address of the router. Not all provide it and those that do usually only provide access to a couple of DDNS services, so check your router to see if it does support it and if there is a suitable one for your needs there. If there is you can configure that and then, instead of connecting to the IP address of your router for issuing commands you instead connect to the DDNS domain name allocated to your router.

However, there is one much simpler option that solves all the problems in one go: reverse the communications.

Instead of the server connecting to the router to forward the port through to the Arduino to issue a command, it is far simpler (though I will admit not as responsive) for the Arduino, from whatever IP address it happens to be on, to just connect to the server itself and ask it for any pending commands.

If you have the ability to run your own software on the server you could even create your own small TCP server which the Arduino connects permanently to. The server can then send commands down that TCP connection just as if you were working with an Arduino directly connected by serial.

In both those situations it doesn't matter what IP address your router is on. You don't need to do any port forwarding. The Arduino can be on any IP address on your network.

And further more you have more security - no one could connect to your Arduino through the internet and cause havoc.

1
  • thank you! just shared this with my programmer friend and he thinks it's a great idea. He´s suggested using Heroku as a server, although we should pay a monthly fee for it, as Heroku "falls asleep" if it isn't used for 30 minutes, and we can't risk that. Do you have any further advice? such as a platform to use (i.e, it could be Heroku), or a free alternative, or just any other suggestion in programming and deployment? We'll be expectant.
    – peque
    Feb 5, 2017 at 4:05
0

I would use socket.io for this. Here is a goos presentation on this:

http://www.robojay.us/wp-content/uploads/2016/08/NRB-socket-io.pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.