snprintf auf einem Nano gibt ? statt meines Floats aus, wenn ich die JSON-Nutzlast baue
Von English mit KI übersetzt ·
Ich arbeite meist mit ARM/ESP32, deshalb hat mich die AVR-Seite überrascht. Nano (ATmega328P) mit einem SHT31 und einem W5500-Ethernet-Modul, veröffentlicht mit PubSubClient an Hermetry. Ich baue die Nutzlast wie immer:
char buf[96];
snprintf(buf, sizeof(buf), "{"temp_c":%.2f,"rh":%.1f,"seq":%lu}", t, h, seq);
mqtt.publish("v1/telemetry", buf);
Die serielle Ausgabe zeigt {"temp_c":?,"rh":?,"seq":1043}, und Hermetry speichert nichts davon, was logisch ist, denn das ist kein JSON. Ich möchte auf einem Chip mit 2 KB RAM keine String-Verkettung einbinden. Was ist der übliche Weg?
Mostly do ARM/ESP32, so the AVR side surprised me. Nano (ATmega328P) with an SHT31 and a W5500 Ethernet module, publishing to Hermetry with PubSubClient. I build the payload like I always do:
char buf[96];
snprintf(buf, sizeof(buf), "{"temp_c":%.2f,"rh":%.1f,"seq":%lu}", t, h, seq);
mqtt.publish("v1/telemetry", buf);
Serial shows {"temp_c":?,"rh":?,"seq":1043}, and Hermetry doesn’t store anything from it, which makes sense because that’s not JSON. I don’t want to pull String concatenation in on a 2 KB RAM chip. What’s the usual way?
Von English mit KI übersetzt ·
Ist das eine printf-Einstellung in der IDE? Ich glaube, irgendwo gibt es ein Flag für Floats
is it a printf setting in the IDE? I think there’s a flag for floats somewhere
Von English mit KI übersetzt ·
Liam hat halb recht. Die printf-Familie von avr-libc wird standardmäßig ohne Float-Unterstützung gebaut, um Flash zu sparen, daher gibt %f ? aus. Man kann die Float-Version einbinden (-Wl,-u,vfprintf -lprintf_flt), das kostet aber etwa 1,5 KB Flash, und die Arduino-IDE macht die Flags lästig.
Auf Nanos wandle ich die Floats zuerst mit dtostrf um und formatiere dann Strings:
char ts[8], hs[8], buf[96];
dtostrf(t, 0, 2, ts); // width 0 = no padding
dtostrf(h, 0, 1, hs);
snprintf(buf, sizeof(buf), "{"temp_c":%s,"rh":%s,"seq":%lu}", ts, hs, seq);
Oder sende Ganzzahlen: temp_c10 als 2154 und skaliere es später zurück. Gleiches Kontingent, keine Float-Formatierung. Prüfe außerdem auf NaN, wenn das Lesen des SHT31 fehlschlägt: dtostrf gibt nan aus, das ist kein gültiges JSON, und die ganze Nachricht wird als Rohtext gespeichert. Überspringe den Schlüssel, wenn das Lesen fehlschlägt.
Liam’s half right. avr-libc’s printf family is built without float support by default to save flash, so %f prints ?. You can link the float version (-Wl,-u,vfprintf -lprintf_flt) but it costs about 1.5 KB of flash and the Arduino IDE makes the flags annoying.
What I do on Nanos is convert the floats with dtostrf first, then format strings:
char ts[8], hs[8], buf[96];
dtostrf(t, 0, 2, ts); // width 0 = no padding
dtostrf(h, 0, 1, hs);
snprintf(buf, sizeof(buf), "{"temp_c":%s,"rh":%s,"seq":%lu}", ts, hs, seq);
Or send integers: temp_c10 as 2154 and scale it back later. Same quota, zero float formatting. Also check for NaN when the SHT31 read fails: dtostrf prints nan, which isn’t valid JSON, and the whole message gets stored as raw text. Skip the key when the read fails.
Von English mit KI übersetzt ·
Dann dtostrf. Jetzt gültiges JSON, die Schlüssel erscheinen. Der NaN-Punkt hat mich zehn Minuten später erwischt, als ich den Sensor zum Testen abgezogen habe, also guter Tipp.
dtostrf it is. Valid JSON now, keys showing up. The NaN point bit me ten minutes later when I unplugged the sensor to test, so good call.
Von English mit KI übersetzt ·
Oh, super, von dtostrf hatte ich überhaupt keine Ahnung
oh nice, didn’t know about dtostrf at all
- Du musst angemeldet sein, um auf dieses Thema antworten zu können.
Mitdiskutieren
Melde dich an, um Fragen zu stellen und zu beantworten, hilfreiche Beiträge mit „Gefällt mir“ zu markieren und die Antwort zu kennzeichnen, die dein Problem gelöst hat. Jeder Beitrag bringt Punkte und Stufen.

