功能描述
通過mcu與esp01通過uart連接,通過at指令來創建socket連接,接收遠端控制燈的指令,來實現燈亮,滅及閃爍;
開啟兩路串口,usart0(GPIO09-TX,GPIO10-RX)連接esp01,進行收發at指令,usart1(GPIO02-TX,GPIO03-RX)作為log輸出口;
接線圖
ps.電阻可以不接,直接EN和3.3V拉一根線也能起來;
有條件的話,可以選esp01s,不用接電阻。
代碼流程介紹
uart0連接esp01,收發數據使用中斷方式進行,接收數據檢測到OK,ERROR等關鍵字以后關閉接收。
uart中斷
void USART0_IRQHandler(void) { if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) { /* receive data */ rx_buffer[rx_count++] = usart_data_receive(USART0); if(rx_count >= BUFFER_SIZE) { usart_interrupt_disable(USART0, USART_INT_RBNE); printf("rx buffer is full!!\n"); recv_flag = 0; } if(strstr((const char *)rx_buffer, "OK\r\n") || strstr((const char *)rx_buffer, "ERROR\r\n") ){ usart_interrupt_disable(USART0, USART_INT_RBNE); recv_flag = 0; } else if(strstr((const char *)rx_buffer, "blink") || strstr((const char *)rx_buffer, "on") || strstr((const char *)rx_buffer, "off") ){ usart_interrupt_disable(USART0, USART_INT_RBNE); recv_flag = 0; } } if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE)) { /* transmit data */ usart_data_transmit(USART0, tx_buffer[tx_count++]); if(tx_count >= BUFFER_SIZE) { usart_interrupt_disable(USART0, USART_INT_TBE); send_flag = 0; } if(tx_count >= send_len) { usart_interrupt_disable(USART0, USART_INT_TBE); send_flag = 0; send_len = 0; } } }
uart初始化:
/* enable COM GPIO clock */ rcu_periph_clock_enable(RCU_GPIOA); /* connect port to USARTx_Tx */ gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9); /* connect port to USARTx_Rx */ gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10); /* configure USART Tx as alternate function push-pull */ gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9); gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9); /* configure USART Rx as alternate function push-pull */ gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10); gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10); /* enable USART clock */ rcu_periph_clock_enable(RCU_USART0); /* USART configure */ usart_deinit(USART0); usart_baudrate_set(USART0, 115200U); usart_receive_config(USART0, USART_RECEIVE_ENABLE); usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); usart_enable(USART0); nvic_irq_enable(USART0_IRQn, 0, 0);
LED初始化:
GPIO_InitTypeDef GPIO_InitStruct = {0}; /*GPIO Port Clock Enable */ rcu_periph_clock_enable(RCU_GPIOA); /*Configure GPIO pin : PtPin PA8*/ GPIO_InitStruct.gpio_periph = GPIOA; GPIO_InitStruct.mode = GPIO_MODE_OUTPUT; GPIO_InitStruct.pin = GPIO_PIN_8; GPIO_InitStruct.otype = GPIO_OTYPE_PP; GPIO_InitStruct.speed = GPIO_OSPEED_50MHZ; GPIO_InitStruct.pull_up_down = GPIO_PUPD_NONE; gpio_mode_set(GPIO_InitStruct.gpio_periph,GPIO_InitStruct.mode,GPIO_InitStruct.pull_up_down,GPIO_InitStruct.pin); /*Configure GPIO pin Output Level */ gpio_output_options_set(GPIO_InitStruct.gpio_periph,GPIO_InitStruct.otype,GPIO_InitStruct.speed,GPIO_InitStruct.pin); GPIO_BC(GPIO_InitStruct.gpio_periph) = GPIO_InitStruct.pin; /* EXTI interrupt init*/
主程序代碼:
int main(void) { e_at_state at_state = e_init; /* enable DMA clock */ init(); led_flash(5); while(1) { switch (at_state) { case e_init: sat("ATE0\r\n", "OK"); if(!sat("AT\r\n", "OK")){ led_on(); at_state = e_at_ok; } else{ delay_1ms(100); led_off(); } break; case e_at_ok: if(!sat("AT+CWJAP="testssid","12345678"\r\n", "WIFI GOT IP")) { at_state = e_wifi_ok; } else{ delay_1ms(100); } break; case e_wifi_ok: if(!sat("AT+CIFSR\r\n", "CIFSR:STAIP")) { at_state = e_sock_ok; } else { delay_1ms(100); } at_state = e_sock_ok; break; case e_sock_ok: //close socket sat("AT+CIPCLOSE\r\n", "OK"); //start a new socket if(!sat("AT+CIPSTART="TCP","192.168.1.36",1314\r\n", "CONNECT")) { //sat("AT+CIPSEND=10","OK"); at_state = e_connect_ok; } else { delay_1ms(100); } break; case e_connect_ok: { char at_resp[128] = {0}; wait_atrsp(at_resp); if(strstr(at_resp,"on")){ printf("light on\n"); led_on(); } else if(strstr(at_resp,"off")){ printf("light off\n"); led_off(); } else if(strstr(at_resp, "blink")) { printf("buling buling\n"); led_flash(10); } else { printf("other cmd got:[%s]\r\n", at_resp); } } break; default: printf("Get into other:%d\n",at_state); break; } //delay_1ms(100); } }
待改善
usart接收數據采用關鍵字判斷結束,效率較低;
at指令接收沒有超時,如果得不到期望結果會卡住;
socket異常斷開沒有做處理;
后面可以接一個繼電器控制插座或者臺燈
因為esp01升級不到最新版本,無法支持mqtt,只能點對點控制玩玩了,支持mqtt就可以做更多事情;
異步中斷處理usart_interrupt_flag_get不是很理解,正常接收或者發送完數據應該是可以退出,但是好像一直無法退出,不知道是使用的問題還是哪里出的問題,有經驗的大神可以交流交流指導指導。
審核編輯:劉清
-
GPIO
+關注
關注
16文章
1216瀏覽量
52267 -
UART接口
+關注
關注
0文章
124瀏覽量
15322 -
ESP8266
+關注
關注
50文章
962瀏覽量
45231
發布評論請先 登錄
相關推薦
評論