Codebeat
Codebeat

Reputation: 6610

Esp8266 WiFi STA cannot see Esp32 WiFi AP network, why?

Started to develop a wiresless 'cable' solution (with websockets) between two ESPs, a wireless serial 'cable' between computer and a serial device to mimick a direct wired connection. Was working great however just accidentally fried one of the ESPs (short a serial cable connection to higher voltage - sigh) when testing. Replaced one of the ESP32s with an ESP8266. Suspect this should work however it did not.

The problem is the ESP8266 (client) cannot find the network of the ESP32 (server). Why it doesn't work? My computer can see the server and can connect. Fried ESP32 the same, no problem.

Tried the WiFiScan demo on the ESP8266 and can detect all other WiFi SSIDs/MACs in neighborhood however cannot detect the ESP32 server it's SSID/MAC.

Why it doesn't work? What is the difference and how can I solve this?


ESP32 - code of the server

#ifdef ESP_PLATFORM 
 #include <WiFi.h>
#else 
 #include <ESP8266WiFi.h>
#endif

#include <WebSocketsServer.h>

#define SSID "TheGeekMan"
#define PASSWORD "*********"

 // WebSocket Cable Connection parameters
#define WSCC_SOCKET_PORT   5000
#define WSCC_WEBSOCK_PORT  81
#define WSCC_MAX_DATA_SIZE 100

#define WSCC_PSD_NO_DATA     0x00  // Not an error
#define WSCC_PSD_SUCCESS     0x01  // Not an error 
#define WSCC_PSD_CONN_ERROR  0x02  // Is error
#define WSCC_PSD_DATA_ERROR  0x03  // Is error
#define WSCC_PSD_DMOVE_ERROR 0x04  // Is error
#define WSCC_PSD_INVALID_IP  0x05  // Is error

#define WSCC_DSD_NOT_AVAIL     0x00   // Not an error
#define WSCC_DSD_SUCCESS       0x01   // Not an error
#define WSCC_DSD_DATA_ERROR    0x02   // Is error
#define WSCC_DSD_DATA_LOSS     0x03   // Is error
#define WSCC_DSD_DATA_OVERFLOW 0x04   // Is error

#define WSCC_SERIAL_DEBUG_IO Serial
#ifdef ESP_PLATFORM
 #define WSCC_SERIAL_DATA_IO  Serial2
#else
 #define WSCC_SERIAL_DATA_IO Serial
#endif

#define WSCC_SERIAL_DATA_READ_TIMEOUT  5000UL
#define WSCC_SERIAL_DATA_WRITE_TIMEOUT 5000UL
#define WSCC_SOCKET_DATA_READ_TIMEOUT  5000UL


#define WSCC_LED_LOGIC_ON   HIGH
#define WSCC_LED_LOGIC_OFF  LOW
#define WSCC_LED_SIGNAL     2 // LED_BUILTIN 

typedef uint8_t TWsccBuff[WSCC_MAX_DATA_SIZE+1];
#define PWsccBuff TWsccBuff*

static uint8_t          buffer[WSCC_MAX_DATA_SIZE+1];
//static IPAddress  clientAddress = INADDR_NONE;
//static WiFiServer server( WSCC_SOCKET_PORT );
static WebSocketsServer webSocket = WebSocketsServer( WSCC_WEBSOCK_PORT );
static int8_t           iBuiltInLEDState = -1;
static bool             bWebSockConnected = false;


bool isOnBoardLEDOn()
{
  return ( iBuiltInLEDState > 0 );
}

void setOnBoardLED( bool bOn )
{
  if( iBuiltInLEDState != bOn )
  {
    iBuiltInLEDState = bOn;
    digitalWrite( WSCC_LED_SIGNAL, bOn?WSCC_LED_LOGIC_ON:WSCC_LED_LOGIC_OFF ); 
  }
}

void setOnBoardLED()
{
  setOnBoardLED( true );
}

void setOnBoardLEDOn()
{
  setOnBoardLED( true );
}

void setOnBoardLEDOff()
{
  setOnBoardLED( false );
}

void toggleOnBoardLED( uint16_t iDelay )
{
  setOnBoardLED( !isOnBoardLEDOn() );
  if( iDelay > 0 )
   { delay( iDelay ); }
}

void toggleOnBoardLED()
{
  toggleOnBoardLED(0);
}


