Hi, looking for extensive type manipulation in Spark language


For christmas, my lovely girlfriend gave me a spark core and some others pieces to make an awesome autonomous bot.
The goal is to have it controled by my tel and a java IA app.

For now he is controlled by the user, and i try to bring back some sensors data to display it on the smartphone.
I use UDP as communication protocol, it work fine, the command are reactiv and all, but i have a probleme with the sensors reading.

I think it’s something with the conversion from float to unsigned char[] to make it go in an UDP packet, i’m a Java coder and not used to this kind of low level issues, plus i have never did C

Here is my reading code

Udp.read(recbuf, pp >= 7 ? 7 : pp);
String mess = "";
for (int i = 0; i < 7; i++) {
    mess += recbuf[i];
//Serial.println(mess + " <= boffer ");
// Ignore other chars
//Store sender ip and port
IPAddress ipAddress = Udp.remoteIP();
int port = Udp.remotePort();
        int reading;
        double voltage;
        char sep[] = {':'};
        strcpy(sendbuff, sep);
        reading = analogRead(axeX);
        voltage = (reading * 3.3) / 4095;
        float axeXvalue = (voltage /0.55) -3;
        char axeXvalueB[8]; //= static_cast<unsigned char*>(axeZvalue);
        snprintf(axeXvalueB, sizeof(axeXvalueB), "%g", axeXvalue);
        strcat(sendbuff, axeXvalueB);
        strcat(sendbuff, sep);
        reading = analogRead(axeY);
        voltage = (reading * 3.3) / 4095;
        float axeYvalue = (voltage /0.55) -3;
        char axeYvalueB[8];
        snprintf(axeYvalueB, sizeof(axeYvalueB), "%g", axeYvalue);
        strcat(sendbuff, axeYvalueB);
        strcat(sendbuff, sep);
        reading = analogRead(axeZ);
        voltage = (reading * 3.3) / 4095;
        float axeZvalue = (voltage /0.55) -3;
        char axeZvalueB[8];
        snprintf(axeZvalueB, sizeof(axeZvalueB), "%g", axeZvalue);
        strcat(sendbuff, axeZvalueB);
        //strcat(sendbuff, sep);
        //float irValue = lectureTension();
        //char irValueB[4];// = static_cast<unsigned char*>(irValue);
        //snprintf(irValueB, sizeof(irValueB), "%f", irValue);
        //dtostrf(irValueB, sizeof(irValueB), 3, irValue)
        //strcat(sendbuff, irValueB);
        // Echo back data to sender
        Udp.beginPacket(ipAddress, port);
        Udp.write(reinterpret_cast<unsigned char*>(sendbuff), sizeof(sendbuff));
        // Echo back data to sender
        //Udp.beginPacket(ipAddress, port);
        //Udp.write(recbuf, 7);

As you can see it, the process is complicated, and possibly uselessly complicated. I did it from diverse post i found all over the net, but it’s complicated to sort all the response since Spark is not exactly C, neither Arduino.

It’s over complicated, and it don’t work as intended. The reading are from an accelerometer, i’m trying to get the value in G, wich i can see by publishing it in the cloud (it look like 0.0012234) but on the smartphone the char[] contains “:0i0:1-0:0O0:” for example where it should be “:0.001234:0.012332:0.002121” for example

So i need your help, can you point me a complete tutorial working tutorial to solve my problem, or at least an hint on where to search would be cool.

If you want i can post more informations about my project. I planned to do a complete explanation with Spark and android code, and the schematics if you think someone can be interested.

Cordialy :slight_smile:

@arfost, I would simplify by taking all your readings first and then doing a single sprintf() into a char array buffer large enough to hold your entire UPD packet. The sprintf command uses the same syntax as printf but the “output” is to a char array. So you can do the formatted output and your separators in a single print. :smile:

Hi @arfost

I don’t think you wanted sizeof() in your formatting since that returns the number of bytes used to store argument which is not necessarily related to the number of digits you wanted to print. As @peekay123 says, I think one sprintf will likely be all you need here:

  sprintf(sendbuff, ":%8.6f:%8.6f:%8.6f", axeXvalue, axeYvalue, axeZvalue);

This prints each value with a leading colon, then 8-character spaces with 6-digits after the decimal point. You can play with the format directives to get it exactly how you want it.

1 Like


We had a big issue at work so i didn’t find the time to test your solution before today.

It work perfectly, thanks for your help :slight_smile:

I’m now generating statistics value to begin to work on the IA, the fun is beginning thanks to you.

1 Like

So when i’ll finally succed in creating skynet, you’ll be at the beginning of it :slight_smile: