Bonjour à tous,
J'aimerais afficher la température ambiante d'une pièce sur une page html mais je ne sais pas comment faire.
faut-il obligatoirement un shield ethernet ?
Je possède un capteur LM35, j'ai déjà fait un programme permettant d'afficher la température sur le moniteur série d'arduino.
Pour plus de détails, je suis à l'écoute de vos questions. Merci ![]()
#include <SPI.h>
#include <Ethernet.h>
#include <MsTimer2.h>
#include <delay.h>// ----------- Raccourcis --------------
#define Resistance 985// ----------- Définition des variables globals --------------
float Lux_moy = 0;
float Amps = 0;
float volts = 0;
boolean Flag = false;
unsigned long Time_us = 0;
unsigned long Time_ms = 0;
float Temperature = 0;// ----------- Parametres de la mesure --------------
unsigned char Periode_echantillonage = 20 ; // T = 20ms
unsigned char nbr_echantillon = 40 ; // n
float Tempo; // en us// ----------- Déclarations des sous fonctions --------------
void InterruptTimer2(void);// ----------- Déclarations des parametres réseaux --------------
byte ip[] = { 192,168,0,30 };
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x65, 0x6f }; // Au hasard
EthernetServer server(80);void setup()
{
Serial.begin(115200);
Ethernet.begin(mac, ip);
server.begin();
Serial.println("Demarrage du serveur");
Serial.print("Adresse IP: ");
Serial.println(Ethernet.localIP());Serial.println(" ");
MsTimer2::set(5000, InterruptTimer2); // IT toutes les 5 secondes pour rafraichir température et luminosité
Tempo = Periode_echantillonage / nbr_echantillon; // Calculs
Tempo = Tempo * 100;Serial.print("Nombre d'echantillon : ");
Serial.println(nbr_echantillon);
Serial.print("Periode d'echantillonnage : ");
Serial.print(Periode_echantillonage);
Serial.println(" ms");
Serial.print("Acquisition toutes les : ");
Serial.print(Tempo);
Serial.println(" us");
Serial.println(" ");
nbr_echantillon --; // Pour le programme
MsTimer2::start();
}void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("Nouveau 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) // Si saut de ligne + suite de la ligne vide
{
// 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();
client.println("");
client.println("");
client.println("<body style = "color: rgb(255,255,255); background-color: rgb(0,0,0);">");client.print("La temperature dans la piece est de ");
client.print("");
if(Temperature <= 10) // froid
{
client.print("<FONT COLOR="#0000FF">");
}
else if(Temperature >= 30) // chauf
{
client.print("<FONT COLOR="FF0000">");
}
else // normal
{
client.print("<FONT COLOR="00FF00">");
}client.print(Temperature);
client.print("");
client.println("");
client.print(" C");client.println("
");
client.print("Luminosite dans la piece : ");
client.print("<FONT COLOR="FFFF00">");
client.println("");
client.print(Lux_moy);
client.print("");
client.println("");
client.println(" lux");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(2);
// close the connection:
client.stop();
Serial.println("Client deconnecte");
}
Arduino MEGA 2560 + Shiel Ethernet + un LM35 + un capteur de luminosité
Voici la partie HTML, avec une super fonction qui affiche la température en rouge quand il fait trop chaud, en bleue quand il fait trop froid et en vert quand ... quand il fait bon ![]()
Techniquement, une IT toutes les 5s pour mouliner les valeurs : température et luminosité de la pièce.
A chaque fois qu'une personne se connecte à l'@ IP configurée, ça lance le code HTML.
75% du code est tiré de l'exemple de la librairie Ethernet.
Bon courage
Clément
Avec une précision que pour la solution de derder9161 oui il faut un shield Ethernet.
Mais sinon tu peux aussi récupérer les valeur sur ton ordi et traiter les données avec ton ordi par la liaison série dans ce cas pas besoin de shield supplémentaire.
A toi de voir si tu veux quelque chose d'autonome ou pas, relié au PC ou pas, etc...