2024年7月7日 星期日

WOKWI ESP32 and MQTT REALY

 WOKWI ESP32 and MQTT   REALY






#include <WiFi.h>
#include <PubSubClient.h>

//Relays for switching appliances
#define Relay1            15     //D15 LED-Orangs = Water-1
#define Relay2            2     //D2   LED-Blue  = Water-2
#define Relay3            19    //D19  LED-Blue   = Motor
#define Relay4            23    //D23  LED-RED    = Fan

//Nenu For Control
int menu;

//Changs to:
#define sub1 "alex9ufo/SWcnt1"
#define sub2 "alex9ufo/SWcnt2"
#define sub3 "alex9ufo/SWcnt3"
#define sub4 "alex9ufo/SWcnt4"

// Update these with values suitable for your network.
const char* ssid =  "Wokwi-GUEST";   // your network SSID (name)
const char* password = "";   // your network password
//const char* mqtt_server = "broker.mqttdashboard.com";// choose your mqtt server
const char* mqtt_server = "broker.mqttgo.io";
const char* username = "";
const char* pass = "";

WiFiClient espClient;
PubSubClient client(espClient);

unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE  (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
//=====================================================================
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 callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  String messageTemp;
  for(int i=0; i<length; i+=1) {
    messageTemp += (char)payload[i];
  }
  messageTemp.trim();
  Serial.println(messageTemp);
 
  if (String(topic)=="alex9ufo/SWcnt1") {
      if(messageTemp=="ON"){
        //Serial.println("Led ON");
        digitalWrite(Relay1,HIGH);
      }  
      if(messageTemp=="OFF"){
        //Serial.println("Led OFF");
        digitalWrite(Relay1,LOW);
      }
  }
  if (String(topic)=="alex9ufo/SWcnt2") {
      if(messageTemp=="ON"){
        //Serial.println("Led ON");
        digitalWrite(Relay2,HIGH);
      }  
      if(messageTemp=="OFF"){
        //Serial.println("Led OFF");
        digitalWrite(Relay2,LOW);
      }
  }

  if (String(topic)=="alex9ufo/SWcnt3") {
      if(messageTemp=="ON"){
        //Serial.println("Led ON");
        digitalWrite(Relay3,HIGH);
      }  
      if(messageTemp=="OFF"){
        //Serial.println("Led OFF");
        digitalWrite(Relay3,LOW);
      }
  }

  if (String(topic)=="alex9ufo/SWcnt4") {
      if(messageTemp=="ON"){
        //Serial.println("Led ON");
        digitalWrite(Relay4,HIGH);
      }  
      if(messageTemp=="OFF"){
        //Serial.println("Led OFF");
        digitalWrite(Relay4,LOW);
      }
  }

}
//=====================================================================
void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESP32Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), username, pass) ) {
      Serial.println("connected");
      client.subscribe(sub1);
      client.subscribe(sub2);
      client.subscribe(sub3);
      client.subscribe(sub4);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
//=====================================================================
void setup() {
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}
//=====================================================================
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...