Sensor pins don't update values after wifi connection - esp32

Hello, I am trying to post sensor data to a rest api server. All seems good on the networking side of things. Wifi connection is also established on the esp32. My sensor is also working as I have tested it without turning on Wifi before hand. My problem is when I establish a Wifi connection with the esp I seem to lose my way of reading values from my sensor which I figure is working properly. I receive OK from the http responses which is a good sign, but my sensor seems to... not wanna turn on...
I appreciate any help in advance.

Here is the code:

#define FASTLED_INTERNAL
#include <FastLED.h>
#include <WiFi.h>
#include <HTTPClient.h>

/************************* LEDs & Sensor setup *********************************/
#define LED_TYPE    WS2812B

#define LDR         14    //pin gpio14 to read photoresistor
#define LED_PIN     12    //pin gpio13 to control LEDs
#define NUM_LEDS    5     //control 94 LEDs
#define BRIGHTNESS  200   //initialize LED brightness to 200, range is 0-255

CRGB leds[NUM_LEDS];

/* photoresistor */
#define Vin 5     // 5V input
#define R 10000   // 10k res
int photo_r_value = 0;
int lux;  // Lux value
String lux_str = "";

/******************************************************************************/

/**************************** Network Setup ***********************************/

const char *ssid = "";
const char *password = "";

// Your Domain name with URL path or IP address with path
String serverName = "http://192.168.0.8:4020/api/sensor/update-sensor"; // This IP is my computers IP which the ESP can see.

/******************************************************************************/

unsigned long lastTime = 0;
unsigned long timerDelay = 5000;


boolean set = true;
uint8_t rgb = 0;

void setup() {
  delay(3000); // power-up safety delay

  Serial.begin(115200);

  /// Wifi Setup
  WiFi.begin(ssid, password);

  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to Wifi network with IP address: ");
  Serial.println(WiFi.localIP());
  
    

  /// initialize LED strip
  FastLED.addLeds<LED_TYPE, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50); // BRIGHTNESS

}

void loop() {
  
  //delay(1000);

  readLDR();
  breath();
  FastLED.show();

  //if (Serial.available()) {
    //lux_str = Serial.readString();
  //}

    
  // FIXME: sensor values not being read once wifi connection is established ??? 
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {

    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http; // Declare an object of class HTTPClient

      String serverPath = serverName + "?photo1=" + lux_str;
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      //http.begin(serverPath);
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }

}

void readLDR() {
  // read in photoresistor value
  photo_r_value = analogRead(LDR);
  lux = sensorRawToPhys(photo_r_value);
  lux_str = String(lux);

  // try adjusting time here to less than the time required to make the GET request
  //EVERY_N_SECONDS(3) {
    //Serial.print("Photoresitor raw value: ");
    //Serial.print(photo_r_value);
    //Serial.print(", lux value: ");
    //Serial.print(lux);
    //Serial.print(" lumen");
    //Serial.println();
  //}
}

int sensorRawToPhys(int raw) {
  
  float Vout = float(raw) * (Vin / float(4095));  // analog reading to volatge conversion
  float R_LDR = (R * (Vin - Vout)) / Vout;  // voltage to resistance conversion  -- voltage div formula
  int lux_val = 500 / (R_LDR/1000);   // resistance to lumen conversion
  
  return lux_val;
}

void breath() {
  
  if (set) {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB(rgb, rgb, rgb);
    }
    
    EVERY_N_MILLISECONDS(15) {
      rgb++;
    }
    
    if (rgb == 255) {
      //Serial.println(rgb);
      set = false;
    }
  }

  if (set == false) {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB(rgb, rgb, rgb);
    }
    
    EVERY_N_MILLISECONDS(15) {
      rgb--;
    }
    
    if (rgb == 0) {
      //Serial.println(rgb);
      set = true;
    }
  }

}

My first time posting on here so sorry if this is overkill and all the code wasn't necessary.

What is that mysterious, secret, sensor?

I'm not familiar with the ESP32.

What happens if you re-instate some of the serial prints in readLDR? Does it print the correct values?

We prefer complete code so we can see how everything hangs together so thank you for that.

a photoresistor :slight_smile:

Why do You apply fog around the real facts? No info in that text.
Photoresistors..... Slow devices meaning quick light changes might take seconds to be readable by the controller.

Yes, it prints the correct values as far as the initial values go. So, only 0 for photo_r_value and lux. I find this really strange because when I comment out any related wifi connection stuff (i.e,..

/// Wifi Setup
  WiFi.begin(ssid, password);

  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to Wifi network with IP address: ");
  Serial.println(WiFi.localIP());

and

if ((millis() - lastTime) > timerDelay) {

    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http; // Declare an object of class HTTPClient

      String serverPath = serverName + "?photo1=" + lux_str;
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      //http.begin(serverPath);
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }

those readings work as expected. I can't tell if there is some sort of blocking going on somewhere in the wifi setup and how to avoid it, or if the analog pin I am using some how gets overtaken by the wifi module. The later seems unlikely but I am also new with working with the esp, so not quite sure.

I get readings from the sensor whenever wifi setup is not instantiated. So, I feel like my problem lies there. It seems that the wifi connection does some sort of blocking on being able to read anything from the analog pin. That's where my knowledge of what's going on sort of halts. When viewing the variables that are related to the sensor: photo_r_val and lux when wifi is established they stay at their initial value: 0.

You have an advanced build and debugging calls for thinking. You need to identify parts in the total chain and debug them one by one. It's hopeless to go for the entire system.
Use serial monitor and, in the code, Serial.println (millis());. Priniting millis could tell how the flow is.

Ok, thanks. I'll give this a try and see what I come up with.

I countered the same issue. Have u found the solution?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.