How to combine 2 numbers into a 7 digit number seperated by a point?

I created the following code to read the value from a feed of the emoncms in this case "power-kwh":

void readFeed(byte feedid)
{
  if (client.connect(webserver,80))
  {
    client.print("GET /emoncms/feed/value.json?id=");
    client.println(feedid);
    client.println();
    if(client.find("\""))
    {
      number1 = client.parseInt();
      if(client.find("."))
      {
        number2 = client.parseInt();
      }  
      client.stop();
    }
    Serial.print(number1);
    Serial.print(".")
    Serial.println(number2);
  }
  else
  {
    Serial.println("Connection failed");
    Serial.println("Close connection...");
    client.stop();
  }
}

then to update the emoncms with a pulse count for example (but it can also be used to add other values then pulse counting) I created:

void emon_status(char *device, byte status)
{
  if (client.connect(webserver,80))
  {
    client.print("GET /emoncms/input/post.json?apikey=xxxxxxxxxxxxxxxxxxx&json={");
    client.print(device);
    client.print(":");
    client.print(status);
    client.println("} HTTP/1.1");
    client.println("Host: someserver.net");
    client.println("User-Agent: Arduino-ethernet");
    client.println("Connection: close");
    client.println();
    /*Serial.print(device);
    Serial.print(" = ");
    Serial.println(status);*/
    client.stop();
  }
  else
  {
    Serial.println("Connection failed");
    Serial.println("Close connection...");
    client.stop();
  }
}

in order to read the feed value and send the value back to the emoncms I can use for example:

readFeed(17);
if (number1 > 0)
{
  emon_stat("power-kwh",2000);
}

So basicly it reads an existing value from the emoncms first, and then I want to use the existing value and add a number to it..

So what I want to do instead of using "2000" in the above example, is combine: number1 + number2 and add 0.001 => to add 1 pulse change to it...

to create the value (number1.number2+0.001)

but that doesn't work offcourse..

how can I combine number1 and number2 in a point seperated value and add 0.001 to it, to send it back?

Is the resulting data type supposed to be a floating point number or a string?

thanx

@DeltaG; So in my case a 3 digit number behind the point would be:

number3 = (number1 + (number2/1000) + 0.001);

and then:

readFeed(17);
if (number1 > 0)
{
  emon_stat("power-kwh",number3);
}

@SirNickity: yeah that would be my second question, if I declare number3 as a floating point, it produces a number with only 2 digits behind the point

how do I declare number3? double is not working either...

Hi there

if I declare number 3 as a floating point, it produces a number with only 2 digits behind the point

Do you mean that something like the following only prints two decimal places?

Serial.println(number3);

If so, try changing it to:

Serial.println(number3, 4);

More information here: Serial.print() - Arduino Reference

Regards

Ray

Serial.println(number3, 3);

prints the number with 3 digits behind it, but it doesn't send the number to the emoncms

for example number1 = 2000
number 2 = 123

then:

number3 = (number1 + (number2/1000) + 0.001);

should produce 2000.124

it does print this 1 time with Serial.print

but doesn't send anything to the emoncms, and the next time it Serial.prints the number when there was a pulse count, it stil is 2000.124 instead of 2000.125 and 2000.126 etc.. it doesn't count up.

it doesn't send the data to emoncms, probably because it is declared with a floating point, wich is apparently not correct

it reads 2000.123 from the feed, and the next time the number is still 200.123 what it reads from the feed, it doesn't update the data...

This is almost definitely a problem with mismatched data types.

One thing you'll find out quickly with FP numbers is that they aren't very accurate. The storage technique that holds that number in memory is susceptible to errors, which is why you will get beaten severely by any employer or instructor that catches you using floating point number to calculate money. :wink: So that might be what's going wrong when you add 0.001 to a value. It might not always end up exactly what you expect it to be. There's nothing you can do about that.

Next, when you feed this value to the print routines, function overloading will examine the data type you give it and behave accordingly. This is a C++ thing, and it's really convenient, but it can lead you to the false assumption that it always works like that. It doesn't. So, when you pass a FP number to another function, it may (probably will) get truncated to an integer (goodbye to the decimal point and everything after it) unless the function specifically deals with FP input.

So how to get around this? One, you can work in what's called "fixed point decimal", which basically amounts us to scaling up your unit of measure such that the 1s place in an integer value equates to some other place of significance other than one. One of the most common examples is dealing with money in cents. One dollar would be 100 cents in fixed-point decimal math. Ten dollars would be 1000 cents. So on and so forth. So, to have a number with 3 significant digits beyond the decimal, just multiply all your numbers by 1000 and then just add them together like any other integer. Beware though, you'll be closer to the maximum value that can be held in small integers, so you might want to stick to uint32_t or unsigned long as a data type. (And of course, any function you use has to be built to handle these large types as well. No free lunch.)

Forgot to point out that your status() function takes a device name (char array, aka string) and a BYTE value (otherwise known as char, or uint8_t, or an 8-bit unsigned integer). That means, when you pass a floating point value to it, it will not only be truncated to an integer (2000.123 would end up as 2000), but you're limited to a number that can be stored in 8 bits, which tops out at 255. When you truncate 2000.123 to an 8-bit integer, you get 208 (the lower 8 bits of the 16-bit integer encoding for 2000, which is binary 000 0111 1101 0000.)

For that to work, buffer[ ] should have 9 elements.

What if b is 56? Should the string be "1234.56" or should it be "1234.056"?