项目
这个例子是基于HiSpark Wi-Fi物联网套件Hi3861板子项目实战3-触摸彩屏LCD,从本例程中,在 HarmonyOS中Hi3861学习到
1。和谐liteOS Hi3861 WIFI设置和操作
2。和谐LiteOS Hi3861 UDP长链接控制方式
3。通过UDP控制彩屏LCD输出和 触摸输入操作
4。附上实战的原始码和个人开发的虚拟彩屏LCD带触摸,方便Hi3861,Hi3518EV300等没有彩屏驱动的MCU,也能通过彩屏LCD调试 1。 原始 代码 如下: #include“ ohos_init.h” #include“ cmsis_os2.h” #include“ hi_wifi_api.h” #include“ lwip / ip_addr.h” #include“ lwip / neti fapi.h” #include“ lwip / sockets.h” “ #include” MQTTPacket.h“ #include” transport.h“ #include #include #include #include”
#include“ wifiiot_gpio_ex.h”
#include“ lwip / netifapi.h”
#include“ lwip / api_shell.h”
#include“ net_common.h”
#include“ cJSON.h”
#define PARAM_HOTSPOT_SSID“ Honor” //您的AP SSID
#定义PARAM_HOTSPOT_PSK “12345678” //你的AP PSK
的#define PARAM_SERVER_ADDR “192.168.43.36”
的#define PARAM_SERVER_PORT 6201
#定义PARAM_HOTSPOT_TYPE WIFI_SEC_TYPE_PSK //定义在wifi_device_config.h
的#define R_LED WIFI_IOT_GPIO_IDX_10
的#define B_LED WIFI_IOT_GPIO_IDX_12
的#define G_LED WIFI_IOT_GPIO_IDX_11
的#define DOWN 0×01
#define UP 0x02
#define MOVE 0x03
静态字符响应[128] =“”;
static int sockfd = 0;
static int sockfd_isConect = 0;
static struct sockaddr_in toAddr = {0};
static int lcdTouch[3] ={0};
void UdpClient(const char* host, unsigned short port);
static void APPTask(void *arg);
static void PrintLinkedInfo(WifiLinkedInfo* info)
{
if (!info) return;
static char macAddress[32] = {0};
unsigned char* mac = info->bssid;
snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %srn",
macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
}
static volatile int g_connected = 0;
static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
{
if (!info) return;
printf("%s %d, state = %d, info = rn", __FUNCTION__, __LINE__, state);
PrintLinkedInfo(info);
if (state == WIFI_STATE_AVALIABLE) {
g_connected = 1;
} else {
g_connected = 0;
}
}
static void OnWifiScanStateChanged(int state, int size)
{
printf("%s %d, state = %X, size = %drn", __FUNCTION__, __LINE__, state, size);
}
static WifiEvent g_defaultWifiEventListener = {
.OnWifiConnectionChanged = OnWifiConnectionChanged,
.OnWifiScanStateChanged = OnWifiScanStateChanged
};
static struct netif* g_iface = NULL;
int ConnectToHotspot(WifiDeviceConfig* apConfig)
{
WifiErrorCode errCode;
int netId = -1;
errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
printf("RegisterWifiEvent: %drn", errCode);
errCode = EnableWifi();
printf("EnableWifi: %drn", errCode);
errCode = AddDeviceConfig(apConfig, &netId);
printf("AddDeviceConfig: %drn", errCode);
g_connected = 0;
errCode = ConnectTo(netId);
printf("ConnectTo(%d): %drn", netId, errCode);
while (!g_connected) { // wait until connect to AP
osDelay(10);
}
printf("g_connected: %drn", g_connected);
g_iface = netifapi_netif_find("wlan0");
if (g_iface) {
err_t ret = netifapi_dhcp_start(g_iface);
printf("netifapi_dhcp_start: %drn", ret);
osDelay(100); // wait DHCP server give me IP
ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
printf("netifapi_netif_common: %drn", ret);
}
return netId;
}
void DisconnectWithHotspot(int netId)
{
if (g_iface) {
err_t ret = netifapi_dhcp_stop(g_iface);
printf("netifapi_dhcp_stop: %drn", ret);
}
WifiErrorCode errCode = Disconnect(); // disconnect with your AP
printf("Disconnect: %drn", errCode);
errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
printf("UnRegisterWifiEvent: %drn", errCode);
RemoveDevice(netId); // remove AP config
printf("RemoveDevice: %drn", errCode);
errCode = DisableWifi();
printf("DisableWifi: %drn", errCode);
}
static void NetTask(void *arg)
{
(void)arg;
WifiDeviceConfig config = {0};
// 准备AP的配置参数
strcpy(config.ssid, PARAM_HOTSPOT_SSID);
strcpy(config.preSharedKey, PARAM_HOTSPOT_PSK);
config.securityType = PARAM_HOTSPOT_TYPE;
for(;;){
osDelay(10);
int netId = ConnectToHotspot(&config);
int timeout = 2;
while (timeout--) {
printf("After %d seconds, I will startrn", timeout);
osDelay(100);
}
UdpClient(PARAM_SERVER_ADDR, PARAM_SERVER_PORT);
// mqtt_connect();
printf("disconnect to AP ...rn");
DisconnectWithHotspot(netId);
printf("disconnect to AP done!rn");
}
}
int udpsend(char *msg,int len){
if(sockfd_isConect==0)return -1;
ssize_t retval = sendto(sockfd, msg, len, 0, (struct sockaddr *)&toAddr, sizeof(toAddr));
if (retval < 0) {
printf("sendto failed!rn");
return -1;
}
return 1;
}
void UdpClient(const char* host, unsigned short port)
{
int counts=0;
ssize_t retval = 0;
char* con = "connect;pc";
while (g_connected){
sockfd = socket(AF_INET, SOCK_DGRAM, 0); // UDP socket
printf("ip%s:%drn",host,port);
toAddr.sin_family = AF_INET;
toAddr.sin_port = htons(port);
if (inet_pton(AF_INET, host, &toAddr.sin_addr) <= 0) {
printf("inet_pton failed!rn");
goto do_cleanup;
}
retval = sendto(sockfd, con, sizeof(con), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));
if (retval < 0) {
printf("sendto failed!rn");
goto do_cleanup;
}
GpioSetOutputVal(B_LED, 1);
printf("request connectrn");
sockfd_isConect =1;
struct sockaddr_in fromAddr = {0};
socklen_t fromLen = sizeof(fromAddr);
struct timeval timeOut;
timeOut.tv_sec = 5; //设置2s超时
timeOut.tv_usec = 0;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeOut, sizeof(timeOut)) < 0)
{
printf("time out setting failedn");
}
for(;;){
retval = recvfrom(sockfd, &response, sizeof(response), 0, (struct sockaddr *)&fromAddr, &fromLen);
if (retval == -1 && errno == EAGAIN) //阻塞接收超时
{
printf("timeout..n");
if(++counts>=5){
// goto do_cleanup;
break;
}
}
else{
int x=0 ,y=0;
counts=0;
response[retval] = '