//https://www.hivemq.com/demos/websocket-client/
#include <WiFi.h>
#include <PubSubClient.h>
// Replace with your network credentials
const char* ssid = "WiFi_IoT";
const char* password = "00002222";
// Replace with your MQTT broker details
const char* mqttServer = "180.180.216.61";
const int mqttPort = 1883;
// Replace with your LED pin
const int ledPin = 23;
unsigned int Relay_Pin[6] = { 32, 33, 25, 26, 4, 5 };
unsigned long T_Time1;
bool State[10] = { 1, 0, 1, 0, 1 };
bool SW[10] = { 1, 0, 1, 0, 1 };
WiFiClient espClient;
PubSubClient client(espClient);
String Topic_SW = "KP/SW0";
String Topic_ST = "KP/STATE0";
void setup() {
// Start Serial Monitor
Serial.begin(115200);
delay(1000);
Serial.println("WiFi Begin");
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
//WiFi.begin(ssid, password);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Set the LED pin as an output
pinMode(ledPin, OUTPUT);
for (int i = 0; i < 6; i++) {
pinMode(Relay_Pin[i], OUTPUT);
}
// Set up MQTT client
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqttServer, "nt", "password")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("KP/SW10");
client.subscribe("KP/SW00");
client.subscribe("KP/SW01");
client.subscribe("KP/SW02");
client.subscribe("KP/SW03");
client.subscribe("KP/SW04");
client.subscribe("KP/SW05");
client.subscribe("KP/SW06");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
if (String(topic) == "KP/SW10") {
if (messageTemp == "LED ON") {
digitalWrite(ledPin, HIGH); // turn the LED on
Serial.println("ledPin, HIGH");
} else if (messageTemp == "LED OFF") {
digitalWrite(ledPin, LOW); // turn the LED off
Serial.println("ledPin, LOW");
}
Serial.println(messageTemp);
}
for (int i = 0; i < 6; i++) {
if (String(topic) == "KP/SW0" + String(i)) {
State[i] = messageTemp.toInt();
String str = String(messageTemp);
String topic_0 = Topic_ST + String(i);
client.publish(topic_0.c_str(), str.c_str());
Serial.println(" >>>>>>>>>>> " + topic_0 + " : " + str);
digitalWrite(Relay_Pin[i], State[i]);
}
}
}
void XX(String topic, float data) {
char tempString[8];
dtostrf(data, 1, 2, tempString);
String str = String(data);
client.publish(topic.c_str(), str.c_str());
Serial.println(topic + " : " + str);
}
void loop() {
if (millis() - T_Time1 > 1000) {
T_Time1 = millis();
digitalWrite(ledPin, !digitalRead(ledPin));
float TempF = random(25, 28);
float HumidF = random(65, 75);
float Lux = random(0, 30000);
float Do = random(0, 20);
float pH = random(0, 14);
float EC = random(0, 2000);
float W_Temp = random(22, 25);
// for (int i = 0; i < 5; i++) {
// State[i] = !State[i];
// }
XX("KP/AirTemp01", TempF);
XX("KP/AirHumid01", HumidF);
XX("KP/Lux01", Lux);
XX("KP/Do01", Do);
XX("KP/pH01", pH);
XX("KP/EC01", EC);
XX("KP/W_Temp01", W_Temp);
XX("KP/State00", State[0]);
XX("KP/State01", State[1]);
XX("KP/State02", State[2]);
XX("KP/State03", State[3]);
XX("KP/State04", State[4]);
String allStr = { String(TempF) + "," + String(HumidF) + "," + String(Lux) + "," + String(Do) + ","
+ String(pH) + "," + String(EC) + "," + String(W_Temp) + "," + String(State[0]) + "," + String(State[1])
+ "," + String(State[2]) + "," + String(State[3]) + "," + String(State[4]) };
// XX("KP/All_data", allStr);
client.publish("KP/All_data", allStr.c_str());
}
if (!client.connected()) {
reconnect();
}
client.loop();
}