#include "relay.h" //用户继电器引脚初始化 void relayInit(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIOA_RCC_ENABLE; GPIO_InitStruct.Pin = Realy1_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(Realy1_GPIO_Port, &GPIO_InitStruct); GPIO_InitStruct.Pin = Realy2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(Realy2_GPIO_Port, &GPIO_InitStruct); GPIO_InitStruct.Pin = Realy3_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(Realy3_GPIO_Port, &GPIO_InitStruct); GPIO_InitStruct.Pin = Realy4_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(Realy4_GPIO_Port, &GPIO_InitStruct); Relay1_OFF; Relay2_OFF; Relay3_OFF; Relay4_OFF; } // 继电器响应网络数据 //收到子设备控制指令:+MSUB: "/iot/data/down/a284c67982b089f9",119 byte,{"id":"111750363407137710","header":"iot.prop.set","version":"1.0","body":{"sw1":{"time":1746600094207,"value":false}}} void relayAction(uint8_t *data) { uint8_t i = 0; char *TempPointer; // 临时指针 char CmdId[18] = {0}; // 存贮id的数据,以响应服务器下发的数据 char IdFlag = 0; // id的计数标志位,用于判断ID中是否出现异常字符 if(strstr((char *)data,MqttInfo_Struct.Topic)) { //查找 /iot/data/down/ 出现的位置 TempPointer = strstr((char *)data,"\"id\":"); // 记录ID位置 {"id":"1117 TempPointer += 6; //除去"id": 5个字符 应该从第6个开始取 IdFlag = 0; log_info("ID起始位置为%s", TempPointer); //打印结果:ID起始位置为977187088401715548","header":"iot.sub.prop.set","version":"1.0","body":{"sw2":{"time":1713146022764,"value":true}}} for (i = 0; i < 18; i++) { // 存储id(响应帧,必要参数) if (((TempPointer[i] >= '0') && (TempPointer[i] <= '9')) || ((TempPointer[i] >= 'a') && (TempPointer[i] <= 'z')) || ((TempPointer[i] >= 'A') && (TempPointer[i] <= 'Z'))) { CmdId[i] = TempPointer[i]; IdFlag++; } else { IdFlag = 0; log_info("ID中混有非法字符,将不会发送反馈数据"); log_info("ID指针为%s", TempPointer); //此语句千万不能用printf 而是得用log_info 否则程序卡死 很难找到原因 } } if (IdFlag >= 18) { // ID为18位,当ID>=18时说明ID正常 log_info("ID中一切字符正常"); } if(strstr((char *)data,"\"sw")) { //先判断收到的数据是不是开关量 TempPointer = strstr((char *)data,"\"sw"); // 记录sw位置 switch(TempPointer[3]) { // 判断操作哪个开关 case '1': if (strstr((char *)data, "true")) { log_info("开关1打开"); Relay1_ON;// } if (strstr((char *)data, "false")) { log_info("开关1关闭"); Relay1_OFF;// } break; case '2': if (strstr((char *)data, "true")) { log_info("开关2打开"); Relay2_ON;// } if (strstr((char *)data, "false")) { log_info("开关2关闭"); Relay2_OFF;// } break; case '3': if (strstr((char *)data, "true")) { log_info("开关3打开"); Relay3_ON;// } if (strstr((char *)data, "false")) { log_info("开关3关闭"); Relay3_OFF;// } break; case '4': if (strstr((char *)data, "true")) { log_info("开关4打开"); Relay4_ON;// } if (strstr((char *)data, "false")) { log_info("开关4关闭"); Relay4_OFF;// } break; } } //推送回复消息 OK CAT1_printf("AT+MPUB=\"/iot/data/up/%s\",0,0,\"{\\22id\\22:\\22%s\\22,\\22code\\22:0,\\22message\\22:\\22OK\\22}\"\r\n",MqttInfo_Struct.ClientID, CmdId); } }