With this article you will learn how to create a firmware and an app to securely Internet remote control (from inside AND outside your home network) an Arduino MEGA board with a Ethernet Shield. The IoT communication infrastructure used for this is the Nabto platform which you can try for free and easily create remote control IoT systems for all types of microcontrollers and remote control clients (apps, tablets, pc’s etc.). The app and firmware is a simple Heating control demonstration which easily can be altered to your own need.
What you need:
- Arduino MEGA2560 board
- Wiznet W5100 Ethernet Shield version 1
(If you know all about Arduino, please scroll down to “About uNabto” section)
The Arduino Mega 2560 is a microcontroller board based on the ATmega2560. It has 54 digital input/output pins (of which 14 can be used as PWM outputs), 16 analog inputs, 4 UARTs (hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with an AC-to-DC adapter or battery to get started.
The Arduino Mega is the addition to the Arduino family. This board is physically larger than all the other Arduino boards and offers significantly more digital and analog pins. The MEGA uses a different processor allowing greater program size and more. The Mega2560 differs from all preceding boards in that it does not use the FTDI USB-to-serial driver chip. Instead, it features the ATmega16U2 programmed as a USB-to-serial converter. The Mega has four hardware serial ports, which means maximum speed if you need a second or third (or fourth) port.
1. Microcontroller: ATmega1280 or 2560
2. Operating Voltage: 5V
3. Input Voltage(recommended): 7-12V
4. Input Voltage(limits): 6-20V
5. Digital I/O Pins: 54 (of which 14 provide PWM output)
6. Analog Input Pins: 16
7. DC Current per I/O Pin: 40 mA
8. DC Current for 3.3V Pin: 50 mA
9. Flash Memory: 128KB or 256KB
10. SRAM: 8 KB
11. EEPROM: 4 KB
12. Clock Speed: 16 MHz
Wiznet W5100 Ethernet Shield:
The Arduino Ethernet Shield connects your Arduino device to the internet. Just plug this module onto your Arduino board, connect it to your network with an RJ45 cable. As always with Arduino, every element of the platform – hardware, software and documentation – is freely available and open-source.
- Operating voltage 5V (supplied from the Arduino Board)
- Ethernet Controller: W5100 with internal 16K buffer
- Connection speed: 10/100Mb
- Connection with Arduino on SPI port
The Arduino Ethernet Shield allows an Arduino board to connect to the internet. It is based on the Wiznet W5100 Ethernet chip (datasheet). The Wiznet W5100 provides a network (IP) stack capable of both TCP and UDP.
The Ethernet Shield has a standard RJ-45 connection, with an integrated line transformer and Power over Ethernet enabled.
The shield also includes a reset controller, to ensure that the W5100 Ethernet module is properly reset on power-up. Previous revisions of the shield were not compatible with the Mega and need to be manually reset after power-up.
The current shield has a Power over Ethernet (PoE) module designed to extract power from a conventional twisted pair Category 5 Ethernet cable.
- IEEE802.3af compliant
- Low output ripple and noise (100mVpp)
- Input voltage range 36V to 57V
- Overload and short-circuit protection
- 9V Output
- High efficiency DC/DC converter: type 75% @ 50% load
- 1500V isolation (input to output)
Interfacing Arduino MEGA with Ethernet Shield:
Ethernet Shield Arduino board connects to a LAN or the Internet. Installation is very simple. Plug the Ethernet shield connectors in the expansion card connectors of Arduino and then connect the Ethernet cable to the RJ45 connector slot. In the image below you can see the Arduino Mega with an installed expansion board Ethernet Shield.
Nabto is an IoT application platform that helps you to
1. Quickly develop high-quality client side apps using SDKs and/or template apps
2. Customize the device side application using the provided demo application.
3. The uNabto framework and the client APIs can be downloaded using the below link.
The drawing below gives a brief overview of how the Nabto platform actually work. The Device represents the Arduino Mega and the uNabto server (uNabto SDK and Device specific Platform adapters) is running on the device. As soon as device connects to the internet it identifies itself at the Nabto Basestation, using its unique ID which is already registered in Nabto Cloud Console. If a Client wants to connect to the device, a connect request with the device ID is sent to the Basestation, and a direct connection to the device is established after verifying the identity of client. A client can be a native mobile app or an abstraction framework like our Heat Control Ionic starter app used in this demo.
Get more information on the Nabto Cloud Console and the Client/Device SDKs at www.console.cloud.nabto.com.
The uNabto Platform Adapter:
The uNabto Platform Adapter is a small component that abstracts the Native Platforms network and time functionality. The Platform Adapter is part of the uNabto server.
The uNabto server is divided into two layers:
- The uNabto framework (uNabto SDK)
- The uNabto Platform Adapter abstraction layer between the framework and the Native Platform
The device specific uNabto server is readily available in the Nabto Cloud Console to port it to the Arduino module.
Implementing the Platform Adapter:
The Platform Adapter acts as link between the generic uNabto framework and the Arduino platform. The adapter is divided into single files, as suggested in the Nabto documentation (TEN023 Nabto device SDK guide, section 12):
- unabto_config.h: Basic uNabto configuration
- unabto_platform_types.h: Define all necessary uNabto types
- unabto_platform.h: Platform specific ad-hoc functions
- time_adapter.cpp: Time functions
- random_adapter.cpp: Random generator functions
- dns_adapter.cpp: DNS resolver functions
- network_adapter.cpp: Communication functions
- log.cpp: Logging
Heat Pump Demo:
The Heat Pump demo showcases how to use the Nabto request response model on the Atmel AVR platform. This demo uses the Arduino MEGA board and Wiznet W5100 Ethernet shield (Ethernet module version 2) to perform the actions.
Heat Pump Library – Arduino Mega:
Get the Nabto Arduino Mega2560 library from [https://github.com/nabtodaemon/heatcontrol-arduinomega#heatcontrol-arduinomega] and follow the below installation instructions.
- Add the library to the Arduino IDE via
Sketch -> Include Library -> Add .ZIP Library
Browse to the folder containing the downloaded library file and add the unabto-arduinomega-sdk-2.1.1.zip (downloaded zip file)
- Open the HeatPump.ino example via
File -> Examples -> Nabto-Mega2560 -> HeatPump
The sample sketch includes the Nabto class, which encapsulates the Nabto setup. For the sketch to work, the below settings are to be changed. The setting should specify the board’s MAC address (found on the Ethernet board) followed by the unique Device ID and pre-shared key of the device obtained from console.cloud.nabto.com
// Enter device id and pre-shared key from console.cloud.nabto.com const char* DEVICE_ID = "abc.xyz.trial.nabto.net/"; const char* PRE_SHARED_KEY = "4f2a03f29f509035c03bc229ae870849";
(i.e. Sign-up for a Nabto Cloud Console account, Create a product, Create licenses, copy a license Id and License Key into the DEVICE_ID and PRE_SHARED_KEY in the code section mentioned above)
The loop function inside the sketch is used to call the tick() method of the Nabto class that triggers the framework to check for new UDP packets and send responses. The time between ticks should be around 10 milliseconds. This is achieved by a hard delay, but you can also use the time to do application related tasks. For example, the demo uses it to update the brightness of the LED and to simulate the room temperature in the demo application tick function.
Test your device:
After compiling and uploading your HeatPump sketch to the Arduino Mega, it establishes a connection to your Ethernet network and starts the uNabto server. In your serial monitor you should see the following printout:
Now, using the Heat Control Ionic starter app (also available on google play store) connect your device and verify that the inbuilt LED changes its brightness according to the target heat.
Read more about obtaining the the Heat Control app or how to compile, possible adjust, your own version here https://www.nabto.com/pc-tutorial
When the Heat Control Ionic app is started for the first time, you need to pair your client app with the device. Then on, the paired device will be automatically discovered if available online.
1. Why is the device not able to communicate with Basestation?
Check the firewall settings on your network.
Ensure your data packets are been transmitted to Basestation and not blocked by your firewall, it must allow outgoing UDP traffic.
2. Device is online, why it is not discovered by my client app?
Ensure the device and the client are connected on the same network.
Search for your device, once discovered pair your client app with the device for the first time use.
Later, the paired device will be listed when the app is started.
3. Device can be accessed locally (on same network), why it cannot be accessed from outside network?
Your device might not be attached to the Basestation. Check the device application logs to locate the below message:
“State change from WAIT_GSP to ATTACHED”
If found, then should be able to connect from outside network. Note, your client must be paired with the device before accessing it.