781 views|4 replies

224

Posts

0

Resources
The OP
 

【STM32MP135F-DK】9-Network evaluation, using QT to display network weather [Copy link]

This post was last edited by qiao--- on 2024-1-4 18:03

We have successfully transplanted Qt before, and now we can develop the Qt interface. In this evaluation, I plan to test the network function of the board, and the main method of evaluation is to use the online API to obtain the network weather and finally display it on Qt.

1. Find an API that can get the weather online

I used the following link http://query.asilu.com/weather/baidu/ . When we enter this link, a series of JSON data will be displayed, as shown below. What we need to do is to parse the following JSON data

2. Create a Qt project

We open the qtCreator under the Linux virtual machine, and the project creation mode can be based on my previous evaluation [STM32MP135F-DK] 7-Deploy QT development environment + run the first QT program https://en.eeworld.com/bbs/thread-1267168-1-1.html .

We add network to the .pro file, which can add some network libraries.

Next, we build a simple interface. I directly drag and use the existing graphics library of qtCreator. I use the following layout. Remember to set the window size to 480*272. This has been mentioned in my previous review, so I will not repeat it here.

Then add the following code in widget.h (I show all the code here)

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtNetwork>//网络类
#include <QNetworkAccessManager>//网络管理类
//Json数据类
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots: //槽函数
    void slot_http(QNetworkReply* reply);//网络连接
    void slot_pushButton_consult();//查询按钮

private:
    Ui::Widget *ui;
    QNetworkAccessManager* http;
};
#endif // WIDGET_H

Add the following code in widget.cpp. Here we mainly use some functions to parse the JSON data.

#include "widget.h"
#include "ui_widget.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    http = new QNetworkAccessManager;
    http->get(QNetworkRequest(QUrl(QString("http://query.asilu.com/weather/baidu/?city=%1").arg(ui->comboBox_city->currentText()))));//连接天气网站
    connect(http,SIGNAL(finished(QNetworkReply*)),this,SLOT(slot_http(QNetworkReply *)));//调用槽函数

}

Widget::~Widget()
{
    delete ui;
}
void Widget::slot_http(QNetworkReply *reply)
{
    QByteArray Data = reply->readAll();
    QJsonDocument JsonDocument = QJsonDocument::fromJson(Data);
    QJsonObject JsonObject = JsonDocument.object();
    QString City = JsonObject["city"].toString();
    ui->textEdit->setText("");
    for(int i = 0;i < 7;++i)
    {
        QString date = JsonObject["weather"].toArray()[i].toObject()["date"].toString();
        QString weather = JsonObject["weather"].toArray()[i].toObject()["weather"].toString();
        QString temp = JsonObject["weather"].toArray()[i].toObject()["temp"].toString();
        QString wind = JsonObject["weather"].toArray()[i].toObject()["wind"].toString();
        ui->textEdit->append(date+"  "+weather+"  "+temp+"  "+wind);
    }
}

void Widget::slot_pushButton_consult()
{
    http->get(QNetworkRequest(QUrl(QString("http://query.asilu.com/weather/baidu/?city=%1").arg(ui->comboBox_city->currentText()))));
}


We can preview the effect on the computer before porting it to the board.

3. Compile and transplant to the board to see the effect

We use qmake && make to compile our project and copy it to our board using the scp command. When I execute my qt executable file in the terminal, the following effect appears on the screen:

I have only added the weather for four municipalities here. Friends who are interested can implement it for all cities.

Summary: The development board can accurately obtain network data by obtaining network weather through the API. The network connection is stable and the data transmission is accurate.

image.png (42.12 KB, downloads: 0)

image.png
This post is from stm32/stm8

Latest reply

It feels a bit like Windows 2000. I think the artist should design some pictures.   Details Published on 2024-1-5 15:00
 

6829

Posts

11

Resources
2
 

The development board can obtain network weather through the API and accurately obtain data from the network. The network connection is stable and the data transmission is accurate.

The posts of the big guys are awesome and are spreading everywhere!

This post is from stm32/stm8

Comments

Thanks for your support  Details Published on 2024-1-5 13:27
 
 

224

Posts

0

Resources
3
 
lugl4313820 published on 2024-1-4 21:19 The development board can obtain network weather through the API to accurately obtain network data. The network connection is stable and the data transmission is accurate. ...

Thanks for your support


This post is from stm32/stm8
 
 

6027

Posts

6

Resources
4
 

It feels a bit like Windows 2000. I think the artist should design some pictures.

This post is from stm32/stm8

Comments

That's right, it's probably because of the font display of Windows that I ported.  Details Published on 2024-1-7 21:09
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

224

Posts

0

Resources
5
 
Qintianqintian0303 posted on 2024-1-5 15:00 It feels a bit like Windows 2000. I think the artist should design a few pictures.

That's right, it's probably because of the font display of Windows that I ported.

This post is from stm32/stm8
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Featured Posts
Practical sine wave generating circuit diagram

Practical sine wave generating circuit diagram Author: Unknown Article Source: Unknown Practical sine wave gene ...

This may be a solution to the error in connecting the STM32 VCP host computer to the serial port

This problem has troubled me for several days. When using the USB HS CDC of the STM32 microcontroller, the following pro ...

[ESP32-Korvo Review] Part 2: Demo Experience + ESP-IDF Compilation Environment Installation

This post was last edited by Digital Leaf on 2021-1-27 20:53 The board doesn't even come with a data cable, so you need ...

Problems with freewheeling diodes in MOS drive motors

This post was last edited by me on 2021-3-17 11:36 As the title says, and as shown in the figure below, when using MOS ...

I would like to ask about the phase relationship of the all-pass response

This post was last edited by sunboy25 on 2021-7-4 09:44 I would like to ask why the phase relationship of the universal ...

2021 Electronics Contest Topic D Internet-based Camera Measurement System (Topic D) - Binocular Video Measurement System

This post was last edited by bqgup on 2021-11-5 19:39 1. Electronic Competition Question D 571548 571549571550 2. Objec ...

Discussion on the Reasons for the Temperature Drop of MOS Tubes Connected in Parallel

A circuit was originally designed to test the size of AC current. The design value is 5A maximum, with multiple rang ...

[Problem Feedback] Anlu TangDynasty ChipWatcher network search problem

I think that similar to this, the logic of entering keywords in the text box after Name to search for the corresponding ...

Several questions about using Altium Designer19 (I have always used PADS, please let me know if you know, thank you. I don't want to say...

The third of several questions about using Altium Designer 19 (I have been using PADS, if you know, please let me know, ...

[ADI TMC2208 Stepper Motor Evaluation Board] Part 8: Temperature Rise Test

This post was last edited by Maker_kun on 2024-9-8 21:34 Test the temperature rise data, and the heat generated by the ...

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list