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 192.168.0.1 which is the IP of the Router.

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

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

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

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 —–> api.thingspeak.com.

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:

application/x-www-form-urlencoded

and

multipart/form-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(tsData.length());
client.print(“\n\n”); // 2 new lines

client.print(tsData);

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.

updateThingSpeak(“field1=”+analogPin0);

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.

WIZNET W5500 is used by Arduino.ORG & is referred as Ethernet shield 2.

We shall use the classic Ethernet shield W5100 in this post.

The shield has long pin headers to plug on the Arduino (UNO or MEGA). Other shields can be stacked on top of this board.There is an onboard micro-SD card slot, which can be used to store larger files.

The Ethernet Library is used to write sketches which connect to the internet using the shield. You need not download the library , as it is inbuilt in Arduino IDE.

The on-board micro SD card reader is accessible through the SD Library. Both the SD card reader & Ethernet board work on SPI bus .

The shield also includes a reset controller, to ensure that the W5100 Ethernet module is properly reset on power-up.

This board is different from the economy version using ENC28J60 device.The library for this ENC is different , ETHERSHIELD library hosted by NANODE project.Libraries are different for WIZNET board & ENC board.

Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header). This is on digital pins 10, 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega.

On both boards, pin 10 is used to select the W5100 and pin 4 for the SD card. These pins cannot be used for general I/O. On the Mega, the hardware SS pin, 53, is not used to select either the W5100 or the SD card, but it must be kept as an output or the SPI interface won’t work.

As the W5100 and SD card share the SPI bus, only one can be active at a time. If you’re not using one of the peripherals in your program, however, you’ll need to explicitly deselect it. To do this with the SD card, set pin 4 as an output and write a high to it. For the W5100, set digital pin 10 as a high output.

The shield provides a standard RJ45 Ethernet jack.A straight RJ45 cable is used to connect the shield to the Router.

A RESET button is on the shield which resets both the W5100 and the Arduino board.

The shield contains a number of 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

Apart from these LED s , there are 2 more LEDs seen below the RJ45 jack.When Ethernet cable is inserted from a working Router , you can see these LEDs glowing below the RJ45 jack, one glows steadily & the other blinks as per data flow.

The examples in this section will all make use of the Arduino Ethernet Library.This library comes with Arduino, so no downloading or installing required

You can see the examples under File –> Examples –> ETHERNET

The Ethernet library allows the Arduino to turn into one of two Ethernet devices: a client or a server. Turning your Arduino into client allows you to connect to and request data from servers.

Another example included with Arduino’s Ethernet library is WebServer. This sketch sets up the Arduino as a server device, and hosts up a webpage whenever a client connects to it. The webpage is simple – it shows the status of the analog pins.

Open the WEBSERVER sketch

Webserver is a simple sketch which will display on a web browser, the data gathered from the analogue inputs. However don’t upload it yet, it needs a slight modification.

You need to specify the IP Address of the Ethernet shield – which is done inside the sketch. This is simple, go to the line:

IPAddress ip(192,168,1,177);

You can alter it to match your own setup. For example, in my home the router’s IP address is 192.168.1.1, the pc is assigned 192.168.1.6 , the tablet is assigned 192.168.1.3 , .. As the IP address is in the range 192.168.1.xxx , I just leave this to 192.168.1.177

You also have the opportunity to change your MAC address.As we use only one shield , we can leave this as it is.

Open your web bowser & type in the IP address 192.168.1.177:80 , the port address 80 is default for HTTP.Hence you need to type just the address 192.168.1.177 in the URL bar ,skipping the :80 part.

The web browser will query the ethernet shield, which will return the values from the analogue ports on the Arduino board.As such we’ve not connected anything to the Analog pins of Arduino, you get random values refreshed every second.

Remember , this is from a local browser & not from outside world.If you like to see the webserver from anywhere in the world , you need to PORT FORWARD your Static IP address to this local IP address 192.168.1.177 .This is set inside the Router’s configuration.

If your IP is not Static (mostly Dynamic which changes on every reboot) , you need to register with a service like www.noip.com

For more details of port forwarding , consider reading this post & video