#include <WiFi.h>
#include <ArduinoJson.h> //ArduinoJson by Benoit Blanchon Version 6.13.0
#include <MQTT.h> //MQTT by Joel Gaehwiler Version 2.4.7
// Replace the next variables with your SSID/Password combination
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WiFiClient client;
//MQTT setting
const char* broker ="broker.hivemq.com";
const char* unique_id = "341cddd6-8358-5e54-83e0-2f9132174c56";
String topic_subscribe = "ptss/ibc/leds";
MQTTClient mqtt(1024);
// LED Pin
const byte ledPin = 2;
const byte led1Pin22 = 22;
const byte led2Pin23 = 23;
const byte buzzerPin21 = 21;
//Global Variable
unsigned long ledTick=0;
//User Function - setup_wifi()
//------------------------------------------------
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//------------------------------------------------
//User Function - setup_mqtt()
//------------------------------------------------
void setup_mqtt(){
while(!mqtt.connect(unique_id)){
Serial.print(".");
delay(500);
}
Serial.println("MQTT connected.");
mqtt.subscribe(topic_subscribe);
}
//------------------------------------------------
//CALL THIS FUNCTION WHEN RECEIVED MESSAGE FROM MQTT
//-------------------------------------------------------------
void messageReveived(String &topic_subscribe, String &payload){
Serial.print("Incoming Topic:");
Serial.print(topic_subscribe);
Serial.print(", Payload");
Serial.println(payload);
//Decode DATA from JSON format
//e.g.: {"value":"on"}
DynamicJsonDocument doc(1024);
deserializeJson (doc,payload);
String value = doc["value"];
Serial.println(value);
//Application according to JSON Data
if(value == "on"){
digitalWrite(led1Pin22,HIGH);
Serial.println("LED on");
}
else if(value == "off"){
digitalWrite(led1Pin22,LOW);
Serial.println("LED off");
}
else if (value == "on2"){
digitalWrite(led2Pin23, HIGH);
Serial.println("LED2 on");
}
else if (value == "off2"){
digitalWrite(led2Pin23, LOW);
Serial.println("LED2 off");
}
else if (value == "buzOn"){
tone(buzzerPin21,262,250);
Serial.println("Buzzer Beeping");
}
}
//------------------------------------------------------------------
void setup() {
Serial.begin(115200);
//dht.setup(dhtPin,DHTesp::DHT11);
pinMode(ledPin, OUTPUT);
pinMode(led1Pin22,OUTPUT);
pinMode(led2Pin23, OUTPUT);
mqtt.begin(broker,1883,client);
mqtt.onMessage(messageReveived);
setup_wifi();
setup_mqtt();
}
void loop() {
mqtt.loop();
delay(10);
if(!mqtt.connected()){
setup_mqtt();
}
if(millis()>ledTick){
ledTick = millis()+200;
digitalWrite(ledPin,digitalRead(ledPin)^1);
}
}