// #include <ArduinoJson.h>
// #define LED_PIN2 22
// #define LED_PIN3 23
// #define BUTTON_PIN 19
// #define LED_BUILTIN 2
// int LedState = LOW;
// int LastButtonState;
// int ButtonState;
// int Status_value;
// //millis() status online
// unsigned long period = 2000;
// unsigned long last_time = 0;
// unsigned long buttonPressedStartTime = 0;
// const unsigned long buttonPressDuration = 10000; // 10 seconds
// bool isButtonPressed = false;
// unsigned long lastCountTime = 0;
// const unsigned long CountInterval = 1000; // 1 second
// void setup() {
// Serial.begin(115200);
// pinMode(LED_BUILTIN, OUTPUT);
// // wash status
// pinMode(LED_PIN2, OUTPUT);
// pinMode(LED_PIN3, OUTPUT);
// pinMode(BUTTON_PIN, INPUT_PULLUP);
// }
// void loop() {
// ButtonFunction();
// }
// void ButtonFunction() {
// char* Status_Online;
// char* Status_Offline;
// if(Status_value != 1){
// if (millis() - last_time > period) {
// last_time = millis();
// Status_Online = "Status: Online";
// Serial.println(Status_Online);
// }
// }else{
// if (millis() - last_time > period) {
// last_time = millis();
// Status_Offline = "Status: Washing";
// Serial.println(Status_Offline);
// }
// }
// ButtonState = digitalRead(BUTTON_PIN);
// if (ButtonState == LOW && !isButtonPressed) {
// isButtonPressed = true;
// buttonPressedStartTime = millis();
// digitalWrite(LED_PIN3, LOW);
// digitalWrite(LED_PIN2, HIGH);
// Serial.println("LED2 ON");
// Serial.println("Start...");
// }
// if (isButtonPressed) {
// unsigned long currentTime = millis();
// unsigned long elapsedTime = currentTime - buttonPressedStartTime;
// int count = map(elapsedTime, 0, buttonPressDuration, 10, 0);
// Status_value = 1;
// if (count <= 0) {
// digitalWrite(LED_PIN2, LOW);
// Serial.println("LED2 OFF");
// Serial.println("End...");
// isButtonPressed = false;
// Status_value =0;
// } else if (currentTime - lastCountTime >= CountInterval) {
// lastCountTime = currentTime;
// Serial.print("Time : ");
// Serial.print(count);
// Serial.print(" min ");
// Serial.println();
// }
// }
// }
/*----------------------Communication Define------------------------------------*/
// #include <WiFi.h>
// #include <PubSubClient.h>
// #include <ArduinoJson.h>
// #include "DHT.h"
// #define WIFI_STA_NAME "Wokwi-GUEST"
// #define WIFI_STA_PASS ""
// #define MQTT_SERVER "iot.nipa.cloud"
// #define MQTT_PORT 1883
// #define MQTT_USERNAME "ZGkxdtZQnIDIsDFmV9Ce"
// #define MQTT_PASSWORD ""
// #define MQTT_NAME "ESP32_TEST"
// WiFiClient client;
// PubSubClient mqtt(client);
// #define LED_PIN1 21
// #define LED_PIN2 22
// #define LED_PIN3 23
// #define BUTTON_PIN 19
// #define LED_BUILTIN 2
// int LedState = LOW;
// int LastButtonState;
// int ButtonState;
// int Status_value;
// bool isButtonPressed = false;
// //millis() status online
// unsigned long period = 2000;
// unsigned long last_time = 0;
// //millis() Button Pressed
// unsigned long buttonPressedStartTime = 0;
// const unsigned long buttonPressDuration = 10000; // 10 seconds
// //millis() countTime
// unsigned long lastCountTime = 0;
// const unsigned long CountInterval = 1000; // 1 second
// //JSON parameter
// // JSON Washing
// char jsonString[200];
// int timeWashing;
// bool ledStatus;
// int value;
// // JSON DHT22
// char jsonTemp[200];
// int Temp;
// int Humi;
// void setup() {
// Serial.begin(115200);
// pinMode(LED_BUILTIN, OUTPUT);
// Serial.println();
// Serial.println();
// Serial.print("Connecting to ");
// Serial.println(WIFI_STA_NAME);
// WiFi.mode(WIFI_STA);
// WiFi.begin(WIFI_STA_NAME, WIFI_STA_PASS);
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
// digitalWrite(LED_PIN1, !digitalRead(LED_PIN1));
// }
// digitalWrite(LED_BUILTIN, HIGH);
// digitalWrite(LED_PIN1, HIGH);
// Serial.println("");
// Serial.println("WiFi connected");
// Serial.println("IP address: ");
// Serial.println(WiFi.localIP());
// mqtt.setServer(MQTT_SERVER, MQTT_PORT);
// mqtt.setCallback(callback);
// // wash status
// pinMode(LED_PIN2, OUTPUT);
// pinMode(LED_PIN3, OUTPUT);
// pinMode(BUTTON_PIN, INPUT_PULLUP);
// }
// void loop() {
// /*----------------------Communication Section------------------------------------*/
// if (mqtt.connected() == false) {
// //Serial.print("MQTT connection... ");
// if (mqtt.connect(MQTT_NAME, MQTT_USERNAME, MQTT_PASSWORD)) {
// //Serial.println("connected");
// mqtt.subscribe("v1/devices/me/attributes");
// } else {
// Serial.println("failed");
// delay(5000);
// }
// } else {
// mqtt.loop();
// }
// ButtonFunction();
// }
// /*----------------------Communication Function------------------------------------*/
// void callback(char* topic, byte* payload, unsigned int length) {
// payload[length] = '\0';
// String topic_str = topic, payload_str = (char*)payload;
// Serial.println("[" + topic_str + "]: " + payload_str);
// //digitalWrite(LED_PIN2, (payload_str == "ON") ? HIGH : LOW);
// if (payload_str == "ON") {
// digitalWrite(LED_PIN2,HIGH);
// pubMsg("I'm on");
// } else {
// digitalWrite(LED_PIN2,LOW);
// pubMsg("I'm off");
// }
// }
// void pubMsg(char* msg){
// char* topic = "v1/devices/me/attributes";
// mqtt.publish(topic,msg);
// //Serial.print("publish to topic : " + String(topic));
// //Serial.println(topic);
// }
// void pubTime(const char* time) {
// char* topic = "/ESP32_1/LED/FEEDBACK";
// mqtt.publish(topic,time);
// //Serial.print("publish to topic : " + String(topic));
// //Serial.println(topic);
// }
// void ButtonFunction() {
// char* Status_Online;
// char* Status_Offline;
// if(Status_value != 1){
// if (millis() - last_time > period) {
// last_time = millis();
// Status_Online = "Status: Online";
// Serial.println(Status_Online);
// }
// }else{
// if (millis() - last_time > period) {
// last_time = millis();
// Status_Offline = "Status: Washing";
// Serial.println(Status_Offline);
// }
// }
// ButtonState = digitalRead(BUTTON_PIN);
// if (ButtonState == LOW && !isButtonPressed) {
// isButtonPressed = true;
// buttonPressedStartTime = millis();
// digitalWrite(LED_PIN3, LOW);
// digitalWrite(LED_PIN2, HIGH);
// Serial.println("LED2 ON");
// Serial.println("Start...");
// }
// if (isButtonPressed) {
// unsigned long currentTime = millis();
// unsigned long elapsedTime = currentTime - buttonPressedStartTime;
// int count = map(elapsedTime, 0, buttonPressDuration, 10, 0);
// Status_value = 1;
// if (count <= 0) {
// digitalWrite(LED_PIN2, LOW);
// Serial.println("LED2 OFF");
// Serial.println("End...");
// isButtonPressed = false;
// Status_value =0;
// } else if (currentTime - lastCountTime >= CountInterval) {
// lastCountTime = currentTime;
// Serial.print("Time : ");
// Serial.print(count);
// Serial.print(" min ");
// Serial.println();
// jsonStatus(count, true, 100); // sent to NIPA
// pubMsg(jsonString);
// }
// }
// }
// void jsonStatus(int newTimeWashing, bool newledStatus, int newvalue) {
// // Create a JSON object
// StaticJsonDocument<200> jsonDocument;
// timeWashing = newTimeWashing;
// ledStatus = newledStatus;
// value = newvalue;
// jsonDocument["key1"] = timeWashing;
// jsonDocument["key2"] = ledStatus;
// jsonDocument["key3"] = value;
// serializeJson(jsonDocument, jsonString);
// Serial.println(jsonString);
// }
// void jsonDTH22(int newTemp, int newHumi) {
// // Create a JSON object
// StaticJsonDocument<200> jsonDocument;
// Temp = newTemp;
// Humi = newHumi;
// jsonDocument["key4"] = Temp;
// jsonDocument["key5"] = Humi;
// serializeJson(jsonDocument, jsonTemp);
// Serial.println(jsonTemp);
// }