WiFi connection breaks sensor code

Hello. I have a project where the idea is to read data from a photoresistor, and send this data to a server via UDP.
The lower code works as expected, i have a photoresistor connected to GPIO2, leds connected to GPIO4,5 on a olimex poe iso board. In the main loop, a green LED is lit as default, the sensor data is being red, then based on the condition of the resistance value of the sensor, 0(dark) - 4098( fully lit) the program turns off the green LED and turns on a red LED if the sensor value drops below a certain point (the sensor is not being lit by a light source anymore).

#include <NTPClient.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include "time.h"
#include <SPI.h>
#include <Wire.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <ESP32Time.h>


//define photoresistor on analog pin 2
int pResistor = 2;

//define counter
int c = 0;

//define variable for timing. Time now is current time at main loop, period is the value for the delay timer.
unsigned long time_now = 0;
int period = 500;


//define pins for led lights
const int Gled = 4;
const int Rled = 5;

//define variable for the olimex on-board button
int OBB;


//define variable for photoresistor value
int value;

void setup() {
  //define serial speed
  Serial.begin(115200);
  //setup the pResistor pin as input and Gled/Rled as output pin
  pinMode(pResistor, INPUT);
  pinMode(Gled, OUTPUT);
  pinMode(Rled, OUTPUT);
  //make the on-board button of the olimex board an input to see the status of the button (1 is released, 0 is pressed)
  pinMode(34, INPUT);


  //print message Setup completed
  Serial.println("Setup Completed");
}

void loop() {

//read the status of the olimex on-board button pin
OBB = digitalRead(34);

//turn on the green LED
digitalWrite(Gled, HIGH);

//turn off the red LED
digitalWrite(Rled, LOW);

//start the counter for delay timer
time_now = millis();

//read the pResistor value and store it in variable
value = analogRead(pResistor);


//Print photoresistor value and the no. of times sensor has been triggered to serial monitor
SerialPrint();

//define the function which checkes if the presistor value has changed (tigger is triggered)
sensorCounter();



}

//write the value of the photoresistor in serial monitor 
void SerialPrint(){
  //Print photoresistor value and the no. of times sensor has been triggered to serial monitor
  Serial.print("The photoresistor value with led ON is: ");
  Serial.print(value);
  Serial.print("  The no. of times sensor has been triggered is ");
  Serial.println(c);
}

//define the function which checkes if the presistor value has changed (tigger is triggered)
void sensorCounter(){
  if(value <=2000) {
    //turn on the red LED
    digitalWrite(Rled, HIGH);
    //add 1 to counter
    c=c+1;
    //ADJUST THIS DELAY IN THE VARIABLE "period" IN THE BEGUINING OF THE CODE
    while(millis() < time_now + period){
     //code waits until period is up
    }
  }
}


void ifButtonPressed(){
  //function which turns on the LED light and prints the photoresitor state if the on-board button of the olimex is pressed
    if (OBB == 1) {
                  //turn on the green LED
                  digitalWrite(Gled, HIGH);
                  //write the value of the photoresistor in serial monitor 
                  Serial.print("The photoresistor value with led ON is: ");
                  Serial.print(value);
                  Serial.print("  The Gled value with led ON is: ");
                  Serial.print(Gled);
                  Serial.print("  The OBB value inside IF is ");
                  Serial.println(OBB);
                  }
    else{
        //turn off the green LED
        digitalWrite(Gled, LOW);
        //write the value of the photoresistor while button is not pressed
        Serial.print("The photoresistor value with LED OFF is: ");
        Serial.print(value);
        Serial.print("  The Gled value with led OFF is: ");
        Serial.print(Gled);
        Serial.print("  The OBB value inside ELSE is ");
        Serial.println(OBB);
        }

}

The problems occur when i try to add a wifi/udp functionality. The following code only has wifi function added, to connect to a wifi network at setup. The sensor stops working completely, always showing 0 as the value. I have no idea why this is not working, the wifi code is copied from another project where it works without problems.

#include <NTPClient.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include "time.h"
#include <SPI.h>
#include <Wire.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <ESP32Time.h>


//define photoresistor on analog pin 2
int pResistor = 2;

//define counter
int c = 0;

//define variable for timing. Time now is current time at main loop, period is the value for the delay timer.
unsigned long time_now = 0;
int period = 500;

//period 2 for wifi timer
int period2 = 10000;

//status of the WIFI
int status = WL_IDLE_STATUS;
//SSID name and password, must be 2.4GHZ!!!
char ssid[] = "myssid";        // your network SSID (name)
char pass[] = "mypass";    // your network password (use for WPA, or use as key for WEP)
//define the IP address to which the UDP stream will be forwarded to
IPAddress UDPServer(x, x, x, x);
//define the UDP transport method
WiFiUDP Udp;

//define pins for led lights
const int Gled = 4;
const int Rled = 5;

//define variable for the olimex on-board button
int OBB;


//define variable for photoresistor value
int value;

