This my code :
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <DHT.h>
#include <Adafruit_Sensor.h>
#define BLYNK_PRINT Serial
#define WIFI_SSID "-"
#define WIFI_PASSWORD "-"
// You get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char host[] = "KGK6boy4kCS9ZTW915FV5YmVT9hV12uN";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxx";
char pass[] = "xxxxx";
#define DHTPIN 4
#define DHTTYPE DHT11
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
char pumpon [] = "ON";
char pumponmanual [] = "ON MANUAL";
char pumpoff [] = "OFF";
int solenoidPin = 4; //This is the output pin on the Arduino we are using
int temperature;
int humidity;
void pushswitch_button_Sensor()
{
Blynk.run();
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
Blynk.virtualWrite(V5, temperature);
Blynk.virtualWrite(V6, humidity);
if (temperature >= 24 && humidity <= 60) {
Serial.println(" - Surrounding is HOT");
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
Blynk.virtualWrite(V10, pumpon);
}
else if (temperature <= 24 && humidity >= 60) {
Serial.println(" - Surrounding is COLD");
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
Blynk.virtualWrite(V10, pumpoff);
}
}
void setup() {
lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
// put your setup code here, to run once:
pinMode(solenoidPin, OUTPUT); //Sets the pin as an output
// Debug console
Serial.begin(9600);
Serial.println("DHT22 Output!");
dht.begin();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
timer.setInterval(1000L, pushswitch_button_Sensor);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Blynk.begin(host, ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
BLYNK_WRITE (V8)
{
int pinValue = param.asInt ();
if (pinValue == 1) {
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
Serial.println ( " SOLENOID ON FROM MANUAL CONTROL" );
Blynk.run();
Blynk.virtualWrite (V10, pumponmanual);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
Blynk.virtualWrite(V5, temperature);
Blynk.virtualWrite(V6, humidity);
int pinValue = 0;
Blynk.syncVirtual (V8);
}
else {
pushswitch_button_Sensor();
}
}
void displayData_LCD() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Temp.: "); // Prints string "Temp." on the LCD
lcd.print(temperature); // Prints the temperature value from the sensor
lcd.print(" C");
Serial.print("temperature = ");
Serial.print(temperature);
Serial.print("C ");
lcd.setCursor(0, 1);
lcd.print("Humi.: ");
lcd.print(humidity);
lcd.print(" %");
Serial.print("Current humidity = ");
Serial.print(humidity);
Serial.print("% ");
delay (1000);
}
void sendData_MYSQL() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print("connecting to ");
Serial.println(host);
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
}
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 5555;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// This will send the request to the server
client.print(String("GET http://your_hostname/iot_project/connect.php?") +
("&temperature=") + temperature +
("&humidity=") + humidity +
" HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 1000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
void loop() {
Blynk.run();
timer.run();
displayData_LCD();
pushswitch_button_Sensor();
sendData_MYSQL();
}
But the reading of DHT is not show in serial monitor......