Temperatura via web

Ciao a tutti ho riadattato il codice di un esempio, lo scopo è quello di mettere in una pagina html il valore della temperatura letta da un DS18B20, sulla seriale il valore letto è perfetto, tramite web da un valore tipo 32569. Come modulo la utilizzo questo

https://i.ytimg.com/vi/sY1WJ78l8N8/maxresdefault.jpg

Penso che l’errore sia solo di codifica

#include <EtherCard.h>

#include <OneWire.h>

//START region Ethernet config

#define STATIC 1  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,1,10 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };
#endif

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];

const int ledPin = 2;
boolean ledStatus;

char* on = "ON";
char* off = "OFF";
char* statusLabel;
char* buttonLabel;

//END region Ethernet config

//START region temperature sensor config
OneWire  ds(4);  

double temperature = 10.10;
//END region temperature sensor config

void setup () {
 
  Serial.begin(57600);
  Serial.println("Termostato web");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
  Serial.println( "Impossibile accedere al controller ethernet");
  #if STATIC
    ether.staticSetup(myip, gwip);
  #else
    if (!ether.dhcpSetup())
      Serial.println("DHCP fallito");
  #endif

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  Serial.println();
  
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  ledStatus = false;
  
}
  
void loop() {

  temperature = getTemp();

  Serial.println(temperature);
 
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  
  if(pos) {
    
    if(strstr((char *)Ethernet::buffer + pos, "GET /?status=ON") != 0) {
      Serial.println("Received ON command");
      ledStatus = true;
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?status=OFF") != 0) {
      Serial.println("Received OFF command");
      ledStatus = false;
    }
    
    if(ledStatus) {
      digitalWrite(ledPin, HIGH);
      statusLabel = on;
      buttonLabel = off;
    } else {
      digitalWrite(ledPin, LOW);
      statusLabel = off;
      buttonLabel = on;
    }
//      
//    BufferFiller bfill = ether.tcpOffset();
//    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
//      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
//      "<html><head><title>WebLed</title></head>"
//      "<body>LED Status: $S "
//      "<a href=\"/?status=$S\"><input type=\"button\" value=\"$S\"></a>"
//      "</body></html>"      
//      ), statusLabel, buttonLabel, buttonLabel);
//    ether.httpServerReply(bfill.position());

    BufferFiller bfill = ether.tcpOffset();
    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"      
      "<!DOCTYPE html><html xmlns=http://www.w3.org/1999/xhtml>"
      "<head>"
      "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
      "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />"
      "<title></title>"
      "<style>html,body{height:100%}body{text-align:center;font-size:30px}div,input{width:100%;min-height:10%}input[type=button]{height:40px}</style>"
      "</head><body>"
      "<h2>Termostato WEB</h2>"));

      bfill.emit_p(PSTR("<div>Temperatura</div><div><b>"));
      bfill.emit_raw(PSTR("$D.0"),temperature);
      bfill.emit_p(PSTR(" °C</b></div>"));
  
      bfill.emit_p(PSTR("<div>Stato caldaia</div><div>$S</div>"
      "<div><a href=\"/?status=$S\"><input type=\"button\" value=\"$S\"></a></div>"
      "</body></html>"    
      ), statusLabel, buttonLabel, buttonLabel);
      
    ether.httpServerReply(bfill.position());







    
  }
}

float getTemp(){
  //returns the temperature from one DS18B20 in DEG Celsius
 
  byte data[12];
  byte addr[8];
 
  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }
 
  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }
 
  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }
 
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end
 
  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad
 
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
 
  ds.reset_search();
 
  byte MSB = data[1];
  byte LSB = data[0];
 
  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
 
  return TemperatureSum;
 
}
bfill.emit_raw(PSTR("$D.0"),temperature);

Prova a cambiare la D in T:

bfill.emit_raw(PSTR("$T.0"),temperature);

adesso nella pagina html stampa questo

�A�� °C

Prova questa versione di EtherCard: https://github.com/jcw/ethercard

PS: In HTML, °C andrebbe scritto °C.

