Как преобразовать значение с плавающей запятой в строку без использования sstream для arduino?

У меня есть датчик DHT11, подключенный к экрану Yún, и я читаю данные с датчика, используя библиотеку DHT:

indoorHumidity = dhtBedRom.readHumidity();
// Read temperature as Celsius
indorTempinC = dhtBedRom.readTemperature();
// Read temperature as Fahrenheit
indorTempinF = dhtBedRom.readTemperature(true);
// Compute heat index, Must send in temp in Fahrenheit!
hi = dhtBedRom.computeHeatIndex(indorTempinF, indoorHumidity);
hIinCel = (hi + 40) / 1.8 - 40;
dP = (dewPointFast(indorTempinC, indoorHumidity));
dPF = ((dP * 9) / 5) + 32;

а затем я пытаюсь поместить данные точки росы и температуры, влажности и индекса тепла в BridgeClient ключ, чтобы я мог прочитать его в программе Python, которая отображает HTML и отображает с помощью Python bottle WSGI рамки.

Эти строки выдают ошибки:

Bridge.put(DEWPNTkey, dP);
Bridge.put(HEADINDXkey, hIinCel);

говоря:

no matching function for call to 'SerialBridgeClass::put(String&, float&)'

1 ответ

Решение

Для метода Bridge.put() в качестве второго параметра требуется символ или строка. Таким образом, мы можем использовать конструктор String для этого.

void setup()
{
  Serial.begin(115200); // To test this make sure your serial monitor's baud matches this, or change this to match your serial monitor's baud rate.

  double floatVal = 1234.2; // The value we want to convert

  // Using String()
  String arduinoString =  String(floatVal, 4); // 4 is the decimal precision

  Serial.print("String(): ");
  Serial.println(arduinoString);

  // You would use arduinoString now in your Bridge.put() method.
  // E.g. Bridge.put("Some Key", arduinoString)
  // 
  // In your case arduinoString would have been dP or hIinCel.

  // In case you need it as a char* at some point
  char strVal[arduinoString.length() + 1]; // +1 for the null terminator.
  arduinoString.toCharArray(strVal, arduinoString.length() + 1);

  Serial.print("String() to char*: ");
  Serial.println(strVal);
}

void loop()
{

}

И мы получаем:

String(): 1234.2000
String() to char*: 1234.2000

Иди сюда, чтобы прочитать о нулевом терминаторе.

Другие вопросы по тегам