Communication between two ESP8266 module

parmar7725274
Posts: 20
Joined: Wed Jun 03, 2015 1:20 pm

Communication between two ESP8266 module

Postby parmar7725274 » Thu Jun 04, 2015 1:02 am

Hi, I have two ESP8266 Module and I want TCP communication between them.

I have written firmware to configure ONE ESP as AccessPoint and another one as a STATION. Both are working fine with router, I means ESP8266 with Station configuration is able to connect with router and Using my cell phone I can connect ESP8266 which have SoftAP configuration. That means both are working fine but when I’m trying to Connect each other without router, Its not happening. ESP8266 with station configuration gives error NO ap found.

Any help ?

Thanks in advance.

ESP_Faye
Posts: 1592
Joined: Mon Oct 27, 2014 11:08 am

Re: Communication between two ESP8266 module

Postby ESP_Faye » Thu Jun 04, 2015 10:18 am

Hi,

Here is a demo of ESP8266 station connects to AP http://bbs.espressif.com/viewtopic.php?f=21&t=228

Please notice that "stationConf.bssid_set = 0; " need to be set.

Thanks for your interest in ESP8266 !

parmar7725274
Posts: 20
Joined: Wed Jun 03, 2015 1:20 pm

Re: Communication between two ESP8266 module

Postby parmar7725274 » Fri Jun 05, 2015 1:41 am

Hi,

Thanks for reply.

But unfortunateley I still facing same problem.

Here is code, which I'm using.

ESP-1 : SoftAp ( Access Point / SERVER )

Code: Select all

void ICACHE_FLASH_ATTR
user_init()
{
   uart_init(BIT_RATE_115200, BIT_RATE_115200);
   os_delay_us(100);

   #ifdef PLATFORM_DEBUG
   ets_uart_printf("ESP8266 platform starting...\r\n");
   #endif
   
   struct softap_config apConfig;
   struct ip_info ipinfo;
   char ssid[33];
   char password[33];
   char macaddress[17];
   char info[150];

   wifi_softap_dhcps_stop();

   if( wifi_get_phy_mode() != PHY_MODE_11N )
   {
      ets_uart_printf("*** Setting PHY_MODE ...\r\n");
      wifi_set_phy_mode( PHY_MODE_11N );
   }

   if(wifi_get_opmode() != SOFTAP_MODE)
   {
      #ifdef PLATFORM_DEBUG
      ets_uart_printf("ESP8266 not in SOFTAP mode, restarting in SOFTAP mode...\r\n");
      #endif
      wifi_set_opmode(SOFTAP_MODE);
      //after esp_iot_sdk_v0.9.2, need not to restart
      //system_restart();
   }

   IP4_ADDR(&ipinfo.ip, 10, 10, 10, 1);
   IP4_ADDR(&ipinfo.gw, 10, 10, 10, 1);
   IP4_ADDR(&ipinfo.netmask, 255, 255, 255, 0);

   wifi_set_ip_info(SOFTAP_IF, &ipinfo);

   wifi_get_macaddr(SOFTAP_IF, macaddr);

   wifi_softap_get_config(&apConfig);

   os_memset(apConfig.ssid, 0, sizeof(apConfig.ssid));

   os_sprintf(ssid, "%s", WIFI_APSSID);

   os_memcpy(apConfig.ssid, ssid, os_strlen(ssid));

   if (wifi_get_opmode() == SOFTAP_MODE)
   {
      #ifdef WIFI_APWPA

      os_memset(apConfig.password, 0, sizeof(apConfig.password));

      os_sprintf(password, "%s", WIFI_APPASSWORD);

      os_memcpy(apConfig.password, password, os_strlen(password));

      apConfig.authmode = AUTH_WPA_WPA2_PSK;

      #else

      apConfig.authmode = AUTH_OPEN;

      #endif

      apConfig.channel = 1;

      apConfig.max_connection = 4;

      apConfig.ssid_hidden = 0;

      wifi_softap_set_config(&apConfig);
   }

   os_delay_us(500000);
   wifi_softap_dhcps_start();

   #ifdef PLATFORM_DEBUG
   if (wifi_get_opmode() == SOFTAP_MODE)
   {
      wifi_softap_get_config(&apConfig);
      os_sprintf(macaddress, MACSTR, MAC2STR(macaddr));
      os_sprintf(info,"OPMODE: %u, SSID: %s, PASSWORD: %s, CHANNEL: %d, AUTHMODE: %d, MACADDRESS: %s PHU MODE : %d\r\n",
               wifi_get_opmode(),
               apConfig.ssid,
               apConfig.password,
               apConfig.channel,
               apConfig.authmode,
               macaddress,
               wifi_get_phy_mode());
      ets_uart_printf(info);
   }
   #endif

   //BtnInit();

   #ifdef PLATFORM_DEBUG
   ets_uart_printf("ESP8266 platform started!\r\n");
   #endif
}