void setup() {
  //define serial speed
  Serial.begin(115200);
  //setup the pResistor pin as input and Gled/Rled as output pin
  pinMode(pResistor, INPUT);
  pinMode(Gled, OUTPUT);
  pinMode(Rled, OUTPUT);
  //make the on-board button of the olimex board an input to see the status of the button (1 is released, 0 is pressed)
  pinMode(34, INPUT);
  
  // attempt to connect to WiFi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    while (millis() < time_now + period2) {
      //code waits until period is up
        } 
    }
    
  //print WiFi Status
  Serial.println("Connected to wifi");
  printWiFiStatus();

  //print message Setup completed
  Serial.println("Setup Completed");
}

void loop() {

//read the status of the olimex on-board button pin
OBB = digitalRead(34);

//turn on the green LED
digitalWrite(Gled, HIGH);

//turn off the red LED
digitalWrite(Rled, LOW);

//start the counter for delay timer
time_now = millis();

//read the pResistor value and store it in variable
value = analogRead(pResistor);


//Print photoresistor value and the no. of times sensor has been triggered to serial monitor
SerialPrint();

//define the function which checkes if the presistor value has changed (tigger is triggered)
sensorCounter();



}

//write the value of the photoresistor in serial monitor 
void SerialPrint(){
  //Print photoresistor value and the no. of times sensor has been triggered to serial monitor
  Serial.print("The photoresistor value with led ON is: ");
  Serial.print(value);
  Serial.print("  The no. of times sensor has been triggered is ");
  Serial.println(c);
}

//define the function which checkes if the presistor value has changed (tigger is triggered)
void sensorCounter(){
  if(value <=2000) {
    //turn on the red LED
    digitalWrite(Rled, HIGH);
    //add 1 to counter
    c=c+1;
    //ADJUST THIS DELAY IN THE VARIABLE "period" IN THE BEGUINING OF THE CODE
    while(millis() < time_now + period){
     //code waits until period is up
    }
  }
}


void ifButtonPressed(){
  //function which turns on the LED light and prints the photoresitor state if the on-board button of the olimex is pressed
    if (OBB == 1) {
                  //turn on the green LED
                  digitalWrite(Gled, HIGH);
                  //write the value of the photoresistor in serial monitor 
                  Serial.print("The photoresistor value with led ON is: ");
                  Serial.print(value);
                  Serial.print("  The Gled value with led ON is: ");
                  Serial.print(Gled);
                  Serial.print("  The OBB value inside IF is ");
                  Serial.println(OBB);
                  }
    else{
        //turn off the green LED
        digitalWrite(Gled, LOW);
        //write the value of the photoresistor while button is not pressed
        Serial.print("The photoresistor value with LED OFF is: ");
        Serial.print(value);
        Serial.print("  The Gled value with led OFF is: ");
        Serial.print(Gled);
        Serial.print("  The OBB value inside ELSE is ");
        Serial.println(OBB);
        }

}

//function for defining WiFi status
void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

I would really appreciate any feedback here, as i am stuck and don't really know how to procede. I have newer had issues with other sensor readings when connecting to wifi.

Equipment used:
Olimex PoE ISO
PGM5537 photoresistor
390Ohm resistor
Red, Green LED

Connections:
photoresistor connected: to +5V - photoresistor1/2, photoresistor 2/2 - GPIO2 - Resistor - GND
LEDs connected to GPIO 4 - LED 1/2, LED 2/2- GND , GPIO 5 - LED 1/2, LED 2/2 - GND

Thanks for any input

Br

David

I posted on a similar post just yesterday... :).

The ESP32 integrates two 12-bit SAR (Successive Approximation Register) ADCs supporting a total of 18 measurement channels (analog enabled pins).

The ADC driver API supports ADC1 (8 channels, attached to GPIOs 32 - 39), and ADC2 (10 channels, attached to GPIOs 0, 2, 4, 12 - 15 and 25 - 27). However, the usage of ADC2 has some restrictions for the application:

ADC2 is used by the Wi-Fi driver. Therefore the application can only use ADC2 when the Wi-Fi driver has not started .
Some of the ADC2 pins are used as strapping pins (GPIO 0, 2, 15) thus cannot be used freely.

Can you use the ADC1 pins?

1 Like

This was indeed the case. Set the GPIO36 as the analog input for the photoresistor, and it works with wifi enabled. Thanks man, you're awesome!

Br

David

time_now is initialised to zero, but the current time is not being captured before the above while loop runs. In any case, since millis() counts the number of seconds since the program started running and the startup code up to this point will have taken just a few milliseconds to run, the above may as well be just:

    while(millis() < period){
     //code waits until period is up
    }

However, ideally, the program should capture the current number of milliseconds elapsed before running the loop:

    time_now = millis();
    while(millis() < time_now + period){
     //code waits until period is up
    }

This has no bearing on your photoresistor state detection which has been explained by red_car. Its just an observation.

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