#include <PubSubClient.h>
#include <WiFi.h>
#define ON 1
#define OFF 0
#define BUZZER_ON_STATUS "TRUE"
#define BUZZER_OFF_STATUS "FALSE"
// command for LED
#define RED_LED_ON "RED_ON"
#define RED_LED_OFF "RED_OFF"
#define GREEN_LED_ON "GREEN_ON"
#define GREEN_LED_OFF "GREEN_OFF"
#define BUZZER_ON "BUZZER_ON"
#define BUZZER_OFF "BUZZER_OFF"
// initial WIFI
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WiFiClient espClient;
// initial MQTT client
const char* mqttServer = "mqtt.netpie.io";
const int mqttPort = 1883;
const char* clientID = "f894f914-11d9-4252-a7e1-aea0c34ec9d5";
const char* mqttUser = "agJ7NVtzYftwyWLGn4WX4QzEkq5oUnmG";
const char* mqttPassword = "v1Aw3MkS3x17mYFKMhDeovufzxXdFFHj";
const char* data_pub = "@shadow/data/update";
const char* topic_pub = "@msg/lab_ict_kps/iot/status";
const char* topic_sub = "@msg/lab_ict_kps/iot/commands";
// send buffer
String publishMessage;
PubSubClient client(espClient);
// LED initial pin
const int RED_LED_PIN = 32; // change pin number here
const int GREEN_LED_PIN = 33; // change pin number here
const int BUZZER_PIN = 35;
int redLED_status = OFF, greenLED_status = OFF;
String buzzer_status = BUZZER_OFF_STATUS;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println(); // DEBUG:
Serial.print("Connecting to "); // DEBUG:
Serial.println(ssid); // DEBUG:
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("."); // DEBUG:
}
Serial.println(); // DEBUG:
Serial.println("WiFi connected"); // DEBUG:
Serial.println("IP address: "); // DEBUG:
Serial.println(WiFi.localIP()); // DEBUG:
}
void reconnectMQTT() {
// Loop until we're reconnected
char mqttinfo[80];
snprintf (mqttinfo, 75, "Attempting MQTT connection at %s:%d (%s/%s)...", mqttServer, mqttPort, mqttUser, mqttPassword);
while (!client.connected()) {
Serial.println(mqttinfo); // DEBUG:
String clientId = clientID;
if (client.connect(clientId.c_str(), mqttUser, mqttPassword)) {
Serial.println("...MQTT Broker Connected"); // DEBUG:
client.subscribe(topic_sub);
} else {
Serial.print("failed, rc="); // DEBUG:
Serial.print(client.state()); // DEBUG:
Serial.println(" try again in 5 seconds"); // DEBUG:
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void messageReceivedCallback(char* topic, byte* payload, unsigned int length) {
char payloadMsg[80];
Serial.print("Message arrived in topic: "); // DEBUG:
Serial.println(topic); // DEBUG:
Serial.print("Message:"); // DEBUG:
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
payloadMsg[i] = (char) payload[i];
}
payloadMsg[length] = '\0'; // put end of string to buffer
Serial.println(); // DEBUG:
Serial.println("-----------------------"); // DEBUG: show line
processMessage(payloadMsg);
}
void processMessage(String recvCommand) {
if (recvCommand == RED_LED_ON) {
digitalWrite(RED_LED_PIN, HIGH);
redLED_status = ON;
} else if (recvCommand == RED_LED_OFF) {
digitalWrite(RED_LED_PIN, LOW);
redLED_status = OFF;
} else if (recvCommand == GREEN_LED_ON) {
digitalWrite(GREEN_LED_PIN, HIGH);
greenLED_status = ON;
} else if (recvCommand == GREEN_LED_OFF) {
digitalWrite(GREEN_LED_PIN, LOW);
greenLED_status = OFF;
} else if (recvCommand == BUZZER_ON) {
digitalWrite(BUZZER_PIN, HIGH);
buzzer_status = BUZZER_ON_STATUS;
}else if (recvCommand == BUZZER_OFF) {
digitalWrite(BUZZER_PIN, LOW);
buzzer_status = BUZZER_OFF_STATUS;
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqttServer, mqttPort);
client.setCallback(messageReceivedCallback);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
redLED_status = OFF;
greenLED_status = OFF;
// turn off all LED at start
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
}
void loop() {
if(WiFi.status() != WL_CONNECTED){
setup_wifi();
}else{
reconnectMQTT();
}
client.loop();
// prepare publish message
publishMessage = "{\"data\": {\"red_led\": " + String(redLED_status) + ", \"green_led\": " + String(greenLED_status) + " , \"buzzer\": " +buzzer_status + " }}";
Serial.println(publishMessage); // DEBUG: show publish message
// publishing message
client.publish(topic_pub, publishMessage.c_str());
delay(1000);
client.publish(data_pub, publishMessage.c_str());
delay(1000);
}