Having some problems with basic C++ / Arduino typecasting… I think.
I’m playing with the Nightshade energyShield and trying to Spark.publish() a sprintf’d JSON string but the numbers are getting messed up, and I can’t figure out whether it’s the Wire() not working or if it is a sprintf typecasting problem.
Variables in question:
- (int) Voltage in mW
- (int) Current in mA
- (int) Percent of charge in 0.5% increments
- (int) Temperature in 0.125 C increments
When I Serial.print(variable)
any of these, they produce some reasonable looking numbers:
- Voltage (mV) 4079 mV
- Current(mA) -2 mA
- Charge (%) 90.0 %
- Temperature © 25.37 C
- Adapter Voltage (mV) 3079 mV
But after a sprintf()
into this:
sprintf(publishString,"{\"mV\": %i, \"mA\": %i, \"percent\": %3.3f, \"temp\": %3.3f, \"Vadp\": %i}",Voltage,Current,Percent,Temperature,AdapterVoltage);
… and Spark.publish(), I get something like this:
{"data":"{\"mV\": 9993, \"mA\": 5118, \"percent\": 127.500, \"temp\": 255.875, \"","ttl":"60","published_at":"2014-11-17T04:42:21.680Z","coreid":"xxxxx"}
Code examples are more or less this test script and the .cpp and .h files for the energyShield. Does this look like a typecasting problem or am I screwing something else up?