Hey guys! I have a weird problem programming my college project: a weather station with ESP32.
Context:
I’m using a LDR as a UV index sensor (I know that’s not right but we can’t afford that sensor).
So, I’m using a ESP32 with Arduino and when I try to read the value given by the LDR it shows 0 on the serial monitor when I’m using “Ubidots.h”. ubidots.send() to be precise.
I’ll show you the code and the serial monitor
> // IMPORTANT: Device type are only supported through HTTP
>
> /****************************************
> * Include Libraries
> ****************************************/
>
> #include "Ubidots.h"
> #include <WiFi.h> // importing all the required libraries
> #include "Arduino.h"
> #include "DHT.h"
> #include "BMP085.h"
> #include <Wire.h>
>
> /****************************************
> Define Instances and Constants
> ****************************************/
> //const char* DEVICE_TYPE = "my-type"; // Edit here your device type label
> const char* WIFI_SSID = "xxxxxx"; // Put here your Wi-Fi SSID
> const char* WIFI_PASS = "xxxxx"; // Put here your Wi-Fi password
>
> const char* UBIDOTS_TOKEN = "xxxxxxxx"; // Put here your Ubidots TOKEN
> Ubidots ubidots(UBIDOTS_TOKEN, UBI_UDP);
>
> #define DHTPIN 5 // dht sensor is connected to D5
> #define DHTTYPE DHT11 // DHT 11
> DHT dht(DHTPIN, DHTTYPE); // initialise dht sensor
>
> BMP085 myBarometer; // initialise pressure sensor
>
> #define LDRPIN 0 //LDR conectado en GPIO 0
>
> float temperature; // parameters
> float humidity;
> float pressure;
> float mbar;
> int LDR;
> float uv;
> int norte;
> int sur;
> int este;
> int oeste;
> int velocidad;
> int sensor;
> /****************************************
> Auxiliar Functions
> ****************************************/
>
> void sendSensor() // function to read sensor values
> {
> humidity = dht.readHumidity();
> temperature = dht.readTemperature();
> if (isnan(humidity) || isnan(temperature))
> {
> Serial.println("Failed to read from DHT sensor!");
> return;
> }
>
> pressure = myBarometer.bmp085GetPressure(myBarometer.bmp085ReadUP()); // read pressure value in pascals
> mbar = pressure / 100; // convert pascals to millibar (millibar = hectopascales)
>
>
> LDR = analogRead(LDRPIN);
> Serial.println(LDR);
>
> uv= map(LDR,0,4095,0,11);
>
> //LDR= 5;
> norte=1;
> sur=0;
> este=0;
> oeste=0;
> velocidad=15;
>
> ubidots.add("Temperatura", temperature); // Change for your variable name
> ubidots.add("Humedad", humidity);
> ubidots.add("Presion", mbar);
> ubidots.add("Indice UV", uv);
> ubidots.add("Norte", norte);
> ubidots.add("Sur", sur);
> ubidots.add("Este", este);
> ubidots.add("Oeste", oeste);
> ubidots.add("Velocidad Viento", velocidad);
> }
>
> /****************************************
> Main Functions
> ****************************************/
>
> void setup() {
>
> Serial.begin(115200);
> myBarometer.init();
> dht.begin();
> delay(1000);
> ubidots.wifiConnect(WIFI_SSID, WIFI_PASS);
> delay(1000);
> }
>
> void loop() {
>
> sendSensor();
>
> bool bufferSent = false;
> bufferSent = ubidots.send(); // Will send data to a device label that matches the device Id
>
>
> if (bufferSent) {
> // Do something if values were sent properly
> Serial.println("Values sent by the device");
>
> }
> delay(5000);
> }
Here’s the serial monitor
On the contrary, when I run this example code for analogRead:
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(115200);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int LDR = analogRead(0);
// print out the value you read:
Serial.println(LDR);
delay(1000); // delay in between reads for stability
}
It shows this:
In conclusion, I’m clearly missing something and I can’t find out what it is. Please help me