#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoJson.h>
#include "DHTesp.h"
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "broker.hivemq.com";
const char* stopic = "led22";//รับข้อมูลเข้า
const char* ptopic = "temp666";//ส่งข้อมูลออก
const char* btopic = "btn22";//ส่งข้อมูลออก
const char* bbtopic = "btn33";//ส่งข้อมูลออก
//const int oneWireBus = 4; // dùng pin GPIO4
const int DHT_PIN = 15; // DHT22
char result_temp[10]; // Buffer big enough for 7-character float
char result_hum[10]; // Buffer big enough for 7-character float
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
DHTesp dhtSensor;
//btnlock
int btn1 ;
int btnlock1;
int state1;
int btn2 ;
int btnlock2;
int state2;
int btn1lock;
int btn2lock;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void ReceivedMessage(char* topic, byte* payload, unsigned int length) {
// Output the first character of the message to serial (debug)
StaticJsonDocument<200> doc;
deserializeJson(doc, payload, length);
// Extract values from the JSON data
String id = doc["id"];//ส่งข้อมูลจาก node red เป็น string
String data = doc["data"];//
Serial.print(id);
Serial.print(":");
Serial.println(data);
// เพิ่ม
if(id == "pin12" && data == "on" ){
digitalWrite(12, LOW);
delay(10);
}else if(id== "pin12" && data == "off" ){
digitalWrite(12, HIGH);
}delay(20);
if(id == "pin13" && data == "on" ){
digitalWrite(13, LOW);
delay(10);
}else if(id== "pin13" && data == "off" ){
digitalWrite(13, HIGH);
}delay(20);
if(id == "pin14" && data == "on" ){
digitalWrite(14, LOW);
delay(10);
}else if(id== "pin14" && data == "off" ){
digitalWrite(14, HIGH);
}delay(20);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(ptopic, "hello world");
// ... and resubscribe
client.subscribe(stopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(1000);
}
}
}
void publish_temp()
{
TempAndHumidity data_temp = dhtSensor.getTempAndHumidity();
dtostrf(data_temp.temperature, 6, 2, result_temp); // Leave room for too large numbers!
dtostrf(data_temp.humidity, 6, 2, result_hum); // Leave room for too large numbers!
DynamicJsonDocument json(100);
//ส่งข้อมูลออกไป
json["temp"] = result_temp;
json["humi"] = result_hum;
delay(20);
// json["counter"] = counter;
String jsonString;
serializeJson(json, jsonString);
const char* payload = jsonString.c_str();
// Serial.print("Publishing: ");
///Serial.println(payload);
client.publish(ptopic, payload);
//Serial.print("Done!");
}
void setup() {
//LED
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
btnlock1=1;
state1 =0;
btnlock2=1;
state2 =0;
btn1lock=1;
btn2lock=1;
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(ReceivedMessage);
}
void loop() {
if (!client.connected()) {
reconnect();
}
btn1 = digitalRead(2);
btn2 = digitalRead(4);
client.loop();
publish_temp();
// btn();
if(btn1 == 1 && btn1lock == 1 ){
client.publish(btopic, "1");
Serial.println(btn1);
btn1lock= 0;
}
if(btn1 == 0){
btn1lock = 1;
}
delay(10);
if(btn2 == 1 && btn2lock == 1 ){
client.publish(bbtopic, "-1");
Serial.println(btn2);
btn2lock= 0;
}
if(btn2 == 0){
btn2lock = 1;
}
delay(10);
}