|
后面自己看看吧,我要下班了,所以撤了,如果不懂的,可以說一下,如果有不對的,也說一下,我就昨天正好看關(guān)于WIFI的代碼啊啊,學(xué)習(xí)AT指令玩玩,如果理解不對,技術(shù)水平低,也請見諒。充實(shí)自己,加油,如果我的CSDN資料開放的話,也可以看到相關(guān)解說,還沒寫完,估計(jì)暫不開放
- void ESP8266_StaTcpClient_UnvarnishTest ( void )//客服端,采用TCP協(xié)議通信
- {
- uint8_t ucId, ucLen;//定義ID號,數(shù)據(jù)長度
- uint8_t ucLed1Status = 0, ucLed2Status = 0, ucLed3Status = 0, ucBuzzerStatus = 0;//控制模塊初始化,例如LED1,LED2.。。。
- char cStr [ 100 ] = { 0 }, cCh;//定義數(shù)組以及變量,方便后面使用
- char * pCh, * pCh1;//定義指針
- DHT11_Data_TypeDef DHT11_Data;//DHT11溫濕度傳感器數(shù)據(jù)
-
- printf ( "\r\n正在配置 ESP8266 ......\r\n" );//打印正在配置WIFI信息(正常應(yīng)該是串口顯示)
- macESP8266_CH_ENABLE();//wifi 模塊使能,應(yīng)該使其工作,代碼不全,我估計(jì)實(shí)際就是是CH_PD相應(yīng)引腳置高
-
- ESP8266_AT_Test ();//AT指令測試,正常是發(fā)送AT+空格,返回OK
-
- ESP8266_Net_Mode_Choose ( AP );//選擇工作模式(AP模式)這個(gè)模式相當(dāng)于WIFI熱點(diǎn)
- while ( ! ESP8266_CIPAP ( macUser_ESP8266_TcpServer_IP ) );//檢測服務(wù)器IP(具體的能看一下代碼好說)
- while ( ! ESP8266_BuildAP ( macUser_ESP8266_BulitApSsid, macUser_ESP8266_BulitApPwd, macUser_ESP8266_BulitApEcn ) ); //建立AP 并設(shè)置相關(guān)信息
-
- ESP8266_Enable_MultipleId ( ENABLE );//允許多路通信
-
- while ( ! ESP8266_StartOrShutServer ( ENABLE, macUser_ESP8266_TcpServer_Port, macUser_ESP8266_TcpServer_OverTime ) );
- ESP8266_Inquire_ApIp ( cStr, 20 );
- printf ( "\r\n本模塊WIFI為%s,密碼開放\r\nAP IP 為:%s,開啟的端口為:%s\r\n手機(jī)網(wǎng)絡(luò)助手連接該 IP 和端口,最多可連接5個(gè)客戶端\r\n",
- macUser_ESP8266_BulitApSsid, cStr, macUser_ESP8266_TcpServer_Port );//設(shè)定5個(gè)原因也好玩,自己看代碼
-
-
- strEsp8266_Fram_Record .InfBit .FramLength = 0;//接收數(shù)據(jù)位初始化
- strEsp8266_Fram_Record .InfBit .FramFinishFlag = 0; //最后一位檢測為初始化
-
- while ( 1 )
- {
- if ( strEsp8266_Fram_Record .InfBit .FramFinishFlag )//標(biāo)志位位1
- {
- USART_ITConfig ( macESP8266_USARTx, USART_IT_RXNE, DISABLE ); //禁用串口接收中斷
- strEsp8266_Fram_Record .Data_RX_BUF [ strEsp8266_Fram_Record .InfBit .FramLength ] = '\0';//增加空格符
-
- // printf ( "\r\n%s\r\n", strEsp8266_Fram_Record .Data_RX_BUF );//
- if ( ( pCh = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "CMD_LED_" ) ) != 0 ) //strstr字符串比較函數(shù),存在后者字符返回1
- {
- cCh = * ( pCh + 8 );//為啥要加8呢?實(shí)際上是因?yàn)镃MD_LED_占8個(gè)字符,要用的數(shù)據(jù)在它后一位,起始位為0,所以加8后指針調(diào)用就是它要的數(shù)據(jù)
-
- switch ( cCh )
- {
- case '1':
- cCh = * ( pCh + 10 );//同理,
- switch ( cCh )
- {
- case '0':
- macLED1_OFF ();
- ucLed1Status = 0;
- break;
- case '1':
- macLED1_ON ();
- ucLed1Status = 1;
- break;
- default :
- break;
- }
- break;
-
- case '2':
- cCh = * ( pCh + 10 );
- switch ( cCh )
- {
- case '0':
- macLED2_OFF ();
- ucLed2Status = 0;
- break;
- case '1':
- macLED2_ON ();
- ucLed2Status = 1;
- break;
- default :
- break;
- }
- break;
- case '3':
- cCh = * ( pCh + 10 );
- switch ( cCh )
- {
- case '0':
- macLED3_OFF ();
- ucLed3Status = 0;
- break;
- case '1':
- macLED3_ON ();
- ucLed3Status = 1;
- break;
- default :
- break;
- }
- break;
-
- default :
- break;
-
- }
-
- sprintf ( cStr, "CMD_LED_%d_%d_%d_ENDLED_END", ucLed1Status, ucLed2Status, ucLed3Status );
-
- }
-
- else if ( ( pCh = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "CMD_BUZZER_" ) ) != 0 )
- {
- cCh = * ( pCh + 11 );
-
- switch ( cCh )
- {
- case '0':
- macBEEP_OFF ();
- ucBuzzerStatus = 0;
- break;
- case '1':
- macBEEP_ON ();
- ucBuzzerStatus = 1;
- break;
- default:
- break;
- }
-
- sprintf ( cStr, "CMD_BUZZER_%d_ENDBUZZER_END", ucBuzzerStatus );
-
- }
-
- else if ( ( ( pCh = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "CMD_UART_" ) ) != 0 ) &&
- ( ( pCh1 = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "_ENDUART_END" ) ) != 0 ) )
- {
- if ( pCh < pCh1)
- {
- ucLen = pCh1 - pCh + 12;
- memcpy ( cStr, pCh, ucLen );
- cStr [ ucLen ] = '\0';
- }
- }
- else if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "CMD_READ_ALL_END" ) != 0 )
- {
- DHT11_Read_TempAndHumidity ( & DHT11_Data );
- sprintf ( cStr, "CMD_LED_%d_%d_%d_ENDLED_DHT11_%d.%d_%d.%d_ENDDHT11_BUZZER_%d_ENDBUZZER_END",
- ucLed1Status, ucLed2Status, ucLed3Status, DHT11_Data .temp_int,
- DHT11_Data .temp_deci, DHT11_Data .humi_int, DHT11_Data .humi_deci,
- ucBuzzerStatus );
- }
-
-
- if ( ( pCh = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "+IPD," ) ) != 0 )
- {
- ucId = * ( pCh + strlen ( "+IPD," ) ) - '0';
- ESP8266_SendString ( DISABLE, cStr, strlen ( cStr ), ( ENUM_ID_NO_TypeDef ) ucId );
- }
-
- strEsp8266_Fram_Record .InfBit .FramLength = 0;
- strEsp8266_Fram_Record .InfBit .FramFinishFlag = 0;
-
- USART_ITConfig ( macESP8266_USARTx, USART_IT_RXNE, ENABLE ); //使能串口接收中斷
-
- }
-
- }
-
-
- }
復(fù)制代碼
|
|