#include <WiFi.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>
#include <HTTPClient.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const char* token = "Pl83f8QIyHQxeOrSW3PlsYn1jBcCqNUoSNA8a1ndSmq"; // Token จาก https://notify-bot.line.me/th/
#include "DHT.h"
#define pump 12
#define frog 14
#define led3PIN 27
int DHTPIN = 15;
int adjusthumi = 80;
int adjusttemp = 55;
DHT dht(DHTPIN,DHT22);
String autofrog = "on";
String autopump = "on";
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "broker.netpie.io";
const int mqtt_port = 1883;
const char* mqtt_Client = "17b1dc36-ec59-4043-88fe-ebd32b579bce";
const char* mqtt_username = "64aZBr6JQdEKGKyK1WVV8rBds1CyZDBh"; //TOKEN
const char* mqtt_password = "zfooGGWBFaPPwJC1qxgLyAvoGL6CTQt3"; //Secret
WiFiClient espClient;
PubSubClient client(espClient);
char msg[100];
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection…");
if (client.connect(mqtt_Client, mqtt_username, mqtt_password)) {
Serial.println("connected");
client.subscribe("@msg/#"); //รับทุกค่าจาก mqtt
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println("try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String message;
String tpc;
for (int i = 0; i < length; i++) { //เรียง payload จากแนวดิ่ง เป็นบรรทัดเดียว
message = message + (char)payload[i];
}
Serial.println(message);
tpc = getMsg(topic, message); //เก็บ topic, message
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(frog, OUTPUT);
pinMode(pump, OUTPUT);
pinMode(led3PIN, OUTPUT);
digitalWrite(frog, LOW);
digitalWrite(pump, LOW);
dht.begin();
lcd.init(); // lcd.init();เริ่มต้น LCD
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Hello User");
lcd.setCursor(0,1);
lcd.print("LCD I2C TEST");
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA); //ให้ MCU เป็น Station Mode รอรับไวไฟ
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
client.setServer(mqtt_server, mqtt_port); //SetServer
client.setCallback(callback); // SetCallback
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
float Humi = dht.readHumidity();
float Temp = dht.readTemperature();
Serial.print("Humidity is :");
Serial.println(Humi);
Serial.print("Temperature is :");
Serial.println(Temp);
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Humi is :");
lcd.setCursor(9,0);
lcd.print(Humi);
lcd.setCursor(0,1);
lcd.print("Temp is :");
lcd.setCursor(9,1);
lcd.print(Temp);
//if(Temp > 35 ){
// digitalWrite(pump, HIGH);
// Serial.println("pump is ON");
// }
// else{
// digitalWrite(pump, LOW);
// Serial.println("pump is OFF");
// }
// if(Temp < 50 ){
// digitalWrite(frog, HIGH);
// Serial.println("frog is ON");
//}
// else{
// digitalWrite(frog, LOW);
// Serial.println("frog is OFF");
// }
if (!client.connected()) { //เช็คว่าเชื่อมต่อกับ mqtt ได้หรือยัง ถ้ายังให้ Reconnect
reconnect();
}
if (autopump == "on") {
if (Temp > adjusttemp) {
digitalWrite(pump, HIGH);
//เปิด
} else {
digitalWrite(pump, LOW); //ปิด
}
}
if (autofrog == "on") {
if (Humi < adjusthumi) {
digitalWrite(frog, HIGH);
//เปิด
} else {
digitalWrite(frog, LOW); //ปิด
}
}
int f = digitalRead(frog); //อ่านค่า Led เพื่อส่งค่าให้ Netpie
int p = digitalRead(pump); //อ่านค่า Led เพื่อส่งค่าให้ Netpie
//r = autoled;
String data = "{\"data\":{\"Humidity\": " + String(Humi) + ",\"Temperature\": " + String(Temp) + ",\"pump\": " + String(p) + ",\"frog\": " + String(f) + ",\"adjusttemp\": " + String(adjusttemp) + ",\"adjusthumi\": " + String(adjusthumi) + "}}";
//เก็บข้อมูลเพื่อส่งให้ Netpie ในรูปแบบ Json
Serial.println("data");
Serial.println(data);
data.toCharArray(msg , (data.length() + 1)); //แปลงเป็น CharArray เก็บใน msg
Serial.println("msg");
Serial.println(msg);
client.publish("@shadow/data/update", msg); //ส่งข้อมูล msg ไปยัง Netpie ผ่านShadow
client.loop(); //ให้ mqtt ทำงานเรื่อยๆ
delay(1000);
}
String getMsg(String topic_, String message_) { //รับค่า Topic และ msg จาก Callback
if (topic_ == "@msg/pump") { // เช็ค Topic จาก Netpie เรื่อง led
if (message_ == "on") { //netpie["???"].publish("@msg/led","on")
digitalWrite(pump, HIGH); //led เปิด
autopump = "off"; // เปลี่ยนค่าในตัวแปรใหม่
}
else if (message_ == "off") { //netpie["???"].publish("@msg/led","off")
digitalWrite(pump, LOW); //led ปิด
autopump = "on";
}
}
if (topic_ == "@msg/frog") { // เช็ค Topic จาก Netpie เรื่อง led
if (message_ == "on") { //netpie["???"].publish("@msg/led","on")
digitalWrite(frog, HIGH); //led เปิด
autofrog = "off"; // เปลี่ยนค่าในตัวแปรใหม่
}
else if (message_ == "off") { //netpie["???"].publish("@msg/led","off")
digitalWrite(frog, LOW); //led ปิด
autofrog = "on";
}
}
if (topic_ == "@msg/adjusthumi") {
if (message_ == "80") {
adjusthumi = 80;
}
else if (message_== "70"){
adjusthumi = 70;
}
else if (message_== "60"){
adjusthumi = 60;
}
else if (message_== "50"){
adjusthumi = 50;
}
}
if (topic_ == "@msg/adjusttemp") {
if (message_ == "55") {
adjusttemp = 55;
}
else if (message_== "50"){
adjusttemp = 50;
}
else if (message_== "45"){
adjusttemp = 45;
}
else if (message_== "40"){
adjusttemp = 40;
}
}
return autopump,autofrog;
}