ESP-2 : Station mode ( CLIENT )

Code: Select all

void platform_init( void )
{
   uint8 ret;
   char ssid[32]       = "NeelKanth";
   char password[64]    = "1234512345";

   struct station_config stationConf, stationConfig;

   ret = wifi_station_disconnect( );
   os_printf("\n\r wifi_station_disconnect : %d \n", ret);

  //wifi_station_dhcpc_stop( );

    //Setup wifi
    os_printf(" Start in STATION mode: %d \r\n",wifi_get_opmode());

    if( wifi_get_opmode() != STATION_MODE )
    {
       os_printf(" Start in STATION mode\r\n");
       wifi_set_opmode( STATION_MODE );               // Station Mode
    }

   wifi_set_phy_mode( PHY_MODE_11N );

   if( wifi_station_get_config( &stationConf ) )
   {
      os_memset( stationConf.ssid,       0, sizeof(stationConf.ssid) );
      os_memset( stationConf.password,    0, sizeof(stationConf.password) );

      os_sprintf( stationConf.ssid,       "%s", ssid );
      os_sprintf( stationConf.password,    "%s", password );

       //need not mac address
      stationConf.bssid_set = 0;

      if( !wifi_station_set_config( &stationConf ) )
      {
         os_printf("Failed to set STATION MODE configuration\r\n");
      }
   }

//   wifi_station_dhcpc_start( );

   ret = wifi_station_connect ( );
   os_printf("\n\r wifi_station_connect : %d \n", ret);

   wifi_station_set_auto_connect(1);

   if(wifi_station_get_config(&stationConfig))
   {
      os_printf("\nSTA config: OPMODE: %u, SSID: %s, PASSWORD: %s, PHy Mode : %d\r\n",
                                       wifi_get_opmode(),
                                       stationConfig.ssid,
                                       stationConfig.password,
                                       wifi_get_phy_mode() );
   }

}

/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void user_init(void)
{

    system_timer_reinit();

    uart_init(BIT_RATE_115200, BIT_RATE_115200);

    platform_init();

   // network_init();

}


Please Help me !!

Thanks in advance.

- Prakash.

ESP_Faye
Posts: 1592
Joined: Mon Oct 27, 2014 11:08 am

Re: Communication between two ESP8266 module

Postby ESP_Faye » Fri Jun 05, 2015 10:11 am

Hi,

Here is a demo of ESP8266 softAP configuration http://bbs.espressif.com/viewtopic.php?f=21&t=227

Please notice that call “wifi_softap_get_config” first,otherwise,softap_config.ssid_len or softap_config.beacon_interval may be random value.. This may cause connection fail.

Thanks for your interest in ESP8266 !

parmar7725274
Posts: 20
Joined: Wed Jun 03, 2015 1:20 pm

Re: Communication between two ESP8266 module

Postby parmar7725274 » Sat Jun 13, 2015 1:26 pm

Hi Faye !

Thanks for your help.

I'm facing some error. Can you take a look of log and suggest any solution ? Please.

SoftAp Log :
mode : sta(18:fe:34:9e:16:b5) + softAP(1a:fe:34:9e:16:b5)
add if0
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
add if1
bcn 100
add 1
aid 1
station: 18:fe:34:9e:18:21 join, AID = 1
station: 18:fe:34:9e:18:21 leave, AID = 1
rm match
add 1
aid 1
station: 18:fe:34:9e:18:21 join, AID = 1
station: 18:fe:34:9e:18:21 leave, AID = 1


Station log :
mode : sta(18:fe:34:9e:18:21)
add if0
scandone
add 0
aid 1
pm open phy_2,type:2 0 0
cnt
reconnect
rm match
pm close 7 0 0/10001392
scandone
add 0
aid 1
pm open phy_2,type:2 0 0
cnt
reconnect
rm match
pm close 7 0 0/10001361
scandone
add 0
aid 1
pm open phy_2,type:2 0 0
cnt
reconnect
rm match
pm close 7 0 0/10000192
WiFi connecting fail


Regards,
Prakash P.

parmar7725274
Posts: 20
Joined: Wed Jun 03, 2015 1:20 pm

Re: Communication between two ESP8266 module

Postby parmar7725274 » Sat Jun 13, 2015 1:44 pm

Hi Faye,

I tried with each combination of SOFTAP_MODE and STATIONAP_MODE for AccessPoint
and STATION_MODE and STATIONAP_MODE for Station.

Every time Facing Same problem.

Thanks & Regards,
Prakash P.

ESP_Faye
Posts: 1592
Joined: Mon Oct 27, 2014 11:08 am

Re: Communication between two ESP8266 module

Postby ESP_Faye » Tue Jun 16, 2015 2:33 pm

Hi,

Please try to add configuration of apConfig.beacon_interval

Code: Select all

      apConfig.beacon_interval = 100;
      wifi_softap_set_config(&apConfig);


Thanks for your interest in ESP8266 !

