1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| #include "stm32f10x.h" #include <string.h> #include <stdio.h> #include "HuaweiIOT_at_esp8266.h"
uint8_t uart1_rec_i = 0; extern uint8_t atok_rec_flag; char uart1_recdata = 0, uart1_recstring[256], sub_string[150], analysis_Str[256]; extern uint16_t speed; uint16_t speed_flag;
void MyUSART_Init(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Init(USART1, &USART_InitStructure);
NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_Cmd(USART1, ENABLE);
}
void USART1_IRQHandler(void) { char str_temp[128]; char request_id[48];
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { USART_ClearITPendingBit(USART1, USART_IT_RXNE); uart1_recdata = USART_ReceiveData(USART1); if (uart1_recdata != '\n') { if ((uart1_rec_i == 0) && (uart1_recdata == ' ')) { } else { uart1_recstring[uart1_rec_i] = uart1_recdata; uart1_rec_i++; } } else { if (uart1_rec_i >= 2) { strncpy(sub_string, uart1_recstring, 2); if (strcmp(sub_string, "OK") == 0) { atok_rec_flag = 1; }
strncpy(sub_string, uart1_recstring, 5); if (strcmp(sub_string, "ready") == 0) { atok_rec_flag = 1; }
strncpy(sub_string, uart1_recstring, 5); if (strcmp(sub_string, "ERROR") == 0) { atok_rec_flag = 0; } if (uart1_rec_i >= 190) { strncpy(analysis_Str, uart1_recstring, 12); if (strcmp(analysis_Str, "+MQTTSUBRECV") == 0) { memset(analysis_Str, 0, sizeof(analysis_Str)); strncpy(request_id, uart1_recstring + 84, 36); strncpy(analysis_Str, uart1_recstring + 127, 5); if (strcmp(analysis_Str, "paras") == 0) { memset(analysis_Str, 0, sizeof(analysis_Str)); strncpy(analysis_Str, uart1_recstring + 136, 7); if (strcmp(analysis_Str, "ActVel1") == 0) {
if (uart1_recstring[144] == ':') { speed = (uart1_recstring[145] - '0') * 10 + (uart1_recstring[146] - '0');
} else if (uart1_recstring[145] == '1') { printf("开灯\r\n"); } printf("AT+MQTTPUB=0,\"$oc/devices/%s/sys/commands/response/request_id=%s\",\"\",0,0\r\n", HUAWEI_MQTT_DeviceID, request_id);
memset(analysis_Str, 0, sizeof(analysis_Str)); memset(str_temp, 0, sizeof(str_temp)); } } }
} } uart1_rec_i = 0; memset(uart1_recstring, 0, sizeof(uart1_recstring)); memset(sub_string, 0, sizeof(sub_string)); } } }
int fputc(int ch, FILE *f) { USART_SendData(USART1, (uint8_t) ch); while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); return ch; }
|