0.前言
1.准备工作
-
开发板型号: MYD-YA157C,512MB DDR3,4GB eMMC -
主控芯片: STM32MP157AAC3 -
光盘资料版本: MYD-YA157C-20191225.iso
-
12v电源适配器 -
USB-TTL模块:115200/8/1/无 -
网线
-
开发板Linux版本:Linux 4.19.9 -
开发板IP:192.168.1.136 -
主机配置:Ubuntu 16.04 -
主机IP:192.168.1.111
2.数据接口的获取
#
包含最新疫情数据、各省市最新数据、其他国家最新数据
https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5
#
包含历史数据
https://view.inews.qq.com/g2/getOnsInfo?name=disease_other
#
最新的辟谣信息
https://vp.fact.qq.com/loadmore?page=0
#
辟谣信息详情
https://vp.fact.qq.com/miniArtData?id=a2141851348ee5f3772c761e25bb57d7
https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5
chinaTotal
和
chinaAdd
这两组数据。
{
"ret":
0,
"data": {
"lastUpdateTime":
"2020-03-04 11:12:04",
"chinaTotal": {
"confirm":
80422,
"heal":
49914,
"dead":
2984,
"nowConfirm":
27524,
"suspect":
520,
"nowSevere":
6416
},
"chinaAdd": {
"confirm":
120,
"heal":
2654,
"dead":
38,
"nowConfirm":
-2572,
"suspect":
-67,
"nowSevere":
-390
},
...........其他数据.............
"isShowAdd":
true
}
}
3.Qt界面的实现
3.1 JSON数据的解析
//打开保存的JSON数据文件,并调用解析函数
void Dialog::parseData(QString filename)
{
QFile file(filename);
if(!file.open(QIODevice::ReadOnly))
{
qDebug() <<
"file open failed";
return;
}
QByteArray allData = file.readAll();
file.close();
// qDebug() << allData;
getData(allData);
file.remove();
//删除文件
return;
}
//把数据解析出来并显示在标签上
void Dialog::getData(QByteArray str)
{
cJSON *ret_obj;
cJSON *root_obj;
root_obj = cJSON_Parse(str);
//创建JSON解析对象,返回JSON格式是否正确
if (!root_obj)
{
disInfo(
"JSON format error");
qDebug() <<
"json format error";
}
else
{
disInfo(
"json format ok");
qDebug() <<
"json format ok";
ret_obj = cJSON_GetObjectItem(root_obj,
"ret");
if(cJSON_IsNumber(ret_obj))
{
int ret =
1;
ret = ret_obj->valueint;
// qDebug() << ret_obj->valueint;
}
char *data_str = cJSON_GetObjectItem(root_obj,
"data")->valuestring;
cJSON *data_obj = cJSON_Parse(data_str);
if(!data_obj)
{
qDebug() <<
"data json err";
cnt_error++;
QString error =
"err:" + QString::number(cnt_error);
ui->lbe_error->setText(error);
}
else
{
qDebug() <<
"data json ok";
char *lastUpdateTime = cJSON_GetObjectItem(data_obj,
"lastUpdateTime")->valuestring;
qDebug() << lastUpdateTime;
ui->lbe_update_time->setText(lastUpdateTime);
cJSON *chinaTotal_obj = cJSON_GetObjectItem(data_obj,
"chinaTotal");
int chinaTotal_confirm = cJSON_GetObjectItem(chinaTotal_obj,
"confirm")->valueint;
int chinaTotal_heal = cJSON_GetObjectItem(chinaTotal_obj,
"heal")->valueint;
int chinaTotal_dead = cJSON_GetObjectItem(chinaTotal_obj,
"dead")->valueint;
int chinaTotal_nowConfirm = cJSON_GetObjectItem(chinaTotal_obj,
"nowConfirm")->valueint;
int chinaTotal_suspect = cJSON_GetObjectItem(chinaTotal_obj,
"suspect")->valueint;
int chinaTotal_nowSevere = cJSON_GetObjectItem(chinaTotal_obj,
"nowSevere")->valueint;
ui->lbe_total_confirm->setNum(chinaTotal_confirm);
ui->lbe_total_heal->setNum(chinaTotal_heal);
ui->lbe_total_dead->setNum(chinaTotal_dead);
ui->lbe_total_nowConfirm->setNum(chinaTotal_nowConfirm);
ui->lbe_total_suspect->setNum(chinaTotal_suspect);
ui->lbe_total_nowSevere->setNum(chinaTotal_nowSevere);
cJSON *chinaAdd_obj = cJSON_GetObjectItem(data_obj,
"chinaAdd");
int chinaAdd_confirm = cJSON_GetObjectItem(chinaAdd_obj,
"confirm")->valueint;
int chinaAdd_heal = cJSON_GetObjectItem(chinaAdd_obj,
"heal")->valueint;
int chinaAdd_dead = cJSON_GetObjectItem(chinaAdd_obj,
"dead")->valueint;
int chinaAdd_nowConfirm = cJSON_GetObjectItem(chinaAdd_obj,
"nowConfirm")->valueint;
int chinaAdd_suspect = cJSON_GetObjectItem(chinaAdd_obj,
"suspect")->valueint;
int chinaAdd_nowSevere = cJSON_GetObjectItem(chinaAdd_obj,
"nowSevere")->valueint;
lbeDisplay(ui->lbe_add_confirm, chinaAdd_confirm);
lbeDisplay(ui->lbe_add_heal, chinaAdd_heal);
lbeDisplay(ui->lbe_add_dead, chinaAdd_dead);
lbeDisplay(ui->lbe_add_nowConfirm, chinaAdd_nowConfirm);
lbeDisplay(ui->lbe_add_suspect, chinaAdd_suspect);
lbeDisplay(ui->lbe_add_nowSevere, chinaAdd_nowSevere);
}
// cJSON_Delete(ret_obj);
// cJSON_Delete(data_obj);
cJSON_Delete(root_obj);
//释放内存
disInfo(
"更新完成");
cnt_success++;
QString success =
"ok:" + QString::number(cnt_success);
ui->lbe_success->setText(success);
}
}
//数据的显示
void Dialog::lbeDisplay(QLabel *lbe,
int num)
{
if(num >
0)
lbe->setText(
"+" + QString::number(num));
else
lbe->setText(QString::number(num));
}
3.2 获取本地IP地址
//forexample:192.168.1.111
QString Dialog::GetLocalmachineIP()
{
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
for(QHostAddress &addr : ipAddressesList)
{
// 找到不是本地ip,并且是ipv4协议,并且不是169开头的第一个地址
if(addr != QHostAddress::LocalHost && addr.protocol() == QAbstractSocket::IPv4Protocol && !addr.toString().startsWith(
"169"))
{
ipAddress = addr.toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
return ipAddress;
}
4.在开发板上运行Qt程序
#
使用网线把开发板连接上路由器
#
使用udhcpc自动获取IP地址
udhcpc
#
查看获取到的ip地址
ifconfig
#
确认连接到互联网
ping www.baidu.com
#
如果有回复数据,说明已经成功连接上互联网
#
使用scp命令或共享目录的方式把可执行文件传输到开发板上
scp qte_2019_ncov root@192.168.1.109:/home/root
#
执行程序
./qte_2019_ncov
5.使用无线模块联网
#
关闭eth0
ifconfig eth0 down
#
启用wlan0
rfkill unblock wifi
ifconfig wlan0 up
#
在当前文件夹生成WiFi配置文件
wpa_passphrase "M6_Note" "qwert125" > wifi.conf
#
查看生成的WiFi配置信息
cat wifi.conf
#
加载WiFi配置文件
wpa_supplicant -B -c wifi.conf -i wlan0
#
扫描附近的WiFi信息
iw dev wlan0 scan | grep SSID
#
自动获取IP地址
udhcpc -i wlan0
#
设置DNS
echo "nameserver 114.114.114.114" > /etc/resolv.conf
#
连接互联网
iw wlan0 link
#
测试网络连接
ping www.wangchaochao.top
#
!/bin/bash
WF_SSID="M6_Note"
WF_PASSWORD="qwert125"
#
关闭eth0
ifconfig eth0 down
#
使能wlan0
rfkill unblock wifi
ifconfig wlan0 up
#
输出WiFi信息
echo "WiFi_SSID:$WF_SSID"
echo "WiFi_PASSWORD:$WF_PASSWORD"
#
在当前文件夹生成WiFi配置信息
wpa_passphrase $WF_SSID $WF_PASSWORD > $WF_SSID.conf
#
加载WiFi配置文件
wpa_supplicant -B -c /home/root/$WF_SSID.conf -i wlan0
#
扫描附近的WiFi
iw dev wlan0 scan | grep SSID
#
自动获取IP地址
udhcpc -i wlan0
#
配置DNS
echo "nameserver 114.114.114.114" > /etc/resolv.conf
#
连接网络
iw wlan0 link
echo "WiFi连接成功"
6.代码下载
#
Github
https://github.com/whik/qte_2019_ncov
#
Gitee
https://gitee.com/whik/qte_2019_ncov
开发板详情:
购买链接:
https://m.tb.cn/h.VV4FqAE?sm=57599d
系列教程
-
我的博客: www.wangchaochao.top -
我的公众号: mcu149
本文分享自微信公众号 - 电子电路开发学习(mcu149)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
来源:oschina
链接:https://my.oschina.net/u/4684806/blog/4722707