ESP8266 + MQTT + 土壤湿度传感器

ESP8266 + MQTT + 土壤湿度传感器

连线

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

#define PIN_AO A0
#define PIN_DO 4

// 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-2saf-cacf5cb52af5";

int M0 = 1024; // 干燥
int M1 = 464; // 最小数 100% 湿度
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 update_data(); // 更新数据

void setup()
{
  Serial.begin(115200);
  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();
  update_data();
}

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();
}

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

void update_data(){
  if (client.connected())
  {
    Serial.print("AO => ");
    Serial.print(analogRead(PIN_AO));

    Serial.print(" DO =>");
    Serial.print(digitalRead(PIN_DO));

    float HUMI = (M0 - analogRead(PIN_AO)) / ((M0 - M1) / 100); //将湿度模拟量转换成数字量
    Serial.print("HUMI =>");
    Serial.print(HUMI);
    Serial.println("% ");

    if(HUMI <= 30){
      msg = "主人,我渴了, 我要喝水";
      Serial.println("主人,我渴了, 我要喝水");
    }else{
      msg = "主人,我不渴";
      Serial.println("主人,我不渴");
    }
    delay(1000);
    client.publish(MQTT_TOPIC_SUB_DATA, msg);
  }
}
posted @ 2022-05-31 22:48  半截肥皂  阅读(575)  评论(2编辑  收藏  举报