A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By EUA
#5019 I want to use device directly instead of AT commands. I believe no one need an arduino since this device much more capable than arduino (at least from UNO).

But I do not understand current IoT samples.Hard to understand / follow code since its fragmented.

All I want to have simple Hello World application that:
  • Sets Wifi connection (to connect my router example) and get IP from dhcp
  • Initialize / Open a TCP or UDP port and listen commands from it

in simple way.
But can't find any code that do this job properly.

Here is my try. At least it can setup wifi connection:
Code: Select allvoid ICACHE_FLASH_ATTR
WiFiConnector( void ){
    char temp[32];
    //set mode to STA
    wifi_set_opmode(1);

    //prepare SSID and Passwd of the router
    struct station_config stationConf;
    os_bzero(&stationConf, sizeof(struct station_config));
    strcpy( stationConf.ssid, "yourssid" );
    strcpy( stationConf.password, "password" );
   
    //Good to issue disconnect first.
    wifi_station_disconnect());

    //Setting up wifi
    wifi_station_set_config(&stationConf);

    //Start connectiong
    wifi_station_connect();

    //Here is the tricky part.   
    //we have to check if there is connection. But we can't use os_delay_us(1000000) with "while" loop because wdt_reset occurs.
    //so we just put a timer that calls the function each second about if connection is OK
    os_timer_disarm(&WifiConnectionDelay);
    os_timer_setfn(&WifiConnectionDelay, (os_timer_func_t *)WifiConnectionDelayTimer, NULL);
    os_timer_arm(&WifiConnectionDelay, 1000, 0);
}

For wait WiFi connect:
Code: Select allvoid ICACHE_FLASH_ATTR
WifiConnectionDelay(void *arg)
{
  static uint8_t DelayTime = 0;
  uart0_sendStr("Connecting...\r\n");
  uint8_t State;

  //disable timer for a while
  os_timer_disarm(&WifiConnectionDelay);
  State = wifi_station_get_connect_status();
  if(State == STATION_GOT_IP)
  {
    next_function_to_call_after_wifi_connected();
    return;
  }
  //restart timer
  os_timer_arm(&WifiConnectionDelay, 1000, 0);
}

than for check your own IP:
Code: Select allvoid ICACHE_FLASH_ATTR
next_function_to_call_after_wifi_connected(void){
     struct ip_info pTempIp;
     wifi_get_ip_info(0x00, &pTempIp);
     os_sprintf(temp, "IP:\"%d.%d.%d.%d\"\r\n", IP2STR(&pTempIp.ip));
     uart0_sendStr(temp);
   
    setup_TCP_or_UDP_connection_function_here();///
   }


But I cannot write TCP or UDP section right now.
Can anyone share such an code/application?
User avatar
By ROKR
#11644 Hi, EAU,

The answer maybe is too late, but can be usefull for someone else.

Firstly, I would cut wifi_station_connect() from WiFiConnector() function and put it under system_os_task() function.

Then I have to declare the function next_function_to_call_after_wifi_connected() like this:

Code: Select allstatic esp_tcp esptcp;
static struct espconn tcp_conn_config;

void ICACHE_FLASH_ATTR
next_function_to_call_after_wifi_connected(void)
{
    tcp_conn_config.type = ESPCONN_TCP;
    tcp_conn_config.state = ESPCONN_NONE;
    tcp_conn_config.proto.tcp = &esptcp;
    tcp_conn_config.proto.tcp->local_port = 5600;

    espconn_regist_connectcb(&tcp_conn_config, tcp_listen_cb);
    espconn_accept(&tcp_conn_config);
}


and declare callbacks for TCP API:

Code: Select allvoid ICACHE_FLASH_ATTR tcp_recv_cb(void *arg, char *pdata, unsigned short len)
{
    struct espconn *pespconn = arg;

    LOGD("Received %d bytes from " IPSTR "\n", len, IP2STR(&pespconn->proto.tcp->remote_ip));
}

LOCAL ICACHE_FLASH_ATTR
void tcp_recon_cb(void *arg, sint8 err)
{
    struct espconn *pespconn = arg;

    LOGD("Address " IPSTR ":%d err %d reconnection\n", IP2STR(&pespconn->proto.tcp->remote_ip),
         pespconn->proto.tcp->remote_port, err);
}


LOCAL ICACHE_FLASH_ATTR
void tcp_discon_cb(void *arg)
{
    struct espconn *pespconn = arg;

    LOGD("Address " IPSTR ":%d disconnected\n", IP2STR(&pespconn->proto.tcp->remote_ip),
         pespconn->proto.tcp->remote_port);
}

void ICACHE_FLASH_ATTR tcp_listen_cb(void *arg)
{
    struct espconn *pesp_conn = arg;

    espconn_regist_recvcb(pesp_conn, tcp_recv_cb);
    espconn_regist_reconcb(pesp_conn, tcp_recon_cb);
    espconn_regist_disconcb(pesp_conn, tcp_discon_cb);
}