-
Notifications
You must be signed in to change notification settings - Fork 98
Not able to read analog port when using the autoconnect example #39
Comments
@MaPoLom Thanks for using the library, your nice words and reporting the very rare and interesting bug. I trace the bug out, and this is actually the bug of ESP32 MultiWiFi. The ESP8266 MultiWiFi has no issue with analogRead. I'll continue to:
If the ESP32 WiFI code is OK with analogRead, I think I'll rewrite the ESP_WiFiManager as well as ESPAsync_WiFiManager library to get rid of ESP32 MultiWiFi code and use the regular ESP32 WiFi. You can use the following code to check and verify #if !( ESP32 || ESP8266 )
#error This code is intended to run on the ESP32/ESP8266 platform! Please check your Tools->Board setting.
#endif
#define USE_MULTI_WIFI true
#if ESP32
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#elif ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#endif
#define SSID_MAX_LEN 32
#define PASS_MAX_LEN 64
typedef struct
{
char ssid[SSID_MAX_LEN + 1];
char pass[PASS_MAX_LEN + 1];
} WiFi_Credentials;
WiFi_Credentials WiFi_Creds[] =
{
{ "HueNet1", "12345678"},
{ "HueNet2", "12345678"},
{ "HueNet3", "12345678"}
};
#define NUMBER_SSIDS ( sizeof(WiFi_Creds) / sizeof(WiFi_Credentials) )
uint8_t status;
int sensorPin = 12; //A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
uint8_t connectMultiWiFi(void)
{
Serial.println("\nConnecting Wifi...");
int i = 0;
status = wifiMulti.run();
delay(3000);
while ( ( i++ < 10 ) && ( status != WL_CONNECTED ) )
{
status = wifiMulti.run();
if ( status == WL_CONNECTED )
break;
else
delay(3000);
}
if ( status == WL_CONNECTED )
{
Serial.println("WiFi connected to after " + String(i) + " times.");
Serial.println("SSID = " + WiFi.SSID());
Serial.println("RSSI = " + String(WiFi.RSSI()));
Serial.println("Channel: " + String(WiFi.channel()));
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else
Serial.println("WiFi not connected");
return status;
}
void check_WiFi(void)
{
if ( (WiFi.status() != WL_CONNECTED) )
{
Serial.println("\nWiFi lost. Call connectMultiWiFi in loop");
connectMultiWiFi();
}
}
void readAnalog(void)
{
//----- start custom code
sensorValue = analogRead(sensorPin);
Serial.print("Pin " + String(sensorPin) + " = ");
Serial.println(sensorValue);
//---- end custom code
}
void check_status(void)
{
static ulong checkstatus_timeout = 0;
static ulong checkwifi_timeout = 0;
static ulong current_millis;
#define WIFICHECK_INTERVAL 1000L
#define ANALOGREAD_INTERVAL 2000L
current_millis = millis();
#if USE_MULTI_WIFI
// Check WiFi every WIFICHECK_INTERVAL (1) seconds.
if ((current_millis > checkwifi_timeout) || (checkwifi_timeout == 0))
{
check_WiFi();
checkwifi_timeout = current_millis + WIFICHECK_INTERVAL;
}
#endif
// Print hearbeat every ANALOGREAD_INTERVAL (2) seconds.
if ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0))
{
readAnalog();
checkstatus_timeout = current_millis + ANALOGREAD_INTERVAL;
}
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print("\nStarting WiFiMulti_ESP32_AnalogRead on ");
Serial.println(ARDUINO_BOARD);
#if USE_MULTI_WIFI
for (int i = 0; i < NUMBER_SSIDS; i++)
{
wifiMulti.addAP(WiFi_Creds[i].ssid, WiFi_Creds[i].pass);
}
connectMultiWiFi();
#endif
}
void loop()
{
// put your main code here, to run repeatedly
check_status();
} and this is the terminal output 1. No MultiWiFi by using
|
Extremely bad news is that the normal ESP32 WiFi is also interfering with analogRead So no way to get out, at least now until the ESP32 bug is found and fixed. ESP8266 is always OK. If possible, you can switch your project to
Use the following simple sketch to test and verify #if !( ESP32 || ESP8266 )
#error This code is intended to run on the ESP32/ESP8266 platform! Please check your Tools->Board setting.
#endif
#define USE_WIFI false
#if ESP32
#include <WiFi.h>
int sensorPin = 27; //12; // select the input pin for the potentiometer
#elif ESP8266
#include <ESP8266WiFi.h>
int sensorPin = 12; //A0; // select the input pin for the potentiometer
#endif
#define SSID_MAX_LEN 32
#define PASS_MAX_LEN 64
typedef struct
{
char ssid[SSID_MAX_LEN + 1];
char pass[PASS_MAX_LEN + 1];
} WiFi_Credentials;
WiFi_Credentials WiFi_Creds[] =
{
{ "HueNet1", "12345678"},
{ "HueNet2", "12345678"},
{ "HueNet3", "12345678"}
};
#define NUMBER_SSIDS ( sizeof(WiFi_Creds) / sizeof(WiFi_Credentials) )
uint8_t status;
int sensorValue = 0; // variable to store the value coming from the sensor
uint8_t connectWiFi(void)
{
Serial.println("\nConnecting Wifi...");
int i = 0;
WiFi.begin(WiFi_Creds[0].ssid, WiFi_Creds[0].pass);
status = WiFi.status();
delay(3000);
while ( ( i++ < 10 ) && ( status != WL_CONNECTED ) )
{
status = WiFi.status();
if ( status == WL_CONNECTED )
break;
else
delay(3000);
}
if ( status == WL_CONNECTED )
{
Serial.println("WiFi connected to after " + String(i) + " times.");
Serial.println("SSID = " + WiFi.SSID());
Serial.println("RSSI = " + String(WiFi.RSSI()));
Serial.println("Channel: " + String(WiFi.channel()));
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else
Serial.println("WiFi not connected");
return status;
}
void check_WiFi(void)
{
if ( (WiFi.status() != WL_CONNECTED) )
{
Serial.println("\nWiFi lost. Call connectMultiWiFi in loop");
connectWiFi();
}
}
void readAnalog(void)
{
//----- start custom code
sensorValue = analogRead(sensorPin);
Serial.print("Pin " + String(sensorPin) + " = ");
Serial.println(sensorValue);
//---- end custom code
}
void check_status(void)
{
static ulong checkstatus_timeout = 0;
static ulong checkwifi_timeout = 0;
static ulong current_millis;
#define WIFICHECK_INTERVAL 1000L
#define ANALOGREAD_INTERVAL 2000L
current_millis = millis();
#if USE_WIFI
// Check WiFi every WIFICHECK_INTERVAL (1) seconds.
if ((current_millis > checkwifi_timeout) || (checkwifi_timeout == 0))
{
check_WiFi();
checkwifi_timeout = current_millis + WIFICHECK_INTERVAL;
}
#endif
// Print hearbeat every ANALOGREAD_INTERVAL (2) seconds.
if ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0))
{
readAnalog();
checkstatus_timeout = current_millis + ANALOGREAD_INTERVAL;
}
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print("\nStarting WiFi_ESP32_AnalogRead on ");
Serial.println(ARDUINO_BOARD);
#if USE_WIFI
connectWiFi();
#endif
}
void loop()
{
// put your main code here, to run repeatedly
check_status();
}
and this is the terminal output 1. No WiFi by using
|
OK, I find out the root cause of this issue as follows 1. ESP32 has 2 ADCs, named ADC1 and ADC22. ESP32 ADCs functions
3.. ESP32 WiFi uses ADC2 for WiFi functions
See in file adc_common.c
So I suggest to use ADC1, and pins GPIO32-GPIO39 Use the following simple sketch to test and verify #if !( ESP32 || ESP8266 )
#error This code is intended to run on the ESP32/ESP8266 platform! Please check your Tools->Board setting.
#endif
#define USE_MULTI_WIFI false
#if ESP32
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
int sensorPin1 = 34; // select the input pin for the potentiometer
int sensorPin2 = 35; // select the input pin for the potentiometer
#elif ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
int sensorPin1 = 12; // select the input pin for the potentiometer
int sensorPin2 = 13; // select the input pin for the potentiometer
#endif
#define SSID_MAX_LEN 32
#define PASS_MAX_LEN 64
typedef struct
{
char ssid[SSID_MAX_LEN + 1];
char pass[PASS_MAX_LEN + 1];
} WiFi_Credentials;
WiFi_Credentials WiFi_Creds[] =
{
{ "HueNet1", "12345678"},
{ "HueNet2", "12345678"},
{ "HueNet3", "12345678"}
};
#define NUMBER_SSIDS ( sizeof(WiFi_Creds) / sizeof(WiFi_Credentials) )
uint8_t status;
int sensorValue = 0; // variable to store the value coming from the sensor
uint8_t connectMultiWiFi(void)
{
Serial.println("\nConnecting Wifi...");
int i = 0;
status = wifiMulti.run();
delay(3000);
while ( ( i++ < 10 ) && ( status != WL_CONNECTED ) )
{
status = wifiMulti.run();
if ( status == WL_CONNECTED )
break;
else
delay(3000);
}
if ( status == WL_CONNECTED )
{
Serial.println("WiFi connected to after " + String(i) + " times.");
Serial.println("SSID = " + WiFi.SSID());
Serial.println("RSSI = " + String(WiFi.RSSI()));
Serial.println("Channel: " + String(WiFi.channel()));
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else
Serial.println("WiFi not connected");
return status;
}
void check_WiFi(void)
{
if ( (WiFi.status() != WL_CONNECTED) )
{
Serial.println("\nWiFi lost. Call connectMultiWiFi in loop");
connectMultiWiFi();
}
}
void readAnalog(void)
{
sensorValue = analogRead(sensorPin1);
Serial.print("Pin " + String(sensorPin1) + " = ");
Serial.println(sensorValue);
sensorValue = analogRead(sensorPin2);
Serial.print("Pin " + String(sensorPin2) + " = ");
Serial.println(sensorValue);
}
void check_status(void)
{
static ulong checkstatus_timeout = 0;
static ulong checkwifi_timeout = 0;
static ulong current_millis;
#define WIFICHECK_INTERVAL 1000L
#define ANALOGREAD_INTERVAL 2000L
current_millis = millis();
#if USE_MULTI_WIFI
// Check WiFi every WIFICHECK_INTERVAL (1) seconds.
if ((current_millis > checkwifi_timeout) || (checkwifi_timeout == 0))
{
//check_WiFi();
checkwifi_timeout = current_millis + WIFICHECK_INTERVAL;
}
#endif
// Print hearbeat every ANALOGREAD_INTERVAL (2) seconds.
if ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0))
{
readAnalog();
checkstatus_timeout = current_millis + ANALOGREAD_INTERVAL;
}
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print("\nStarting WiFiMulti_ESP32_AnalogRead on ");
Serial.println(ARDUINO_BOARD);
#if USE_MULTI_WIFI
for (int i = 0; i < NUMBER_SSIDS; i++)
{
wifiMulti.addAP(WiFi_Creds[i].ssid, WiFi_Creds[i].pass);
}
connectMultiWiFi();
#endif
}
void loop()
{
// put your main code here, to run repeatedly
check_status();
} and this is the terminal output with analogRead value varies when pulled up, float or pulled down
Again thanks for your report of this very interesting hidden issue.. And be confident as you're not beginner as you think. Issue closed now. Good luck with ESP32. |
@MaPoLom If somehow you must use those pins serviced by ADC2 (GPIO0, 2, 4, 12, 13, 14, 15, 25, 26 and 27), use the following fix to work with ESP32 WiFi #if !( ESP32 || ESP8266 )
#error This code is intended to run on the ESP32/ESP8266 platform! Please check your Tools->Board setting.
#endif
#define USE_MULTI_WIFI true
#if ESP32
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define USE_ADC2 true
//#define USE_ADC2 false
#if USE_ADC2
// ADC2 for GPIO0, 2, 4, 12, 13, 14, 15, 25, 26 and 27
int sensorPin1 = 12;
int sensorPin2 = 14;
#else
// DC1 for GPIO032-GPIO39
int sensorPin1 = 34;
int sensorPin2 = 35;
#endif
// Kludge to fix ADC2 + WiFi
#include "soc/sens_reg.h" // needed for manipulating ADC2 control register
uint64_t reg_b; // Used to store ADC2 control register
//////
#elif ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
int sensorPin1 = 12; // select the input pin for the potentiometer
int sensorPin2 = 13; // select the input pin for the potentiometer
#endif
#define SSID_MAX_LEN 32
#define PASS_MAX_LEN 64
typedef struct
{
char ssid[SSID_MAX_LEN + 1];
char pass[PASS_MAX_LEN + 1];
} WiFi_Credentials;
WiFi_Credentials WiFi_Creds[] =
{
{ "HueNet1", "12345678"},
{ "HueNet2", "12345678"},
{ "HueNet3", "12345678"}
};
#define NUMBER_SSIDS ( sizeof(WiFi_Creds) / sizeof(WiFi_Credentials) )
uint8_t status;
int sensorValue = 0; // variable to store the value coming from the sensor
uint8_t connectMultiWiFi(void)
{
Serial.println("\nConnecting Wifi...");
int i = 0;
status = wifiMulti.run();
delay(3000);
while ( ( i++ < 10 ) && ( status != WL_CONNECTED ) )
{
status = wifiMulti.run();
if ( status == WL_CONNECTED )
break;
else
delay(3000);
}
if ( status == WL_CONNECTED )
{
Serial.println("WiFi connected to after " + String(i) + " times.");
Serial.println("SSID = " + WiFi.SSID());
Serial.println("RSSI = " + String(WiFi.RSSI()));
Serial.println("Channel: " + String(WiFi.channel()));
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else
Serial.println("WiFi not connected");
return status;
}
void check_WiFi(void)
{
if ( (WiFi.status() != WL_CONNECTED) )
{
Serial.println("\nWiFi lost. Call connectMultiWiFi in loop");
connectMultiWiFi();
}
}
#if (ESP32 && USE_ADC2)
void save_ADC_Reg(void)
{
// Save ADC2 control register value : Do this before begin Wifi/Bluetooth
reg_b = READ_PERI_REG(SENS_SAR_READ_CTRL2_REG);
}
void restore_ADC_Reg(void)
{
// ADC2 control register restoring
WRITE_PERI_REG(SENS_SAR_READ_CTRL2_REG, reg_b);
SET_PERI_REG_MASK(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV);
}
#endif
void readAnalog(void)
{
#if (ESP32 && USE_ADC2)
restore_ADC_Reg();
#endif
sensorValue = analogRead(sensorPin1);
Serial.print("Pin " + String(sensorPin1) + " = ");
Serial.println(sensorValue);
sensorValue = analogRead(sensorPin2);
Serial.print("Pin " + String(sensorPin2) + " = ");
Serial.println(sensorValue);
}
void check_status(void)
{
static ulong checkstatus_timeout = 0;
static ulong checkwifi_timeout = 0;
static ulong current_millis;
#define WIFICHECK_INTERVAL 1000L
#define ANALOGREAD_INTERVAL 2000L
current_millis = millis();
#if USE_MULTI_WIFI
// Check WiFi every WIFICHECK_INTERVAL (1) seconds.
if ((current_millis > checkwifi_timeout) || (checkwifi_timeout == 0))
{
//check_WiFi();
checkwifi_timeout = current_millis + WIFICHECK_INTERVAL;
}
#endif
// Print hearbeat every ANALOGREAD_INTERVAL (2) seconds.
if ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0))
{
readAnalog();
checkstatus_timeout = current_millis + ANALOGREAD_INTERVAL;
}
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print("\nStarting WiFiMulti_ESP32_AnalogRead on ");
Serial.println(ARDUINO_BOARD);
#if (ESP32 && USE_ADC2)
save_ADC_Reg();
#endif
#if USE_MULTI_WIFI
for (int i = 0; i < NUMBER_SSIDS; i++)
{
wifiMulti.addAP(WiFi_Creds[i].ssid, WiFi_Creds[i].pass);
}
connectMultiWiFi();
#endif
}
void loop()
{
// put your main code here, to run repeatedly
check_status();
} and this is the terminal output when using in sketch
|
Hi, Thank you for your very, very thorough analysis. I'll need a few hours to digest this and choose the right path to follow. Perhaps it's an idea, to make a reference to these findings in the readme? Stay safe! Maurice |
Already updated README Don't hesitate to post an issue next time. Valid or invalid, common or rare problem is always helpful to other people. |
Although your issue has nothing to do with the library and it's not appropriate to hijack an issue, I just suggest you to read HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE) There you'll see that pin 25-26 belong to ADC2 which won't work when you're using WiFi. The obvious fix is to move the ADC pins from pins 25-26 to GPIO32-GPIO39 (for example 32-33). Next time, after you have graduated and become a professional, please give back to the community and post in the correct forum. Good Luck, |
I am very sorry. With my poor English, I understood that the subject was the same. I will delete the post. Thank you for the help anyway. Surely my intention was never to break any rule. |
It's OK to keep it there, possibly to help other users. Don't delete it. |
Hello,
Sorry to bother you with probably a beginners mistake and thank you for creating such a great library.
Issue is the following:
When I compile and run the autoconnect.ino, things run smoothly.
When I'm adding the simple code to read an analog pin (eg pin 12) it always returns 4095. I understand that when the pin is not connected this value should fluctuate but it stays constant.
I'll attach the example.
I'm using an esp32-wroom-32d on max osx
Thank you for your respond
Maurice
org_autoconnect2.txt
The text was updated successfully, but these errors were encountered: