八戒有点帅 发表于 2024-2-14 21:17:08

新手入门sht30做温湿度计yaml不正确

新手入门想做个温湿度传感器,物件准备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(" initial start...");
        pinMode(LED, OUTPUT);
        digitalWrite(LED, LOW);
        Serial.println(" initial end...");
}

/*
* Method: 传感器初始化
*/
void SHT30Initial()
{
        Serial.println(" initial start...");
        Wire.begin(SDA, SCL);

        sht3xd.begin(0x44);
        Serial.printf(" Serial is");
        Serial.println(sht3xd.readSerialNumber());       

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

/*
* Method: wifi初始化,并连接
*/
void wifiInitial()
{
        Serial.println(" initial start...");
        int kRetries = 10;
        WiFi.begin(WIFI_SSID, WIFI_PASS);
        Serial.printf(" 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 Connect done");
            Serial.printf(" IP address is: "); Serial.println(WiFi.localIP());
            Serial.println(" initial end...");
        }
        else
        {
                Serial.println(" Connect falied");
        }
}

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

        Serial.print(" 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 Connect done");
              mqttClient.subscribe(MQTT_HUMIDITY_TOPIC);
              mqttClient.subscribe(MQTT_TEMPERATURE_TOPIC);
              delay(500);
              Serial.println(" initial end...");
    }
    else
    {
                Serial.println("\r\n Connect falied");
    }
}

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

/*
* Method:显示温湿度信息
*/
void printResult(SHT3XD result) {
        if (result.error == NO_ERROR) {
                Serial.printf(": resault is T=");
                Serial.printf("%.1f",result.t);
                char temp;
                sprintf(temp,"%.1f", result.t);
                mqttClient.publish(MQTT_TEMPERATURE_TOPIC, temp);
                Serial.printf("C, RH=");
                Serial.printf("%.1f",result.rh);
                char hu;
                sprintf(hu,"%.1f",result.rh);
                mqttClient.publish(MQTT_HUMIDITY_TOPIC, hu);
                Serial.println("%");
        }
        else {
                Serial.printf(" 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);
}



kjjuhfv 发表于 2024-2-14 21:34:08

哈哈 你下面那堆完全不是esphome的代码

八戒有点帅 发表于 2024-2-14 22:03:51

:funk:这看来是进错门了,这个是不是
/*
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 MQTTDISCONNECT14 << 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;
   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

kjjuhfv 发表于 2024-2-14 22:16:36

本帖最后由 kjjuhfv 于 2024-2-14 22:17 编辑

八戒有点帅 发表于 2024-2-14 22:03
这看来是进错门了,这个是不是
不是
下面这个地址是官网有关这个传感器的例子
https://esphome.io/components/sensor/sht3xd.html手机不好编辑
回去有空了帮你写

kjjuhfv 发表于 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

八戒有点帅 发表于 2024-2-15 06:37:55

kjjuhfv 发表于 2024-2-14 23:52


谢谢,:hug:好人一生平安!新年快乐

八戒有点帅 发表于 2024-2-15 06:44:04

kjjuhfv 发表于 2024-2-14 23:52


这是完整代码?

wfx 发表于 2024-2-15 11:04:14

八戒有点帅 发表于 2024-2-15 06:44
这是完整代码?

先自己新建一个工程,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

george65 发表于 2024-2-15 15:11:03

真心覺得esphome好難我只能繼續用tasmota的圖形介面

kjjuhfv 发表于 2024-2-15 16:27:14

george65 发表于 2024-2-15 15:11
真心覺得esphome好難我只能繼續用tasmota的圖形介面

这个也要看电波的
有时候对上了就很喜欢
像我 怎么都用不来tasmota
esphome就能接收
页: [1] 2
查看完整版本: 新手入门sht30做温湿度计yaml不正确