bool isConnectedToNetwork()
{
   static bool bConnected = false;
   yield();
   
   if( WiFi.status() < 0xFF && WiFi.status() != WL_CONNECTED )
    {
        if( bConnected ) 
         { 
            bConnected = false;
            WSCC_SERIAL_DEBUG_IO.println( "Connection lost!" ); 
         }
        
        WSCC_SERIAL_DEBUG_IO.print( "Waiting for connection...." ); 
 
        WSCC_SERIAL_DEBUG_IO.println( WiFi.status() );
        //WiFi.begin(SSID, PASSWORD);
        toggleOnBoardLED();
        delay(1000);
        return false;
    }
    
    if( !bConnected )
    {
      setOnBoardLEDOn();
      bConnected = true;
      WSCC_SERIAL_DEBUG_IO.print( "Connected! IP is: " );
      WSCC_SERIAL_DEBUG_IO.println( WiFi.softAPIP() );    
    }
    
    return bConnected;
}

bool isConnectedToWebSock()
{
  static uint32_t iConnCheckTime = millis();
  
  if( isConnectedToNetwork() )
  {
     webSocket.loop();

     if( !bWebSockConnected )
     {
        if( millis()-iConnCheckTime > 500 )
        {
         toggleOnBoardLED();
         iConnCheckTime=millis();
       }
     }
     
     return bWebSockConnected;
  }
  
  return false;
}

uint8_t webSocketWriteSerialDataTX()
{
   static uint32_t iCheckInterval = millis();
    
    // Check every 1 ms
   if( millis()-iCheckInterval > 0 )
   { 
     if( WSCC_SERIAL_DATA_IO.available() )
     {
     
       TWsccBuff buffer; 
       uint32_t  iResult = 0;
       
       while( WSCC_SERIAL_DATA_IO.available() && iResult < WSCC_MAX_DATA_SIZE )
       {
         buffer[iResult++]=WSCC_SERIAL_DATA_IO.read();
         toggleOnBoardLED();
       }
       
       buffer[iResult]=0;
       
       if( !webSocket.sendTXT( 0, &buffer[0] , iResult ))
       { 
         setOnBoardLEDOn();
         WSCC_SERIAL_DEBUG_IO.println( "Error client: webSocketWriteSerialDataTX(connection error)" );
         iCheckInterval=millis();
         return WSCC_PSD_CONN_ERROR; 
       } 
     
       yield(); 
       setOnBoardLEDOn();
       iCheckInterval=millis();
       return WSCC_PSD_SUCCESS;
     }
   }
   
   return WSCC_PSD_NO_DATA;
}

uint8_t webSocketReadSerialDataRX( uint8_t* pBuffer, uint32_t iAvail )
{
  toggleOnBoardLED();
  uint8_t iResult = WSCC_DSD_SUCCESS;   
  
  if( iAvail > 0 )
  {
    while( iAvail > 0 )
    {
      if( WSCC_SERIAL_DATA_IO.availableForWrite())
       { 
         if( WSCC_SERIAL_DATA_IO.write( pBuffer, 0x01 ) == 0x01 )
          { 
            --iAvail; 
            ++pBuffer; 
          }
       }
      
      //yield(); 
    }
  }
  else { iResult = WSCC_PSD_NO_DATA; }
  
  setOnBoardLEDOn();
  return iResult;
}


void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
{
  //static bool bOccupied = false;
   
  if(type == WStype_TEXT )
  {
    webSocketReadSerialDataRX( payload, length );
  }
  else
  if(type == WStype_DISCONNECTED )
  {
    bWebSockConnected = false;
    WSCC_SERIAL_DEBUG_IO.println( "Error: Websockets connection lost" );
  }
  else
  if(type == WStype_CONNECTED )
  {
    bWebSockConnected = true;
    setOnBoardLEDOn();
    WSCC_SERIAL_DEBUG_IO.println( "Success: Websockets connection established" );
  }
}

void setup()
{
    pinMode( WSCC_LED_SIGNAL, OUTPUT );
    setOnBoardLEDOff();
     
    uint8_t i = 5;
    while( i--  )
    {
      delay(1000);
      toggleOnBoardLED(); 
    }
      
    if( WSCC_SERIAL_DEBUG_IO != WSCC_SERIAL_DATA_IO )
    {
      WSCC_SERIAL_DEBUG_IO.setDebugOutput(true);
      WSCC_SERIAL_DEBUG_IO.begin(115200);
    }
    
    WSCC_SERIAL_DATA_IO.setDebugOutput(false);
    WSCC_SERIAL_DATA_IO.begin(115200);
    
    WiFi.disconnect(true);
    WiFi.disconnect();
  
    WiFi.mode(WIFI_AP_STA);
    //WiFi.mode(WIFI_AP);
    WiFi.softAP(SSID, PASSWORD);
    
    //server.setNoDelay(true);
    //server.begin();
    
    webSocket.onEvent(webSocketEvent);
    webSocket.begin();

    
    WSCC_SERIAL_DEBUG_IO.print( "Connected! IP is: " );
    WSCC_SERIAL_DEBUG_IO.println( WiFi.softAPIP() );
    WSCC_SERIAL_DEBUG_IO.print("MAC is: ");
    WSCC_SERIAL_DEBUG_IO.println(WiFi.macAddress());

//    Serial.print( "Gateway: " );
//    Serial.println( WiFi.gatewayIP() );
}

