2024年7月24日 星期三

MIT Inventor / WOKWI 4 LED Control / MQTT

MIT Inventor / WOKWI 4 LED Control / MQTT























#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"
const char pubTopic[] = "alex9ufo/lights";

// 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.hivemq.com";
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;
String onoffMsg="";
//=====================================================================
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("Led1 ON");
        digitalWrite(Relay1,HIGH);
        onoffMsg="11";
      }  
      if(messageTemp=="OFF"){
        Serial.println("Led1 OFF");
        digitalWrite(Relay1,LOW);
        onoffMsg="10";
      }
  }
  if (String(topic)=="alex9ufo/SWcnt2") {
      if(messageTemp=="ON"){
        Serial.println("Led2 ON");
        digitalWrite(Relay2,HIGH);
        onoffMsg="21";
      }  
      if(messageTemp=="OFF"){
        Serial.println("Led2 OFF");
        digitalWrite(Relay2,LOW);
        onoffMsg="20";
      }
  }

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

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

}
//=====================================================================
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);
  client.subscribe(sub1);
  client.subscribe(sub2);
  client.subscribe(sub3);
  client.subscribe(sub4);

}
//=====================================================================
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  //=========================================
  if (onoffMsg=="10") {
      String msgStr = "10";
      byte arrSize = msgStr.length() + 1;
      char msg[arrSize];

      Serial.print("PUBLISH DATA: LED");
      Serial.println(msgStr);
      msgStr.toCharArray(msg, arrSize);
      client.publish(pubTopic, msg);
      onoffMsg= "";
      delay(50);
  }
  if (onoffMsg=="11") {
      String msgStr = "11";
      byte arrSize = msgStr.length() + 1;
      char msg[arrSize];

      Serial.print("PUBLISH DATA: LED");
      Serial.println(msgStr);
      msgStr.toCharArray(msg, arrSize);
      client.publish(pubTopic, msg);
      onoffMsg= "";
      delay(50);
  }
  //=========================================
 if (onoffMsg=="20") {
      String msgStr = "20";
      byte arrSize = msgStr.length() + 1;
      char msg[arrSize];

      Serial.print("PUBLISH DATA: LED");
      Serial.println(msgStr);
      msgStr.toCharArray(msg, arrSize);
      client.publish(pubTopic, msg);
      onoffMsg= "";
      delay(50);
  }
  if (onoffMsg=="21") {
      String msgStr = "21";
      byte arrSize = msgStr.length() + 1;
      char msg[arrSize];

      Serial.print("PUBLISH DATA: LED");
      Serial.println(msgStr);
      msgStr.toCharArray(msg, arrSize);
      client.publish(pubTopic, msg);
      onoffMsg= "";
      delay(50);
  }
 //=========================================
 if (onoffMsg=="30") {
      String msgStr = "30";
      byte arrSize = msgStr.length() + 1;
      char msg[arrSize];

      Serial.print("PUBLISH DATA: LED");
      Serial.println(msgStr);
      msgStr.toCharArray(msg, arrSize);
      client.publish(pubTopic, msg);
      onoffMsg= "";
      delay(50);
  }
  if (onoffMsg=="31") {
      String msgStr = "31";
      byte arrSize = msgStr.length() + 1;
      char msg[arrSize];

      Serial.print("PUBLISH DATA: LED");
      Serial.println(msgStr);
      msgStr.toCharArray(msg, arrSize);
      client.publish(pubTopic, msg);
      onoffMsg= "";
      delay(50);
  }
  //=========================================
 if (onoffMsg=="40") {
      String msgStr = "40";
      byte arrSize = msgStr.length() + 1;
      char msg[arrSize];

      Serial.print("PUBLISH DATA: LED");
      Serial.println(msgStr);
      msgStr.toCharArray(msg, arrSize);
      client.publish(pubTopic, msg);
      onoffMsg= "";
      delay(50);
  }
  if (onoffMsg=="41") {
      String msgStr = "41";
      byte arrSize = msgStr.length() + 1;
      char msg[arrSize];

      Serial.print("PUBLISH DATA: LED");
      Serial.println(msgStr);
      msgStr.toCharArray(msg, arrSize);
      client.publish(pubTopic, msg);
      onoffMsg= "";
      delay(50);
  }

}
//=====================================================================

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

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