#include "Sensor.h"
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//***Set server***
const char* mqttServer = "broker.hivemq.com";
int port = 1883;
int pid = 91728327;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
uint8_t r = 5, g = 25, b = 26, gas = 14, dht = 2, buzzer = 16, btn = 23, photon = 34, lcdSDL = 22, lcdSDA = 21;
float startCycle = millis(),startBuzzer = -1;
boolean buzzerState = false;
float temperature_line = 30, humidity_line = 30, lux_line = 10000;
Sensor mySensors(dht,DHT22, photon, gas,btn,buzzer,lcdSDL,lcdSDA, r,g,b);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mySensors.setupSensors(); // Bat buoc phai chay
mySensors.setColor(255,0,0); // set do
mySensors.turnBuzzingOff();
Serial.print("Connecting to WiFi");
wifiConnect();
mqttClient.setServer(mqttServer, port);
mqttClient.setCallback(callback);
mqttClient.setKeepAlive(90);
mySensors.setColor(0,255,0); // set xanh
}
void wifiConnect() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected!");
}
void mqttConnect() {
while(!mqttClient.connected()) {
Serial.println("Attemping MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if(mqttClient.connect(clientId.c_str())) {
Serial.println("connected");
char topicCommand[80];
sprintf(topicCommand,"%d/command",pid);
Serial.println(topicCommand);
mqttClient.subscribe(topicCommand);
//***Subscribe all topic you need***
char topicLimits[100];
sprintf(topicLimits,"%d/limits",pid);
mqttClient.subscribe(topicLimits);
}
else {
Serial.println("try again in 5 seconds");
delay(5000);
}
}
}
//MQTT Receiver
void callback(char* topic, byte* message, unsigned int length) {
Serial.println(topic);
String strMsg;
for(int i=0; i<length; i++) {
strMsg += (char)message[i];
}
char topicLimit[100];
sprintf(topicLimit,"%d/limits",pid);
if(topic == topicLimit){
StaticJsonDocument<256> obj;
DeserializationError error = deserializeJson(obj, strMsg);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
temperature_line = obj["temperature_line"];
humidity_line = obj["humdity_line"];
lux_line = obj["lux_line"];
}
if(strMsg == "buzzer_off"){
mySensors.turnBuzzingOff();
}else if(strMsg == "buzzer_on"){
mySensors.turnBuzzingOn();
}
//***Code here to process the received package***
}
void loop() {
if(!mqttClient.connected()) {
mqttConnect();
}
mqttClient.loop();
if(digitalRead(btn) == HIGH && startBuzzer == -1){
startBuzzer = millis();
}else if(digitalRead(btn) == HIGH){
if(millis() - startBuzzer >= 2000){
if(buzzerState == false){
mySensors.turnBuzzingOn();
buzzerState = true;
}else{
mySensors.turnBuzzingOff();
buzzerState = false;
}
startBuzzer = -1;
delay(1000);
}
if (millis() - startBuzzer >= 5000) {
mySensors.printTextOnLCD("ESP Chip ID: ", String(pid), 10000);
startBuzzer = -1;
delay(1000);
}
}
if(digitalRead(btn) == LOW){
startBuzzer = -1;
}
//***Publish data to MQTT Server***
if(mySensors.isDay()){
mySensors.toggleBackLight(true); // true la bat false la tat
if(startCycle == -1){
startCycle = millis();
float temperature = mySensors.getTemperature();
float humidity = mySensors.getHumidity();
float lux = mySensors.getLux();
char publishData[200];
sprintf(publishData,"{\"series\" : [\"temperature\",\"humidity\",\"lux\"],\"data\":[%f,%f,%f]}",temperature,humidity,lux);
char topic[80];
sprintf(topic,"%d/datas",pid);
boolean state = mqttClient.publish(topic,publishData);
char NotifyData[200];
char notifyTopic[80];
state = false;
if (temperature > temperature_line){
Serial.println(1);
sprintf(NotifyData,"%d", 1);
state = true;
}
else if (humidity < humidity_line){
Serial.println(2);
sprintf(NotifyData,"%d", 2);
state = true;
}
else if (lux > lux_line){
Serial.println(3);
sprintf(NotifyData,"%d", 3);
state = true;
}
if(state == true){
mySensors.turnBuzzingOn();
buzzerState = true;
startBuzzer = -1;
sprintf(notifyTopic,"%d/notify",pid);
boolean Notifystate = mqttClient.publish(notifyTopic,NotifyData);
}
mySensors.printDataCycleOnLCD(1000); // moi thong tin cho 1
}else{
startCycle = millis() - startCycle > 20000 ? -1 : startCycle;
}
}else {
mySensors.toggleBackLight(false);
}
}