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 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.
Type in your browser 192.168.0.1 , the gateway IP & feed in your Router’s username & password (generally both are “admin”)
Under DHCP settings ensure that DHCP is Enabled.
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 192.168.0.101
& the second device is Ethernet shield which is allotted 126.96.36.199.
But in our code we use IP 192.168.0.150 .
Let us change the IP to 192.168.0.150 & make it STATIC.
Click on ADDRESS RESERVATION under DHCP
& then click ADD NEW
Feed in the MAC address of the Ethernet shield & the reserved IP as 192.168.0.150
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 192.168.0.150 & 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
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
You need to sign up with www.thingspeak.com to upload your data to the server.
Open your account on THINGSPEAK & select MYCHANNELS under CHANNELS
Click on NEW CHANNEL
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:
- Gate way IP of your Router.
- IP address of your Shield in range with gateway IP.
- 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(“Host: api.thingspeak.com\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: “); client.print(tsData.length()); client.print(“\n\n”);
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,
- 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:
We use the first one as we u
pdate 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
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
VIDEO TUTORIAL :
source : https://alselectro.wordpress.com/2017/05/01/ethernet-shield-arduino-upload-lm35-data-to-thingspeak/
Tags : 201705, W5100, Ethernet Shield, Arduino
Author : Saravana Electronics