Comments (3)
Here is a complete working example based on your code. I set it to connect to a public ftp server for demo purposes. Note that I changed the location of your zalloc since I was getting an exception using your placement (probably due to proximity of the network i/o code). I threw in a few delays here & there so it won't pound the server to hard.
#include <esp_common.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <uart.h>
#include "lwip/sockets.h"
#define SERVER_IP "90.130.70.73"
#define SERVER_PORT 21
#define TCP_DATA_LEN 256
static int32 sock_fd;
void tcpc_task() {
int32 ret = 0;
int32 recbytes;
char *recv_buf = (char *)malloc(TCP_DATA_LEN);
struct sockaddr_in tcpServerAddr;
memset(&tcpServerAddr, 0, sizeof(tcpServerAddr));
tcpServerAddr.sin_family = AF_INET;
tcpServerAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
tcpServerAddr.sin_len = sizeof(tcpServerAddr);
tcpServerAddr.sin_port = htons(SERVER_PORT);
while (1) {
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) {
printf("ESP8266 TCP client task > socket error\r\n");
close(sock_fd);
vTaskDelay(1000 / portTICK_RATE_MS);
continue;
}
printf("ESP8266 TCP client task > create socket: %d\r\n", sock_fd);
ret = connect(sock_fd, (struct sockaddr* )&tcpServerAddr,
sizeof(struct sockaddr));
if (ret != 0) {
printf("ESP8266 TCP client task > connect fail!\r\n");
close(sock_fd);
vTaskDelay(1000 / portTICK_RATE_MS);
continue;
}
printf("ESP8266 TCP client task > connect ok!\r\n");
while ((recbytes = read(sock_fd, recv_buf, TCP_DATA_LEN)) > 0) {
recv_buf[recbytes] = 0;
printf("ESP8266 TCP client task > recv data %d bytes!\r\n%s\r\n",
recbytes,recv_buf);
write(sock_fd, recv_buf, strlen(recv_buf) + 1);
vTaskDelay(1000 / portTICK_RATE_MS);
}
vTaskDelay(1000 / portTICK_RATE_MS);
recv_buf[0] = 0;
if (recbytes <= 0) {
printf("ESP8266 TCP client task > read data fail!\n");
close(sock_fd);
}
vTaskDelay(10000 / portTICK_RATE_MS);
}
}
void user_init(void) {
//setup a sane baudrate
UART_ConfigTypeDef uart_config;
uart_config.baud_rate = BIT_RATE_115200;
uart_config.data_bits = UART_WordLength_8b;
uart_config.parity = USART_Parity_None;
uart_config.stop_bits = USART_StopBits_1;
uart_config.flow_ctrl = USART_HardwareFlowControl_None;
uart_config.UART_RxFlowThresh = 120;
uart_config.UART_InverseMask = UART_None_Inverse;
UART_ParamConfig(UART0, &uart_config);
struct station_config cfg;
strcpy((char *)cfg.ssid, "your-wifi-ssid");
strcpy((char *)cfg.password, "your-wifi-key");
wifi_set_opmode(STATION_MODE);
wifi_station_set_config(&cfg);
wifi_station_set_auto_connect(1);
vTaskDelay(3000 / portTICK_RATE_MS); //give wifi chance to connect
xTaskCreate(tcpc_task, (signed char *)"tcpc_task", 512, NULL, 1, NULL);
}
from esp8266_rtos_sdk.
Thanks a lot. I have solved the problem. Because of the firewall of Win7, the TCP Server program can't get any connection from outside. When I trust it, the it works well. Thanks you very much.
from esp8266_rtos_sdk.
Super! I'm glad to hear you solved the issue and shared the solution. I too tend to get hung up (sometimes for days) on simple issues. I find having a fully working example speeds up the troubleshooting.
from esp8266_rtos_sdk.
Related Issues (20)
- How do I.. SD card using example please (GIT8266O-835)
- Linking fails on hello_world example with disabled assertions (GIT8266O-836) HOT 1
- Bootloader compilation fails when "UART for console output" is set to "none" (GIT8266O-837)
- Application doesn't work with assertion level set to "disabled" (GIT8266O-839) HOT 1
- master分支下,使用ADC例程读出来的数据过高 (GIT8266O-840)
- Do not get IP address after call esp_wifi_connect() (GIT8266O-841) HOT 1
- can't find vApplicationGetTimerTaskMemory function. (GIT8266O-843) HOT 1
- CONFIG_DISABLE_ROM_UART_PRINT leads to boot loop (GIT8266O-844)
- Error: invalid register 'intclear' for 'wsr' instruction (GIT8266O-845) HOT 1
- esp-netif interface (GIT8266O-846) HOT 1
- Compiled size (GIT8266O-847) HOT 1
- Proble Install Python Package by Using mingw32 on Windows (GIT8266O-848) HOT 4
- export.sh cannot run correctly on zsh (GIT8266O-849)
- [Bug/suggestion] Diverging baud rates in hello_world example (GIT8266O-850)
- Error in configuring ESP8266 RTOS SDK development environment on Linux??? (GIT8266O-851) HOT 7
- Segment loaded at 0x*** lands in same 64KB flash mapping as segment loaded at 0x*** (GIT8266O-852) HOT 2
- cmake error at cmakelists.txt & menuconfig errors (GIT8266O-853)
- menuconfig script fails for ncurses in Arch-Linux (GIT8266O-855) HOT 5
- 烧录protocols/mqtt/tcp的demo后报错 (GIT8266O-858)
- [Bug] `wifi: err2,exceed max time value,max val is 198A5759` (GIT8266O-859)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esp8266_rtos_sdk.