『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1653|回复: 13

[求助] 新手入门sht30做温湿度计yaml不正确

[复制链接]

4

主题

27

帖子

131

积分

注册会员

Rank: 2

积分
131
金钱
104
HASS币
0
发表于 2024-2-14 21:17:08 | 显示全部楼层 |阅读模式
新手入门想做个温湿度传感器,物件准备sht30+Nodemcuv3+home assistant。网上抄作业弄得如下代码,可是在yaml验证时,错误不断。实在不知道怎么解决。请哪位高手指点下到底是哪些地方出了问题
esphome:
  name: a1        //定义一个设备名,局域网内唯一
  //platform: ESP8266 刷入设备芯片
  friendly_name: 温湿度计       //实体名称

esp8266:
  board: nodemcuv3       //芯片版本型号

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "KoIvs56DcYdX6BsqRCGJwqxA5tNs/07u3QWZ4fIOdKM="

ota:
  password: "93cfb6435a5f5798c4a348bc56f7c339"

wifi://要连接的WIFI
  ssid: "12"
  password: "12342***"

  # Enable fallback hotspot (captive portal) in case wifi connection fails     //当联网失败,生成本身热点
  ap:
    ssid: "A1 Fallback Hotspot"
    password: "bu6c68HwXvc0"

captive_portal:
    


#include <Wire.h>
#include <ClosedCube_SHT3XD.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Ticker.h>
#define SDA 5
#define SCL 4
#define LED 2

#define MQTT_CLIENT     "Temperature_Humidity"           // mqtt client_id (ClinetID,每个开关必需单独设置)
#define MQTT_SERVER     "192.168.100.2"                      // mqtt server(树莓派ip地址)
#define MQTT_PORT       1883                                 // mqtt port(MQTT服务器端口)
#define MQTT_SERVER     "192.168.100.2"            //"MQTT服务器IP,若部署在树莓派,则为树莓派IP"                      
#define MQTT_PORT       1883                              // MQTT服务器端口,默认为1883
#define MQTT_HUMIDITY_TOPIC      "simulation/sensor/humidity"    // mqtt 湿度topic
#define MQTT_TEMPERATURE_TOPIC "simulation/sensor/temperature"   // mqtt 温度topic
#define MQTT_USER       "fubu1"                
#define MQTT_PASS       "2*****"                 

#define WIFI_SSID       "12"                
#define WIFI_PASS       "1234****"

ClosedCube_SHT3XD sht3xd;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);

/*
 * Method: 初始化led,设置初始状态为开启
 */
void ledInitial()
{
        Serial.println("[led] initial start...");
        pinMode(LED, OUTPUT);
        digitalWrite(LED, LOW);
        Serial.println("[led] initial end...");
}

/*
 * Method: 传感器初始化
 */
void SHT30Initial()
{
        Serial.println("[SHT30] initial start...");
          Wire.begin(SDA, SCL);
  
        sht3xd.begin(0x44);
        Serial.printf("[SHT30] Serial is");
        Serial.println(sht3xd.readSerialNumber());        

        if (sht3xd.periodicStart(REPEATABILITY_HIGH, FREQUENCY_10HZ) != NO_ERROR)
                Serial.println("[SHT30] Cannot start periodic mode");
}

/*
 * Method: wifi初始化,并连接
 */
void wifiInitial()
{
        Serial.println("[wifi] initial start...");
        int kRetries = 10;
        WiFi.begin(WIFI_SSID, WIFI_PASS);
        Serial.printf("[wifi] Connecting to "); Serial.printf(WIFI_SSID); 
         while ((WiFi.status() != WL_CONNECTED) && kRetries --) 
         {
            delay(500);
            Serial.printf(" .");
          }
        if (WiFi.status() == WL_CONNECTED) 
        {  
                  Serial.println("\r\n[wifi] Connect done");
            Serial.printf("[wifi] IP address is: "); Serial.println(WiFi.localIP());
            Serial.println("[wifi] initial end...");
        }
        else
        {
                Serial.println("[wifi] Connect falied");
        }
}

