其他分享
首页 > 其他分享> > esp8266+mqtt+继电器 (platformio)

esp8266+mqtt+继电器 (platformio)

作者:互联网

esp8266+mqtt+继电器

使用mqtt 控制led灯

项目地址

https://gitee.com/zhudachangs/esp8266-mqtt-relay

#include <Arduino.h>
#include <Ticker.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define pin_d4 2

// wifi info
const char *SSID = "优美屋205";   // 你家的wifi名
const char *PASSWORD = "18111549"; // wifi 密码

// MQTT info https://www.emqx.com/zh/mqtt/public-mqtt5-broker
const char *MQTT_SERVER = "broker-cn.emqx.io"; // MQTT 服务器地址
const int MQTT_PROT = 1883;                    // MQTT 端口

// mqtt 主题
const char *MQTT_TOPIC_PUB_ONLINE = "my/home/pub_online";
const char *MQTT_TOPIC_PUB_UPDATE = "my/home/pub_update"; // 订阅更新
const char *MQTT_TOPIC_SUB_DATA = "my/home/sub_data";     // 发布
const char *MQTT_TOPIC_HEARTBEAT = "my/home/heartbeat";   // 心跳
const char *CLIENT_ID = "esp8266-457065e5-f063-474e-12saf-cacf5cb52af5";

char *msg;

Ticker ticker; 
WiFiClient espClient;
PubSubClient client(espClient);

void init_wifi();                                                        // 初始化wifi
void mqtt_reconnect();                                                   // 重新连接wifi
void mqtt_msg_callback(char *topic, byte *payload, unsigned int length); // mqtt 消息回调
void mqtt_heartbeat();                                                   // 心跳

void setup()
{
  Serial.begin(115200);
  pinMode(pin_d4, OUTPUT);
  init_wifi();
  client.setServer(MQTT_SERVER, MQTT_PROT); // 设置mqtt 服务和端口
  client.setCallback(mqtt_msg_callback);    // 设置mqtt 回调函数

  // 心跳
  ticker.attach(1, mqtt_heartbeat);
}

void loop()
{
  if (!client.connected())
  {
    mqtt_reconnect();
  }
  client.loop();
}

void init_wifi()
{
  Serial.println("Connecting to");
  Serial.println(SSID);

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

void mqtt_reconnect()
{
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");

    // 第一步: 创建连接
    if (client.connect(CLIENT_ID))
    {
      Serial.println("connected");
      client.publish(MQTT_TOPIC_PUB_ONLINE, "online"); // 发布
      client.subscribe(MQTT_TOPIC_PUB_UPDATE);         // 监听
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void mqtt_msg_callback(char *topic, byte *payload, unsigned int length)
{
  Serial.print("Message arrived [");
  Serial.print(topic); // 打印主题信息
  Serial.print("] ");
  String data = "";
  for (unsigned int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]); // 打印主题内容
    data += (char)payload[i];
  }
  Serial.println();

  if(strcmp(MQTT_TOPIC_PUB_UPDATE, topic) == 0){
    if(data == "1"){
      digitalWrite(pin_d4, HIGH);
      Serial.println("ok");
    }else{
      digitalWrite(pin_d4, LOW);
      Serial.println("no");
    }
  }
}

void mqtt_heartbeat()
{
  if (client.connected())
  {
    client.publish(MQTT_TOPIC_HEARTBEAT, "1");
  }
}

标签:platformio,esp8266,void,mqtt,char,MQTT,client,Serial
来源: https://www.cnblogs.com/sjie/p/16345650.html