PROJET BAC Affichage de température du DS18B20 V2 sur page Web

Bonjour :slight_smile:

Je suis actuellement en Terminale STI2D Option SIN et je suis sur un projet dans lequel je dois m'occuper d'afficher certaines mesures telles que la température etc sur une page web.

Voici les composants que j'ai en ma possession :

  • Capteur température DS18B20 V2 (DFRobot) (j'ai mis une image du capteur en pièce jointe)
  • Un Arduino Uno
  • Un Arduino Sield Ethernet V2

J'ai réussis à relever la température prélevée par le DS18B20 sur le moniteur série (screenshot en pièce jointe) ; (note : Sans le shield biensur car pour le moment il ne m'est pas utile :wink: )

et voici le code utilisé :

#include <OneWire.h> // Inclusion de la librairie OneWire
 
#define DS18B20 0x28     // Adresse 1-Wire du DS18B20
#define BROCHE_ONEWIRE 2 // Broche utilisée pour le bus 1-Wire
 
OneWire ds(BROCHE_ONEWIRE); // Création de l'objet OneWire ds
 
// Fonction récupérant la température depuis le DS18B20
// Retourne true si tout va bien, ou false en cas d'erreur
boolean getTemperature(float *temp){
  byte data[9], addr[8];
  // data : Données lues depuis le scratchpad
  // addr : adresse du module 1-Wire détecté
 
  if (!ds.search(addr)) { // Recherche un module 1-Wire
    ds.reset_search();    // Réinitialise la recherche de module
    return false;         // Retourne une erreur
  }
   
  if (OneWire::crc8(addr, 7) != addr[7]) // Vérifie que l'adresse a été correctement reçue
    return false;                        // Si le message est corrompu on retourne une erreur
 
  if (addr[0] != DS18B20) // Vérifie qu'il s'agit bien d'un DS18B20
    return false;         // Si ce n'est pas le cas on retourne une erreur
 
  ds.reset();             // On reset le bus 1-Wire
  ds.select(addr);        // On sélectionne le DS18B20
   
  ds.write(0x44, 1);      // On lance une prise de mesure de température
  delay(800);             // Et on attend la fin de la mesure
   
  ds.reset();             // On reset le bus 1-Wire
  ds.select(addr);        // On sélectionne le DS18B20
  ds.write(0xBE);         // On envoie une demande de lecture du scratchpad
 
  for (byte i = 0; i < 9; i++) // On lit le scratchpad
    data[i] = ds.read();       // Et on stock les octets reçus
   
  // Calcul de la température en degré Celsius
  *temp = ((data[1] << 8) | data[0]) * 0.0625; 
   
  // Pas d'erreur
  return true;
}
 
// setup()
void setup() {
  Serial.begin(9600); // Initialisation du port série
}
 
// loop()
void loop() {
  float temp;
   
  // Lit la température ambiante à ~1Hz
  if(getTemperature(&temp)) {
     
    // Affiche la température
    Serial.print("Temperature : ");
    Serial.print(temp);
    Serial.write(176); // caractère °
    Serial.write('C');
    Serial.println();
  }
}

Tout fonctionne très bien pour le moment mais c'est à partir de LA que les problèmes surviennent :frowning: :
Maintenant je veux utiliser le Shield Ethernet et l'ajouter sur mon protype ainsi qu' afficher la température sur une page Web cette fois ci et non pas sur le moniteur série :confused:
Et j'ai utilisé plusieurs code avec plusieurs adresse IP différentes assignées aux Shield mais sans succès :cry:

Pourtant l'IDE Arduino ne m'a montrer aucune erreur, le code est sans erreurs, mais lorsque j'écris l'adresse IP du Shield sur mon navigateur bah il est dit que le délai d'attente est trop long :-\

Heelpp :slightly_frowning_face:

ds18b20.jpg

bonjour,
et le code pour l'ethernet est ou?

infobarquee:
bonjour,
et le code pour l'ethernet est ou?

Oui le voila, par contre il est anglais celui ci :

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library
#include <OneWire.h>
#include <SPI.h>
#include <Ethernet.h>

