Why won't this print the whole number to the webpage?

Hi all,

I am having a bit of a dilemma here.... This sketch I have attached is meant to print the battery voltage measured on analog0 to a webpage. The issue is it will not add any of the decimal places, I am using the "float command" but still not working. When the code is set to dump the voltage to serial it prints it perfectly to 2 decimal places. Why isn't this working? How should I go about fixing it?

#include "etherShield.h"
#include "ETHER_28J60.h"



// Define MAC address and IP address - both should be unique in your network
static uint8_t mac[6] = {
  0x54, 0x55, 0x58, 0x10, 0x00, 0x24};  
static uint8_t ip[4] = {
  192, 168, 0, 12}; 
static uint16_t port = 80; // Use port 80 - the standard for HTTP                                     

int loadrelay = 7;
int solarrelay = 6;

int volt_val;
int voltage;
int light_val;

ETHER_28J60 ethernet;

void setup()
{ 
  pinMode(loadrelay, OUTPUT);
  pinMode(solarrelay, OUTPUT);
  digitalWrite(solarrelay, HIGH);


  digitalWrite(loadrelay, HIGH);
  Serial.begin(9600);


  ethernet.setup(mac, ip, port);
}void loop()
{
  {
    digitalWrite(solarrelay, HIGH); // turn solar regulator on

    volt_val = analogRead(0);
    float voltage = volt_val * (20.0 / 1023.0);
    Serial.println(voltage); //for calibrating voltage divider
    light_val = analogRead(1);
    //Serial.println(light_val); //for calibrating light sensor

    //low battery cutoff
    if(voltage  <12.5){
      digitalWrite(loadrelay, LOW);
      delay(1000*10);
    }
    else{
      if(light_val >140){
        digitalWrite(loadrelay, LOW);
        delay(10*1000);
      }
      else{
        digitalWrite(loadrelay, HIGH);
      }
    }  
  }
  { 
    if (ethernet.serviceRequest())
    {
      ethernet.print("<meta http-equiv='refresh' content='1'>");
      ethernet.print("<font face='verdana'>");
      ethernet.print("<center><H1>Remote Battery Management</H1></center>");
      volt_val = analogRead(0);
      float voltage = volt_val * (20.0 / 1023.0);
      ethernet.print("<p><b>Battery Bank Voltage</b></p>");
      ethernet.print(voltage); //this is the value which will not display the entire number.
      ethernet.print("<p> Wireless transmitter and IP camera are turned off if battery is low. </p>");
      ethernet.print("</font>");

      ethernet.respond();

    }
  }
}

lochienichols:
Hi all,

I am having a bit of a dilemma here.... This sketch I have attached is meant to print the battery voltage measured on analog0 to a webpage. The issue is it will not add any of the decimal places, I am using the "float command" but still not working. When the code is set to dump the voltage to serial it prints it perfectly to 2 decimal places. Why isn't this working? How should I go about fixing it?

the problem is the ETHER_28J60.h ethernet library only has these two print methods:

	void print(char* text); 	// append the text to the response
	void print(int value);  	// append the number to the

So, it is converting the float to an int.

You need to create a character array, print your float into that buffer, then call ethernet.print(buffer). It will call the first of these two methods.

something like (untested):

....
      char buffer[10];
      sprintf(buffer, "%.2f", voltage);
      ethernet.print(buffer);
....

Thankyou, I will test this out now :slight_smile:

Okay so I have tried with your suggestion, now on the webpage I am left with a question mark where the voltage should be?! What's the next step?

serial.print(buffer); and see what it says.

you could try ethernet.print("4.92") to prove the concept as well.

Okay so,

serial.print(buffer); - this is printing question marks
Ethernet.print("4.92"); - that is printing to the webpage fine

Thankyou for the quick replies

so this line is the problem:

      sprintf(buffer, "%.2f", voltage);

Ahhhh.

float format specifiers are not included in the Arduino libraries for sprintf.

The answer is to do it this way apparently:

So,

....
      char buffer[10];
      dtostrf(voltage, 6, 2, buffer);
      ethernet.print(buffer);
....