2017-03-07 3 views
0

J'ai un capteur de DHT11 connecté à un bouclier yun, et je lis les données du capteur en utilisant la bibliothèque DHT:Comment convertir une valeur flottante en chaîne sans utiliser sstream pour arduino?

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; 

et je suis en train de mettre le point de rosée de données et de la température, l'humidité et l'indice de chaleur à BridgeClient clé de sorte que je puisse le lire dans un programme python qui rend le HTML et affiche en utilisant le framework wsgi bottle de Python.

Ces lignes produisent des erreurs:

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

disant:

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

Voir http://stackoverflow.com/questions/1123201/convert-double-to-string-c. Mélangé la question initiale. Donc, le lien précédent était inutile. – MABVT

Répondre

0

La méthode Bridge.put() a besoin d'un char ou une chaîne comme second paramètre. Nous pouvons donc utiliser le String constructor pour le faire.

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() 
{ 

} 

Et nous obtenons:

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

Go here à lire au sujet de la terminaison nulle.

+0

vous voulez dire que j'ai juste besoin de '#include stdli.h" 'et utiliser le module de chaînes –

+0

Si vous n'avez pas supprimé le chargeur de démarrage Arduino, vous n'avez pas besoin d'inclure' stdlib.h'.) 'est déjà disponible sur le Yún – Morgoth

+0

@Morgoth il peut être utile de mentionner que si l'utilisateur a besoin de' const char * 'à la volée, on peut utiliser' strvar.c_str() 'au lieu de créer un caractère [] 'et quoi-non –