for search of the src port to use.
if all ports are used the loop run endless.
test code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "stdlib_noniso.h"
#include "ets_sys.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include <stdarg.h>
extern int vsnprintf(char * s, size_t n, const char * format, va_list arg);
extern int os_printf_plus(const char * format, ...);
//##########################################################################################################
//##########################################################################################################
void loop();
void setup();
//##########################################################################################################
//##########################################################################################################
#define vsnprintf_P(...) vsnprintf( __VA_ARGS__ )
#define snprintf_P(...) snprintf( __VA_ARGS__ )
//##########################################################################################################
//##########################################################################################################
#define putc(c) os_putc(c)
#define printf(...) os_printf( __VA_ARGS__ )
#define sprintf(...) os_sprintf( __VA_ARGS__ )
#define snprintf(...) os_snprintf( __VA_ARGS__ )
//#define memcmp(...) os_memcmp( __VA_ARGS__ )
#define memcpy(...) os_memcpy( __VA_ARGS__ )
#define memmove(...) os_memmove( __VA_ARGS__ )
#define memset(...) os_memset( __VA_ARGS__ )
#define str2macaddr(...) os_str2macaddr( __VA_ARGS__ )
#define strcat(...) os_strcat( __VA_ARGS__ )
#define strchr(...) os_strchr( __VA_ARGS__ )
#define strcmp(...) os_strcmp( __VA_ARGS__ )
#define strcpy(...) os_strcpy( __VA_ARGS__ )
#define strlen(...) os_strlen( __VA_ARGS__ )
#define strncmp(...) os_strncmp( __VA_ARGS__ )
#define strncpy(...) os_strncpy( __VA_ARGS__ )
#define strstr(...) os_strstr( __VA_ARGS__ )
#define malloc(size) os_malloc(size)
#define free(ptr) os_free(ptr)
//##########################################################################################################
//##########################################################################################################
#define wdt_enable(...)
#define wdt_reset() delay(0)
//##########################################################################################################
//##########################################################################################################
#ifdef __cplusplus
} // extern "C"
#endif
#define SERIAL_BAUD 115200
#define SERIAL1_BAUD 921600
#define WIFI_SSID "ssid"
#define WIFI_PASS "passw"
#define DEBUG_SERIAL Serial1
const char* ssid = WIFI_SSID;
int32_t rssi = -999999;
void wifi_connect(void) {
if(wifi_get_opmode() != WIFI_STA) {
DEBUG_SERIAL.println("[WIFI] Set Mode to WIFI_STA");
WiFi.mode(WIFI_STA);
}
DEBUG_SERIAL.println("[WIFI] scan start");
while(1) {
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
bool found = false;
DEBUG_SERIAL.println("[WIFI] scan done");
delay(0);
if(n == 0) {
DEBUG_SERIAL.println("[WIFI] no networks found");
} else {
DEBUG_SERIAL.print("[WIFI] ");
DEBUG_SERIAL.print(n);
DEBUG_SERIAL.println(" networks found");
for(int i = 0; i < n; ++i) {
const char * ssid_scan = WiFi.SSID(i);
int32_t rssi_scan = WiFi.RSSI(i);
if(strcmp(ssid, ssid_scan) == 0) {
DEBUG_SERIAL.print("---> ");
rssi = rssi_scan;
found = true;
}
// Print SSID and RSSI for each network found
DEBUG_SERIAL.print(i + 1);
DEBUG_SERIAL.print(": ");
DEBUG_SERIAL.print(ssid_scan);
DEBUG_SERIAL.print(" (");
DEBUG_SERIAL.print(rssi_scan);
DEBUG_SERIAL.print(")");
DEBUG_SERIAL.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
delay(0);
}
}
DEBUG_SERIAL.println("");
DEBUG_SERIAL.flush();
delay(0);
if(found) {
break;
} else {
DEBUG_SERIAL.print("[WIFI] ");
DEBUG_SERIAL.print(ssid);
DEBUG_SERIAL.println(" not found! retry in 500ms...");
delay(500);
}
}
printf("[WIFI] Connecting to %s rssi: %d\n", ssid, rssi);
WiFi.begin(ssid, WIFI_PASS);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
printf(".");
}
DEBUG_SERIAL.println("");
DEBUG_SERIAL.println("[WIFI] WiFi connected");
DEBUG_SERIAL.print("[WIFI] IP address: ");
DEBUG_SERIAL.println(WiFi.localIP());
DEBUG_SERIAL.flush();
delay(10);
}
void setup() {
Serial.begin(SERIAL_BAUD);
Serial1.begin(SERIAL1_BAUD);
Serial1.setDebugOutput(true);
for(uint8_t t = 4; t > 0; t--) {
printf("[SETUP] BOOT WAIT %d ...\n", t);
delay(1000);
}
DEBUG_SERIAL.println("[SETUP] BOOT...\n\n\n");
wifi_connect();
}
WiFiClient client;
void loop() {
unsigned long t;
static unsigned long last_nodePing;
t = millis();
DEBUG_SERIAL.print(".");
DEBUG_SERIAL.flush();
if(WiFi.status() != WL_CONNECTED) {
printf("[LOOP] Connection to %s lost Reconnect...\n", ssid);
wifi_connect();
}
if(!client.connected()) {
DEBUG_SERIAL.print(F("[nodejs] Connect... "));
if(!client.connect("192.168.178.1", 80)) {
DEBUG_SERIAL.println(F("failed."));
}
DEBUG_SERIAL.println(F("OK"));
}
if(client.connected()) {
client.print((char) 0x00);
client.print(F("5:::TESTDATA"));
client.print((char) 0xFF);
}
}