// #include <WiFi.h>
// #include <HTTPClient.h>
// #include "DHT.h"
// //#include "DHTesp.h"
// //#include <Arduino_JSON.h>
// const char* ssid = "Wokwi-GUEST";
// const char* password = "";
// String URL = "http:/192.168.20.246/dht22_project/test_data.php";
// int DHTPIN = 15;
// DHT dht(DHTPIN, DHT22);
// int Temperature = 50;
// int Humidity = 50;
// void setup() {
// Serial.begin(115200);
// // WiFi.begin("Wokwi-GUEST", "", 6);
// // while (WiFi.status() != WL_CONNECTED) {
// // delay(1000);
// // Serial.println("กำลังเชื่อมต่อ WiFi...");
// // }
// // Serial.println("-----พร้อมทำงาน-----");
// // delay(1000);
// WiFi.begin(ssid, password);
// Serial.print("Connecting to WiFi");
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// Serial.print("OK! IP=");
// Serial.println(WiFi.localIP());
// dht.begin();
// }
// void loop() {
// String postData = "temperature=" + String(temperature) + "&humidity=" + String(humidity);
// // HTTPClient http;
// // http.begin(URL);
// // int httpCode = http.POST(postDate);
// // string payload = http.getString();
// // http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// // Serial.print("URL : "); Serial.println(URL);
// // Serial.print("Data : "); Serial.println(postData);
// // Serial.print("httpCode : "); Serial.println(httpCode);
// // Serial.print("payload : "); Serial.println(payload);
// Serial.println("---------------------------------------");
// delay(1000);
// }
// void get_DHT22_sensor_data()
// {
// float Humi = dht.readHumidity();
// float temp = dht.readTemperature();
// Serial.print("Humidity is : ");
// Serial.println(Humi);
// Serial.print("Temperature is : ");
// Serial.println(temp);
// delay(2000);
// }
#include <WiFi.h>
#include <HTTPClient.h>
String URL = "http:/192.168.20.246/dht22_project/test_data.php";
const char* ssid = "Wokwi-GUEST";
const char* password = "";
int temperature = 50;
int humidity = 50;
void setup() {
Serial.begin(115200);
connectWiFi();
}
void loop() {
if(WiFi.status() != WL_CONNECTED) {
connectWiFi();
}
String postData = "temperature=" + String(temperature) + "&humidity=" + String(humidity);
HTTPClient http;
http.begin(URL);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST(postData);
String payload = http.getString();
Serial.print("URL : "); Serial.println(URL);
Serial.print("Data: "); Serial.println(postData);
Serial.print("httpCode: "); Serial.println(httpCode);
Serial.print("payload : "); Serial.println(payload);
Serial.println("--------------------------------------------------");
}
void connectWiFi() {
WiFi.mode(WIFI_OFF);
delay(1000);
//This line hides the viewing of ESP as wifi hotspot
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("connected to : "); Serial.println(ssid);
Serial.print("IP address: "); Serial.println(WiFi.localIP());
}