/*
 * Method: MQTT服务器初始化,并连接
 */
void mqttInitial()
{
        Serial.println("[mqtt] initial start...");
        int kRetries = 10;
         mqttClient.setServer (MQTT_SERVER, MQTT_PORT);

         Serial.print("[mqtt] Connecting to ");Serial.print(MQTT_SERVER);
    delay(500);
         while (!mqttClient.connect (MQTT_CLIENT, MQTT_USER, MQTT_PASS) && kRetries --) 
         {
      Serial.print(" .");
      delay(1000);
    }
    if(mqttClient.connected())
    {
                  Serial.println("\r\n[mqtt] Connect done");
              mqttClient.subscribe(MQTT_HUMIDITY_TOPIC);
              mqttClient.subscribe(MQTT_TEMPERATURE_TOPIC);
              delay(500);
              Serial.println("[mqtt] initial end...");
    }
    else
    {
                  Serial.println("\r\n[mqtt] Connect falied");
    }
}

/*
 * Method: 检查Wifi连接状态
 */
void checkAndRetryWifiConnect()
{
        while (WiFi.status() != WL_CONNECTED)
        {
                Serial.println("[wifi] connected lost, wait 5s to retry...");
                delay(5000);
                wifiInitial();
        }
}
/*
 * Method:检查MQTT连接状态
 */
void checkAndRetryMQTTConnect()
{
        while (!mqttClient.connected())
        {
                Serial.println("[mqtt] connected lost, wait 5s to retry...");
                delay(5000);
                mqttInitial();
        }
}

/*
 * Method:显示温湿度信息
 */
void printResult(SHT3XD result) {
        if (result.error == NO_ERROR) {
                Serial.printf("[SHT30]: resault is T=");
                Serial.printf("%.1f",result.t);
                char temp[10];
                sprintf(temp,"%.1f", result.t);
                mqttClient.publish(MQTT_TEMPERATURE_TOPIC, temp);
                Serial.printf("C, RH=");
                Serial.printf("%.1f",result.rh);
                char hu[10];
                sprintf(hu,"%.1f",result.rh);
                mqttClient.publish(MQTT_HUMIDITY_TOPIC, hu);
                Serial.println("%");
        }
        else {
                Serial.printf("[SHT30] ERROR Code is:");
                Serial.println(result.error);
        }
}

void setup() {
        Serial.begin(115200);
        ledInitial();
        SHT30Initial();
        wifiInitial();
        checkAndRetryWifiConnect();
        mqttInitial();
        checkAndRetryMQTTConnect();

}

void loop() {
        printResult(sht3xd.periodicFetchData());
        digitalWrite(LED, HIGH);
        delay(1000);
        digitalWrite(LED, LOW);
}



回复

使用道具 举报

24

主题

582

帖子

2697

积分

金牌会员

Rank: 6Rank: 6

积分
2697
金钱
2115
HASS币
0
发表于 2024-2-14 21:34:08 | 显示全部楼层
哈哈 你下面那堆完全不是esphome的代码
回复

使用道具 举报

4

主题

27

帖子

131

积分

注册会员

Rank: 2

积分
131
金钱
104
HASS币
0
 楼主| 发表于 2024-2-14 22:03:51 | 显示全部楼层
这看来是进错门了,这个是不是
/*
 PubSubClient.h - A simple client for MQTT.
  Nick O'Leary
  http://knolleary.net
*/

#ifndef PubSubClient_h
#define PubSubClient_h

#include <Arduino.h>
#include "IPAddress.h"
#include "Client.h"
#include "Stream.h"

#define MQTT_VERSION_3_1      3
#define MQTT_VERSION_3_1_1    4

// MQTT_VERSION : Pick the version
//#define MQTT_VERSION MQTT_VERSION_3_1
#ifndef MQTT_VERSION
#define MQTT_VERSION MQTT_VERSION_3_1_1
#endif

// MQTT_MAX_PACKET_SIZE : Maximum packet size
#ifndef MQTT_MAX_PACKET_SIZE
#define MQTT_MAX_PACKET_SIZE 128
#endif

// MQTT_KEEPALIVE : keepAlive interval in Seconds
#ifndef MQTT_KEEPALIVE
#define MQTT_KEEPALIVE 15
#endif

// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds
#ifndef MQTT_SOCKET_TIMEOUT
#define MQTT_SOCKET_TIMEOUT 15
#endif

// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
//  in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
//  pass the entire MQTT packet in each write call.
//#define MQTT_MAX_TRANSFER_SIZE 80

// Possible values for client.state()
#define MQTT_CONNECTION_TIMEOUT     -4
#define MQTT_CONNECTION_LOST        -3
#define MQTT_CONNECT_FAILED         -2
#define MQTT_DISCONNECTED           -1
#define MQTT_CONNECTED               0
#define MQTT_CONNECT_BAD_PROTOCOL    1
#define MQTT_CONNECT_BAD_CLIENT_ID   2
#define MQTT_CONNECT_UNAVAILABLE     3
#define MQTT_CONNECT_BAD_CREDENTIALS 4
#define MQTT_CONNECT_UNAUTHORIZED    5

#define MQTTCONNECT     1 << 4  // Client request to connect to Server
#define MQTTCONNACK     2 << 4  // Connect Acknowledgment
#define MQTTPUBLISH     3 << 4  // Publish message
#define MQTTPUBACK      4 << 4  // Publish Acknowledgment
#define MQTTPUBREC      5 << 4  // Publish Received (assured delivery part 1)
#define MQTTPUBREL      6 << 4  // Publish Release (assured delivery part 2)
#define MQTTPUBCOMP     7 << 4  // Publish Complete (assured delivery part 3)
#define MQTTSUBSCRIBE   8 << 4  // Client Subscribe request
#define MQTTSUBACK      9 << 4  // Subscribe Acknowledgment
#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
#define MQTTUNSUBACK    11 << 4 // Unsubscribe Acknowledgment
#define MQTTPINGREQ     12 << 4 // PING Request
#define MQTTPINGRESP    13 << 4 // PING Response
#define MQTTDISCONNECT  14 << 4 // Client is Disconnecting
#define MQTTReserved    15 << 4 // Reserved

#define MQTTQOS0        (0 << 1)
#define MQTTQOS1        (1 << 1)
#define MQTTQOS2        (2 << 1)

#ifdef ESP8266
#include <functional>
#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
#else
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
#endif

class PubSubClient {
private:
   Client* _client;
   uint8_t buffer[MQTT_MAX_PACKET_SIZE];
   uint16_t nextMsgId;
   unsigned long lastOutActivity;
   unsigned long lastInActivity;
   bool pingOutstanding;
   MQTT_CALLBACK_SIGNATURE;
   uint16_t readPacket(uint8_t*);
   boolean readByte(uint8_t * result);
   boolean readByte(uint8_t * result, uint16_t * index);
   boolean write(uint8_t header, uint8_t* buf, uint16_t length);
   uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
   IPAddress ip;
   const char* domain;
   uint16_t port;
   Stream* stream;
   int _state;
public:
   PubSubClient();
   PubSubClient(Client& client);
   PubSubClient(IPAddress, uint16_t, Client& client);
   PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
   PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
   PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
   PubSubClient(uint8_t *, uint16_t, Client& client);
   PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
   PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
   PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
   PubSubClient(const char*, uint16_t, Client& client);
   PubSubClient(const char*, uint16_t, Client& client, Stream&);
   PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
   PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);

   PubSubClient& setServer(IPAddress ip, uint16_t port);
   PubSubClient& setServer(uint8_t * ip, uint16_t port);
   PubSubClient& setServer(const char * domain, uint16_t port);
   PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
   PubSubClient& setClient(Client& client);
   PubSubClient& setStream(Stream& stream);

   boolean connect(const char* id);
   boolean connect(const char* id, const char* user, const char* pass);
   boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
   boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
   void disconnect();
   boolean publish(const char* topic, const char* payload);
   boolean publish(const char* topic, const char* payload, boolean retained);
   boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
   boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
   boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
   boolean subscribe(const char* topic);
   boolean subscribe(const char* topic, uint8_t qos);
   boolean unsubscribe(const char* topic);
   boolean loop();
   boolean connected();
   int state();
};


