/* Các thư viện cần thiết nên được thêm vào từ Library Manager trước khi khời động code:
DHT sensor library for ESPx
PubSubClient
*/
/* Giả lập sử dụng trang wokwi */
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
//#include <PubSubClient.h>
//#include <iostream>
//#include <ctime>
const int DHT_PIN = 15;
const char *mqttUser = "";
const char *mqttPassword = "";
WiFiClient wifiClient;
//PubSubClient mqttClient(wifiClient);
DHTesp dht;
//PubSubClient mqttClient(wifiClient);
unsigned long lastMillis = 0;
const String serverName = "https://watermonitoring-dashboard.onrender.com/postpH"; //"http://water-quality-monitoring1.herokuapp.com/postpH";
StaticJsonDocument<500> doc;
// #define mqttServer "test.mosquitto.org" //The Server where the data is sent to
void setup() {
Serial.begin(115200);
dht.setup(DHT_PIN, DHTesp::DHT22);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
void loop() {
//mqttClient.loop();
float temperature = dht.getTemperature();
String char_nhiet_do = (String)temperature;
char* nhietdo;
nhietdo = &char_nhiet_do[0];
doc["id"] = "Wemos D1";
doc["diachiDevice"] = "192.168.1.40";
doc["loaiCambien"] = "pH";
doc["pH"] = nhietdo;
if(WiFi.status()== WL_CONNECTED){
WiFiClient wifiClient;
HTTPClient http;
http.begin(serverName); //http.begin(wifiClient, serverName);
http.addHeader("Content-Type", "application/json");
String json;
serializeJson(doc, json);
Serial.println(json);
int httpResponseCode = http.POST(json);
delay(500);
Serial.println(httpResponseCode);
http.end();
}
// mqttClient.loop();
// String payload = "{";
// payload += "\"pH\"";
// payload+=":";
// payload += nhietdo; payload += "}";
// char temp[100];
// payload.toCharArray(temp, 100 );
// mqttClient.publish("wemos/mobile/chart/datapH", temp);
}