Пытаюсь освоить передачу по TCP. С заводской прошивкой все работает корректно. С моим кодом удалось каким-то чудом передать одно сообщение. Повторить передачу не удалось. Либо получаю перезагрузку, либо TCP соединение не устанавливается. Прошу подсказать где я ошибаюсь.
Код:
/*
The hello world demo
*/
#include <ets_sys.h>
#include <osapi.h>
#include "user_interface.h"
#include <os_type.h>
#include <gpio.h>
#include "driver/uart.h"
#include "espconn.h"
#include "uart.c"
//LOCAL os_timer_t hello_timer;
extern int ets_uart_printf(const char *fmt, ...);
void tcp_recon_cb(void *arg, sint8 err);
uint32 ICACHE_FLASH_ATTR user_rf_cal_sector_set(void)
{
enum flash_size_map size_map = system_get_flash_size_map();
uint32 rf_cal_sec = 0;
switch (size_map) {
case FLASH_SIZE_4M_MAP_256_256:
rf_cal_sec = 128 - 8;
break;
case FLASH_SIZE_8M_MAP_512_512:
rf_cal_sec = 256 - 5;
break;
case FLASH_SIZE_16M_MAP_512_512:
case FLASH_SIZE_16M_MAP_1024_1024:
rf_cal_sec = 512 - 5;
break;
case FLASH_SIZE_32M_MAP_512_512:
case FLASH_SIZE_32M_MAP_1024_1024:
rf_cal_sec = 1024 - 5;
break;
default:
rf_cal_sec = 0;
break;
}
return rf_cal_sec;
}
void ICACHE_FLASH_ATTR user_rf_pre_init(void)
{
}
void tcpconnect_done_cb(void *arg)
{
struct espconn *conn1=arg;
os_printf("TCP done!\r\n");
uint8 buffer[]="Gaga";
//uint16 len=os_strlen(buffer);
espconn_send(conn1,buffer,5);
}
void connect_done(void)
{
struct espconn conn;
esp_tcp tcp;
struct ip_info ipinfo;
os_printf("TCP_connection_starting...\r\n");
conn.type=ESPCONN_TCP;
conn.state=ESPCONN_NONE;
tcp.remote_port=50153;
tcp.remote_ip[1]=(uint8)(192);
tcp.remote_ip[2]=(uint8)(168);
tcp.remote_ip[3]=(uint8)(43);
tcp.remote_ip[4]=(uint8)(1);
tcp.local_port=espconn_port();
wifi_get_ip_info(STATION_IF,&ipinfo);
os_memcpy(tcp.local_ip,&ipinfo.ip,4);
conn.proto.tcp=&tcp;
espconn_regist_connectcb(&conn,tcpconnect_done_cb);
//espconn_regist_reconcb(&conn,tcp_recon_cb);
espconn_connect(&conn);
}
/*void tcp_recon_cb(void *arg, sint8 err)
{
if (err != ESPCONN_OK)
{
os_printf("Connection error: %d \r\n", err);
}
os_delay_us(2000);
connect_done();
}*/
void eventHandler(System_Event_t *event)
{
switch(event->event)
{
case EVENT_STAMODE_GOT_IP:
os_printf("We got IP!\r\n");
connect_done();
break;
}
}
void init_done(void)
{
os_printf("init_done!\r\n");
wifi_set_phy_mode(PHY_MODE_11N);
wifi_set_opmode(STATION_MODE);
struct station_config stationconfig;
strncpy(stationconfig.ssid,"Lenovo A5000",32);
strncpy(stationconfig.password,"9009Laros",64);
stationconfig.bssid_set=1;
wifi_station_set_config(&stationconfig);
wifi_station_connect();
wifi_station_set_auto_connect(1);
}
void ICACHE_FLASH_ATTR user_init(void)
{
uart_init(BIT_RATE_115200,BIT_RATE_115200);
wifi_set_event_handler_cb(eventHandler);
system_init_done_cb(init_done);
}