Grazie la stavo già usando, e sto pure seguendo la loro guida, http://jeelabs.net/pub/docs/ethercard/classBufferFiller.html ma non stampa nulla nell'html

Ahhh, allora ho capito! Prova questo:

bfill.emit_p(PSTR("$T.0"),temperature);

Nulla questo è quello che stampa ora

T.0 °C

il codice

#include <EtherCard.h>

#include <OneWire.h>

//START region Ethernet config

#define STATIC 1  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,1,10 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };
#endif

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];

const int ledPin = 2;
boolean ledStatus;

char* on = "ON";
char* off = "OFF";
char* statusLabel;
char* buttonLabel;


//END region Ethernet config

//START region temperature sensor config
OneWire  ds(4);  

double temperature;
//END region temperature sensor config


void setup () {
 
  Serial.begin(57600);
  Serial.println("Termostato web");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
  Serial.println( "Impossibile accedere al controller ethernet");
  #if STATIC
    ether.staticSetup(myip, gwip);
  #else
    if (!ether.dhcpSetup())
      Serial.println("DHCP fallito");
  #endif

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  Serial.println();
  
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  ledStatus = false;
  
}
  
void loop() {

  temperature = getTemp();

  //Serial.println(temperature);
 
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);
  
  if(pos) {
    
    if(strstr((char *)Ethernet::buffer + pos, "GET /?status=ON") != 0) {
      Serial.println("Received ON command");
      ledStatus = true;
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?status=OFF") != 0) {
      Serial.println("Received OFF command");
      ledStatus = false;
    }
    
    if(ledStatus) {
      digitalWrite(ledPin, HIGH);
      statusLabel = on;
      buttonLabel = off;
    } else {
      digitalWrite(ledPin, LOW);
      statusLabel = off;
      buttonLabel = on;
    }
//      
//    BufferFiller bfill = ether.tcpOffset();
//    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
//      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
//      "<html><head><title>WebLed</title></head>"
//      "<body>LED Status: $S "
//      "<a href=\"/?status=$S\"><input type=\"button\" value=\"$S\"></a>"
//      "</body></html>"      
//      ), statusLabel, buttonLabel, buttonLabel);
//    ether.httpServerReply(bfill.position());

    BufferFiller bfill = ether.tcpOffset();
    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"      
      "<!DOCTYPE html><html xmlns=http://www.w3.org/1999/xhtml>"
      "<head>"
      "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
      "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />"
      "<title></title>"
      "<style>html,body{height:100%}body{text-align:center;font-size:30px}div,input{width:100%;min-height:10%}input[type=button]{height:40px}</style>"
      "</head><body>"
      "<h2>Termostato WEB</h2>"));

      bfill.emit_p(PSTR("<div>Temperatura</div><div><b>"));
      bfill.emit_p(PSTR("$T.0"),temperature);
      Serial.println(temperature);
      bfill.emit_p(PSTR(" °C</b></div>"));

      
  
      bfill.emit_p(PSTR("<div>Stato caldaia</div><div>$S</div>"
      "<div><a href=\"/?status=$S\"><input type=\"button\" value=\"$S\"></a></div>"
      "</body></html>"    
      ), statusLabel, buttonLabel, buttonLabel);
      
    ether.httpServerReply(bfill.position());







    
  }
}

float getTemp(){
  //returns the temperature from one DS18B20 in DEG Celsius
 
  byte data[12];
  byte addr[8];
 
  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }
 
  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }
 
  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }
 
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end
 
  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad
 
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
 
  ds.reset_search();
 
  byte MSB = data[1];
  byte LSB = data[0];
 
  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
 
  return TemperatureSum;
 
}

Devi scommentare questo in EtherCard.cpp:

//#define FLOATEMIT // uncomment line to enable $T in emit_P for float emitting

Ti ringrazio al 10000% non ci sarei riuscito! Grazie infinite ora funziona perfettamente!

Ottimo :). Ci sono anche altre alternative comunque:

char buf[16];
snprintf (buf, sizeof (buf), "%.2lf", temperature);
bfill.emit_p(PSTR("$S"), buf);

Oppure ancora puoi usare dtostrf(). Oppure puoi usare un intero per la temperatura, ecc...