In the previous post we’ve seen how to connect the Arduino ETHERNET SHIELD to a Router directly & upload LM35 data to Thingspeak.
What if you do not have physical access to your Router & your Laptop is connected over WIFI to the Router. Or you cannot run a RJ45 cable from Router to Ethernet shield.
The procedure we follow is similar to the one we did for connecting Raspberry PI to Laptop.
First step is to enable SHARING of your existing internet.In my case , Laptop is connected to Router
over WIFI.
Open Control Panel –> Network & Internet –> Network & Sharing Center
Click on the existing internet connection
On the next screen click PROPERTIES & then SHARING
Tick mark to ENABLE SHARING of Internet connection
Now plug in the USB cable to Arduino & then connect the RJ45 Straight cable from shield to the RJ45
Port of LAPTOP.
As soon as you connect the RJ45 cable you can see an UNIDENTIFIED NETWORK
Click on that new Network , select PROPERTIES.
On the next screen double click on Internet Protocol version 4 (TCP/IPV4)
You can see an IP address like 192.168.137.1.
Note that this is the new Gateway IP of the new network formed by Ethernet shield.If you do not see any IP , just select the “ Use the following IP “ & feed in manually the IP.
Now RUN –> CMD > IPCONFIG /ALL
to verify the IP.
Under Ethernet adapter the Gateway IP of new network is displayed.
Note that this is the GATEWAY IP & Ethernet shield will be allotted IP in this range .
Let us try some built in example codes.
Under Examples –> Ethernet –> select DhcpAddressPrinter
This will print the IP allotted to the Shield.
Upload the code & open up the Serial monitor.
The IP allotted to the shield here is 192.168.137.30 which is in range of new Gateway .
Next example is WEBSERVER
Open the WEBSERVER code .
You need to change the IP Address as 192.168.137.xxx
Here change it to 192.168.137.177 where the SERVER will be started. Note the usage of comma instead of dot between each byte.
Upload the code & open the serial monitor to verify that SERVER is started.
You can use your Browser as CLIENT.
Type in the IP 192.168.137.177 to access the server.
On the Serial monitor “Client connected “ information will apper.
On the browser you can see the values of ANALOG Pins of Arduino.
As nothing is connected to the Analog pins , you get random values.
Next example is the TELNETCLIENT
Open the code.
Here Ethernet shield is used as CLIENT & you need to change the IP of client in range with the new Gateway 192.168.137.177
Next is the IP of the SERVER . This may be another PC or Android phone connected to the same Network .
Here the SERVER is started on PC with IP 192.168.0.103
Note that this IP is in the range of Gateway of Router 192.168.0.xxx
Scroll down further & note the PORT number on which the SERVER will LISTEN.
Here it is 10002.
Upload the code on to Arduino.
To test the setup , you need to start a SERVER on another PC on the same network with IP 192.168.0.103
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 192.160.0.100.
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
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
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[] = “api.thingspeak.com”;
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;
if (client.connected())
{
Serial.println(“Connecting to ThingSpeak…”);
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println(“Connection to ThingSpeak failed (“+String(failedCounter, DEC)+”)”);
Serial.println();
}
}
else
{
failedCounter++;
Serial.println(“Connection to ThingSpeak Failed (“+String(failedCounter, DEC)+”)”);
Serial.println();
lastConnectionTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println(“Connecting Arduino to network…”);
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println(“DHCP Failed, reset Arduino to try again”);
Serial.println();
}
else {
Serial.println(“Arduino connected to network using DHCP”);
Serial.println();
Serial.println(“Data being uploaded to THINGSPEAK Server…….”);
Serial.println();
}
delay(1000);
}
————————————————–
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:
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.
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.
Finally, we have the actual message that we’re sending.
The Arduino board can be connected to the Internet using this Ethernet Shield which is based on the
WIZNET W5100 Ethernet chip . The Wiznet W5100 provides a network (IP) stack capable of both TCP and UDP.
WIZNET W5100 is the classic one , widely used by Arduino.CC .
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
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
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.
To know the IP address , go to Control panel –> Network & Internet –> Network & Sharing Center
Click on your Internet connection .On the next window click Details to see the IPV4 address of your PC.
You can also use a IP SCANNER tool to know all the IP address allotted by your Router
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
Upload the sketch & open the Serial monitor to see the result.
Example Sketch 2 : Display a web page from Arduino
———————————
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177); // change IP address according to your network
EthernetServer server(80); // create a server at port 80
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == ‘\n’ && currentLineIsBlank) {
// send a standard http response header
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”);
client.println();
// send web page
client.println(“<!DOCTYPE html>”);
client.println(“<html>”);
client.println(“<head>”);
client.println(“<title>Arduino Web Page</title>”);
client.println(“</head>”);
client.println(“<body>”);
client.println(“<h1>Hello from Arduino!</h1>”);
client.println(“<p>A web page from the Arduino server</p>”);
client.println(“</body>”);
client.println(“</html>”);
break;
}
// every line of text received from the client ends with \r\n
if (c == ‘\n’) {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != ‘\r’) {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
———————————————————