Nano'da JSON yükü oluştururken snprintf float yerine ? yazdırıyor
Yapay zekâ tarafından English dilinden çevrildi ·
Genellikle ARM/ESP32 ile çalışıyorum, bu yüzden AVR tarafı beni şaşırttı. SHT31 ve W5500 Ethernet modülü bağlı Nano (ATmega328P), PubSubClient ile Hermetry’ye yayın yapıyor. Yükü her zamanki gibi oluşturuyorum:
char buf[96];
snprintf(buf, sizeof(buf), "{"temp_c":%.2f,"rh":%.1f,"seq":%lu}", t, h, seq);
mqtt.publish("v1/telemetry", buf);
Seri port {"temp_c":?,"rh":?,"seq":1043} çıktısını gösteriyor ve Hermetry bundan hiçbir şey kaydetmiyor; bu mantıklı, çünkü bu JSON değil. 2 KB RAM’li bir çipte String birleştirmesi kullanmak istemiyorum. Bunun için yaygın yöntem nedir?
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?
Yapay zekâ tarafından English dilinden çevrildi ·
Bu IDE’deki bir printf ayarı mı? Bir yerde float için bir bayrak olduğunu düşünüyorum
is it a printf setting in the IDE? I think there’s a flag for floats somewhere
Yapay zekâ tarafından English dilinden çevrildi ·
Liam’in yarısı haklı. avr-libc’nin printf ailesi, flash tasarrufu için varsayılan olarak float desteği olmadan derlenir, bu yüzden %f ? yazdırır. Float sürümünü bağlayabilirsiniz (-Wl,-u,vfprintf -lprintf_flt) ancak bu yaklaşık 1.5 KB flash maliyeti getirir ve Arduino IDE bayrakları ayarlamayı zahmetli hale getirir.
Nano’larda yaptığım şey, önce floatları dtostrf ile dönüştürüp ardından dizeleri biçimlendirmek:
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);
Ya da tam sayı gönderin: temp_c10 değerini 2154 olarak gönderip sonra ölçekleyin. Aynı kota, sıfır float biçimlendirme. Ayrıca SHT31 okuması başarısız olduğunda NaN kontrolü yapın: dtostrf nan yazdırır, bu geçerli JSON değildir ve tüm mesaj ham metin olarak kaydedilir. Okuma başarısız olduğunda anahtarı atlayın.
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.
Yapay zekâ tarafından English dilinden çevrildi ·
dtostrf bu. Artık geçerli JSON, anahtarlar görünüyor. NaN sorunu on dakika sonra başıma geldi, test için sensörü çıkardığımda. Yani iyi bir tavsiyeymiş.
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.
Yapay zekâ tarafından English dilinden çevrildi ·
Oh, harika, dtostrf’den hiç haberim yoktu
oh nice, didn’t know about dtostrf at all
- Bu konuyu yanıtlamak için giriş yapmış olmalısınız.
Sohbete katılın
Soru sormak ve yanıtlamak, yararlı gönderileri beğenmek ve sorununuzu çözen yanıtı işaretlemek için oturum açın. Her katkı puan ve seviye kazandırır.

