Esp8266 WiFi STA 看不到 Esp32 WiFi AP 网络,为什么?
Esp8266 WiFi STA cannot see Esp32 WiFi AP network, why?
开始开发两个 ESP 之间的无线 'cable' 解决方案(使用 websockets),计算机和串行设备之间的无线串行 'cable' 以模仿直接有线连接。工作得很好,但在测试时不小心炸毁了其中一个 ESP(将串行电缆连接到更高电压 - 叹息)。用 ESP8266 替换了其中一个 ESP32。怀疑这应该有效,但没有。
问题是ESP8266(客户端)找不到ESP32(服务器)的网络。为什么它不起作用?我的电脑可以看到服务器并可以连接。炸ESP32一样,没问题。
尝试了 ESP8266 上的 WiFiScan 演示,可以检测附近的所有其他 WiFi SSIDs/MACs,但无法检测到 ESP32 服务器,它是 SSID/MAC。
为什么不起作用?有什么区别,我该如何解决?
ESP32 - 服务器代码
#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 - 客户端代码
#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();
}
}
某些国家/地区(例如美国)不使用 WiFi 频道 12-14。也许 ESP32 AP 选择了其中一个频道,而 ESP8266 默认配置了来自不允许它们的国家/地区的设置。将 AP 信道设置为 1-11 范围内的某个合理安全值。
我可以看到默认频道 should be 1,但我建议您尝试一下,也许将其设置为 6:
WiFi.softAP(SSID, PASSWORD, 6);
开始开发两个 ESP 之间的无线 'cable' 解决方案(使用 websockets),计算机和串行设备之间的无线串行 'cable' 以模仿直接有线连接。工作得很好,但在测试时不小心炸毁了其中一个 ESP(将串行电缆连接到更高电压 - 叹息)。用 ESP8266 替换了其中一个 ESP32。怀疑这应该有效,但没有。
问题是ESP8266(客户端)找不到ESP32(服务器)的网络。为什么它不起作用?我的电脑可以看到服务器并可以连接。炸ESP32一样,没问题。
尝试了 ESP8266 上的 WiFiScan 演示,可以检测附近的所有其他 WiFi SSIDs/MACs,但无法检测到 ESP32 服务器,它是 SSID/MAC。
为什么不起作用?有什么区别,我该如何解决?
ESP32 - 服务器代码
#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 - 客户端代码
#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();
}
}
某些国家/地区(例如美国)不使用 WiFi 频道 12-14。也许 ESP32 AP 选择了其中一个频道,而 ESP8266 默认配置了来自不允许它们的国家/地区的设置。将 AP 信道设置为 1-11 范围内的某个合理安全值。
我可以看到默认频道 should be 1,但我建议您尝试一下,也许将其设置为 6:
WiFi.softAP(SSID, PASSWORD, 6);