Page 1 of 1

esp32 dev board not connecting to wifi

Posted: Mon Aug 27, 2018 1:43 pm
by ojosbourne
i have an issue with my esp32 development board not connecting to wifi it just says connecting on the serial monnitor but never connects all the login details are correct.
the code has been hashed together from several sources however but was working

Code: Select all

 #include <Adafruit_Sensor.h>
//#include "DHT.h"
#include <WiFi.h>
#include <PubSubClient.h>
#include "SparkFunHTU21D.h"
//Create an instance of the object
HTU21D myHumidity;

const char* ssid = "skynet";
const char* password = "Sexyfrog9919";
const char* mqttServer = "192.168.0.196";
const int mqttPort = 1883;
const char* mqttUser = "ojosbourne";
const char* mqttPassword = "Skingsking9919";

#define temperature_topic "topic/temp"       //Topic temperature
#define humidity_topic "topic/humid"         //Topic humidity
//#define debug_topic "debug"                   //Topic for debugging


//here we use pin IO23 of ESP32 to read data
//#define DHTPIN 14
//our sensor is DHT22 type
//#define DHTTYPE DHT22
//create an instance of DHT sensor
//DHT dht(DHTPIN, DHTTYPE);

WiFiClient espClient;
PubSubClient client(espClient);


void setup() {
//void setup() (

//   connect to wifi 



Serial.begin(115200);
WiFi.begin(ssid, password);
 
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.println("Connecting to WiFi..");
}
 
Serial.println("Connected to the WiFi network");
 // connect to mqtt
 
client.setServer(mqttServer, mqttPort);
 
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
 
if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
 
Serial.println("connected");
 
} else {
 
Serial.print("failed with state ");
Serial.print(client.state());
delay(200);
 
}
//}

  
  //Serial.begin(115200);
  //Serial.println("DHT22 sensor!");
  //call begin to start sensor
  //dht.begin();

  Serial.println("HTU21D Example!");

  myHumidity.begin();
}

}
 



void loop() {

  float h = myHumidity.readHumidity();
  float t = myHumidity.readTemperature();

  
  //use the functions which are supplied by library.
  //float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  //float t = dht.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from htu21d sensor!");
    return;
  }
  // print the result to Terminal
  //client.publish(humidity_topic, (h));
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
 // client.publish(temperature_topic, (t));
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");

   // Publish values to MQTT topics
    client.publish(temperature_topic, String(t).c_str(), true);   // Publish temperature on mosohomes/temp1
    //if ( debug ) {    
      Serial.println("Temperature sent to MQTT.");
    //}
    delay(200); //some delay is needed for the mqtt server to accept the message
    
    client.publish(humidity_topic, String(h).c_str(), true);      // Publish humidity on mosohomes/humid1
    
    //if ( debug ) {
                  Serial.println("Humidity sent to MQTT.");
                 //}
  //we delay a little bit for next read
  delay(10000);
}

  

Re: esp32 dev board not connecting to wifi

Posted: Tue Aug 28, 2018 4:52 pm
by ojosbourne
i managed to fix it by changing while (WiFi.status() != WL_CONNECTED) {

to while (WiFi.status() == WL_CONNECTED) {

was driving me nuts i was looking at loads of code a noticed this was different in one of the sketches

Re: esp32 dev board not connecting to wifi

Posted: Sun Sep 02, 2018 7:02 pm
by boarchuz
Well that's a very... creative... solution. :lol:

How is that working out for you?

Re: esp32 dev board not connecting to wifi

Posted: Mon Sep 03, 2018 4:58 am
by ArtemN
Its a strange bug. newer version of esp tools led to problem with transaction for example in standart simplewifiserver.ino code! In old version all fine, but in last version client.stop() not end connection, then in second loop client.connected() = true, but client.available() = false... in this branche of code have no "break" for out from while ;) and program looped in infinity cycle for the moment when browser make a timeout and client.connected() be false.
It semed that there is another possibility to say browser that "connection end" not waitin browser timeout, but i novice in HTTP and have no idea yet.

Re: esp32 dev board not connecting to wifi

Posted: Fri Jan 18, 2019 2:48 am
by ArtemN
It seemed that wifi goto in sleep mode. I disable sleep mode after Wifi.begin() and all work fine!

void setup()
{
...
WiFi.begin(ssid, password);
WiFi.setSleep(false);// this code solves my problem
...
}

Re: esp32 dev board not connecting to wifi

Posted: Mon Jul 22, 2019 11:52 pm
by silgarth
I am seeing a similar issue, sometimes the Wifi will not connect even after several resets. And then it connects almost everytime. This is really unreliable, the Wifi code is running in the second core.

void wifiSetup()
{
xTaskCreatePinnedToCore(
wifiTask, /* Function to implement the task */
"Wifi Task", /* Name of the task */
10000, /* Stack size in words */
NULL, /* Task input parameter */
2, /* Priority of the task */
&wifiTaskHandle, /* Task handle. */
0); /* Core where the task should run */
}
void wifiSetupInternal() {

gWifiStatus = 1;
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
esp_wifi_set_ps(WIFI_PS_NONE);
Serial.print("MAC: ");
Serial.println(WiFi.macAddress());
gmacAddr = WiFi.macAddress();
// commented out to allow WiFi to pick address
// WiFi.config(ip, gateway, netmask);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
rst:0x1 (POWERON_RESET),boot:0x13 (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:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:9280
load:0x40080400,len:5848
entry 0x40080698
34

Version: esp32 - version 1 2019-05-02 - test D



Connecting to TXTdev
MAC: 24:0A:C4:1D:5E:74
.........................

Re: esp32 dev board not connecting to wifi

Posted: Tue Jul 23, 2019 6:14 am
by HermannSW
I noticed similar, whenever I flashed [F]CameraWebServer to ESP32-CAM module, or powered on the module, Wifi nearly never connects on first reset (with Serial Monitor started before), and almost always connects on second reset ...

Re: esp32 dev board not connecting to wifi

Posted: Tue Sep 01, 2020 5:32 am
by mongoose
Please refer to this solution
https://arduino.stackexchange.com/a/77753