Hello
lately, I was trying to deploy Temperature and Humidity measurement on Node-Red Using an ESP32 DevKit module.
Through Arduino IDE I Checked that module successfully reads values and connects to my WiFi.
In Node-Red side, it appears to be all ok, But I have my Doubts.
The Flow that I implemented its this:
and the Code:
#include "WiFi.h"
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
const char* ssid = ////////////////////////wifi Name
const char* password = //////////////////////////WiFi key
int port = 1880;
const char* URL = "SensorTemperatura"; //
const char* host = "http://127.0.0.1:1880";
unsigned long lastSend;
float humidity = 0.0;
float temperature = 0.0;
String payload = "";
DHT dht(DHTPIN, DHTTYPE);
void setup(){
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{ delay(500);
Serial.print(".");
} Serial.println(" connected");
dht.begin();
}
void loop(){ if ( millis() - lastSend > 1000 ) {
leerSensor();
delay(10);
json();
servicioPost();
lastSend = millis();
}
}void leerSensor() { humidity = dht.readHumidity();// Lee la humedad
temperature = dht.readTemperature(); // Lee la temperatura
if (isnan(humidity) || isnan(temperature)) { // Verifica si hay errores de lectura en el sensor.
Serial.println("Failed to read from DHT sensor!"); // Mensaje de falla en el sensor
return; //Si los hay, vuelve a intentar la comunicaciĂłn }}
}
}
void servicioPost() {
WiFiClient client;
Serial.printf("\n[Connecting to %s ... ", host);
if (client.connect(host, port)) {
Serial.println("connected]");
Serial.println("[Sending a request]");
client.print(String("POST /") + URL + "HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Accept: /" + "*\r\n" + "Content-Length: " + payload.length() + "\r\n" + "Content-Type: application/json" + "\r\n" + "\r\n" + payload );
Serial.println("[Response:]");
while (client.connected() || client.available())
{ if (client.available())
{ String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("\n[Disconnected]");
}
else { Serial.println("connection failed!]");
client.stop();
}
}
void json() {
String humedad = String(humidity, 2);
String temperatura = String(temperature, 2);
payload = "{"; payload += "\"temperatura\":"; payload += temperatura;
payload += ",";
payload += "\"humedad\":"; payload += humedad;
payload += "}";
Serial.println(temperatura);
}
and the Error that I Received its this:
will have any idea what I'm doing wrong????
Regards!!