Espressif Systems ESP8266EX Programming Manual page 24

Esp8266 rtos sdk
Hide thumbs Also See for ESP8266EX:
Table of Contents

Advertisement

!
#define SERVER_IP "192.168.1.124"
#define SERVER_PORT 1001
3. Implement the TCP communication via socket programming.
Create a socket:
sta_socket = socket(PF_INET, SOCK_STREAM, 0);
if (-1 == sta_socket) {
close(sta_socket);
vTaskDelay(1000 / portTICK_RATE_MS);
printf("ESP8266 TCP client task > socket fail!\n");
continue;
}
printf("ESP8266 TCP client task > socket ok!\n");
Create a TCP connection:
bzero(&remote_ip, sizeof(struct sockaddr_in));
remote_ip.sin_family = AF_INET;
remote_ip.sin_addr.s_addr = inet_addr(SERVER_IP);
remote_ip.sin_port = htons(SERVER_PORT);
if (0 != connect(sta_socket, (struct sockaddr *)(&remote_ip), sizeof(struct sockaddr)))
{
close(sta_socket);
vTaskDelay(1000 / portTICK_RATE_MS);
printf("ESP8266 TCP client task > connect fail!\n");
continue;
}
printf("ESP8266 TCP client task > connect ok!\n");
Send data packets via TCP communication:
if (write(sta_socket, pbuf, strlen(pbuf) + 1) < 0){
close(sta_socket);
vTaskDelay(1000 / portTICK_RATE_MS);
printf("ESP8266 TCP client task > send fail\n");
continue;
}
printf("ESP8266 TCP client task > send success\n");
free(pbuf);
Receive data packets via TCP communication:
char *recv_buf = (char *)zalloc(128);
while ((recbytes = read(sta_socket , recv_buf, 128)) > 0) {
recv_buf[recbytes] = 0;
Espressif
20 37
!
/!
3. Sample Codes
2017.05

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the ESP8266EX and is the answer not in the manual?

Questions and answers

Table of Contents