Go Down

Topic: itoa function with dot in (Read 548 times) previous topic - next topic

smallpoul

Hi again,
             i finally got my sketche working with the enc28j60. Now i'm able to send to my domotic box the temperature of a one-wire sensor. The only problem that i have is it dont take in concideration the . and the to number next to it so if temperature is 21.56 only 21 will be sent.
Here the code:
Code: [Select]
void loop () {
  ether.packetLoop(ether.packetReceive());
  if (millis() > timer) {
    timer = millis() + 10000;
    sensors.requestTemperatures();
    Serial.println(sensors.getTempCByIndex(0));
    Serial.println();
    Serial.println("<<< REQ ");
    char myIntAsString[24]; // 7 bytes is enough to contain any int, including minus sign and terminating zero
    itoa((sensors.getTempCByIndex(0)), myIntAsString, 10);
    Serial.print(myIntAsString);
    ether.browseUrl(PSTR("/data_request?id=variableset&DeviceNum=38&serviceId=urn:upnp-org:serviceId:TemperatureSensor1&Variable=CurrentTemperature&Value="), myIntAsString, website, my_callback);
}
}


Here the result in the serial print:
Code: [Select]
P:  192.168.2.124
GW:  192.168.2.1
DNS: 192.168.2.1
SRV: 192.168.2.19
19.25

<<< REQ
19>>>
HTTP/1.1 200 OK
content-Type: text

...


So before convert it to a compatible const char*, sensor read 19.25, but it send 19.

Any idea
Thanks


Delta_G

itoa stands for interred to ASCII.  Interfere don't have decimal places and that function isn't going to work for a number that isn't an interger.

Look at the function dtostrf().  That might get you what you want.
Ad hoc, ad loc, and quid pro quo.  So little time - so much to know!  ~Jeremy Hillary Boob Ph.D

AWOL

Quote
itoa stands for interred to ASCII.
Let's bury this one - "Integer To ASCII"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

smallpoul

got it thanks

Code: [Select]
  ether.packetLoop(ether.packetReceive());
  if (millis() > timer) {
    timer = millis() + 5000;
    sensors.requestTemperatures();
    Serial.println();
    Serial.println("<<< REQ ");
    float sensor1 = (sensors.getTempCByIndex(0));
    char buffer[10];
    dtostrf(sensor1, 5, 2, buffer);
    Serial.println(buffer);
    delay(500);
    ether.browseUrl(PSTR("/data_request?id=variableset&DeviceNum=38&serviceId=urn:upnp-org:serviceId:TemperatureSensor1&Variable=CurrentTemperature&Value="), buffer, website, my_callback);


I need to put a delay before sending the url cause it was giving me sometime some bad number.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy