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