I have a soil moisture connected to an ESP32. The soil moisture is powered by the 3v of the ESP32 and the ground is connected to the ground. The A0 of the soil moisture is connected to the D4 on the ESP32.
When I run my code without connecting to the internet, I am able to get a reading. But once I run my function connectToWiFi(), the line WiFi.begin resets my reading to 0 and that's the only value I will get from the program moving forward.
This is my code that I've been using for debugging:
#include <Arduino.h>
#include <WiFi.h>
int msensor = 4;
int msvalue = 0;
#define WIFI_NETWORK "xxx"
#define WIFI_PASSWORD "xxx"
#define WIFI_TIMEOUT_MS 20000
void setup() {
Serial.begin(9600);
pinMode(msensor, INPUT);
delay(500);
msvalue = analogRead(msensor);
Serial.print("soil mositure 0: ");
Serial.println(msvalue); //will get a reading
connectToWiFi();
}
void loop() {
}
void connectToWiFi() {
Serial.println("Connecting to wifi");
delay(500);
msvalue = analogRead(msensor);
Serial.print("soil mositure 1: ");
Serial.println(msvalue); //will get a reading
WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);
delay(500);
msvalue = analogRead(msensor);
Serial.print("soil mositure 2: ");
Serial.println(msvalue); //reading will return 0
unsigned long startAttemptTime = millis();
while(WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < WIFI_TIMEOUT_MS) {
Serial.print(".");
delay(100);
}
delay(500);
msvalue = analogRead(msensor);
Serial.print("\nsoil mositure 3: ");
Serial.println(msvalue); //reading will return 0
}
And here is my serial monitor:
soil mositure 0: 4095
20:33:45.999 -> Connecting to wifi
20:33:46.466 -> soil mositure 1: 4095
20:33:47.074 -> soil mositure 2: 0
20:33:47.122 -> .........................
20:33:50.077 -> soil mositure 3: 0
Why does connecting to the WiFi change my value?