Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 10 months ago.
Send data from remote server to mbed on home network
I feel like I'm missing something obvious so hopefully someone can help me out.
To stop an internet connected mbed (lets say it's an internet connected mbed lightbulb) from having to poll a remote server all the time to see if the user has changed its settings, how would a server send data to that mbed when needed? I understand how to initiate a connection from the mbed to the server, just not the other way round.
It's safe to assume that the mbed would have already registered it's details with the server in some kind of set up process initiated by the mbed when first installed.
Would the mbed need to be running full webserver and have it's own external ip address?
1 Answer
10 years, 10 months ago.
Having your device poll the server or initiate the connection to the server is the simplest method. Keeping a connection active so that the server can immediately notify the client (mbed) is another, websockets are an example of this.
The issue with the Mbed device being the "server" and listening for connections is if those connections are from outside your LAN they most likely go through NAT, NAT lets you have many devices use a single IP address assigned to your router.
When an incoming packet arrives from the internet to your IP address the router looks at it's NAT table to know where to send the data. Or a port forwarding list.
This table gets populated by the router tracking outgoing connections, if you set up an mbed to listen to a UDP/TCP socket the router won't know where to send the data when it arrives from the internet.
A static port forward solves this but will need configuring for each network you put the device on. You could set your router's DMZ address to match the mbed, all unsolicited traffic then gets sent to the mbed. Or if the router supports it your device can use upnp to tell the router it is listening on a certain port.
Remember though once you open the door to the internet anyone can come in! Which is another benefit of making the mbed device the mbed as you then centralise most of the security concerns with the server.
Thanks, that makes sense now.
So, a router forwards incoming data to the correct device dependant on what port it comes in on and matches it to the port the outbound request was sent through - while I think/know that http is stateless and so an permanent "open" connection wouldn't be possible, is it possible to do some kind of long polling? For example, the mbed checks in with the server and the server doesn't respond until there is some change to respond with? I guess the NAT table keeping track of which port relates to which private ip address is only valid for so long so the server could send a "no change" response before the timeout, while not truly an open connection it at least means that the mbed isn't hitting the server ever 20 seconds just to receive a "no change" response 999 times out of 1,000.
From a security point of view I can see what a port forwarding is bad but I assume for normal connections the router checks that the incoming data is coming from the url/ip address that the initial request went to? Otherwise anyone could hijack on inbound traffic...?
posted by 30 Jan 2014I'm unsure about the HTTP questions. The Mbed can keep a TCP connection open to a server there are many protocols you can use HTTP might not be the best if your needing a fast response time. MQTT, Websockets are a few to consider.
As far as I know yes for incoming data to be forward to a LAN host via the NAT table it has to have been the address the client sent data too.
posted by 30 Jan 2014