Как преобразовать значение с плавающей запятой в строку без использования 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&)'

person Ciasto piekarz    schedule 07.03.2017    source источник
comment
См. stackoverflow.com/questions/1123201/convert-double- to-string-c. Перепутал первоначальный вопрос. Так что предыдущая ссылка оказалась бесполезной.   -  person MABVT    schedule 08.03.2017


Ответы (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

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

person Morgoth    schedule 08.03.2017
comment
вы хотите сказать, что мне просто нужно #include stdli.h" и использовать строковый модуль - person Ciasto piekarz; 08.03.2017
comment
Если вы не удалили загрузчик Arduino, вам не нужно включать stdlib.h. String() уже доступен на Yún. - person Morgoth; 08.03.2017
comment
@Morgoth, возможно, стоит упомянуть, что в случае, если пользователю нужно const char* на лету, можно использовать strvar.c_str() вместо создания char[] и т.д. - person Patrick Trentin; 08.03.2017