2024年6月27日 星期四

WOKWI ESP32 + MQTT GO Broker

WOKWI ESP32 + MQTT GO Broker


// Create an MQTT Connection
// TCP Connection
// import the WiFi and PubSubClient libraries
//The WiFi library allows ESP32 to establish connections with Wi-Fi networks
//The PubSubClient library enables ESP32 to connect to an MQTT broker for publishing messages and subscribing to topics.
#include <WiFi.h>
#include "PubSubClient.h"
// WiFi
char ssid[]="Wokwi-GUEST";
char pass[]="";
// MQTT Broker
const char *mqtt_broker = "broker.MQTTGO.io";//"broker.emqx.io";//Public Broker
const char *mqtt_username = "mqttgo";
const char *mqtt_password = "public";
const int mqtt_port = 1883;


const char *topic_subsrcibe = "mqttgo/esp32/power";//Topic ESp32 Subs
const char *topic_publish="mqttgo/esp32/hi";

WiFiClient hamada;//WIFI Opject
PubSubClient client(hamada);//OOP
//int y ;
//void function(int x);
//call_function(y);
void callback(char *topic, byte *payload, unsigned int length);
#define LED 2
bool ledState=false;
void setup(){
   // Setting LED pin as output
    pinMode(LED, OUTPUT);
    digitalWrite(LED, LOW);  // Turn off the LED initially
    //Open a serial connection to display program results and establish a connection to the Wi-Fi network.
    // Set software serial baud to 115200;
    Serial.begin(115200);
    // Connecting to a Wi-Fi network
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Connecting to WiFi..");
    }
        Serial.println("Connected to the Wi-Fi network");
    // Utilize PubSubClient to establish a connection with the MQTT broker.
    //connecting to a mqtt broker
    Serial.println("Before Connect MQTT");
    //Broker Configuration
    client.setServer(mqtt_broker, mqtt_port);
   
   while (!client.connected()) {
        String client_id = "esp32-client-";
        client_id =client_id+ String(WiFi.macAddress());
        //consloe.log("x"+"y");//xy

        //ESP32-ID -----> esp32-client-macaddress
        //variable.c_str()--->char [] , String
        Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
        if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
            Serial.println("Public MQTT broker connected");
        } else {
            Serial.print("failed with state ");
            Serial.print(client.state());//1 , 2 , 5
            delay(2000);
        }
    }
    Serial.println("After Connect MQTT");
    client.publish(topic_publish, "Hi, I'm ESP32 ^^");
    client.subscribe(topic_subsrcibe);
    client.setCallback(callback);
}

void loop() {
    client.loop();
}



void callback(char* topic, byte* message, unsigned int length) {
  printPayload(topic, message, length);  
}

void printPayload(char* topic, byte* message, unsigned int length) {
  // Printing Received Message
  Serial.print("Message received on topic: ");
  Serial.println(topic);

  String messageTemp;
  for(int i=0; i<length; i+=1) {
    messageTemp += (char)message[i];
  }
  if(String(topic) == "mqttgo/esp32/power"){
      Serial.println("OK");
      if(messageTemp=="ON"){
        Serial.println("Led ON");
        digitalWrite(2,HIGH);
      }  
      if(messageTemp=="OFF"){
        Serial.println("Led OFF");
        digitalWrite(2,LOW);
      }
  }
  Serial.println(messageTemp);
}










沒有留言:

張貼留言

Arduino 物聯網應用 - 上課教材

Arduino 物聯網應用 - 上課教材 https://dic.vbird.tw/arduino/list.php Arduino 物聯網應用 - 課程列表 我們會從 Arduino 的認識、IDE 環境的熟悉、與操作電腦的序列埠連動的功能、Arduino 開發語言的熟悉、 簡...