Ciao a tutti, scrivo per condividere con voi il mio primo progetto con Arduino.
Si tratta di un webserver molto semplice che visualizza la temperatura rilevata da un sensore LM35.
Nella prima versione utilizzavo un solo sensore, ma non soddisfatto della precisione ho optato per 2 sensori e un calcolo della media su 3 letture cadauno. Inoltre invece di utilizzare il voltaggio standard da 0-5V ho impostato Arduino per utilizzare 0-1.1V con la stringa analogReference(INTERNAL);
Per maggiori approfondimenti vedete qui: Arduino Playground - LM35HigherResolution
In questo modo ottengo una risoluzione di 0.1°C e con 3 letture su 2 diversi sensori ottengo una precisione decisamente soddisfacente.
Come noterete non ho utilizzato le porte analogiche 0,1 poichè utilizzate dall'ethernet shild (ci ho sbattuto la testa per una bella mezzora prima di accorgermi dell'errore).
Ho utilizzato il web server più semplice che ho trovato e modificato un po' il codice che allego di seguito:
/*
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 pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 4 Sep 2010
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
float tempC = 0.0;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x30, 0x6C };
byte ip[] = { 192, 168, 1, 128 };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
analogReference(INTERNAL); // Utilizzo il voltaggio di riferimento 1.1V
}
void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// 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();
client.print("<h1>Arduino WebServer</h1>");
//Faccio una prima lettura che di fatto andrò a scartare
tempC = (analogRead(3)+analogRead(4));
delay(50);
//Effettuo 3 letture per ogni sensore e sommo i valori
tempC = (analogRead(3)+analogRead(4));
delay(50);
tempC = tempC+(analogRead(3)+analogRead(4));
delay(50);
tempC = tempC+(analogRead(3)+analogRead(4));
tempC = tempC/6/9.31; //Divido per il numero di letture 6 e per 9.31
client.print("Temperatura : ");
client.print(tempC);
client.println("
");
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(50);
// close the connection:
client.stop();
}
}
In futuro voglio implementare più sensori (luminosità, umidità, ecc.) e salvare uno storico delle letture su una scheda microSD, con accesso sempre via Web.
Avete consigli o suggerimenti?