4- Connect Arduino Ethernet over HTTPS

The Arduino Ethernet Shield connects your Arduino to the internet in mere minutes. Just plug this module onto your Arduino board, connect it to your network with an RJ45 cable, and you are almost done to start controlling your world through the internet.

Connect Arduino Ethernet To AskSensors Over HTTPS

a) Arduino Ethernet Shield :

– Features:

  • Requires an Arduino board.
  • Operating voltage 5V, supplied from the Arduino Board.
  • Ethernet Controller: Wiznet Ethernet controller W5100 with internal 16K buffer.
  • The Wiznet W5100 provides a network (IP) stack capable of both TCP and UDP.
  • Connection speed: up to 10/100Mb.
  • Connection with Arduino on SPI port: It uses the ICSP header pins and pin 10 as chip select for the SPI connection to the Ethernet controller chip.
  • The latest revision of the Ethernet Shield includes a micro-SD card slot on board, which can be used to store files for serving over the network.
  • The Ethernet Module has a standard RJ45 connection, with an integrated line transformer.
  • The connection to a network is made with a RJ45 Ethernet cable.

– Software Library:

  • The Ethernet shield relies on the Arduino Ethernet library.
  • The library comes bundled with the Arduino IDE.
  • We will need to change some network settings in the program to correspond to our network.

– Informational LEDs:

  • PWR: indicates that the board and shield are powered.
  • LINK: indicates the presence of a network link and flashes when the shield transmits or receives data.
  • FULLD: indicates that the network connection is full duplex.
  • 100M: indicates the presence of a 100 Mb/s network connection (as opposed to 10 Mb/s).
  • RX: flashes when the shield receives data.
  • TX: flashes when the shield sends data.
  • COLL: flashes when network collisions are detected.

b) Prerequisites :

c) Hardware requirements :

To follow along with this tutorial you will simply need:

  • Computer running Arduino software.
  • An Arduino board such as the Arduino Uno.
  • An Arduino Ethernet Shield.
  • USB micro cable to connect ESP32 development board to the computer.
  • An Ethernet cable, for connecting to your network router.

d) Software :

There are hundreds of tutorials about connecting the Arduino to the web through the Ethernet Shields, so I’m not going to explain this part.
Download this demo from the AskSensors Github page.
The code uses DHCP and DNS for the server and is supposed to work right away with few changes:

  • If you are using more than one Ethernet shield on a network, make sure that each Ethernet shield on the network must have a unique mac address.
  • Change the IP address in the sketch to match the IP address range of your network.
  • Set the Api Key In of your sensor.

// MAC
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(..., ..., ..., ...);
// ASKSENSORS config.
const char* apiKeyIn = "......................"; // Change it with your API KEY IN

 

e) Run the code :

  • Plug the Ethernet shield into the Arduino Uno board.
  • Connect the Ethernet shield to your router/network through the Ethernet cable.
  • Connect the Arduino to the computer through the USB cable. Power will be supplied to the two boards via the USB cable.
  • Open your code in Arduino IDE, Select the correct Arduino board and COM port. Then, upload the code to your Arduino board. make sure that the code was uploaded successfully.
  • You can use the reset button on the shield to reset both the the Ethernet Controller and the Arduino board.
  • After reset/power up, open a serial terminal, you should see the Arduino printing the program status: the arduino connects to the network (takes few seconds), then sends the dummy data to the AskSensors over HTTP get requests.
  • After receiving the request of wrting data to the specific Sensor from the client, the server first sends a HTTP response telling the number of modules updated successfully (‘1’ in our case).

f) Source Code :

A basic source code is shown below. Please refer to the AskSensors Github page to get the latest version and updates.


/*
Connect Arduino Ethernet to AskSensors
* Description: This sketch connects to a website (https://asksensors.com) using an Arduino Wiznet Ethernet shield.
* Author: https://asksensors.com, 2018
* github: https://github.com/asksensors
*/
#include <SPI.h>
#include <Ethernet.h>
// ETHERNET config.
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);
// ASKSENSORS config.
char server[] = "api.asksensors.com"; // ASKSENSORS host name
const int port = 80; // Port: HTTP=80
const char* apiKeyIn = "5V8APAV9Q0OWEBDOSCYRSKGGEIWC3M8Q"; // API KEY IN (change it with your API KEY IN)
// dummy data
int dumData = 100; // set your data
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, port)) {
Serial.print("connected asksensors.com");
//Create a URL for the request
String url = "http://api.asksensors.com/write/";
url += apiKeyIn;
url += "?module1=";
url += dumData;
Serial.print("********** requesting URL: ");
Serial.println(url);
//Make a HTTP request:
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
"Connection: close\r\n\r\n");
client.println();
Serial.println("> Request sent to ASKSENSORS");
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}
Was this article helpful to you? Yes 2 No 4

How can we help?