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:
void 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:
void 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:
void 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?