基于HiSpark_WiFi_IoT智能家居项目实战1-灯光控制UDP版本

这是一个基于HiSpark_WiFi_IoT智能家居项目实战1-灯光控制例程, 之前有看下润和许老师以及连志安老师视频和开源码,HiSpark_WiFi_IoT Hi3861的底层驱动,和一些移植案例做都非常好,如MQTT,OLED 等,非常感谢他们的付出. 他们的案例很值得我们去学习参考 ,在此,我就不必重复造轮子. 下列是我一个智能家居项目实战1-灯光控制例程, 客户APP端的软件包含一个UDP 服务器, Hi3861只要请求连接成功就可以通讯了,如下图:

具体看源码:
//----------------------------------------------------------------------
//#include "MQTTPacket.h"
#include "transport.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "wifi_device.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "lwip/netifapi.h"
#include "lwip/api_shell.h"
#include "net_common.h"
#define PARAM_HOTSPOT_SSID "Honor"   // your AP SSID
#define PARAM_HOTSPOT_PSK  "12345678"  // your AP PSK
#define PARAM_SERVER_ADDR  "192.168.43.36"
#define PARAM_SERVER_PORT   6201
#define PARAM_HOTSPOT_TYPE WIFI_SEC_TYPE_PSK // defined in 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

static char request[] = "light:0\r\n";
static char response[128] = "";
static char light_status = 0;

void UdpClient(const char* host, unsigned short port);
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: %s\r\n",
        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 = \r\n", __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 = %d\r\n", __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: %d\r\n", errCode);
    errCode = EnableWifi();
    printf("EnableWifi: %d\r\n", errCode);
   errCode = AddDeviceConfig(apConfig, &netId);
    printf("AddDeviceConfig: %d\r\n", errCode);
    g_connected = 0;
    errCode = ConnectTo(netId);
    printf("ConnectTo(%d): %d\r\n", netId, errCode);
    while (!g_connected) { // wait until connect to AP
        osDelay(10);
    }
    printf("g_connected: %d\r\n", g_connected);
    g_iface = netifapi_netif_find("wlan0");
    if (g_iface) {
        err_t ret = netifapi_dhcp_start(g_iface);
        printf("netifapi_dhcp_start: %d\r\n", ret);
        osDelay(100); // wait DHCP server give me IP
        ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
        printf("netifapi_netif_common: %d\r\n", ret);
    }
    return netId;
}

void DisconnectWithHotspot(int netId)
{
    if (g_iface) {
        err_t ret = netifapi_dhcp_stop(g_iface);
        printf("netifapi_dhcp_stop: %d\r\n", ret);
    }
    WifiErrorCode errCode = Disconnect(); // disconnect with your AP
    printf("Disconnect: %d\r\n", errCode);
    errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
    printf("UnRegisterWifiEvent: %d\r\n", errCode);
    RemoveDevice(netId); // remove AP config
    printf("RemoveDevice: %d\r\n", errCode);
    errCode = DisableWifi();
    printf("DisableWifi: %d\r\n", errCode);
}

static void NetDemoTask(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 start\r\n", timeout);
        osDelay(100);
    }
    UdpClient(PARAM_SERVER_ADDR, PARAM_SERVER_PORT);
    // mqtt_connect();
    printf("disconnect to AP ...\r\n");
    DisconnectWithHotspot(netId);
    printf("disconnect to AP done!\r\n");
  }
}

static void NetDemoEntry(void)
{
        GpioInit();
        //设置红色,蓝 色,绿色 LED IO为输出状态
        IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10,          WIFI_IOT_IO_FUNC_GPIO_10_GPIO);
        GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT);
        IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
        GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT);
        IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
        GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT);
    osThreadAttr_t attr;
    attr.name = "NetDemoTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 40960;
    attr.priority = osPriorityNormal;
    if (osThreadNew(NetDemoTask, NULL, &attr) == NULL) {
        printf("[NetDemoEntry] Falied to create NetDemoTask!\n");
    }
}
SYS_RUN(NetDemoEntry);
//=================================================
void UdpClient(const char* host, unsigned short port)
{
       ssize_t retval = 0;
        GpioSetOutputVal(R_LED, 1);
        GpioSetOutputVal(B_LED, 1);
        GpioSetOutputVal(G_LED, 1);
        light_status = 1;
        while (g_connected){
           int sockfd = socket(AF_INET, SOCK_DGRAM, 0); // UDP socket
          printf("ip%s:%d\r\n",host,port);
          struct sockaddr_in toAddr = {0};
          toAddr.sin_family = AF_INET;
          toAddr.sin_port = htons(port);
     if (inet_pton(AF_INET, host, &toAddr.sin_addr) <= 0) {
           printf("inet_pton failed!\r\n");
           goto do_cleanup;
    }


    if(light_status == 1){
            request[6] = '1';
    }else{
            request[6] = '0';
    }
    retval = sendto(sockfd, request, sizeof(request), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));
    if (retval < 0) {
        printf("sendto failed!\r\n");
        goto do_cleanup;
    }
    printf("send UDP message {%s} %ld done!\r\n", request, retval);
    struct sockaddr_in fromAddr = {0};
    socklen_t fromLen = sizeof(fromAddr);
    retval = recvfrom(sockfd, &response, sizeof(response), 0, (struct sockaddr *)&fromAddr, &fromLen);
    if (retval <= 0) {
        printf("recvfrom failed or abort, %ld, %d!\r\n", retval, errno);
        goto do_cleanup;
    }
    response[retval] = '\0';
    printf("recv UDP message {%s} %ld done!\r\n", response, retval);
    printf("peer info: ipaddr = %s, port = %d\r\n", inet_ntoa(fromAddr.sin_addr), ntohs(fromAddr.sin_port));
        if (strncmp((char *)response, (char *)"light", 5) == 0){
            printf("response[6]= %c\n",response[6]);
            if(response[6]=='1'){
                                  GpioSetOutputVal(R_LED, 1);
                                  GpioSetOutputVal(B_LED, 1);
                                  GpioSetOutputVal(G_LED, 1);
                                  light_status = 1;
            }
            else{
                                  GpioSetOutputVal(R_LED, 0);
                                  GpioSetOutputVal(B_LED, 0);
                                  GpioSetOutputVal(G_LED, 0);
                                  light_status = 0;
            }
        }
do_cleanup:
    printf("do_cleanup...\r\n");
    close(sockfd);
    }
}

具体源码和测试软件可以点击下面阅读原文下载!

扫二维码|关注我们

HarmonyOS技术社区

电子发烧友与华为官方共建

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hestia是一个基于树莓派的智能家居项目,它利用了现代技术,如物联网(IoT)、云计算和人工智能(AI)等,为用户提供更加智能化、便利的家居体验。 Hestia的主要特点包括: 1. 轻松部署:Hestia可以在树莓派上轻松部署,使用简单快捷。 2. 模块化设计:Hestia采用模块化设计,可以根据用户需求,自由搭配各种传感器和执行器,实现家居自动化控制。 3. 云端控制:用户可以通过手机或电脑,随时随地远程控制家居设备,方便快捷。 4. 智能化:Hestia利用机器学习和人工智能算法,学习用户的生活习惯和喜好,为用户提供更加便捷的智能家居体验。 Hestia的具体实现方案如下: 1. 硬件方案:使用树莓派作为主控制器,搭配各种传感器和执行器,如温度传感器、湿度传感器、光线传感器、电机、继电器等,实现家居设备的自动化控制。 2. 软件方案:使用Java语言开发控制程序,通过网络连接,将传感器和执行器的数据传输到云端服务器。在云端服务器上,使用Python语言进行数据处理和机器学习算法,根据用户的生活习惯和喜好,自动控制家居设备。 3. 用户界面:提供手机App和网页端,用户可以随时随地远程控制家居设备,查看家居设备的状态和历史数据。用户可以通过App或网页端设置家居设备的自动化控制规则和时间表。 总之,Hestia是一款高效、智能、可扩展的智能家居系统,将为用户带来更加便利、舒适的家居体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值