Dear all
The WiFi.begin(ssid, pass) initialize the library network with a specific SSID. In some home network can happen that two or more AP with the same SSID are in place. It is important that the library selects the best AP (the one with the strongest signal). I've noticed, however that using the https://github.com/espressif/arduino-esp32/archive/master.zip ES32 library it doesn't happen, and to connect to an Access point with the same SSID, the below example works once out of two. After the first hw reset of the nINA-W102 SDK the board connects the AP, after the second no, after the third reset, yes and so on.
#include <WiFi.h>
#include "Esp32MQTTClient.h"
// Please input the SSID and password of WiFi
const char* ssid = "My SSID";
const char* password = "pass";
/*String containing Hostname, Device Id & Device Key in the format: */
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */
static const char* connectionString = "HostName=mqtt.eclipse.org";
static bool hasIoTHub = false;
void setup()
{
Serial.begin(115200);
Serial.println("\n\nStarting connecting WiFi\n");
delay(20);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (!Esp32MQTTClient_Init((const uint8_t*)connectionString))
{
hasIoTHub = false;
Serial.println("Initializing IoT hub failed.");
return;
}
hasIoTHub = true;
} // Setup
void loop()
{
if (hasIoTHub)
{
char buff[128];
// replace the following line with your data sent to Azure IoTHub
// snprintf(buff, 128, "{\"topic\":\"iot\"}");
snprintf(buff, 128, "{\"paolo\":\"alarm\"}");
if (Esp32MQTTClient_SendEvent(buff))
{
Serial.println("Sending data succeed");
}
else
{
Serial.println("Failure...");
}
delay(5000);
} // if
} // loop
SERIAL port output
------------------------------- After first RESET of the board ------------------------------------------------
NINA-B102 EVK reset asserted:
Serial output :
...ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10044
load:0x40080400,len:5872
entry 0x400806ac
⸮
Starting connecting WiFi <-AP connected !
.
WiFi connected
IP address:
192.168.10.43
Info: Initializing SNTP
Info: SNTP initialization complete
Info: IoT Hub SDK for C, version 1.1.23
------------------------ After the second RESET of the board ------------------------------------------------
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10044
load:0x40080400,len:5872
entry 0x400806ac
⸮
Starting connecting WiFi
....................................... <- NO connection to the AP