void loop()
{
 // while(1)
  {  
    if( isConnectedToWebSock() )
    {
      webSocketWriteSerialDataTX();
    }  
    
  }  
}

ESP8266 - code of the client

#ifdef ESP_PLATFORM 
 #include <WiFi.h>
#else 
 #include <ESP8266WiFi.h>
#endif

#include <WebSocketsClient.h>

#define SSID "TheGeekMan"
#define PASSWORD "**********"

 // WebSocket Cable Connection parameters
#define WSCC_SOCKET_PORT   5000
#define WSCC_WEBSOCK_PORT  81
#define WSCC_MAX_DATA_SIZE 100

#define WSCC_PSD_NO_DATA     0x00  // Not an error
#define WSCC_PSD_SUCCESS     0x01  // Not an error 
#define WSCC_PSD_CONN_ERROR  0x02  // Is error
#define WSCC_PSD_DATA_ERROR  0x03  // Is error
#define WSCC_PSD_DMOVE_ERROR 0x04  // Is error
#define WSCC_PSD_INVALID_IP  0x05  // Is error

#define WSCC_DSD_NOT_AVAIL     0x00   // Not an error
#define WSCC_DSD_SUCCESS       0x01   // Not an error
#define WSCC_DSD_DATA_ERROR    0x02   // Is error
#define WSCC_DSD_DATA_LOSS     0x03   // Is error
#define WSCC_DSD_DATA_OVERFLOW 0x04

#define WSCC_SERIAL_DATA_READ_TIMEOUT  5000UL
#define WSCC_SERIAL_DATA_WRITE_TIMEOUT 5000UL
#define WSCC_SOCKET_DATA_READ_TIMEOUT  5000UL

#define WSCC_DSD_NOT_AVAIL  0x00
#define WSCC_DSD_SUCCESS    0x01
#define WSCC_DSD_DATA_ERROR 0x02

#define WSCC_LED_LOGIC_ON   HIGH
#define WSCC_LED_LOGIC_OFF  LOW
#define WSCC_LED_SIGNAL     2 //LED_BUILTIN 

#define WSCC_SERIAL_DEBUG_IO Serial
#ifdef ESP_PLATFORM
 #define WSCC_SERIAL_DATA_IO  Serial2
#else
 #define WSCC_SERIAL_DATA_IO Serial
#endif

typedef uint8_t TWsccBuff[WSCC_MAX_DATA_SIZE+1];
#define PWsccBuff TWsccBuff*

static WebSocketsClient webSocket;
static int8_t           iBuiltInLEDState = -1;
static bool             bWebSockConnected = false;

bool isOnBoardLEDOn()
{
  return ( iBuiltInLEDState > 0 );
}


void setOnBoardLED( bool bOn )
{
  if( iBuiltInLEDState != bOn )
  {
    iBuiltInLEDState = bOn;
    digitalWrite( WSCC_LED_SIGNAL, bOn?WSCC_LED_LOGIC_ON:WSCC_LED_LOGIC_OFF ); 
  }
}

void setOnBoardLED()
{
  setOnBoardLED( true );
}

void setOnBoardLEDOn()
{
  setOnBoardLED( true );
}

void setOnBoardLEDOff()
{
  setOnBoardLED( false );
}

void toggleOnBoardLED( uint16_t iDelay )
{
  setOnBoardLED( !isOnBoardLEDOn() );
  if( iDelay > 0 )
   { delay( iDelay ); }
}

void toggleOnBoardLED()
{
  toggleOnBoardLED(0);
}

bool isConnectedToNetwork()
{
   static bool     bConnected = false;
   static uint32_t iCheckTimer = millis();
   yield();

   if( millis()-iCheckTimer >= 1500UL )
   { 
     if( WiFi.status() != WL_CONNECTED  )
     {
        if( bConnected ) 
        { 
            WiFi.disconnect();
            WiFi.reconnect();
            bConnected = false;
            WSCC_SERIAL_DEBUG_IO.println( "Connection lost!" ); 
        }
        
        WSCC_SERIAL_DEBUG_IO.print( "Waiting for connection...." ); 
 
        WSCC_SERIAL_DEBUG_IO.println( WiFi.status() );
        //WiFi.reconnect();
        yield();
        //WiFi.begin(SSID, PASSWORD);
        toggleOnBoardLED();
        iCheckTimer = millis();
        return false;
     }
    
     if( !bConnected )
     {
        setOnBoardLEDOn();
        bConnected = true;
        WSCC_SERIAL_DEBUG_IO.print( "Connected! IP is: " );
        WSCC_SERIAL_DEBUG_IO.println( WiFi.localIP() );    
     }
   
     iCheckTimer = millis();
   }
   
   return bConnected;
}

