Pages: [1]   Go Down
Author Topic: itoa function with dot in  (Read 319 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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

 
Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 651
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
itoa stands for interred to ASCII.
Let's bury this one - "Integer To ASCII"
Logged

"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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

got it thanks

Code:
  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.
Logged

Pages: [1]   Go Up
Jump to: