ARDUINO ETHERNET

Introduction

Using Arduino with Ethernet is a great option for connecting your Arduino board to the Internet in a few minutes. It provides a fastest and reliable connectivity to your IoT devices. There are Ethernet shields that can extend Arduino features, like the Arduino Ethernet Shield for standard Arduinos, or the Arduino MKR ETH Shield for MKR devices. There are also external modules that can be plugged to any microcontroller, like the ENC28J60 module.
In this documentation we cover how to connect devices over Ethernet by using both approaches, the default Arduino Ethernet Shields, and the external ENC28J60 module.

Arduino with Ethernet Shield

Arduino Ethernet Shield
The following example will allow connecting your Arduino device with the Ethernet Shield to the cloud platform in a few lines. Just modify the arduino_secrets.h file with your own information.
ArduinoEthernet.ino
arduino_secrets.h
1
#define THINGER_SERIAL_DEBUG
2
3
#include <ThingerEthernet.h>
4
#include "arduino_secrets.h"
5
6
ThingerEthernet thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
7
8
void setup() {
9
// open serial for debugging
10
Serial.begin(115200);
11
12
pinMode(2, OUTPUT);
13
14
// pin control example (i.e. turning on/off a light, a relay, etc)
15
thing["led"] << digitalPin(2);
16
17
// resource output example (i.e. reading a sensor value, a variable, etc)
18
thing["millis"] >> outputValue(millis());
19
20
// more details at http://docs.thinger.io/arduino/
21
}
22
23
void loop() {
24
thing.handle();
25
}
Copied!
1
#define USERNAME "your_user_name"
2
#define DEVICE_ID "your_device_id"
3
#define DEVICE_CREDENTIAL "your_device_credential"
Copied!

Arduino with ENC28J60

The ENC28J60 is a very cheap Ethernet controller that can be used with our Arduinos to extend its connectivity. The main advantage of this controller is that it is inexpensive, as you can find this module for a few dollars. The bad news is that all the TCP/IP stack, DNS features, and so on, must run in the microcontroller itself, so there is no enough space in stock Arduinos for building our program. This way, for integrating the thinger.io libraries in the sketch, it would be necessary to disable the DHCP protocol (that uses UDP under the hood), and assign a manual IP address. If this is OK for your project, or you have a compatible microcontroller with more resources (like ESP8266, Teensy, STM32F, etc), then this module can be a great option.
ENC28J60 Ethernet Module
There are some libraries for managing this boards, but we will use UIPEthernet, as it provides an standard interface that is compatible with the stock Thinger libraries.
The following example will allow connecting a device using the ENC28J60 interface to the cloud platform in a few lines using the WiFi interface. Just modify the arduino_secrets.h file with your own information.
ArduinoENC28J60.ino
arduino_secrets.h
1
// Install UIPEthernet for ENC28J60
2
// https://github.com/UIPEthernet/UIPEthernet
3
#define THINGER_SERIAL_DEBUG
4
5
#include <ThingerENC28J60.h>
6
#include "arduino_secrets.h"
7
8
ThingerENC28J60 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
9
10
void setup() {
11
// open serial for debugging
12
Serial.begin(115200);
13
14
// ENC28J60 using fixed IP Address. DHCP is too big for the sketch.
15
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
16
Ethernet.begin(mac, IPAddress(192, 168, 1, 125));
17
18
pinMode(LED_BUILTIN, OUTPUT);
19
20
// pin control example (i.e. turning on/off a light, a relay, etc)
21
thing["led"] << digitalPin(LED_BUILTIN);
22
23
// resource output example (i.e. reading a sensor value)
24
thing["millis"] >> outputValue(millis());
25
26
// more details at http://docs.thinger.io/arduino/
27
}
28
29
void loop() {
30
thing.handle();
31
}
Copied!
1
#define USERNAME "your_user_name"
2
#define DEVICE_ID "your_device_id"
3
#define DEVICE_CREDENTIAL "your_device_credential"
Copied!
Last modified 2mo ago