sending float values - I know $d is for integers, but what about float values?

I tried searching for it but I can’t find equivalent symbol for sending float values. I do know $D is for integers.
I created a server that displays temperature values using ENC28J60 library. Everything works fine, but I can’t display float values and I know it’s because of the symbol. If I convert temperature to integer then it works just fine. Sure I could make a workaround this and use integers, but I really want to know what’s the symbol for floats ?

 bfill.emit_p(PSTR(
    "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n"
    "\r\n"
    "<meta http-equiv='refresh' content='6'/>"
    "<title>Temperature</title>" 
    "$D C"),
      temp);
  return bfill.position();

Floats are not supported in sprintf, so maybe they aren't supported there either. Try dtostrf to turn the float into a string.

awesome
....but I still need a character to put after a dollar sign for it to work - it seems $D expects an integer

After what $ sign? The format specifier for float is %f. BUT IT DOESN'T WORK! You have to use dtostrf() to to convert the float to a string. Then, you can use %s for the string format specifier.

He's not using sprintf. He's using bfill.emit_p, whatever that is.

I checked both %s and %S and nothing works

bfill.emit_p is defined here -> http://jeelabs.net/pub/docs/ethercard/classBufferFiller.html
and I found that...

#define PGM_P const char *

Used to declare a variable that is a pointer to a string in program space.

but WTF it means is beyond me
:slight_smile:

anyway I used a workaround solution, but I regret not reading on ethernet shields first. I bought one based on ENC28J60 IC just because it was smaller. Now that I see how simple standard ethernet library is when compared with the one for ENC28J60 I cry a little.

You should post your entire sketch.

My guess is that this is the BufferFiller.emit_p() method from EtherCard.h.
Code here: Google Code Archive - Long-term storage for Google Code Project Hosting.

Looking at the code, there is no provision for floating point. You will have to convert your float using dtostrf()
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga060c998e77fb5fc0d3168b3ce8771d42
and then pass that using a $S.

Naah wait it does work. You are right I should have posted my entire sketch back then - I must have forked up my dtostrf() implementation.
Thank you all

##include <EtherCard.h>
#include <OneWire.h>
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,1,200 };
OneWire  ds(A5);  // on pin A5
byte Ethernet::buffer[800];
BufferFiller bfill;

float celsius,fahrenheit;
int temp1,temp2;
char zxc[5];
void setup () {
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
  ether.staticSetup(myip);
  
   Serial.begin(9600);
 
}
  
static word homePage() {
  long t = millis() / 1000;
  word h = t / 3600;
  byte m = (t / 60) % 60;
  byte s = t % 60;
  bfill = ether.tcpOffset();
  bfill.emit_p(PSTR(
    "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n"
    "\r\n"
    "<meta http-equiv='refresh' content='6'/>"
    "<title>Temperature</title>" 
    "<hr4>$S C</hr4>"), dtostrf(celsius,3,2,zxc)
      );
  return bfill.position();
}

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

  
  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,1);         // start conversion, 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

  unsigned int 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);
    if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw << 2; // 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");
  
 temp1 = abs(celsius);
 temp2 = abs((celsius - temp1) * 100) ;
 
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if (pos)  // check if valid tcp data is received
ether.httpServerReply(homePage()); // send web page data
}

Messy I know

ohh and BTW is it just my impression that the ENC28J60 Ethernet library is just a nightmare to use - unless you have background in C++ or something

kerimil:
ohh and BTW is it just my impression that the ENC28J60 Ethernet library is just a nightmare to use - unless you have background in C++ or something

I've got a background in C++, and after reading all the work needed to get that card to work, I put it aside. :slight_smile: