【HarmonyOS HiSpark Wi-Fi IoT 套件试用连载】驱动AHT20并显示温湿度 - HarmonyOS技术社区 - 电子技术论坛 - 广受欢迎的专业电子论坛! (elecfans.com)
在上面的AHT20的基础上增加wifi连网:
1、在原有的i2caht20文件夹下新建iot_wifi.c、iot_wifi.h(当然也可以从其他工程复制过来的)
iot_wifi.c:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "lwip/netif.h"
#include "lwip/netifapi.h"
#include "lwip/ip4_addr.h"
#include "lwip/api_shell.h"
#include "cmsis_os2.h"
#include "wifi_device.h"
#include "wifi_error_code.h"
#include "ohos_init.h"
#include "iot_wifi.h"
#include "oled_ssd1306.h"
#define DEF_TIMEOUT 15
#define ONE_SECOND 1
#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK
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 int g_staScanSuccess = 0;
static int g_ConnectSuccess = 0;
static int ssid_count = 0;
static WifiEvent g_wifiEventHandler = {0};
WifiErrorCode error;
#define SELECT_WLAN_PORT "wlan0"
int WifiConnect(const char *ssid, const char *psk)
{
WifiScanInfo *info = NULL;
unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
static struct netif *g_lwip_netif = NULL;
osDelay(200);
printf("<--System Init-->\r\n");
WiFiInit();
if (EnableWifi() != WIFI_SUCCESS) {
printf("EnableWifi failed, error = %d\r\n", error);
return -1;
}
if (IsWifiActive() == 0)
{
printf("Wifi station is not actived.\r\n");
return -1;
}
info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
if (info == NULL) {
return -1;
}
do {
ssid_count = 0;
g_staScanSuccess = 0;
Scan();
WaitSacnResult();
error = GetScanInfoList(info, &size);
} while (g_staScanSuccess != 1);
for (uint8_t i = 0; i < ssid_count; i++) {
printf("no:%03d, ssid:%-30s, rssi:%5d\r\n", i+1, info[i].ssid, info[i].rssi/100);
}
for (uint8_t i = 0; i < ssid_count; i++) {
if (strcmp(ssid, info[i].ssid) == 0) {
int result;
printf("Select:%3d wireless, Waiting...\r\n", i+1);
WifiDeviceConfig select_ap_config = {0};
strcpy(select_ap_config.ssid, 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);
}
}
if (g_lwip_netif) {
dhcp_start(g_lwip_netif);
printf("begain to dhcp\r\n");
}
for(;;) {
if(dhcp_is_bound(g_lwip_netif) == ERR_OK) {
printf("<-- DHCP state:OK -->\r\n");
netifapi_netif_common(g_lwip_netif, dhcp_clients_info_show, NULL);
break;
}
printf("<-- DHCP state:Inprogress -->\r\n");
osDelay(100);
}
err_t ret = 0;
char str_iddr[20];
ip4_addr_t ip = {0};
ip4_addr_t netmask = {0};
ip4_addr_t gw = {0};
ret = netifapi_netif_get_addr(g_lwip_netif, &ip, &netmask, &gw);
if (ret == ERR_OK) {
sprintf(str_iddr,"%s",ip4addr_ntoa(&ip));
OledShowString(0,6, str_iddr, FONT8x16);
}
osDelay(100);
return 0;
}
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)
{
if (size > 0) {
ssid_count = size;
g_staScanSuccess = 1;
}
printf("callback function for wifi scan:%d, %d\r\n", state, size);
return;
}
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
{
if (info == NULL) {
printf("WifiConnectionChanged:info is null, stat is %d.\n", state);
} else {
if (state == WIFI_STATE_AVALIABLE) {
g_ConnectSuccess = 1;
} else {
g_ConnectSuccess = 0;
}
}
}
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 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");
}
}
static int WaitConnectResult(void)
{
int ConnectTimeout = DEF_TIMEOUT;
while (ConnectTimeout > 0) {
sleep(ONE_SECOND);
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;
}
int BOARD_InitWifi(void)
{
return 0;
}
int BOARD_ConnectWifi(const char *wifiSSID, const char *wifiPWD)
{
return WifiConnect(wifiSSID, wifiPWD);
}
int BOARD_DisconnectWifi()
{
Disconnect();
return 0;
}
iot_wifi.h:
#ifndef IOT_WIFI_H_
#define IOT_WIFI_H_
int BOARD_InitWifi(void);
int BOARD_ConnectWifi(const char *wifiSSID, const char *wifiPWD);
int BOARD_DisconnectWifi(void);
#endif
修改i2caht20/BUILD.gn,增加iot_wifi.c的源:
static_library("i2cd_emo") {
sources = [
"aht20_test.c",
"aht20.c",
"oled_ssd1306.c",
"iot_wifi.c",
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
"//base/iot_hardware/peripheral/interfaces/kits",
"//device/hisilicon/hispark_pegasus/sdk_liteos/include",
"//foundation/communication/wifi_lite/interfaces/wifiservice",
]
}
3、在启动任务函数中增加,联网函数:
void Aht20TestTask(void* arg)
{
(void) arg;
hi_s32 my_wifi_stat;
uint32_t retval = 0;
char str_temp[12];
char str_Humi[12];
hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_I2C0_SDA);
hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_I2C0_SCL);
hi_i2c_init(HI_I2C_IDX_0, 400*1000);
OledInit();
BOARD_ConnectWifi("yssid", "wifipwd");
OledFillScreen(0x00);
OledShowString(26, 0, "AHT20-TEST!", FONT8x16);
retval = AHT20_Calibrate();
printf("AHT20_Calibrate: %d\r\n", retval);
while (1) {
float temp = 0.0, humi = 0.0;
retval = AHT20_StartMeasure();
printf("AHT20_StartMeasure: %d\r\n", retval);
retval = AHT20_GetMeasureResult(&temp, &humi);
sprintf(str_temp,"TEMP:%.2f",temp);
sprintf(str_Humi,"HUMI:%.2f",humi);
printf("AHT20_GetMeasureResult: %d, temp = %.2f, humi = %.2f\r\n", retval, temp, humi);
OledShowString(26,2,str_temp,FONT8x16);
OledShowString(26,4,str_Humi,FONT8x16);
sleep(1);
}
}
void Aht20Test(void)
{
osThreadAttr_t attr;
attr.name = "Aht20Task";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 4096;
attr.priority = osPriorityNormal;
if (osThreadNew(Aht20TestTask, NULL, &attr) == NULL) {
printf("[Aht20Test] Failed to create Aht20TestTask!\n");
}
}
APP_FEATURE_INIT(Aht20Test);
然后编译,下载到开发板,用串口助手可以看到联网信息:
ping一下:
【总结】wifi连网只需要拷贝iot_wifi.c过来就OK了。经验就是多看例程,多写。