Ethernet shield acts as wired Gateway between your Arduino & Internet.

This post demonstrates how to upload  temperature data from LM35 sensor to the server on THINGSPEAK.Before starting you should know how to connect the shield to Network.

There are 2 options by which your shield is connected to the Network.

First is the direct connection to your Router .You’ve an available RJ-45 port on your Router & you directly use a straight RJ45 cable to plug Ethernet shield to Router .Your PC/Laptop is also connected to the same Router either by wire or through WIfi (if your Router is WIFI capable).

Second is the indirect connection.Your shield is connected to your Laptop’s RJ45 with wire.Your Laptop is connected to Router through WIFI.In this case you need to bridge the connections or use the shield as unidentified second network with a different IP.

In this post we use the first Direct connection to Router method.

Check this connection image where my Ethernet shield is connected directly to  Router.My PC is also connected to the same Router by WIFI . Router is provided Internet access by the Internet Service Provider ACT.






To start with let us fix the IP of Ethernet shield STATIC , so that it is always the same on every boot up of Router.

From your PC  RUN –> CMD

Type in  ipconfig /all

The Default Gateway here is which is the IP of the Router.

All clients connected to this Router will be assigned IP in this range  where the last byte is from 2 to 255 (allotted on first come basis)

Image 1

In the above screenshot you can see (ipV4 address), my PC is assigned IP

IP is assigned automatically by Router , only when the DHCP is enabled in Router setting.

Type in your browser , the gateway IP & feed in your Router’s username & password  (generally both are “admin”)

Under DHCP settings ensure that DHCP is Enabled.

Image 2


Now click on the DHCP CLIENT LIST to know the devices connected to the Router.

In my case 2 devices connected.One is my PC which is allotted IP

& the second device is Ethernet shield which is allotted

But in our code we use IP .

Let us change the IP to & make it STATIC.



& then click ADD NEW



Feed in the MAC address of the Ethernet shield & the reserved IP as

The MAC address is the one you used in the code.This may be random unless you use more than one shield in the same network.


Click on SAVE & then REBOOT your Router to enable the changes made.


Now under DHCP Client list you can see your shield allotted & permanent (means STATIC IP)




Connect the 1st pin of LM35 to 5v of Ethernet shield , 3rd pin to GND. 2nd pin is connected to A0

Analog pin.

Connect the USB cable from ARDUINO to PC & note the PORT allotted.The RJ45 STRAIGHT CABLE is connected from Ethernet shield to Router directly.

Upload the following code on to Arduino.You need not change the MAC address.

You need to change the Gateway IP as per your Router & also change the IP of shield in this range.

Also you need to change the THINGSPEAK API according to your CHANNEL setting

Download Arduino CODE Here


#include <SPI.h>
#include <Ethernet.h>

int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;

// Local Network Settings
byte mac[]     = { 0xD4, 0xA8, 0xE2, 0xFE, 0xA0, 0xA1 }; // Must be unique on local network
byte ip[]      = { 192,168,0,150 };                // Must be unique on local network
byte gateway[] = { 192,168,0,1};
byte subnet[]  = { 255, 255, 255, 0 };

// ThingSpeak Settings
char thingSpeakAddress[] = “”;
String writeAPIKey = “QGBA1G3872XSHBAC”;    // Write API Key for a ThingSpeak Channel
const int updateInterval = 10000;        // Time interval in milliseconds to update ThingSpeak

// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;

// Initialize Arduino Ethernet Client
EthernetClient client;

void setup()

Ethernet.begin(mac, ip, gateway, subnet);
Serial.print(“ETHERNET SHIELD ip  is     : “);
// Start Ethernet on Arduino

void loop()

tempc = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;

String analogPin0 = String(tempc);

// Print Update Response to Serial Monitor
if (client.available())
char c =;

// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)


// Update ThingSpeak
if(!client.connected() && (millis() – lastConnectionTime > updateInterval))

lastConnected = client.connected();

void updateThingSpeak(String tsData)
if (client.connect(thingSpeakAddress, 80))
client.print(“POST /update HTTP/1.1\n”);
client.print(“Connection: close\n”);
client.print(“X-THINGSPEAKAPIKEY: “+writeAPIKey+”\n”);
client.print(“Content-Type: application/x-www-form-urlencoded\n”);
client.print(“Content-Length: “);


lastConnectionTime = millis();

if (client.connected())
Serial.println(“Connecting to ThingSpeak…”);

failedCounter = 0;

Serial.println(“Connection to ThingSpeak failed (“+String(failedCounter, DEC)+”)”);


