commit
5e5b92d8e8
|
@ -32,8 +32,10 @@ lite_component("app") {
|
|||
# "TW203_Module_tricolor_light:module_tricolor_light_example",
|
||||
# "TW204_Module_enviroment:module_enviroment_example",
|
||||
# "TW205_Module_motor:module_motor_example",
|
||||
# "TW207_Module_gyro:module_gyro_example",
|
||||
# "TW301_Network_wifista:network_wifista_example",
|
||||
# "TW302_Network_wifiap:network_wifiap_example",
|
||||
# "TW303_Network_mqttclient:network_mqttclient_example",
|
||||
# "TW402_APP_oled_u8g2:app_oled_u8g2_example",
|
||||
]
|
||||
}
|
||||
|
|
|
@ -28,9 +28,10 @@
|
|||
| TW105 | I2C | i2c_sht30 | [I2C温湿度传感器采集](TW105_I2C_sht30/README.md) | 核心板 + 马达扩展板|
|
||||
| TW106 | UART | uart | [UART串口自发自收](TW106_UART/README.md) | 核心板 |
|
||||
| TW201 | Module | oled | [OLED显示屏驱动](TW201_Module_oled/README.md) | 核心板 + OLED扩展板|
|
||||
| TW207 | Module | gyro | [GYRO驱动](TW207_Module_gyro/README.md) | 核心板 + GYRO扩展板|
|
||||
| TW203 | Module | tricolor_light| [人体感应联动三色灯](TW203_Module_tricolor_light/README.md) | 核心板 + 人体感应板|
|
||||
| TW205 | Module | motor | [马达开发板演示](TW205_Module_motor/README.md) | 核心板 + 马达板|
|
||||
| TW207 | Module | gyro | [GYRO驱动](TW207_Module_gyro/README.md) | 核心板 + GYRO扩展板|
|
||||
| TW301 | Network | wifista | [WiFi-STA联网演示](TW301_Network_wifista/README.md) | 核心板 |
|
||||
| TW302 | Network | wifiap | [WiFi-AP热点演示](TW302_Network_wifiap/README.md) | 核心板 |
|
||||
| TW303 | Network | wifiap | [MQTT演示](TW303_Network_mqttclient/README.md) | 核心板 |
|
||||
| TW402 | APP | oled_u8g2 | [OLED U8G2库综合应用演示](TW402_APP_oled_u8g2/README.md) | 核心板 + OLED扩展板|
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021 Talkweb Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "ohos_init.h"
|
||||
#include "ohos_types.h"
|
||||
|
|
|
@ -185,4 +185,4 @@ static void I2cSht30Entry(void)
|
|||
return;
|
||||
}
|
||||
|
||||
SYS_RUN(I2cSht30Entry);
|
||||
APP_FEATURE_INIT(I2cSht30Entry);
|
||||
|
|
|
@ -88,4 +88,4 @@ static void uartEntry(void)
|
|||
return;
|
||||
}
|
||||
|
||||
SYS_RUN(uartEntry);
|
||||
APP_FEATURE_INIT(uartEntry);
|
||||
|
|
|
@ -303,7 +303,7 @@ static void oledDemoTask(void)
|
|||
osThreadTerminate(g_oledTaskID);
|
||||
}
|
||||
|
||||
static void oledDemoEntry(void)
|
||||
static void OLEDDemoEntry(void)
|
||||
{
|
||||
osThreadAttr_t attr;
|
||||
attr.name = "oledDemoTask";
|
||||
|
@ -321,5 +321,5 @@ static void oledDemoEntry(void)
|
|||
return;
|
||||
}
|
||||
|
||||
SYS_RUN(oledDemoEntry);
|
||||
APP_FEATURE_INIT(OLEDDemoEntry);
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# Copyright (c) 2021 Talkweb Information System Co.,ltd.
|
||||
# Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
|
@ -110,4 +110,4 @@ static void CGsensorExampleEntry(void)
|
|||
}
|
||||
}
|
||||
|
||||
SYS_RUN(CGsensorExampleEntry);
|
||||
APP_FEATURE_INIT(CGsensorExampleEntry);
|
||||
|
|
|
@ -1,46 +1,3 @@
|
|||
# 灯光控制和蜂鸣器使用
|
||||
# 温湿度和烟感
|
||||
|
||||
## 概述
|
||||
I2C(Inter Integrated Circuit)总线是由Philips公司开发的一种简单、双向二线制同步串行总线。
|
||||
I2C以主从方式工作,通常有一个主设备和一个或者多个从设备,主从设备通过SDA(SerialData)串行数据线以及SCL(SerialClock)串行时钟线两根线相连,如图1 所示。
|
||||
I2C数据的传输必须以一个起始信号作为开始条件,以一个结束信号作为传输的停止条件。数据传输以字节为单位,高位在前,逐个bit进行传输。
|
||||
I2C总线上的每一个设备都可以作为主设备或者从设备,而且每一个设备都会对应一个唯一的地址,当主设备需要和某一个从设备通信时,通过广播的方式,将从设备地址写到总线上,如果某个从设备符合此地址,将会发出应答信号,建立传输。
|
||||
I2C接口定义了完成I2C传输的通用方法集合,包括:
|
||||
I2C控制器管理: 打开或关闭I2C控制器
|
||||
I2C消息传输:通过消息传输结构体数组进行自定义传输
|
||||
|
||||
## 接口说明
|
||||
1. I2C初始化函数:unsigned int IoTI2cInit(unsigned int id, unsigned int baudrate)
|
||||
参数说明:
|
||||
id: 对应的I2C号
|
||||
baudrate: I2C通信速率
|
||||
return: IOT_SUCCESS表示初始化成功
|
||||
|
||||
2. I2C去初始化函数:unsigned int IoTI2cDeinit(unsigned int id)
|
||||
参数说明:
|
||||
id: 对应的I2C号
|
||||
return: IOT_SUCCESS表示去除初始化成功
|
||||
|
||||
3. I2C写数据函数:unsigned int IoTI2cWrite(unsigned int id, unsigned short deviceAddr, const unsigned char *data, unsigned int dataLen)
|
||||
参数说明:
|
||||
id: 对应的I2C号
|
||||
deviceAddr: I2C设备地址
|
||||
data: 写入的数据缓冲区地址
|
||||
dataLen: 写入的数据字节数
|
||||
return: IOT_SUCCESS表示写入成功
|
||||
|
||||
4. I2C读数据函数:unsigned int IoTI2cRead(unsigned int id, unsigned short deviceAddr, unsigned char *data, unsigned int dataLen)
|
||||
参数说明:
|
||||
id: 对应的I2C号
|
||||
deviceAddr: I2C设备地址
|
||||
data: 读取数据缓冲区地址
|
||||
dataLen: 读取的字节数
|
||||
return: IOT_SUCCESS表示读取成功
|
||||
|
||||
5. I2C速率设置函数:unsigned int IoTI2cSetBaudrate(unsigned int id, unsigned int baudrate)
|
||||
参数说明:
|
||||
id: 对应的I2C号
|
||||
baudrate: 设置的I2C速率值
|
||||
|
||||
## 例程原理简介
|
||||
本例程通过I2C接口函数,与oled点阵屏进行通信,进行oled的相关显示实验
|
||||
|
|
|
@ -230,4 +230,4 @@ static void THsensorExampleEntry(void)
|
|||
}
|
||||
}
|
||||
|
||||
SYS_RUN(THsensorExampleEntry);
|
||||
APP_FEATURE_INIT(THsensorExampleEntry);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
|
|
|
@ -1,3 +1,18 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __BUZZE_H__
|
||||
#define __BUZZE_H__
|
||||
#include <stdint.h>
|
||||
|
|
|
@ -1,3 +1,18 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __SENSOR_H
|
||||
#define __SENSOR_H
|
||||
|
||||
|
|
|
@ -1,3 +1,18 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __GYRO_H__
|
||||
#define __GYRO_H__
|
||||
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef WIFI_CONNECT_DEMO_H
|
||||
#define WIFI_CONNECT_DEMO_H
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
@ -122,4 +122,4 @@ static void EntryWifiApService(void)
|
|||
return;
|
||||
}
|
||||
|
||||
SYS_RUN(EntryWifiApService);
|
||||
APP_FEATURE_INIT(EntryWifiApService);
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
|
|
@ -1,17 +1,18 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __WIFI_AP_SERVICE_H__
|
||||
#define __WIFI_AP_SERVICE_H__
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
# Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
static_library("network_mqttclient_example") {
|
||||
sources = [
|
||||
"src/wifi_connect.c",
|
||||
"src/mqtt_main.c",
|
||||
]
|
||||
|
||||
cflags = [ "-Wno-unused-variable" ]
|
||||
cflags += [ "-Wno-unused-but-set-variable" ]
|
||||
|
||||
include_dirs = [
|
||||
"//foundation/communication/wifi_lite/interfaces/wifiservice" ,
|
||||
"include"
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//third_party/paho_mqtt:pahomqtt_static",
|
||||
]
|
||||
}
|
|
@ -0,0 +1,349 @@
|
|||
# Niobe开发板MQTT联网演示
|
||||
|
||||
本案例程序将演示怎么在拓维Niobe WiFi IoT Core开发板上编写一个连接MQTT服务器的业务程序,实现开发板联网上报数据到服务器。
|
||||
|
||||
## 简述
|
||||
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级"通讯协议,该协议构建于TCP/IP协议上,由IBM在1999年发布。MQTT最大优点在于,可以以极少的代码和有限的带宽,为连接远程设备提供实时可靠的消息服务。作为一种低开销、低带宽占用的即时通讯协议,使其在物联网、小型设备、移动应用等方面有较广泛的应用。
|
||||
|
||||
MQTT是一个基于客户端-服务器的消息发布/订阅传输协议。MQTT协议是轻量、简单、开放和易于实现的,这些特点使它适用范围非常广泛。在很多情况下,包括受限的环境中,如:机器与机器(M2M)通信和物联网(IoT)。其在,通过卫星链路通信传感器、偶尔拨号的医疗设备、智能家居、及一些小型化设备中已广泛使用。
|
||||
|
||||
## MQTT协议中的方法
|
||||
MQTT协议中定义了一些方法(也被称为动作),来于表示对确定资源所进行操作。这个资源可以代表预先存在的数据或动态生成数据,这取决于服务器的实现。通常来说,资源指服务器上的文件或输出。主要方法有:
|
||||
|
||||
- Connect。等待与服务器建立连接。
|
||||
- Disconnect。等待MQTT客户端完成所做的工作,并与服务器断开TCP/IP会话。
|
||||
- Subscribe。等待完成订阅。
|
||||
- UnSubscribe。等待服务器取消客户端的一个或多个topics订阅。
|
||||
- Publish。MQTT客户端发送消息请求,发送完成后返回。
|
||||
|
||||
## 特点
|
||||
- 开放消息协议,简单易实现
|
||||
- 发布订阅模式,一对多消息发布
|
||||
- 基于TCP/IP网络连接,提供有序,无损,双向连接。
|
||||
- 1字节固定报头,2字节心跳报文,最小化传输开销和协议交换,有效减少网络流量。
|
||||
- 消息QoS支持,可靠传输保证
|
||||
- “最多一次”,尽操作环境所能提供的最大努力分发消息。消息可能会丢失。例如,这个等级可用于环境传感器数据,单次的数据丢失没关系,因为不久之后会再次发送。
|
||||
- “至少一次”,保证消息可以到达,但是可能会重复。
|
||||
- “仅一次”,保证消息只到达一次。例如,这个等级可用在一个计费系统中,这里如果消息重复或丢失会导致不正确的收费。
|
||||
- 异常连接断开发生时,能通知到相关各方。
|
||||
|
||||
## 结构体详解
|
||||
|
||||
```
|
||||
typedef struct
|
||||
{
|
||||
/** The eyecatcher for this structure. must be MQTC. */
|
||||
char struct_id[4];
|
||||
/** The version number of this structure. Must be 0 */
|
||||
int struct_version;
|
||||
/** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
|
||||
*/
|
||||
unsigned char MQTTVersion;
|
||||
MQTTString clientID;
|
||||
unsigned short keepAliveInterval;
|
||||
unsigned char cleansession;
|
||||
unsigned char willFlag;
|
||||
MQTTPacket_willOptions will;
|
||||
MQTTString username;
|
||||
MQTTString password;
|
||||
} MQTTPacket_connectData;
|
||||
```
|
||||
|
||||
**描述:**
|
||||
|
||||
MQTT客户端连接参数
|
||||
|
||||
**参数:**
|
||||
|
||||
| 名字 | 描述 |
|
||||
| ------ | -------- |
|
||||
|MQTTVersion| MQTT协议版本号|
|
||||
|clientID| 用户ID|
|
||||
|keepAliveInterval| 保活周期|
|
||||
|cleansession| 是否清除session信息|
|
||||
|willFlag| 是否使用临终遗言|
|
||||
|will| 临终遗言信息|
|
||||
|username| 用户名|
|
||||
|password| 用户密钥|
|
||||
|
||||
**示例代码如下:**
|
||||
|
||||
```
|
||||
// 连接参数
|
||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
||||
// 客户ID,唯一
|
||||
data.clientID.cstring= "Talkweb";
|
||||
// 不使用临终遗言
|
||||
data.willFlag = 0;
|
||||
// MQTT版本号
|
||||
data.MQTTVersion = 3;
|
||||
// 保存存活时间
|
||||
data.keepAliveInterval = 0;
|
||||
// 重新连接后是否清除以前的信息 0表示不清除,1表示重连清除
|
||||
data.cleansession = 1;
|
||||
// 服务器用户名
|
||||
data.username.cstring = "Talkweb";
|
||||
// 服务器用户密码
|
||||
data.password.cstring = "Talkweb1996";
|
||||
```
|
||||
------------------------------------------------
|
||||
```
|
||||
typedef struct MQTTMessage
|
||||
{
|
||||
enum QoS qos;
|
||||
unsigned char retained;
|
||||
unsigned char dup;
|
||||
unsigned short id;
|
||||
void *payload;
|
||||
size_t payloadlen;
|
||||
} MQTTMessage;
|
||||
```
|
||||
**描述:**
|
||||
|
||||
MQTT消息结构体
|
||||
|
||||
**参数:**
|
||||
|
||||
| 名字 | 描述 |
|
||||
| ------ | -------- |
|
||||
|qos |服务等级|
|
||||
|retained| 是否保留|
|
||||
|dup |重发标识|
|
||||
|id |消息id|
|
||||
|payload |消息数据|
|
||||
|payloadlen |消息数据长度|
|
||||
|
||||
## 函数详解
|
||||
|
||||
```
|
||||
void NetworkInit(Network* n)
|
||||
```
|
||||
|
||||
**描述:** 初始化网络
|
||||
|
||||
**参数:**
|
||||
|
||||
| 名字 | 描述 |
|
||||
| ----- | ------------------------- |
|
||||
| n | 要初始化的Network结构体 |
|
||||
|
||||
```
|
||||
int NetworkConnect(Network* n, char* addr, int port)
|
||||
```
|
||||
|
||||
**描述:**
|
||||
|
||||
连接MQTT服务器
|
||||
|
||||
**参数:**
|
||||
|
||||
| 名字 | 描述 |
|
||||
| ----- | ------------------------- |
|
||||
| n | 已经初始化的Network. |
|
||||
|addr | MQTT服务器IP地址|
|
||||
|port | MQTT服务器端口|
|
||||
|
||||
```
|
||||
void MQTTClientInit(MQTTClient* c, Network* network, unsigned int command_timeout_ms,
|
||||
unsigned char* sendbuf, size_t sendbuf_size, unsigned char* readbuf, size_t readbuf_size)
|
||||
```
|
||||
|
||||
**描述:**
|
||||
|
||||
MQTT客户端初始化
|
||||
|
||||
**参数:**
|
||||
|
||||
| 名字 | 描述 |
|
||||
| ----- | ------------------------- |
|
||||
| c | MQTTClient客户端 |
|
||||
|network | 网络结构体|
|
||||
|command_timeout_ms | 延时毫秒|
|
||||
|sendbuf| 发送内容数组|
|
||||
|sendbuf_size|发送内容数组长度|
|
||||
|readbuf|接收内容数组|
|
||||
|readbuf_size|接收内容数组长度|
|
||||
|
||||
|
||||
```
|
||||
int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options)
|
||||
```
|
||||
|
||||
**描述:**
|
||||
|
||||
MQTT客户端连接服务器
|
||||
|
||||
**参数:**
|
||||
|
||||
| 名字 | 描述 |
|
||||
| ------ | -------- |
|
||||
| c | MQTT客户端 |
|
||||
| options | 连接参数 详细参数请查看上面定义 |
|
||||
|
||||
```
|
||||
int MQTTSubscribe(MQTTClient* c, const char* topicFilter, enum QoS qos,
|
||||
messageHandler messageHandler)
|
||||
```
|
||||
|
||||
**描述:**
|
||||
|
||||
订阅主题
|
||||
|
||||
**参数:**
|
||||
|
||||
| 名字 | 描述 |
|
||||
| --------- | --------------------------- |
|
||||
| c | MQTT客户端|
|
||||
| topicFilter | 订阅的主题|
|
||||
| qos | 订阅质量|
|
||||
||00:最多一次,即:<=1|
|
||||
||01:至少一次,即:>=1|
|
||||
||10:一次,即:=1|
|
||||
||11:预留|
|
||||
|messageHandler|消息处理回调函数|
|
||||
|
||||
```
|
||||
int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message)
|
||||
```
|
||||
|
||||
**描述:**
|
||||
|
||||
订阅主题
|
||||
|
||||
**参数:**
|
||||
|
||||
| 名字 | 描述 |
|
||||
| --------- | --------------------------- |
|
||||
|c | MQTT客户端|
|
||||
|topicName | 发布的主题|
|
||||
|message | MQTTMessage 消息|
|
||||
|
||||
## 软件设计
|
||||
|
||||
**主要代码分析**
|
||||
|
||||
```c
|
||||
static void MQTTDemoTask(void)
|
||||
{
|
||||
// 连接WIFI
|
||||
WifiConnect(SELECT_WIFI_SSID, SELECT_WIFI_PASSWORD);
|
||||
printf("Starting ...\n");
|
||||
int rc, count = 0;
|
||||
MQTTClient client;
|
||||
|
||||
// 初始化网络参数
|
||||
NetworkInit(&network);
|
||||
printf("NetworkConnect ...\n");
|
||||
begin:
|
||||
// 连接服务器网络
|
||||
NetworkConnect(&network, "123.60.8.120", 1883);
|
||||
printf("MQTTClientInit ...\n");
|
||||
// 初始化客户端
|
||||
MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
|
||||
|
||||
// 连接参数
|
||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
||||
// 客户ID,唯一
|
||||
data.clientID.cstring= "Talkweb";
|
||||
// 不使用临终遗言
|
||||
data.willFlag = 0;
|
||||
// MQTT版本号
|
||||
data.MQTTVersion = 3;
|
||||
// 保存存活时间
|
||||
data.keepAliveInterval = 0;
|
||||
// 重新连接后是否清除以前的信息 0表示不清除,1表示重连清除
|
||||
data.cleansession = 1;
|
||||
// 服务器用户名
|
||||
data.username.cstring = "Talkweb";
|
||||
// 服务器用户密码
|
||||
data.password.cstring = "Talkweb1996";
|
||||
|
||||
printf("MQTTConnect ...\n");
|
||||
// 登录服务器
|
||||
rc = MQTTConnect(&client, &data);
|
||||
// 失败处理
|
||||
if (rc != 0) {
|
||||
printf("MQTTConnect: %d\n", rc);
|
||||
NetworkDisconnect(&network);
|
||||
MQTTDisconnect(&client);
|
||||
osDelay(200);
|
||||
goto begin;
|
||||
}
|
||||
|
||||
printf("MQTTSubscribe ...\n");
|
||||
// 订阅主题,并绑定回调函数处理接收到的消息
|
||||
rc = MQTTSubscribe(&client, "pubtopic", 2, MessageArrived);
|
||||
// 订阅失败处理
|
||||
if (rc != 0) {
|
||||
printf("MQTTSubscribe: %d\n", rc);
|
||||
osDelay(200);
|
||||
goto begin;
|
||||
}
|
||||
while (count++) {
|
||||
MQTTMessage message;
|
||||
// 发布的内容
|
||||
char payload[] = {"{\"name\": \"Talkweb\",\"url\": \"https://www.talkweb.com.cn/\"}"};
|
||||
// 仅一次
|
||||
message.qos = 2;
|
||||
message.retained = 0;
|
||||
message.payload = payload;
|
||||
message.payloadlen = strlen(payload);
|
||||
// 发布
|
||||
if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0) {
|
||||
printf("Return code from MQTT publish is %d\n", rc);
|
||||
NetworkDisconnect(&network);
|
||||
MQTTDisconnect(&client);
|
||||
goto begin;
|
||||
}
|
||||
osDelay(100);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
## 编译调试
|
||||
|
||||
### 修改对接热点的账号密码
|
||||
|
||||
修改`mqtt_main.h`第32行和33行的WiFi热点SSID和密码,改成自己环境中的WiFi热点。
|
||||
|
||||
```
|
||||
// 默认WiFi名和密码
|
||||
#define SELECT_WIFI_SSID "111"
|
||||
#define SELECT_WIFI_PASSWORD "0987654321"
|
||||
```
|
||||
|
||||
### 修改 BUILD.gn 文件
|
||||
|
||||
修改 `applications/app/BUILD.gn` 路径中的 BUILD.gn 文件,指定 `network_mqttclient_example` 参与编译。
|
||||
|
||||
```
|
||||
# "TW208_Module_ds1307:module_ds1307_example",
|
||||
# "TW209_Module_gps:module_gps_example",
|
||||
# "TW301_Network_wifista:network_wifista_example",
|
||||
# "TW302_Network_wifiap:network_wifiap_example",
|
||||
"TW303_Network_mqttclient:network_mqttclient_example",
|
||||
# "TW304_Network_httpclient:network_httpclient_example",
|
||||
# "TW305_Network_ntpclient:network_ntpclient_example",
|
||||
```
|
||||
|
||||
### 运行结果
|
||||
|
||||
示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,会打印连接到的Wifi热点信息,以及订阅MQTT服务器主题,并接收发布后返回的结果。
|
||||
|
||||
```
|
||||
Starting ...
|
||||
NetworkConnect ...
|
||||
MQTTClientInit ...
|
||||
MQTTConnect ...
|
||||
MQTTSubscribe ...
|
||||
wait for suback
|
||||
Message arrived on topic pubtopic: {"code":-20,"desc":"appId cannot be empty"}
|
||||
Message arrived on topic pubtopic: {"code":-20,"desc":"appId cannot be empty"}
|
||||
Message arrived on topic pubtopic: {"code":-20,"desc":"appId cannot be empty"}
|
||||
Message arrived on topic pubtopic: {"code":-20,"desc":"appId cannot be empty"}
|
||||
Message arrived on topic pubtopic: {"code":-20,"desc":"appId cannot be empty"}
|
||||
Message arrived on topic pubtopic: {"code":-20,"desc":"appId cannot be empty"}
|
||||
Message arrived on topic pubtopic: {"code":-20,"desc":"appId cannot be empty"}
|
||||
Message arrived on topic pubtopic: {"code":-20,"desc":"appId cannot be empty"}
|
||||
```
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __WIFI_CONNECT_H__
|
||||
#define __WIFI_CONNECT_H__
|
||||
|
||||
int WifiConnect(const char *ssid,const char *psk);
|
||||
|
||||
#endif /* __WIFI_CONNECT_H__ */
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,149 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Sergio R. Caprile - clarifications and/or documentation extension
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cmsis_os2.h"
|
||||
#include "ohos_init.h"
|
||||
|
||||
#include "MQTTClient.h"
|
||||
#include "wifi_connect.h"
|
||||
|
||||
static unsigned char sendBuf[1000];
|
||||
static unsigned char readBuf[1000];
|
||||
|
||||
// 默认WiFi名和密码
|
||||
#define SELECT_WIFI_SSID "111"
|
||||
#define SELECT_WIFI_PASSWORD "0987654321"
|
||||
|
||||
Network network;
|
||||
|
||||
/**
|
||||
* @brief 订阅主题处理消息函数
|
||||
* @param MessageData 消息数据
|
||||
* @return
|
||||
*/
|
||||
void MessageArrived(MessageData* data)
|
||||
{
|
||||
printf("Message arrived on topic %.*s: %.*s\n",
|
||||
data->topicName->lenstring.len, data->topicName->lenstring.data,
|
||||
data->message->payloadlen, data->message->payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MQTT任务处理
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
static void MQTTDemoTask(void)
|
||||
{
|
||||
// 连接WIFI
|
||||
WifiConnect(SELECT_WIFI_SSID, SELECT_WIFI_PASSWORD);
|
||||
printf("Starting ...\n");
|
||||
int rc, count = 0;
|
||||
MQTTClient client;
|
||||
|
||||
// 初始化网络参数
|
||||
NetworkInit(&network);
|
||||
printf("NetworkConnect ...\n");
|
||||
begin:
|
||||
// 连接服务器网络
|
||||
NetworkConnect(&network, "123.60.8.120", 1883);
|
||||
printf("MQTTClientInit ...\n");
|
||||
// 初始化客户端
|
||||
MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));
|
||||
|
||||
// 连接参数
|
||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
||||
// 客户ID,唯一
|
||||
data.clientID.cstring= "Talkweb";
|
||||
// 不使用临终遗言
|
||||
data.willFlag = 0;
|
||||
// MQTT版本号
|
||||
data.MQTTVersion = 3;
|
||||
// 保存存活时间
|
||||
data.keepAliveInterval = 0;
|
||||
// 重新连接后是否清除以前的信息 0表示不清除,1表示重连清除
|
||||
data.cleansession = 1;
|
||||
// 服务器用户名
|
||||
data.username.cstring = "Talkweb";
|
||||
// 服务器用户密码
|
||||
data.password.cstring = "Talkweb1996";
|
||||
|
||||
printf("MQTTConnect ...\n");
|
||||
// 登录服务器
|
||||
rc = MQTTConnect(&client, &data);
|
||||
// 失败处理
|
||||
if (rc != 0) {
|
||||
printf("MQTTConnect: %d\n", rc);
|
||||
NetworkDisconnect(&network);
|
||||
MQTTDisconnect(&client);
|
||||
osDelay(200);
|
||||
goto begin;
|
||||
}
|
||||
|
||||
printf("MQTTSubscribe ...\n");
|
||||
// 订阅主题,并绑定回调函数处理接收到的消息
|
||||
rc = MQTTSubscribe(&client, "pubtopic", 2, MessageArrived);
|
||||
// 订阅失败处理
|
||||
if (rc != 0) {
|
||||
printf("MQTTSubscribe: %d\n", rc);
|
||||
osDelay(200);
|
||||
goto begin;
|
||||
}
|
||||
while (++count) {
|
||||
MQTTMessage message;
|
||||
// 发布的内容
|
||||
char payload[] = {"{\"name\": \"Talkweb\",\"url\": \"https://www.talkweb.com.cn/\"}"};
|
||||
// 仅一次
|
||||
message.qos = 2;
|
||||
message.retained = 0;
|
||||
message.payload = payload;
|
||||
message.payloadlen = strlen(payload);
|
||||
// 发布
|
||||
if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0) {
|
||||
printf("Return code from MQTT publish is %d\n", rc);
|
||||
NetworkDisconnect(&network);
|
||||
MQTTDisconnect(&client);
|
||||
count = 0;
|
||||
goto begin;
|
||||
}
|
||||
osDelay(100);
|
||||
}
|
||||
}
|
||||
|
||||
// MQTTDemo教程
|
||||
static void MQTTDemo(void)
|
||||
{
|
||||
osThreadAttr_t attr;
|
||||
|
||||
attr.name = "MQTTDemoTask";
|
||||
attr.attr_bits = 0U;
|
||||
attr.cb_mem = NULL;
|
||||
attr.cb_size = 0U;
|
||||
attr.stack_mem = NULL;
|
||||
attr.stack_size = 10240;
|
||||
attr.priority = osPriorityNormal;
|
||||
|
||||
if (osThreadNew((osThreadFunc_t)MQTTDemoTask, NULL, &attr) == NULL) {
|
||||
printf("[MQTT_Demo] Failed to create MQTTDemoTask!\n");
|
||||
}
|
||||
}
|
||||
|
||||
APP_FEATURE_INIT(MQTTDemo);
|
|
@ -0,0 +1,278 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
// #include "hiview_log.h"
|
||||
#include "ohos_init.h"
|
||||
#include "cmsis_os2.h"
|
||||
|
||||
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/netifapi.h"
|
||||
#include "lwip/ip4_addr.h"
|
||||
#include "lwip/api_shell.h"
|
||||
|
||||
#include "wifi_device.h"
|
||||
|
||||
|
||||
#define DEF_TIMEOUT 15
|
||||
#define ONE_SECOND 1
|
||||
|
||||
#define SELECT_WLAN_PORT "wlan0"
|
||||
// #define SELECT_WIFI_SSID "aaa"
|
||||
// #define SELECT_WIFI_PASSWORD "talkweb1996"
|
||||
#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK
|
||||
|
||||
static int g_staScanSuccess = 0;
|
||||
static int g_ConnectSuccess = 0;
|
||||
static int ssid_count = 0;
|
||||
|
||||
WifiErrorCode error;
|
||||
WifiEvent g_wifiEventHandler = {0};
|
||||
|
||||
int sock_fd;
|
||||
int addr_length;
|
||||
|
||||
#define STD_TIMEZONE_OFFSET +8 /* 设置中国 */
|
||||
const int timeZone = 8;
|
||||
|
||||
|
||||
static void WiFiInit(void);
|
||||
static void WaitSacnResult(void);
|
||||
static int WaitConnectResult(void);
|
||||
static void OnWifiScanStateChangedHandler(int state, int size);
|
||||
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info);
|
||||
static void OnHotspotStaJoinHandler(StationInfo *info);
|
||||
static void OnHotspotStateChangedHandler(int state);
|
||||
static void OnHotspotStaLeaveHandler(StationInfo *info);
|
||||
|
||||
static void OnHotspotStaJoinHandler(StationInfo *info)
|
||||
{
|
||||
(void)info;
|
||||
printf("STA join AP\n");
|
||||
return;
|
||||
}
|
||||
|
||||
static void OnHotspotStaLeaveHandler(StationInfo *info)
|
||||
{
|
||||
(void)info;
|
||||
printf("HotspotStaLeave:info is null.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
static void OnHotspotStateChangedHandler(int state)
|
||||
{
|
||||
printf("HotspotStateChanged:state is %d.\n", state);
|
||||
return;
|
||||
}
|
||||
|
||||
static void WiFiInit(void)
|
||||
{
|
||||
printf("<--Wifi Init-->\r\n");
|
||||
g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
|
||||
g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
|
||||
g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
|
||||
g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
|
||||
g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
|
||||
error = RegisterWifiEvent(&g_wifiEventHandler);
|
||||
if (error != WIFI_SUCCESS)
|
||||
{
|
||||
printf("register wifi event fail!\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("register wifi event succeed!\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void OnWifiScanStateChangedHandler(int state, int size)
|
||||
{
|
||||
(void)state;
|
||||
if (size > 0)
|
||||
{
|
||||
ssid_count = size;
|
||||
g_staScanSuccess = 1;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int WifiConnect(const char *ssid, const char *psk)
|
||||
{
|
||||
|
||||
WifiScanInfo *wifi_info = NULL;
|
||||
unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
|
||||
static struct netif *g_lwip_netif = NULL;
|
||||
WifiDeviceConfig select_ap_config = {0};
|
||||
|
||||
osDelay(200);
|
||||
printf("<--System Init-->\r\n");
|
||||
|
||||
// 初始化WIFI
|
||||
WiFiInit();
|
||||
|
||||
if (EnableWifi() != WIFI_SUCCESS)
|
||||
{
|
||||
printf("EnableWifi failed, error = %d\n", error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//判断WIFI是否激活
|
||||
if (IsWifiActive() == 0)
|
||||
{
|
||||
printf("Wifi station is not actived.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
//分配空间,保存WiFi信息
|
||||
wifi_info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
|
||||
if (wifi_info == NULL)
|
||||
{
|
||||
printf("faild to create wifiscanInfo.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
//重置标志位
|
||||
ssid_count = 0;
|
||||
g_staScanSuccess = 0;
|
||||
//开启wifi扫描
|
||||
Scan();
|
||||
|
||||
//等待扫描结果
|
||||
WaitSacnResult();
|
||||
|
||||
//获取扫描列表
|
||||
error = GetScanInfoList(wifi_info, &size);
|
||||
} while (g_staScanSuccess != 1);
|
||||
|
||||
//打印WiFi列表
|
||||
printf("********************\r\n");
|
||||
for (uint8_t i = 0; i < ssid_count; i++)
|
||||
{
|
||||
printf("no:%03d, ssid:%-30s, rssi:%5d\r\n", i + 1, wifi_info[i].ssid, wifi_info[i].rssi / 100);
|
||||
}
|
||||
printf("********************\r\n");
|
||||
|
||||
|
||||
//插件指定的wifi是否存在
|
||||
for(uint8_t i = 0; i < ssid_count; i++)
|
||||
{
|
||||
if (strcmp(ssid, wifi_info[i].ssid) == 0)
|
||||
{
|
||||
int result;
|
||||
printf("Select:%3d wireless, Waiting...\r\n", i+1);
|
||||
//拷贝要连接的热点信息
|
||||
strcpy(select_ap_config.ssid, wifi_info[i].ssid);
|
||||
strcpy(select_ap_config.preSharedKey, psk);
|
||||
select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE;
|
||||
|
||||
if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS)
|
||||
{
|
||||
if (ConnectTo(result) == WIFI_SUCCESS && WaitConnectResult() == 1)
|
||||
{
|
||||
printf("WiFi connect succeed!\r\n");
|
||||
g_lwip_netif = netifapi_netif_find(SELECT_WLAN_PORT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(i == ssid_count-1)
|
||||
{
|
||||
printf("ERROR: No wifi as expected\r\n");
|
||||
while(1) osDelay(100);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//启动DHCP
|
||||
if (g_lwip_netif)
|
||||
{
|
||||
dhcp_start(g_lwip_netif);
|
||||
printf("begain to dhcp");
|
||||
}
|
||||
|
||||
|
||||
//等待DHCP
|
||||
for(;;)
|
||||
{
|
||||
if(dhcp_is_bound(g_lwip_netif) == ERR_OK)
|
||||
{
|
||||
printf("<-- DHCP state:OK -->\r\n");
|
||||
|
||||
//打印获取到的IP信息
|
||||
netifapi_netif_common(g_lwip_netif, dhcp_clients_info_show, NULL);
|
||||
break;
|
||||
}
|
||||
|
||||
printf("<-- DHCP state:Inprogress -->\r\n");
|
||||
osDelay(100);
|
||||
}
|
||||
|
||||
osDelay(100);
|
||||
if (wifi_info != NULL) {
|
||||
free(wifi_info);
|
||||
wifi_info = NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int WaitConnectResult(void)
|
||||
{
|
||||
int ConnectTimeout = DEF_TIMEOUT;
|
||||
while (ConnectTimeout > 0)
|
||||
{
|
||||
sleep(1);
|
||||
ConnectTimeout--;
|
||||
if (g_ConnectSuccess == 1)
|
||||
{
|
||||
printf("WaitConnectResult:wait success[%d]s\n", (DEF_TIMEOUT - ConnectTimeout));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ConnectTimeout <= 0)
|
||||
{
|
||||
printf("WaitConnectResult:timeout!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
|
||||
{
|
||||
(void)info;
|
||||
|
||||
if (state > 0)
|
||||
{
|
||||
g_ConnectSuccess = 1;
|
||||
printf("callback function for wifi connect\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("connect error,please check password\r\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void WaitSacnResult(void)
|
||||
{
|
||||
int scanTimeout = DEF_TIMEOUT;
|
||||
while (scanTimeout > 0)
|
||||
{
|
||||
sleep(ONE_SECOND);
|
||||
scanTimeout--;
|
||||
if (g_staScanSuccess == 1)
|
||||
{
|
||||
printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (scanTimeout <= 0)
|
||||
{
|
||||
printf("WaitSacnResult:timeout!\n");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +1,17 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __BOOT_ANIMATION_H__
|
||||
#define __BOOT_ANIMATION_H__
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef __OLED_U8G2_H__
|
||||
#define __OLED_U8G2_H__
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021 Talkweb Co.,ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
|
|
@ -18,10 +18,7 @@ config("pahomqtt_config") {
|
|||
include_dirs = [
|
||||
"MQTTPacket/src",
|
||||
"MQTTClient-C/src",
|
||||
"MQTTClient-C/src/liteOS",
|
||||
"//third_party/iot_link/network/mqtt/mqtt_al",
|
||||
"//third_party/iot_link/inc",
|
||||
"//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include",
|
||||
"MQTTClient-C/src/liteOS",
|
||||
"//kernel/liteos_m/components/cmsis/2.0",
|
||||
]
|
||||
}
|
||||
|
@ -61,16 +58,10 @@ lite_library("pahomqtt_shared") {
|
|||
}
|
||||
|
||||
ndk_lib("pahomqtt_ndk") {
|
||||
if (board_name != "hi3861v100") {
|
||||
lib_extension = ".so"
|
||||
deps = [
|
||||
":pahomqtt_shared"
|
||||
]
|
||||
} else {
|
||||
deps = [
|
||||
":pahomqtt_static"
|
||||
]
|
||||
}
|
||||
deps = [
|
||||
":pahomqtt_static"
|
||||
]
|
||||
|
||||
head_files = [
|
||||
"//third_party/paho_mqtt"
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue