#if defined(CONFIG_AT_COMMAND) || defined(CONFIG_FACTORY_TEST_MODE) ret = hi_at_init(); if (ret == HI_ERR_SUCCESS) { hi_at_sys_cmd_register(); } #endif
/* 如果不需要使用Histudio查看WIFI驱动运行日志等,无需初始化diag */ /* if not use histudio for diagnostic, diag initialization is unnecessary */ /* Shell and Diag use the same uart port, only one of them can be selected */ #ifndef CONFIG_FACTORY_TEST_MODE
// LED3的GPIO初始化 GPIO initialization of LED3 IoTGpioInit(IOT_IO_NAME_GPIO_9);
依赖库:src_hardware_gpio.h, 说明如下
1 2 3 4 5 6 7 8 9 10
/** * @brief Initializes a GPIO device. * * @param id Indicates the GPIO pin number. * @return Returns {@link IOT_SUCCESS} if the GPIO device is initialized; * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description. * @since 2.2 * @version 2.2 */ unsignedintIoTGpioInit(unsignedint id);
2. IoSetFunc()
作用:配置指定IO的复用功能 示例:
1 2
// 设置GPIO9的管脚复用关系为GPIO Set the pin reuse relationship of GPIO9 to GPIO IoSetFunc(IOT_IO_NAME_GPIO_9, IOT_IO_FUNC_GPIO_9_GPIO);
依赖库:src-iot_demo_iot_gpio_ex.c 说明如下:
1 2 3 4 5 6 7
unsignedintIoSetFunc(unsignedint id, unsignedchar val) { if (id >= HI_GPIO_IDX_MAX) { return IOT_FAILURE; } return hi_io_set_func((hi_io_name)id, val); }
### 3.
IoTGpioSetDir() 作用:设置指定IO的管脚方向 示例:
1 2
// GPIO方向设置为输出 GPIO direction set to output IoTGpioSetDir(IOT_IO_NAME_GPIO_9, IOT_GPIO_DIR_OUT);
依赖库:src_hardware_gpio.h, 说明如下
1 2 3 4 5 6 7 8 9 10 11
/** * @brief Sets the direction for a GPIO pin. * * @param id Indicates the GPIO pin number. * @param dir Indicates the GPIO input/output direction. * @return Returns {@link IOT_SUCCESS} if the direction is set; * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description. * @since 2.2 * @version 2.2 */ unsignedintIoTGpioSetDir(unsignedint id, IotGpioDir dir);
### 4.
IoTGpioSetOutputVal() 作用:设置指定IO的输出电平 示例:
1 2 3 4 5 6
/* 设置GPIO09输出低电平 * Set GPIO09 output high level to turn on red traffic light LED 3 * VALUE0: low level * VALUE1: high level */ IoTGpioSetOutputVal(IOT_IO_NAME_GPIO_9, IOT_GPIO_VALUE0);
二、GPIO使用进阶
API说明
1. IoSetPull()
作用:设置指定IO的上下拉电阻 示例:
1 2 3 4 5
/* * 设置GPIO0为上拉功能 * Set GPIO0 as pull-up function */ IoSetPull(IOT_IO_NAME_GPIO_0, IOT_IO_PULL_UP);
依赖库:src-iot_demo_iot_gpio_ex.c 对第二个参数说明如下:
1 2 3 4 5 6 7 8
/** No pull */ IOT_IO_PULL_NONE, /** Pull-up */ IOT_IO_PULL_UP, /** Pull-down */ IOT_IO_PULL_DOWN, /** Maximum value */ IOT_IO_PULL_MAX
### 2. IoTGpioRegisterIsrFunc()
作用:设置指定GPIO的中断功能 示例:
1 2 3 4 5 6 7
/* * 使能GPIO0的中断功能, 上升沿触发中断,LeftBCounterHandler为中断的回调函数 * Enable the interrupt function of GPIO0, the rising edge triggers * the interrupt, and LeftBCounterHandler is the interrupt callback function */ IoTGpioRegisterIsrFunc(IOT_IO_NAME_GPIO_0, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_RISE_LEVEL_HIGH, LeftBCounterHandler, NULL);
/** Interrupt at a low level or falling edge */ IOT_GPIO_EDGE_FALL_LEVEL_LOW = 0, /** Interrupt at a high level or rising edge */ IOT_GPIO_EDGE_RISE_LEVEL_HIGH
staticvoidDemoMsgRcvCallBack(int qos, char *topic, char *payload) { constchar *requesID; char *tmp; IoTCmdResp resp; printf("RCVMSG:QOS:%d TOPIC:%s PAYLOAD:%s\r\n", qos, topic, payload); /* app 下发的操作 */ TrafficLightMsgRcvCallBack(payload); tmp = strstr(topic, CN_COMMAND_INDEX); if (tmp != NULL) { // /< now you could deal your own works here --THE COMMAND FROM THE PLATFORM // /< now er roport the command execute result to the platform requesID = tmp + strlen(CN_COMMAND_INDEX); resp.requestID = requesID; resp.respName = NULL; resp.retCode = 0; ////< which means 0 success and others failed resp.paras = NULL; (void)IoTProfileCmdResp(CONFIG_DEVICE_PWD, &resp); } }
1 2 3 4 5 6 7 8 9 10 11
staticvoidTrafficLightMsgRcvCallBack(char *payload) { printf("PAYLOAD:%s\r\n", payload); if (strstr(payload, TRAFFIC_LIGHT_CMD_CONTROL_MODE) != NULL) { if (strstr(payload, TRAFFIC_LIGHT_RED_ON_PAYLOAD) != NULL) { // RED LED RedLight(); } elseif (strstr(payload, TRAFFIC_LIGHT_RED_OFF_PAYLOAD) != NULL) { RedOff(); } } }
解析: line 4:用 memset_s 函数将 property 变量的内存清零 line
5:设置其类型为字符串类型,名为 “ControlModule”
if:判断g_lightstatus的值并根据情况设置property的value字段 line 12:使用
memset_s 函数将 service 变量的内存清 line 13-14:设置服务 ID 为
“TrafficLight”,并将其服务属性指向 property 变量。 line
15:调用 IoTProfilePropertyReport 函数,使用设备 ID
和服务信息来上报物联网设备的状态信息。
3.demo main task entry
代码结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// /< this is the demo main task entry,here we will set the wifi/cjson/mqtt ready ,and // /< wait if any work to do in the while staticvoidDemoEntry(void) { ConnectToHotspot(); //调用 ConnectToHotspot 函数来连接热点 RedLedInit(); //调用 RedLedInit 函数来初始化红色 LED 灯 CJsonInit(); //调用 CJsonInit 函数来初始化 CJson 库。 IoTMain(); //调用 IoTMain 函数来启动物联网主程序。 IoTSetMsgCallback(DemoMsgRcvCallBack); //调用 IoTSetMsgCallback 函数来设置消息回调函数为 DemoMsgRcvCallBack,详情见条目1 TaskMsleep(30000); // 30000 = 3s连接华为云 /* 主动上报 */ while (1) { // here you could add your own works here--we report the data to the IoTplatform(在这里你可以添加自己的工作--我们将数据上报到物联网平台) TaskMsleep(TASK_SLEEP_1000MS); // know we report the data to the iot platform(将数据上报到物联网平台) IotPublishSample(); } }
// /< this is the demo main task entry,here we will set the wifi/cjson/mqtt ready ,and // /< wait if any work to do in the while staticvoidDemoEntry(void) { ConnectToHotspot(); CJsonInit(); IoTMain(); TaskMsleep(30000); // 30000 = 3s连接华为云 /* 主动上报 */ while (1) { // here you could add your own works here--we report the data to the IoTplatform TaskMsleep(TASK_SLEEP_1000MS); // know we report the data to the iot platform IotPublishSample_Temp_ControlModule(); IotPublishSample_Humi_ControlModule(); IotPublishSample_Gas_ControlModule(); } }
// /<CONFIG THE WIFI /* Please modify the ssid and pwd for the own */ #define CONFIG_AP_SSID "MIX4"// WIFI SSID #define CONFIG_AP_PWD "ez20020328"// WIFI PWD
// /<Configure the iot platform /* Please modify the device id and pwd for your own */ // 设备ID名称,请参考华为物联网云文档获取该设备的ID。例如:60790e01ba4b2702c053ff03_helloMQTT #define CONFIG_DEVICE_ID "64a98f799415fc7a573be8f0_Temp_Humi_Gas_Detector" // 设备密码,请参考华为物联网云文档设置该设备密码。例如:hispark2021 #define CONFIG_DEVICE_PWD "d500d9f5e8b5ad551b4b90181d8f22f0166b8afa91540e8b4bd995648665e483" #define CONFIG_CLIENTID "64a98f799415fc7a573be8f0_Temp_Humi_Gas_Detector_0_0_2023070818"