一、原件介绍

ESP-WROOM-32开发板一块、四个继电器、电源、杜邦线等。

二、开发环境

arduino编辑器

Arduino代码:


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

const char* ssid = "HUAWEI";
const char* password = "123456...";
const char* mqtt_server = "---.---.---.---";

int LED_BUILTIN1=13;
int LED_BUILTIN2=12;
int LED_BUILTIN3=14;
int LED_BUILTIN4=27;
int LED_BUILTIN5=26;

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

void setup_wifi() {

  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  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("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  if ((char)payload[0] == '0') {
    digitalWrite(LED_BUILTIN1, LOW);
  }
  if ((char)payload[0] == '1'){
    digitalWrite(LED_BUILTIN1, HIGH);  
  }
  if ((char)payload[0] == '2'){
    digitalWrite(LED_BUILTIN2, LOW);  
  }
  if ((char)payload[0] == '3'){
    digitalWrite(LED_BUILTIN2, HIGH);  
  }
  if ((char)payload[0] == '4'){
    digitalWrite(LED_BUILTIN3, LOW);  
  }
  if ((char)payload[0] == '5'){
    digitalWrite(LED_BUILTIN3, HIGH);  
  }
  if ((char)payload[0] == '6'){
    digitalWrite(LED_BUILTIN4, LOW);  
  }
  if ((char)payload[0] == '7'){
    digitalWrite(LED_BUILTIN4, HIGH);  
  }
  if ((char)payload[0] == '8'){
    digitalWrite(LED_BUILTIN5, LOW);  
  }
  if ((char)payload[0] == '9'){
    digitalWrite(LED_BUILTIN5, HIGH);  
  }

}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESP32Client-";
    clientId += String(random(0xffff), HEX);
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      client.publish("outTopic", "hello world");
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void setup() {  
  pinMode(LED_BUILTIN1, OUTPUT);
  pinMode(LED_BUILTIN2, OUTPUT);
  pinMode(LED_BUILTIN3, OUTPUT);
  pinMode(LED_BUILTIN4, OUTPUT);
  pinMode(LED_BUILTIN5, OUTPUT);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  reconnect();
  while(!client.subscribe("switch",0))
  {Serial.println("订阅失败;尝试重新订阅!");
   client.subscribe("switch",0);
   delay(300);
   }
   Serial.println("订阅成功~~~");
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
   
  client.loop();

  long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 50, "This is message :%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("text", msg);
  }
}

您已经阅读00:00:00欢迎留言评论,喜欢的话就为作者点个赞或者赏颗糖吧! 分享