I have problem showing a float variable in an Arduino webserver

367 Views Asked by At

I am using Arduino Uno, ENC28j60. using rbbb_server example.

the library I am using: <EtherCard.h>

as can be seen in the code below I am trying to convert my Float var to Str using dtostrf() in the homepage function since emit_p() can not accept float vars. although Serial.println(temp1) is working correctly, but in the browser, I have a problem showing the variable(pic added). I've been looking for a solution but couldn't find any, unfortunately. would appreciate any suggestion.

#include <EtherCard.h>

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,1,203 };

byte Ethernet::buffer[500];
BufferFiller bfill;

static word homePage() {
  String temp1;
  float val = 311.322; 
  char s1[10]; 
  temp1 = dtostrf(val,3,2,s1);
  
  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='1'/>"
    "<title>RBBB server</title>"
      "<h1>my val:$S</h1>"),
      temp1);
      
      Serial.println(temp1);
  return bfill.position();
}

void setup () {
  
  Serial.begin(57600);
  if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
    Serial.println(F("Failed to access Ethernet controller"));
  ether.staticSetup(myip);
}

void loop () {
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if (pos)
    ether.httpServerReply(homePage());
}

output in browser

1

There are 1 best solutions below

0
On BEST ANSWER

I think I see your problem, but I can't test it.

dtostrf returns a char * pointer to the string you want to print here's the docs

It looks like that function is being called and assigning that string to the temp variable which is a String type (idk your experience, but working with text in Arduino can be very confusing check out this thread ).

Try changing temp1 to be a char * like this:

  char * temp1;