In this tutorial we go over how we can connect your Internet Of Thing or device to the Internet. How we connect depends ultimately on what we actually want to do with the device. Shown below is a typical home network. This would normally consist of a router / firewell that connects to your ISP (Internet Service Provider). The connection to your ISP could by via ADSL or Ethernet using a fibre service for example.
When the home router connects to the ISP it will be assigned an IP address which is what is used to communicate with servers or other service on the Internet. This is a public IP address and is addressable by the internet. In the diagram above the public IP address is 188.8.131.52.
This is your public ip address you used to connect to the cactus.io server to view this page.
(If there is more than one ip then you might be connecting using a proxy server or VPN service)
Be aware that this IP address will change everytime that your home router is rebooted or reconnects to the ISP. Some ISP's allow you to pay extra for a fixed IP address. However most will not for home internet services.
Usually your home router functions as a DHCP server which is used to automatically assign ip addresses to any device that plugs into your home network. The home router would normally assign an IP address that is from a private range of IP addresses. In the case of the diagram above it is using the private adddress range of 192.168.1.X. The internet standards define a range of private IP addresses. Routers will not route private IP addresses.
Normally a PC, iPad or laptop when connected to your network would use DHCP to automatically assign it a dynamic address. The PC shown above is assigned 192.168.1.10 but this could change the next time it connects or plugs into your home network. DHCP would be assigned a range of IP addresses to dynamically assign. For example we could use the range of 192.168.1.5 to 192.168.1.40 for dynamic assigment to devices.
For our IoT device we can either use DHCP to assign an IP or we can use a fixed IP address. If our device is just sending data to the cloud then we could use DHCP to assign the address. However if your are running a web server on your device it is recommended that you use a static IP address. Here we used 192.168.1.145. If we used DHCP then this address could change and we would not know what ip address to enter into the url.
Generally a router will automatically assign itself to the first IP in the range. In this case it is 192.168.1.1. This would also be the Gateway IP.
When we enter a URL into our browser it will use a DNS server to convert the name to an IP address. The DNS server would normally be provided by your ISP. In the example above we are using cactus.io for the URL. The DNS server would then return the IP address of 184.108.40.206. The browser will then try to connect to the server via the router. The router will connect to the remote server using the public IP address assigned to your router. The server hosting cactus.io will then send data back to your router using your public ip address (220.127.116.11). The router will then transmit this data back to your PC using the private IP address of 192.168.1.10.
We can connect to a web server running on our IoT device by entering the IP address of http://192.168.1.45 into the browser url. The connection will be made in this case via the switch built into the home router. The switch knows what port the IoT device is connected to and transmits and receives data via this port. The router has no role in this network request as it's a private IP address and the PC and IoT device are on the same network.
We now go through each option in more detail
In this scenario the IoT device is configure as a server. The easist way is to assign a static IP address. This address is coded into the sketch
When a client connects to the IoT device it passes through the switch and out to the device it is connected to. No routing is required here.
For this scenario the IoT device iniates a connection to a local or remote server. This could be using a http REST POST or confiqured as MQTT client. It could also be a custom connection method as long as it initiates the connection to the remote service.
Because the device is creating the connection it allows us to use DHCP and a dynamically assigned IP address.
In the diagram above two of the devices send a HTTP REST Post to a server in the cloud. If the remote server accepts the connection and data it will generally respond with a 200 OK. The client will then close the connection until the next post is sent.
The world is running out of IPV4 IP addresses. This means ISP's are pretty strict on giving out static IP addresses. It's much easier for them to use a dynamic pool of IP addresses to assign to their clients.
Generally home internet services are not available with a static IP address. However you might be able to pay extra for this functionality. Some business accounts are available with a static IP address. The most common reason for wanting a static ip is for hosting a email server.
To connect from the home / office router to your IoT device we need to perform Network Address Translation or Port Forwarding on this router. Through the router administration console we need to tell the router to forward any data packets it receives on it's WAN interface (Internet) using a specific port number is forwarded onto the internal IP address we have assigned to the IoT device.
So when a browser sends a request using the public IP address 18.104.22.168 it hits the router. The router will check its NAT / Port forwarding table and find a match for port 80 or alternatively port 49155. It will then forward the data packet to the internal IP address 192.168.1.45 that was setup in the routers configuration.
In this diagram above we could use http://22.214.171.124 or http://126.96.36.199:49155 to connect to the IoT server. In the first url we do not specify a port number so the browser will use the default port of 80. In the second url we use the port 49155. By using a random private port number we improve the security for our IoT server. If someone does a port scan they will always check port 80 as it's a default. They might not bother scanning private port numbers. For more details on port numbering check out Wikipedia List of TCP and UDP Port Numbers
If you have multiple IoT devices you can setup port forwarding on the router to recognise different port numbers.
One of the most common ways of dealing with a dynamic public (WAN) ip addresses is to use a Dynamic DNS Service provider. Some of these services are free but most require a monthly payment.
Most routers these days support third party Dynamic DNS service providers. You configure the router with the provider, username and password. WHen the router detects a WAN interface IP address change it updates the DDNS service provider with the new IP details.
When you signup with one of these providers you provide a domain name you would like to use. In the example below the name we used is cactusio. This is a subdomain for dydns.com. When we enter http://cactusio.dydns.com into the browser it will do a dnslookup on cactusio.dydns.com. If its a valid domain it will respond with the IP address which just happens to be the public (WAN) IP address of the home / office router.
The office router will detect the data packet matches a record in the port forwarding table and pass the data onto the IoT device using the static ip of 192.168.1.45. The IoT device will respond with the appropriate html to view in the internet client browser.
We have covered some of the basic ways we can connect a device to the Internet. In the corporate world with more extensive networking equipment we have some other options to solve the issue of using static or dynamic ip addresses. If you have a lot of devices it's a lot easier to use DHCP assigned IP addresses instead of hard coding a static address.