#endif
回复

使用道具 举报

24

主题

582

帖子

2697

积分

金牌会员

Rank: 6Rank: 6

积分
2697
金钱
2115
HASS币
0
发表于 2024-2-14 22:16:36 | 显示全部楼层
本帖最后由 kjjuhfv 于 2024-2-14 22:17 编辑
八戒有点帅 发表于 2024-2-14 22:03
这看来是进错门了,这个是不是

不是
下面这个地址是官网有关这个传感器的例子
https://esphome.io/components/sensor/sht3xd.html手机不好编辑
回去有空了帮你写
回复

使用道具 举报

24

主题

582

帖子

2697

积分

金牌会员

Rank: 6Rank: 6

积分
2697
金钱
2115
HASS币
0
发表于 2024-2-14 23:52:32 | 显示全部楼层
esphome:
  name: a1
  friendly_name: a1

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "r6vynkpYz2ThfVN+f3ZCINt/C8ogw53Pd53AjjZtqF8="

ota:
  password: "6ad0bb8123c4becf39ba747d6683b3f8"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "A1 Fallback Hotspot"
    password: "F9zqoJiDcHgM"

captive_portal:
    
i2c:
  sda: 4
  scl: 5
  scan: true
  id: bus_a

sensor:
  - platform: sht3xd
    temperature:
      name: "Living Room Temperature"
    humidity:
      name: "Living Room Humidity"
    address: 0x44
    update_interval: 60s
回复

使用道具 举报

4

主题

27

帖子

131

积分

注册会员

Rank: 2

积分
131
金钱
104
HASS币
0
 楼主| 发表于 2024-2-15 06:37:55 | 显示全部楼层

谢谢,好人一生平安!新年快乐
回复

使用道具 举报

4

主题

27

帖子

131

积分

注册会员

Rank: 2

积分
131
金钱
104
HASS币
0
 楼主| 发表于 2024-2-15 06:44:04 | 显示全部楼层

这是完整代码?
回复

使用道具 举报

16

主题

239

帖子

2941

积分

金牌会员

Rank: 6Rank: 6

积分
2941
金钱
2702
HASS币
0
发表于 2024-2-15 11:04:14 | 显示全部楼层

先自己新建一个工程,WiFi密码改成自己的,在最后加上i2c:
  sda: 4
  scl: 5
  scan: true
  id: bus_a

sensor:
  - platform: sht3xd
    temperature:
      name: "Living Room Temperature"
    humidity:
      name: "Living Room Humidity"
    address: 0x44
    update_interval: 60s
回复

使用道具 举报

15

主题

406

帖子

2738

积分

金牌会员

Rank: 6Rank: 6

积分
2738
金钱
2332
HASS币
0
发表于 2024-2-15 15:11:03 | 显示全部楼层
真心覺得esphome好難  我只能繼續用tasmota的圖形介面
回复

使用道具 举报

24

主题

582

帖子

2697

积分

金牌会员

Rank: 6Rank: 6

积分
2697
金钱
2115
HASS币
0
发表于 2024-2-15 16:27:14 | 显示全部楼层
george65 发表于 2024-2-15 15:11
真心覺得esphome好難  我只能繼續用tasmota的圖形介面

这个也要看电波的
有时候对上了就很喜欢
像我 怎么都用不来tasmota
esphome就能接收
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-27 14:40 , Processed in 0.089591 second(s), 31 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表