// 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, 0x10, 0x0A, 0x7D    // Enter your ethernet MAC address. You will find it behind your arduino board.
};
IPAddress ip(192, 168, 1, 102);        // Set your IP address for Arduino Board

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


OneWire  ds(2);  // on pin 2 

void setup(void) {
  // Open serial communications and wait for port to open:
  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());
}

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }

  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44);        // start conversion, use ds.write(0x44,1) with parasite power on at the end

  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.

  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");
  
   // 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: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.print("<p style='text-align: center;'>&nbsp;</p>");
          client.print("<p style='text-align: center;'><span style='font-size: x-large;'><strong>Welcome To My Home</strong></span></p>");
          client.print("<p style='text-align: center;'><span style='color: #0000ff;'><strong style='font-size: large;'>Room Temperature = ");
          client.println(celsius);
          client.print("</strong></span><h style='text-align: center;'><span style='color: #0000ff;'><strong style='font-size: large;'><sup>o</sup>C</strong></span></h></p>");
          client.print("<p style='text-align: center;'>&nbsp;</p>");
          client.print("<p style='text-align: center;'>&nbsp;</p>");
          client.print("<p style='text-align: center;'>&nbsp;");
          
          // Date and Time script
          client.print("<script language='javascript'>");
          client.println();
          client.print("<!--");
          client.println();
          client.print("var today = new Date()");
          client.println();
          client.print("document.write(today); //--> </script>");
          client.print("</p>");
    
          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");
  }
}

montage derrière quoi comme modem?
vu que tu lui mets une ip fixe, il faut quelle soit en dehors de la plage du dhcp de la box.
fais un ipconfig dans la console de ton pc pour connaitre les infos.
après tu changes l'ip fixe en 192.168.1.5 par exemple si la plage ip est bien en 192.168.1.xxx

infobarquee:
montage derrière quoi comme modem?
vu que tu lui mets une ip fixe, il faut quelle soit en dehors de la plage du dhcp de la box.
fais un ipconfig dans la console de ton pc pour connaitre les infos.
après tu changes l'ip fixe en 192.168.1.5 par exemple si la plage ip est bien en 192.168.1.xxx

Le montage a été branché sur un modem de chez Bouygues pour ce qui est du Shield.
Et pour ce qui est de l'IP, oui la plage IP est bien en 192.168.1.xxx je vais changer l'IP qui est dans le code et la remplacer par 192.168.1.5 et je te tien au courant

RE,

J'ai tester avec l'IP que tu m'a proposé mais rien y fait ça me donne la meme chose :-\ (voir le screenshot en pièce jointe) ,

J'ai aussi mis en pièce jointe le montage de mon prototype :wink: Tu y verra la carte Sield emboitée sur l'Arduino Uno, pour ce qui est des broches, le GND vers le pin GND, le deuxième fil pour l'alimentaion bah je l'ai mis sur le pin 5V, et pour le troisième je l'ai mis sur le pin 2.

si tu fais un ping 192.168.1.5
ca donne quoi?
la led du shield clignote?

infobarquee:
si tu fais un ping 192.168.1.5
ca donne quoi?
la led du shield clignote?

Non la led du shield ne clignote pas, et pour ce que sa donne , je vais mettre un screenshot de ce que sa donne en pièce jointe

teste ping.jpg

rajoutes

unsigned char mask[] = {255,255,255,0};
unsigned char gateway[] = {192,168,1,1};


dans le setup
  Ethernet.begin(mac, ip, gateway, mask);

adnanmhd:

  • Un Arduino Sield Ethernet V2

#include <Ethernet.h>

avec cette carte il faut utiliser la librairie Ethernet2

infobarquee:
rajoutes

unsigned char mask[] = {255,255,255,0};

unsigned char gateway[] = {192,168,1,1};

dans le setup
 Ethernet.begin(mac, ip, gateway, mask);

Je dois rajouter ça ici ? ou bien dans le setup où il y a écrit Ethernet.begin ?

