Created
September 17, 2025 03:50
-
-
Save esmarr58/5f03a1419644363535022c9a4488e8d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void MainWindow::onSerialLineaRecibida(const QString &linea) | |
| { | |
| // Ejemplo de respuesta: {"ok":true,"temp":25.5,"humedad":65.8} | |
| const QByteArray bytes = linea.toUtf8(); | |
| QJsonParseError perr; | |
| const QJsonDocument doc = QJsonDocument::fromJson(bytes, &perr); | |
| if (perr.error != QJsonParseError::NoError || !doc.isObject()) { | |
| qWarning() << "JSON inválido:" << perr.errorString() << "en" << linea; | |
| return; | |
| } | |
| const QJsonObject obj = doc.object(); | |
| // (Opcional) valida "ok" | |
| if (obj.contains("ok") && obj.value("ok").isBool() && !obj.value("ok").toBool()) { | |
| qWarning() << "Mensaje con ok=false"; | |
| return; | |
| } | |
| // Lee temp y humedad si existen | |
| if (obj.contains("temp") && obj.value("temp").isDouble()) { | |
| const double t = obj.value("temp").toDouble(); | |
| // Muestra con 1 decimal (QLCDNumber acepta QString para controlar decimales) | |
| ui->displayTemperatura->display(QString::number(t, 'f', 1)); | |
| } | |
| if (obj.contains("humedad") && obj.value("humedad").isDouble()) { | |
| const double h = obj.value("humedad").toDouble(); | |
| ui->displayHumedad->display(QString::number(h, 'f', 1)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment