Gestire le letture non riuscite
Rilevi NaN e riprovi senza causare arresti anomali.
Gestire le letture non riuscite è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Arduino & IoT Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Arduino & IoT Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Reads Can Fail
Even a good sensor sometimes returns nothing useful. Loose wires, fast polling, or noise can cause a failed read, so plan for it. ⚠️
Meet NaN
When a read fails, the library returns NaN, which means Not a Number. It is the sensor's way of saying I did not get a value.
Why NaN Is Tricky
A NaN never equals anything, not even itself. So you cannot just compare it with a normal number to catch the error.
Detect It with isnan
Use the built-in isnan() function to test a reading. It returns true when the value is the failed NaN result.
if (isnan(t)) {
}Check Before You Use
Always test a reading before you print or act on it. Skipping this check lets bad data slip into your logic unnoticed.
Check Both Values
Guard temperature and humidity together, since either can fail. If one is NaN, treat the whole reading as bad and skip it.
if (isnan(t) || isnan(h)) {
return;
}Print a Warning
When a read fails, print a clear message instead of a value. Future you will thank present you for the helpful note.
Serial.println("Failed to read DHT!");Retry, Do Not Crash
A single failure is normal, so do not stop the program. Simply retry on the next loop and the reading usually succeeds.
Keep the Last Good Value
For a steady display, store the last good reading and reuse it when a read fails. Your screen never shows an ugly blank.
Slow Down to Fix Failures
If failures pile up, you may be reading too fast or your wiring is loose. A longer delay and a firm connection often cure it.
Robust by Design
Handling errors gracefully is what separates a toy from a real device. Your sensor now survives the occasional hiccup calmly.
Quick Check
A DHT read fails. How should your sketch detect that in code?
Recap
You learned that failed reads return NaN, caught them with isnan, warned instead of crashing, and kept your device steady. 🎉
Domande Frequenti
La lezione «Gestire le letture non riuscite» è gratuita?
Sì — il testo completo di «Gestire le letture non riuscite» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Arduino & IoT Academy, passa a CoddyKit PRO. Il corso Arduino & IoT Academy include 4 lezioni in totale.
Cosa imparerò in «Gestire le letture non riuscite»?
Rilevi NaN e riprovi senza causare arresti anomali. Eserciti Arduino & IoT Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Arduino & IoT Academy?
Non è richiesta alcuna esperienza precedente. Arduino & IoT Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Gestire le letture non riuscite»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Arduino & IoT Academy?
Sì. Ogni lezione Arduino & IoT Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Collegare DHT11/DHT22
- Installare la libreria DHT
- Leggere temperatura e umidità
- Gestire le letture non riuscite