Monday, May 4, 2015

Arduino Temperature Web Server

 

The idea is to build a simple temperature Web Server that show the temperature and it changes the format of the website when it riches or gets over the one set on the Arduino sketch.

For this project I wanted a simple design since I don´t have too much knowledge of HTML programming but thanks to the internet there are no such thing like that excuse “I didn’t find information” or “It’s so difficult and I didn’t found a book of it”

I made a simple course on http://www.codecademy.com/ and the good thing is that is FREE!!!

 

Ok, enough of talking that much let´s get down to business.

For this exercise we’ll use:

1 x Arduino UNO or Arduino MEGA or Arduino Leonardo

1 x Ethernet Shield

1 x LM35 Temperature Sensor

 

This is the code for the Temperature Web Server:

/////////////////////////////////////////////////////////////////////////////////////
AUTOR: JUAN BIONDI FECHA: FEBRERO/2014
PROGRAMA: VERSION: 1.0
DISPOSITIVO: ATMEL 328 COMPILADOR: AVR
ENTORNO: PROTEUS SIMULADOR: VSM
TARJETA DE PROGRAMACION: DEBUGGER:
/////////////////////////////////////////////////////////////////////////////////////

Web Server

A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pin A4

This pin has a temperature sensor (LM35)

/////////////////////////////////////////////////////////////////////////////////////
*/

/////////////////////////////////////////////////////////////////////////////////////
// LIBRERIAS //
/////////////////////////////////////////////////////////////////////////////////////

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


/////////////////////////////////////////////////////////////////////////////////////
// VARIABLES GLOBALES //
/////////////////////////////////////////////////////////////////////////////////////
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,30);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(4988);

/////////////////////////////////////////////////////////////////////////////////////
// FUNCIONES //
/////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////
// CONFIGURACION //
/////////////////////////////////////////////////////////////////////////////////////

void setup() {
// Open serial communications and wait for port to open:
analogReference(INTERNAL);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}


// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

/////////////////////////////////////////////////////////////////////////////////////
// PRINCIPAL //
// //
/////////////////////////////////////////////////////////////////////////////////////
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
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"); // the connection will be closed after completion of the response
client.println("Refresh: 3"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<style>");
client.println("body {background-color:lightgray}");
client.println("h1 {color:blue}");
client.println("p {color:red}");
client.println("</style>");
client.println("<FONT FACE=currier > Demostracion de un servidor web para mostrar la temperatura </FONT>");
client.println("<br>");
client.println("<FONT FACE=currier > del aula con un sensor LM35 y un escudo de Arduino.</FONT>");



// output the value of the temperature of the sensor connected to the analog pin
int sensorReading = analogRead(A4);
float temperatura = (sensorReading*1.1*100) / 1024;
client.println("<br>");
client.println("<br>");
client.print("<center>");
client.print("<h1>");
client.print("La temperatura leida es: ");
client.print(temperatura);
client.print(" C");
client.print("</h1>");
client.print("<center>");
client.println("<br>");
client.println("<br>");

/*
/////////////////////////////////////////////////////////////////////////////////////
Now if the temperature is higher than the one we establish it will print on the
remote computer a text with the word WARNING in Spanish
/////////////////////////////////////////////////////////////////////////////////////
*/


if (temperatura >= 25)
{
//termometro();
client.println("<div align=");
String izquierda = "center";
client.println(izquierda);
client.println("<br>");
client.print("<p>");
client.println("!*****************************************************************************************************************************!");
client.print("<p>");
client.println("!___________@@@____@@_______@@____@@@@@____@@@@@___________@@@@_______@@@@________@@@@______________!");
client.print("<p>");
client.println("!_______@@_________@@________@@_______@@______@@____@@@______@@__@@_____@@___@@___@@_______@@___________!");
client.print("<p>");
client.println("!_______@@_________@@________@@_______@@______@@______@@_____@@____@@____@@_____@@_@@_______@@___________!");
client.print("<p>");
client.println("!_______@@_________@@________@@_______@@______@@______@@___@@@@@@@@___@@_____@@_@@_______@@__________!");
client.print("<p>");
client.println("!________@@_________@@______@@________@@______@@____@@@___@@________@@__@@___@@____@@_____@@____________!");
client.print("<p>");
client.println("!____________@@_________@@@__________@@@@@___@@@@@______@@__________@@_@@@@@________@@@@_____________!");
client.print("<p>");
client.println("!******************************************************************************************************************************!");
//client.print("</p>");

}
/*
/////////////////////////////////////////////////////////////////////////////////////
End of the if to write the WARNING word
/////////////////////////////////////////////////////////////////////////////////////
*/

client.println("<br />");
client.println("<img src=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlMaxA9ASwejD_RhQbITq5_lhXv_Ma8DsjEIsT4x3vVHavyYstYN0eT7SrTUEdr0UDLExDbFmhyIXORJHvVWlmtaxxbBIBpS7P51F2OjOJ_lvJAAc9htkeNFq09AWN6yeIPTVq57-RALY/s1600/CIFPN1.jpg alt=some_text>");
client.println("<br />");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}

In the following images you can see how the web server was running on my local network without any problem.


Ethernet1


Normal temperature


Ethernet2


Showing a sign saying WARNING in Spanish because the temperature is over the value set on the Arduino sketch.


 


The next thing to see this from any computer we have to do some thing first:


1.- Open the port used for this application


This video explains really easy how to open ports on your router, remember that every router is different and it may change the configuration on it.


https://www.youtube.com/watch?v=aQXJ7sLSz14


2.- And finding your public IP address.


Here there is another video that shows you how to know your public IP address.


https://www.youtube.com/watch?v=yRrKKJHzHJo


 


You can download the file on the following link:

https://drive.google.com/folderview?id=0B7dtMeeMPK5rfnJCSVlRVTkxRzdSeHBMWDJ6THVPWmdHdm1kZ3NwNTYzSEJ5b0ZmdlFoMG8&usp=sharing

File name: Ethernet_temp.zip

Version of Proteus 8.1

 


BuzzNet Tags:

2 comments: