Hi community, I'm working on esp8266 but I have a problem; I'm sending the data I read from DHT11 to mysql, and there's no problem here, it just sends a nan value and a value of 0 every once in a while in the data I send.I'd appreciate it if you'd help.
- Im using Adafruit DHT library.
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
#include <Wire.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "EA Research Center";
const char* password = "******";
const char* serverName = "***************";
String apiKeyValue = "*********";
String sensorName = "DHT";
String sensorLocation = "KÖLN";
String deviceID = "1";
//String testv1 = "3";
//String testv2 = "3";
//String testv3 = "3";
void setup() {
Serial.begin(115200);
dht.begin();
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());
}
void loop() {
//Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
dht.begin();
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
+ "&location=" + sensorLocation + "&value1=" + String(dht.readTemperature())
+ "&value2=" + String(dht.readHumidity()) + "&value3=" + deviceID + "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
//Send an HTTP POST request every 30 seconds
delay(30000);
}