Serial.println(“Connection to ThingSpeak Failed (“+String(failedCounter, DEC)+”)”);

lastConnectionTime = millis();

void startEthernet()


Serial.println(“Connecting Arduino to network…”);


// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
Serial.println(“DHCP Failed, reset Arduino to try again”);
else {
Serial.println(“Arduino connected to network using DHCP”);
Serial.println(“Data being uploaded to THINGSPEAK Server…….”);





You need to sign up with  to upload your data to the server.

Open your account on THINGSPEAK & select MYCHANNELS under CHANNELS




Feed in Name & Description of your channel & under FIELD1 enter name of field like TEMPERATURE

Scroll down & Enable PUBLIC VIEW.


Save the CHANNEL & then click on API KEY tag


Note down the WRITE API KEY


Replace this code with yours inside the Arduino code.


Upload the code.

Please ensure that you’ve changed following settings in your code before uploading:

1. Gate way IP of your Router.

2. IP address of your Shield in range with gateway IP.

3. Thingspeak  API WRITE KEY from your Channel


Once uploaded you can open the Serial Monitor of Arduino , to see data being uploaded to the Server


On your THINGSPEAK Channel click on Public View to see the Graphical representation of the LM35 data being uploaded.


If you get Arduino Failed to connect to Network,  then you need to check the IP address credentials again.


HTTP POST Method to write data on to server


In the Arduino code data is converted to STRING type & uploaded to Thingspeak. Thingspeak server accepts only String type data.,

The HTTPS method used in code is POST method .Other HTTPS methods are  GET ( to read data), PUT ( to update data), DELETE ( to delete data).

Understand the basics of this method which we will be using in upcoming posts of ESP8266 & other IOT related projects.

We use POST to submit data to THINGSPEAK Server.

POST request is a bunch of STRINGs.

Each request line is separated & ends with a newline.


client.print(“POST /update HTTP/1.1\n”);
client.print(“Connection: close\n”);
client.print(“X-THINGSPEAKAPIKEY: “+writeAPIKey+”\n”);
client.print(“Content-Type: application/x-www-form-urlencoded\n”);
client.print(“Content-Length: “);


The first string is the Message STARTLINE which has 3 elements separated by SPACE

POST /update HTTP/1.1\n

First element is the URI –Uniform Resource Identifier ( not URL) which is nothing but the method POST all in capital letters. Next element is the protocol /update & the 3rd element is the version  HTTP/1.1 . At the end  \n is used for new line , or you can use println instead of print.

Once we send the message start line string, then we need to  send different header fields. The HEADSER FIELDS are  predefined fields that give the Thingspeak server , information about our POST request. Apart from standard fields , there’s some non-standard fields.

If it’s a non-standard field, the header name is usually preceded by an x. Each header field that we send is going to be its own string, separated by a newline.

We’re going to send following different Headers :

1.Host header field,

2.Connection header field,

3.The x ThingSpeak API key header field,

4.Content type header field,

5.  Content length header field.

1} For ThingSpeak, the host is going to be —–>

2) For the connection header, we specify close, and that’s because after we make our request and after we get our response, we want to close the connection with the server. There’s no need to keep that connection open between sending information, so we close it.

3) The X-ThingSpeak API key header is a non-standard header, and that is going to be set to our Write API key that we can find out  from our ThingSpeak channel.

4) The content type header is going to indicate the kind of media that we planned on sending in the header body, In HTTP there are two ways to POST data:




We use the first one as we update string data.

For application/x-www-form-urlencoded, the body of the HTTP message sent to the server is essentially one  query string — name/value pairs are separated by the ampersand (&), and names are separated from values by the equals symbol (=).

5) The content length header specifies the size of the media that we’re going to be sending in bytes.


Once we’ve set the header fields, the last thing we do is send the message body.

The message body is the actual data that we want to update & post to the server. Just like the message start line, and just like the header fields, the message body is going to be a string of text. The message body is  separated from the header fields by two newlines.

client.print(“\n\n”);     // 2 new lines


Those two newlines are what let the server know that  we’re done with the header fields & we’re moving on to the message body.

Our message body is simply listing the field that we want to update with an equals sign followed by the value that we want to update it as.


If we want to do multiple fields, we separate them with the “and” sign. There’s no spaces in this message body

We’ve got the message start line. We’re identifying the method, the URI, the resource we want to use, and the protocol type. Then we specify the header fields. This is giving information to the server about the request that we’re making.

Finally, we have the actual message that we’re sending.

That makes up a basic HTTP POST  request



cooltext753793315   cooltext753790696