parmar7725274
Posts: 20
Joined: Wed Jun 03, 2015 1:20 pm

Re: Communication between two ESP8266 module

Postby parmar7725274 » Wed Jun 17, 2015 1:40 am

Hi Faye,

Thanks for your humble help and spend your valuable time.

I had tried with above solution. But unfortunately it's still giving same problem.It's appreciated if you again take a look on log and suggest some solutions. please.

Again thanks for your help.

Thanks & regards,
Prakash P.

ESP_Faye
Posts: 1592
Joined: Mon Oct 27, 2014 11:08 am

Re: Communication between two ESP8266 module

Postby ESP_Faye » Wed Jun 17, 2015 11:16 am

Espressif_Faye wrote:Hi,

Here is a demo of ESP8266 softAP configuration http://bbs.espressif.com/viewtopic.php?f=21&t=227

Please notice that call “wifi_softap_get_config” first,otherwise,softap_config.ssid_len or softap_config.beacon_interval may be random value.. This may cause connection fail.

Thanks for your interest in ESP8266 !


Please pay attention on the softAP configuration demo,we recommended to call “wifi_softap_get_config” first,otherwise,softap_config.ssid_len or softap_config.beacon_interval may be random value.. This may cause connection fail.

Add codes below

Code: Select all

      apConfig.ssid_len = 0;
      apConfig.beacon_interval = 100;

      wifi_softap_set_config(&apConfig);


Or just try codes below

Code: Select all

void ICACHE_FLASH_ATTR
user_init()
{
   uart_init(BIT_RATE_115200, BIT_RATE_115200);

   #ifdef PLATFORM_DEBUG
   ets_uart_printf("ESP8266 platform starting...\r\n");
   #endif
   
   struct softap_config apConfig;
   struct ip_info ipinfo;
   char ssid[33];
   char password[33];
   char macaddress[17];
   char info[150];

   wifi_softap_dhcps_stop();

   if( wifi_get_phy_mode() != PHY_MODE_11N )
   {
      os_printf("*** Setting PHY_MODE ...\r\n");
      wifi_set_phy_mode( PHY_MODE_11N );
   }

   if(wifi_get_opmode() != SOFTAP_MODE)
   {
      #ifdef PLATFORM_DEBUG
      ets_uart_printf("ESP8266 not in SOFTAP mode, restarting in SOFTAP mode...\r\n");
      #endif
      wifi_set_opmode(SOFTAP_MODE);

   }

   IP4_ADDR(&ipinfo.ip, 10, 10, 10, 1);
   IP4_ADDR(&ipinfo.gw, 10, 10, 10, 1);
   IP4_ADDR(&ipinfo.netmask, 255, 255, 255, 0);

   wifi_set_ip_info(SOFTAP_IF, &ipinfo);

   //wifi_get_macaddr(SOFTAP_IF, macaddr);

   wifi_softap_get_config(&apConfig);

   os_memset(apConfig.ssid, 0, sizeof(apConfig.ssid));

   os_sprintf(ssid, "%s", WIFI_APSSID);

   os_memcpy(apConfig.ssid, ssid, os_strlen(ssid));

   if (wifi_get_opmode() == SOFTAP_MODE)
   {
      #if 1 //def WIFI_APWPA
      os_memset(apConfig.password, 0, sizeof(apConfig.password));
      os_sprintf(password, "%s", WIFI_APPASSWORD);
      os_memcpy(apConfig.password, password, os_strlen(password));
      apConfig.authmode = AUTH_WPA_WPA2_PSK;
      #else
      apConfig.authmode = AUTH_OPEN;
      #endif

      apConfig.channel = 1;
      apConfig.max_connection = 4;
      apConfig.ssid_hidden = 0;
     
      apConfig.ssid_len = 0;
      apConfig.beacon_interval = 100;

      wifi_softap_set_config(&apConfig);
     
      os_printf("wifi_softap_set_config %s %s!\r\n",apConfig.ssid, apConfig.password);
   }

   wifi_softap_dhcps_start();

   #if 1 //def PLATFORM_DEBUG
   os_printf("ESP8266 platform started!\r\n");
   #endif
}


Thanks for your interest in ESP8266 !

parmar7725274
Posts: 20
Joined: Wed Jun 03, 2015 1:20 pm

Re: Communication between two ESP8266 module

Postby parmar7725274 » Fri Jun 19, 2015 1:35 am

Hi Faye,

I appreciate your patience,and thanks for your help.

From first time I understand what you are trying to say and I did it. And I also use your sample code, but in both case I'm getting same output.even though I also try with starting dhcp in STATION mode but no luck.

In your sample code for AP and STATION settings, whatever is given I iust copied and flash it on chip, but that is also not working.

Thanks again for your help, Would you like to suggest some solutions ? Please...

Thanks & Regards,
Prakash P.

Who is online

Users browsing this forum: No registered users and 3 guests