// 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, 0x10, 0x0A, 0x7D    // Enter your ethernet MAC address. You will find it behind your arduino board.
};
IPAddress ip(192, 168, 1, 5);// Set your IP address for Arduino Board
unsigned char mask[] = {255,255,255,0};
unsigned char gateway[] = {192,168,1,1};

rjnc38:
avec cette carte il faut utiliser la librairie Ethernet2

Ah d'accord merci je vais l'ajouter :slight_smile:

Au faite je trouve pas la librairie :confused:

adnanmhd:
Au faite je trouve pas la librairie :confused:

faut faire un peu d'effort aussi pour du travail perso
ethernet2 arduino sur le net et tu trouves tout

infobarquee:
faut faire un peu d'effort aussi pour du travail perso
ethernet2 arduino sur le net et tu trouves tout

Justement j'ai deja chercher et je trouve pas grand chose

Ah nan ça y est je pense avoir trouver

RE,

J'ai fait ce que vous m'aviez dis messieurs mais j'ai des erreurs qui apparaissent dans l'IDE Arduino dans le code, voici les erreurs :

"sketch\Arduinot_DS18B20_Web_Temperature.ino.cpp.o: In function `loop':

C:\Users\Maison\Documents\Arduino\Arduinot_DS18B20_Web_Temperature/Arduinot_DS18B20_Web_Temperature.ino:54: undefined reference to `OneWire::search(unsigned char*, bool)'

C:\Users\Maison\Documents\Arduino\Arduinot_DS18B20_Web_Temperature/Arduinot_DS18B20_Web_Temperature.ino:68: undefined reference to `OneWire::crc8(unsigned char const*, unsigned char)'

C:\Users\Maison\Documents\Arduino\Arduinot_DS18B20_Web_Temperature/Arduinot_DS18B20_Web_Temperature.ino:94: undefined reference to `OneWire::select(unsigned char const*)'

C:\Users\Maison\Documents\Arduino\Arduinot_DS18B20_Web_Temperature/Arduinot_DS18B20_Web_Temperature.ino:101: undefined reference to `OneWire::select(unsigned char const*)'

C:\Users\Maison\Documents\Arduino\Arduinot_DS18B20_Web_Temperature/Arduinot_DS18B20_Web_Temperature.ino:113: undefined reference to `OneWire::crc8(unsigned char const*, unsigned char)'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno."

Et voici le code modifé :

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library
#include <OneWire.h>
#include <SPI.h>
#include <Ethernet2.h>

// 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, 0x10, 0x0A, 0x7D    // Enter your ethernet MAC address. You will find it behind your arduino board.
};
IPAddress ip(192, 168, 1, 5);// Set your IP address for Arduino Board
unsigned char mask[] = {255,255,255,0};
unsigned char gateway[] = {192,168,1,1};


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


OneWire  ds(2);  // on pin 2 

void setup(void) {
  // Open serial communications and wait for port to open:
  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, gateway, mask);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }

  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44);        // start conversion, use ds.write(0x44,1) with parasite power on at the end

  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.

  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");
  
   // 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: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.print("<p style='text-align: center;'>&nbsp;</p>");
          client.print("<p style='text-align: center;'><span style='font-size: x-large;'><strong>Welcome To My Home</strong></span></p>");
          client.print("<p style='text-align: center;'><span style='color: #0000ff;'><strong style='font-size: large;'>Room Temperature = ");
          client.println(celsius);
          client.print("</strong></span><h style='text-align: center;'><span style='color: #0000ff;'><strong style='font-size: large;'><sup>o</sup>C</strong></span></h></p>");
          client.print("<p style='text-align: center;'>&nbsp;</p>");
          client.print("<p style='text-align: center;'>&nbsp;</p>");
          client.print("<p style='text-align: center;'>&nbsp;");
          
          // Date and Time script
          client.print("<script language='javascript'>");
          client.println();
          client.print("<!--");
          client.println();
          client.print("var today = new Date()");
          client.println();
          client.print("document.write(today); //--> </script>");
          client.print("</p>");
    
          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");
  }
}