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.