探索ESP8285(3)通過EMQX服務器點亮一個LED燈

CCC_122:博客只用於學習交流,不涉及任何商業用途,如果有錯誤之處,歡迎指正。

在上一個博客的基礎上 探索ESP8285(2)搭建Windows版MQTT服務器

我們來通過EMQX服務器點亮ESP8285模塊上的LED燈。

首先查得ESP8285模塊上的LED燈引腳是GPIO4,低電平亮起,所以我們先定義GPIO口

在setup裏設置引腳爲輸出模式

在連接裏修改訂閱的主題爲 /mqtt/test/

在callback函數裏修改LED引腳參數

編譯上傳到ESP8285模塊上,接下來測試效果:

打開MQTTBox,連接好服務器,在Payload上輸入1,然後按Publish發佈信息。

ESP8285模塊接收到信息後會自動打開LED。 如果是發送0或者其他的字符,LED燈關閉。

以下是整個測試代碼:

    #include <ESP8266WiFi.h>
    #include <PubSubClient.h>
    
    #define PIN_LED 4


    // Update these with values suitable for your network.

    const char* ssid = "....";
    const char* password = "....";
    //const char* mqtt_server = "iot.eclipse.org";
    const char* mqtt_server = "192.168.1.162";


    const String macAddress = WiFi.macAddress();
    //const char* clientID = macAddress.c_str();
    const char* clientID = "client001";
    const char* userName = "user001";
    const char* passWord = "psd001";

    WiFiClient espClient;
    PubSubClient client(espClient);
    long lastMsg = 0;
    char msg[50];
    int value = 0;

    void setup() {      
      pinMode(PIN_LED, OUTPUT);     // Initialize the LED pin as an output
      
      Serial.begin(115200);
      setup_wifi();
      
      client.setServer(mqtt_server, 1883);
      
      client.setCallback(callback);
    
    }

    void setup_wifi() {

      delay(10);
      // We start by connecting to a WiFi network
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);

      WiFi.begin(ssid, password);

      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }

      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("] ");
      for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
      }
      Serial.println();

      // Switch on the LED if an 1 was received as first character
      if ((char)payload[0] == '1') {
        digitalWrite(PIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
        // but actually the LED is on; this is because
        // it is acive low on the ESP-01)
      } else {
        digitalWrite(PIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
      }
    }

    void reconnect() {
      // Loop until we're reconnected
      while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        // Attempt to connect
        if (client.connect(clientID,userName,passWord)) {
          Serial.println("connected");
          // Once connected, publish an announcement...
          client.publish("/mqtt/test/", "hello world");
          // ... and resubscribe
          client.subscribe("/mqtt/test/#");
        } else {
          Serial.print("failed, rc=");
          Serial.print(client.state());
          Serial.println(" try again in 5 seconds");
          // Wait 5 seconds before retrying
          delay(5000);
        }
      }
    }
    void loop() {

      if (!client.connected()) {
        reconnect();
      }
      client.loop();
      
//      long now = millis();
//      if (now - lastMsg > 2000) {
//        lastMsg = now;
//        ++value;
//        snprintf (msg, 75, "hello world #%ld", value);
//        Serial.print("Publish message: ");
//        Serial.println(msg);
//        client.publish("outTopic", msg);
//      }
    }

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章