#include "DHT.h"
#define LEDPIN1 12
#define LEDPIN2 13
int DHTPIN = 15;
DHT dht(DHTPIN, DHT22);
String status = "on";
int adjh = 80;
int adjt = 55;
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <PubSubClient.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
const char* mqtt_server = "broker.netpie.io";
const int mqtt_port = 1883;
const char* mqtt_Client = "08663441-c3d9-40a4-bf30-a1844c72a7e6";
const char* mqtt_username = "iX26mTXhUagSA9BLAVzd13jMKFWVxeRd";
const char* mqtt_password = "m5x4otFMrnaL1pmnEp55GmALbD8zbaWe";
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:
client.setServer(mqtt_server, mqtt_port); //SetServer
client.setCallback(callback); // SetCallback
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(LEDPIN1 , OUTPUT);
pinMode(LEDPIN2 , OUTPUT);
dht.begin();
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
delay(1000);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("welcome...");
delay(1000);
}
void read_ss_LCD(float h,float t){
//float h = dht.readHumidity();
//float t = dht.readTemperature();
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Humi : ");
LCD.print(h);
LCD.print(" %");
LCD.setCursor(0, 1);
LCD.print("Temp : ");
LCD.print(t);
LCD.print(" C");
}
void netpiecontrol(float h,float t)
{
if (status == "on") {
if (h < adjh) {
digitalWrite(LEDPIN1, HIGH);
} else {
digitalWrite(LEDPIN1, LOW);
}if(t>adjt){
digitalWrite(LEDPIN2, HIGH);
}else {
digitalWrite(LEDPIN2, LOW);
}
}
int p = digitalRead(LEDPIN1); //อ่านค่า Led เพื่อส่งค่าให้ Netpie
int a = digitalRead(LEDPIN2);
//r = autoled;
String data = "{\"data\":{\"Humidity\": " + String(h) + ",\"Temperature\": " + String(t) + ",\"pump\": " + String(p) + ",\"air\": " + String(a) + + ",\"adjh\": " + String(adjh) + + ",\"adjt\": " + String(adjt) + "}}";
//เก็บข้อมูลเพื่อส่งให้ 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(LEDPIN1, HIGH); //led เปิด
status = "off"; // เปลี่ยนค่าในตัวแปรใหม่
}
else if (message_ == "off") { //netpie["???"].publish("@msg/led","off")
digitalWrite(LEDPIN1, LOW); //led ปิด
status = "on";
}
}
if (topic_ == "@msg/air") { // เช็ค Topic จาก Netpie เรื่อง led
if (message_ == "on") { //netpie["???"].publish("@msg/led","on")
digitalWrite(LEDPIN2, HIGH); //led เปิด
status = "off"; // เปลี่ยนค่าในตัวแปรใหม่
}
else if (message_ == "off") { //netpie["???"].publish("@msg/led","off")
digitalWrite(LEDPIN2, LOW); //led ปิด
status = "on";
}
}
if(topic_ == "@msg/adjh")
{
if(message_ == "80"){
adjh = 80;
}else
if(message_ == "70"){
adjh = 70; }else
if(message_ == "60"){
adjh = 60; }else
if(message_ == "50"){
adjh = 50; }
}
if(topic_ == "@msg/adjt")
{
if(message_ == "20"){
adjt = 20;
}else
if(message_ == "25"){
adjt = 25; }else
if(message_ == "30"){
adjt = 30; }else
if(message_ == "35"){
adjt = 35; }
}
return status;
}
void loop() {
if (!client.connected()) { //เช็คว่าเชื่อมต่อกับ mqtt ได้หรือยัง ถ้ายังให้ Reconnect
reconnect();
}
float h = dht.readHumidity();
float t = dht.readTemperature();
read_ss_LCD(h,t);
netpiecontrol(h,t);
}