EasyManua.ls Logo

Espressif Systems ESP8266EX - Page 27

Espressif Systems ESP8266EX
41 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
!
3. Sample Codes
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧}
󰲧󰲧󰲧󰲧}while(ret󰲧!=󰲧0);
󰲧󰲧󰲧󰲧printf("ESP8266󰲧TCP󰲧server󰲧task󰲧>󰲧listen󰲧ok\n”);
Wait until the TCP client is connected to the server; then start receiving data packets
when the TCP communication is established:
󰲧󰲧󰲧󰲧int32󰲧client_sock;
󰲧󰲧󰲧󰲧int32󰲧len󰲧=󰲧sizeof(struct󰲧sockaddr_in);
󰲧󰲧󰲧󰲧for󰲧(;;)󰲧{
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧printf("ESP8266󰲧TCP󰲧server󰲧task󰲧>󰲧wait󰲧client\n”);
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧/*block󰲧here󰲧waiting󰲧remote󰲧connect󰲧request*/
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧if󰲧((client_sock󰲧=󰲧accept(listenfd,󰲧(struct󰲧sockaddr󰲧*)&remote_addr,󰲧(socklen_t󰲧
*)&len))󰲧<󰲧0)󰲧{
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧printf("ESP8266󰲧TCP󰲧server󰲧task󰲧>󰲧accept󰲧fail\n");
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧continue;
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧}
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧printf("ESP8266󰲧TCP󰲧server󰲧task󰲧>󰲧Client󰲧from󰲧%s󰲧%d\n",󰲧
inet_ntoa(remote_addr.sin_addr),󰲧htons(remote_addr.sin_port));
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧char󰲧*recv_buf󰲧=󰲧(char󰲧*)zalloc(128);
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧while󰲧((recbytes󰲧=󰲧read(client_sock󰲧,󰲧recv_buf,󰲧128))󰲧>󰲧0)󰲧{
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧recv_buf[recbytes]󰲧=󰲧0;
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧printf("ESP8266󰲧TCP󰲧server󰲧task󰲧>󰲧read󰲧data󰲧success󰲧%d!\nESP8266󰲧TCP󰲧server󰲧task󰲧
>󰲧%s\n",󰲧recbytes,󰲧recv_buf);
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧}
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧free(recv_buf);
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧if󰲧(recbytes󰲧<=󰲧0)󰲧{
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧printf("ESP8266󰲧TCP󰲧server󰲧task󰲧>󰲧read󰲧data󰲧fail!\n");
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧close(client_sock);
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧}
󰲧󰲧󰲧󰲧}
2. Compile the application program, generate firmware and download it into the ESP8266
module.
3. Power off the module, and change it to operation mode; then power on the module and
run the program.
4. Establish a TCP client using the network debugging tool, then connect the TCP client
with the ESP8266 TCP server, and start sending data.
Espressif
! /!23 37
2017.05

Other manuals for Espressif Systems ESP8266EX

Related product manuals