uint8_t webSocketWriteSerialDataTX()
{
   static uint32_t iCheckInterval = millis();
    
    // Check every 1 ms
   if( millis()-iCheckInterval > 0 )
   { 
     if( WSCC_SERIAL_DATA_IO.available() )
     {
     
       TWsccBuff buffer; 
       uint32_t  iResult = 0;
       
       while( WSCC_SERIAL_DATA_IO.available() && iResult < WSCC_MAX_DATA_SIZE )
       {
         buffer[iResult++]=WSCC_SERIAL_DATA_IO.read();
         toggleOnBoardLED();
       }
       
       buffer[iResult]=0;
       
       if( !webSocket.sendTXT( &buffer[0] , iResult ))
       { 
         setOnBoardLEDOn();
         WSCC_SERIAL_DEBUG_IO.println( "Error client: webSocketWriteSerialDataTX(connection error)" );
         iCheckInterval=millis();
         return WSCC_PSD_CONN_ERROR; 
       } 
     
       yield(); 
       setOnBoardLEDOn();
       iCheckInterval=millis();
       return WSCC_PSD_SUCCESS;
     }
   }
   
   return WSCC_PSD_NO_DATA;
}


uint8_t webSocketReadSerialDataRX( uint8_t* pBuffer, uint32_t iAvail )
{
  toggleOnBoardLED();
  uint8_t iResult = WSCC_DSD_SUCCESS;   
  
  if( iAvail > 0 )
  {
    while( iAvail > 0 )
    {
      if( WSCC_SERIAL_DATA_IO.availableForWrite())
       { 
         if( WSCC_SERIAL_DATA_IO.write( pBuffer, 0x01 ) == 0x01 )
          { 
            --iAvail; 
            ++pBuffer; 
          }
       }
      
      //yield(); 
    }
  }
  else { iResult = WSCC_PSD_NO_DATA; }
  
  setOnBoardLEDOn();
  return iResult;
}


void webSocketEvent( WStype_t type, uint8_t *payload, size_t length)
{
  if(type == WStype_TEXT )
  {
    webSocketReadSerialDataRX( payload, length );
  }
  else
  if(type == WStype_DISCONNECTED )
  {
    bWebSockConnected = false;
    WSCC_SERIAL_DEBUG_IO.println( "Error: Websockets connection lost" );
  }
  else
  if(type == WStype_CONNECTED )
  {
    bWebSockConnected = true;
    setOnBoardLEDOn();
    WSCC_SERIAL_DEBUG_IO.println( "Success: Websockets connection established" );
  }
}

void configWebSockets()
{
  static bool     bIsConfig = false;
  static uint32_t iConnCheckTime = millis();
  
  if( !bIsConfig )
  {
    bIsConfig = true;
    // server address, port, and URL path
    webSocket.begin( WiFi.gatewayIP(), WSCC_WEBSOCK_PORT, "/");
    webSocket.setExtraHeaders();
    webSocket.loop();
  }
  else {
         webSocket.loop();
         if( millis()-iConnCheckTime > 500 )
         {
           if( !bWebSockConnected )
           {
             toggleOnBoardLED();
             iConnCheckTime=millis();
           }
         }
       }    
}

void setup()
{
  
    pinMode( WSCC_LED_SIGNAL, OUTPUT );
    
    setOnBoardLEDOff();
    
    if( WSCC_SERIAL_DEBUG_IO != WSCC_SERIAL_DATA_IO )
    {
      WSCC_SERIAL_DEBUG_IO.setDebugOutput(true);
      WSCC_SERIAL_DEBUG_IO.begin(115200);
    }
    
    WSCC_SERIAL_DATA_IO.setDebugOutput(false);
    WSCC_SERIAL_DATA_IO.begin(115200);
   
    WiFi.disconnect(true);
    WiFi.disconnect();
    
    WiFi.mode(WIFI_STA);
    WiFi.begin(SSID, PASSWORD);


    // event handler
    webSocket.onEvent(webSocketEvent);

    // try ever 5000 again if connection has failed
    webSocket.setReconnectInterval(5000);
}   



void loop()
{ 
    if( isConnectedToNetwork() )
    {
      configWebSockets();
      webSocketWriteSerialDataTX();
    }  
}
  

Upvotes: 1

Views: 842

Answers (1)

Tarmo
Tarmo

Reputation: 4770

WiFi channels 12-14 are not used in some countries (e.g. US). Perhaps the ESP32 AP picked one of those channels, and ESP8266 is configured by default with settings from a country which doesn't allow them. Set the AP channel to some reasonably safe value in range 1-11.

I can see that the default channel should be 1, but I'd suggest experimenting with it, perhaps setting it to 6:

WiFi.softAP(SSID, PASSWORD, 6);

Upvotes: 1

Related Questions