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

 找回密码
 立即注册
查看: 11012|回复: 10

ESPhome中的自定义驱动BMP180_IIC I2C驱动编写实例

[复制链接]

4

主题

57

帖子

277

积分

论坛分享达人

积分
277
金钱
220
HASS币
10
发表于 2020-5-19 18:20:36 | 显示全部楼层 |阅读模式
本帖最后由 lfp136 于 2020-5-21 10:00 编辑

小白分享,难者不会, 会者不难。在平常应用中,总会遇到ESPhome库中没有现成的设备驱动,导致项目停滞。看官网得知:ESPhome和Arduino关系紧密,ESPhome调用传感器的arduino库,实现传感器的驱动开发。正好手上有BMP180 气压和温度测量模块空闲。
1.先用官方库验证接线的传感器,均正常,可以在HA中接收到数据。具体不详说了,BMP180也是就085的库https://esphome.io/components/sensor/bmp085.html
2.自定义设备,按官网文档:https://esphome.io/components/sensor/custom.html
    A. 在ESP主目录中(我的是:C:\Users\Administrator\esphome_config),创建h文件(my_custom_sensor.h):
    B. h文件内容:
#include "esphome.h"
#include "Adafruit_BMP085.h"

class MyCustomSensor : public PollingComponent {
  public:
    Adafruit_BMP085 bmp;
    Sensor *temperature_sensor = new Sensor();
    Sensor *pressure_sensor = new Sensor();
   MyCustomSensor() : PollingComponent(1000) { }

  void setup() override {
    bmp.begin();
  }

  void update() override {
    // This is the actual sensor reading logic.
    float temperature = bmp.readTemperature();
    temperature_sensor->publish_state(temperature);

    int pressure = bmp.readPressure();
    pressure_sensor->publish_state(pressure / 100.0);
  }
};
   C. YAML文件内容:

esphome:
  name: dimmer005
  platform: ESP8266
  board: nodemcuv2
  #加入自定义库
  includes:
    - dimmer005_sensor.h
  libraries:
    - "Adafruit BMP085 Library"

wifi:
  ssid: "你的路由名称"
  password: "你的路由密码"
  ap:
    ssid: "ap_001"
    password: "12345678"

logger:
  # https://esphome.io/components/logger

api:
  password: "12345678"
  # https://esphome.io/components/api
ota:

  # https://esphome.io/components/ota
i2c:
  sda: D4
  scl: D5
sensor:
  - platform: custom
    lambda: |-
      auto my_sensor = new MyCustomSensor();
      App.register_component(my_sensor);
      return {my_sensor->temperature_sensor, my_sensor->pressure_sensor};

  sensors:
  - name: "Temperature1"
    unit_of_measurement: °C
    accuracy_decimals: 1
  - name: "Pressure Sensor1"
    unit_of_measurement: hPa
    accuracy_decimals: 2
    d. 命令行,执行编译:cmd>  Esphome esphome_config\dimmer005.yaml run           正常编译,下载到nodeMCU中,就可以在HA中搜到此设备了。
      总结,本方法是调用PIO库中的“Adafruit BMP085 Library”驱动,实现BMP180驱动的。
      此库在安装VSS后,在PIO界面中可以搜索到。
     可是现实中若在PIO库中也搜不到驱动,那怎么办。
      
3.方法3,调用本地库来驱动BMP180
    加载本机的驱动文件,实现设备的驱动。即可解决所有驱动的问题。
…………还在试验中,待试验完成,就更新出来。

回复

使用道具 举报

4

主题

57

帖子

277

积分

论坛分享达人

积分
277
金钱
220
HASS币
10
 楼主| 发表于 2020-5-19 18:27:24 | 显示全部楼层
.h文件内容和 YAML文件内容的解说,在官网文档:https://esphome.io/components/sensor/custom.html 写的很清楚,我就不写了,免得误导了。这里对小白来讲,就是.h文件的存放位置和内容。其它与正常应用ESP库一样,都是自动完成的。
回复

使用道具 举报

4

主题

57

帖子

277

积分

论坛分享达人

积分
277
金钱
220
HASS币
10
 楼主| 发表于 2020-5-21 09:43:26 | 显示全部楼层
调用本地库,试验结果OK,步骤如下:
一、下载其它BMP180库,存在本地
     1. 我选择了:https://github.com/sparkfun/BMP180_Breakout_Arduino_Library
                与前面的PIO的BMP180库不一样
     2.先用arduino驱动BMP108,验证程序是可以用
     3. 提取驱动文件在ESP主目录中(我的是:C:\Users\Administrator\esphome_config)
               SFE_BMP180.h
               SFE_BMP180.cpp
二、创建自定义设备头文件.h
     1.参考以上的(my_custom_sensor.h),修改为dimmer006_bmp180_b.h(文件名称随你,名称不能用中文)
      2.内容:
        A.        头文件引用为SFE_BMP180.h库
        B.        Adafruit_BMP085 改为“SFE_BMP180 bmp;”
        C.        读值函数不变
        D.        气压值在Adafruit_BMP085库中需要/100;本库直接获得即可(publish_state(pressure);)
#include "esphome.h"
#include "SFE_BMP180.h"

class MyCustomSensor : public PollingComponent {
 public:
  SFE_BMP180 bmp;
  Sensor *temperature_sensor = new Sensor();
  Sensor *pressure_sensor = new Sensor();

  MyCustomSensor() : PollingComponent(1000) { }

  void setup() override {
    bmp.begin();
  }

  void update() override {
    // This is the actual sensor reading logic.
    float temperature = bmp.readTemperature();
    temperature_sensor->publish_state(temperature);

    int pressure = bmp.readPressure();
    pressure_sensor->publish_state(pressure);
  }
};

回复

使用道具 举报

4

主题

57

帖子

277

积分

论坛分享达人

积分
277
金钱
220
HASS币
10
 楼主| 发表于 2020-5-21 09:47:29 | 显示全部楼层
三、    修改驱动库文件
       因为SFE_BMP108库的读值方法与前面Adafruit_BMP085库的读值方法不一样,而自定义设备头文件中读值是按Adafruit_BMP085库的读值方法编写的,所以要在SFE_BMP108库中增加像Adafruit_BMP085库的读值方法,才能正常使用。说的有点绕,简单点就是在SFE_BMP108库中加入新的读值函数:
                      float readTemperature(void);
                int32_t readPressure(void);
1.在SFE_BMP180.H文件中:
         在公有类中,增加:
                //增加,用于外部调用
                float readTemperature(void);
                int32_t readPressure(void);
        //end
全文如下:
/*
        SFE_BMP180.h
        Bosch BMP180 pressure sensor library for the Arduino microcontroller
        Mike Grusin, SparkFun Electronics

        Uses floating-point equations from the Weather Station Data Logger project
        http://wmrx00.sourceforge.net/
        http://wmrx00.sourceforge.net/Arduino/BMP085-Calcs.pdf

        Forked from BMP085 library by M.Grusin

        version 1.0 2013/09/20 initial version
        Verison 1.1.2 - Updated for Arduino 1.6.4 5/2015
        
        Our example code uses the "beerware" license. You can do anything
        you like with this code. No really, anything. If you find it useful,
        buy me a (root) beer someday.
*/

#ifndef SFE_BMP180_h
#define SFE_BMP180_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class SFE_BMP180
{
        public:
                SFE_BMP180(); // base type

                //增加,用于外部调用
                float readTemperature(void);
                int32_t readPressure(void);
        //end

                char begin();
                        // call pressure.begin() to initialize BMP180 before use
                        // returns 1 if success, 0 if failure (bad component or I2C bus shorted?)
                
                char startTemperature(void);
                        // command BMP180 to start a temperature measurement
                        // returns (number of ms to wait) for success, 0 for fail

                char getTemperature(double &T);
                        // return temperature measurement from previous startTemperature command
                        // places returned value in T variable (deg C)
                        // returns 1 for success, 0 for fail

                char startPressure(char oversampling);
                        // command BMP180 to start a pressure measurement
                        // oversampling: 0 - 3 for oversampling value
                        // returns (number of ms to wait) for success, 0 for fail

                char getPressure(double &P, double &T);
                        // return absolute pressure measurement from previous startPressure command
                        // note: requires previous temperature measurement in variable T
                        // places returned value in P variable (mbar)
                        // returns 1 for success, 0 for fail

                double sealevel(double P, double A);
                        // convert absolute pressure to sea-level pressure (as used in weather data)
                        // P: absolute pressure (mbar)
                        // A: current altitude (meters)
                        // returns sealevel pressure in mbar

                double altitude(double P, double P0);
                        // convert absolute pressure to altitude (given baseline pressure; sea-level, runway, etc.)
                        // P: absolute pressure (mbar)
                        // P0: fixed baseline pressure (mbar)
                        // returns signed altitude in meters

                char getError(void);
                        // If any library command fails, you can retrieve an extended
                        // error code using this command. Errors are from the wire library: 
                        // 0 = Success
                        // 1 = Data too long to fit in transmit buffer
                        // 2 = Received NACK on transmit of address
                        // 3 = Received NACK on transmit of data
                        // 4 = Other error

        private:
        
                char readInt(char address, int16_t &value);
                        // read an signed int (16 bits) from a BMP180 register
                        // address: BMP180 register address
                        // value: external signed int for returned value (16 bits)
                        // returns 1 for success, 0 for fail, with result in value

                char readUInt(char address, uint16_t &value);
                        // read an unsigned int (16 bits) from a BMP180 register
                        // address: BMP180 register address
                        // value: external unsigned int for returned value (16 bits)
                        // returns 1 for success, 0 for fail, with result in value

                char readBytes(unsigned char *values, char length);
                        // read a number of bytes from a BMP180 register
                        // values: array of char with register address in first location [0]
                        // length: number of bytes to read back
                        // returns 1 for success, 0 for fail, with read bytes in values[] array
                        
                char writeBytes(unsigned char *values, char length);
                        // write a number of bytes to a BMP180 register (and consecutive subsequent registers)
                        // values: array of char with register address in first location [0]
                        // length: number of bytes to write
                        // returns 1 for success, 0 for fail
                        
                int16_t AC1,AC2,AC3,VB1,VB2,MB,MC,MD;
                uint16_t AC4,AC5,AC6; 
                double c5,c6,mc,md,x0,x1,x2,y0,y1,y2,p0,p1,p2;
                char _error;
};

#define BMP180_ADDR 0x77 // 7-bit address

#define        BMP180_REG_CONTROL 0xF4
#define        BMP180_REG_RESULT 0xF6

#define        BMP180_COMMAND_TEMPERATURE 0x2E
#define        BMP180_COMMAND_PRESSURE0 0x34
#define        BMP180_COMMAND_PRESSURE1 0x74
#define        BMP180_COMMAND_PRESSURE2 0xB4
#define        BMP180_COMMAND_PRESSURE3 0xF4

#endif
回复

使用道具 举报

4

主题

57

帖子

277

积分

论坛分享达人

积分
277
金钱
220
HASS币
10
 楼主| 发表于 2020-5-21 09:49:52 | 显示全部楼层
三、    修改驱动库文件
     2. 修改CPP文件:SFE_BMP180.CPP
增加两个读值函数。仿照读值文件SFE_BMP180_example.ino中的读值逻辑改写的,注意将打印换成返回值即可。具体内容如下,增加项为最后两个:
/*
        SFE_BMP180.cpp
        Bosch BMP180 pressure sensor library for the Arduino microcontroller
        Mike Grusin, SparkFun Electronics

        Uses floating-point equations from the Weather Station Data Logger project
        http://wmrx00.sourceforge.net/
        http://wmrx00.sourceforge.net/Arduino/BMP085-Calcs.pdf

        Forked from BMP085 library by M.Grusin

        version 1.0 2013/09/20 initial version
        Verison 1.1.2 - Updated for Arduino 1.6.4 5/2015

        Our example code uses the "beerware" license. You can do anything
        you like with this code. No really, anything. If you find it useful,
        buy me a (root) beer someday.
*/

#include <SFE_BMP180.h>
#include <Wire.h>
#include <stdio.h>
#include <math.h>


SFE_BMP180::SFE_BMP180()
// Base library type
{
}


char SFE_BMP180::begin()
// Initialize library for subsequent pressure measurements
{
        double c3,c4,b1;
        
        // Start up the Arduino's "wire" (I2C) library:
        
        Wire.begin();

        // The BMP180 includes factory calibration data stored on the device.
        // Each device has different numbers, these must be retrieved and
        // used in the calculations when taking pressure measurements.

        // Retrieve calibration data from device:
        
        if (readInt(0xAA,AC1) &&
                readInt(0xAC,AC2) &&
                readInt(0xAE,AC3) &&
                readUInt(0xB0,AC4) &&
                readUInt(0xB2,AC5) &&
                readUInt(0xB4,AC6) &&
                readInt(0xB6,VB1) &&
                readInt(0xB8,VB2) &&
                readInt(0xBA,MB) &&
                readInt(0xBC,MC) &&
                readInt(0xBE,MD))
        {

                // All reads completed successfully!

                // If you need to check your math using known numbers,
                // you can uncomment one of these examples.
                // (The correct results are commented in the below functions.)

                // Example from Bosch datasheet
                // AC1 = 408; AC2 = -72; AC3 = -14383; AC4 = 32741; AC5 = 32757; AC6 = 23153;
                // B1 = 6190; B2 = 4; MB = -32768; MC = -8711; MD = 2868;

                // Example from http://wmrx00.sourceforge.net/Arduino/BMP180-Calcs.pdf
                // AC1 = 7911; AC2 = -934; AC3 = -14306; AC4 = 31567; AC5 = 25671; AC6 = 18974;
                // VB1 = 5498; VB2 = 46; MB = -32768; MC = -11075; MD = 2432;

                /*
                Serial.print("AC1: "); Serial.println(AC1);
                Serial.print("AC2: "); Serial.println(AC2);
                Serial.print("AC3: "); Serial.println(AC3);
                Serial.print("AC4: "); Serial.println(AC4);
                Serial.print("AC5: "); Serial.println(AC5);
                Serial.print("AC6: "); Serial.println(AC6);
                Serial.print("VB1: "); Serial.println(VB1);
                Serial.print("VB2: "); Serial.println(VB2);
                Serial.print("MB: "); Serial.println(MB);
                Serial.print("MC: "); Serial.println(MC);
                Serial.print("MD: "); Serial.println(MD);
                */
                
                // Compute floating-point polynominals:

                c3 = 160.0 * pow(2,-15) * AC3;
                c4 = pow(10,-3) * pow(2,-15) * AC4;
                b1 = pow(160,2) * pow(2,-30) * VB1;
                c5 = (pow(2,-15) / 160) * AC5;
                c6 = AC6;
                mc = (pow(2,11) / pow(160,2)) * MC;
                md = MD / 160.0;
                x0 = AC1;
                x1 = 160.0 * pow(2,-13) * AC2;
                x2 = pow(160,2) * pow(2,-25) * VB2;
                y0 = c4 * pow(2,15);
                y1 = c4 * c3;
                y2 = c4 * b1;
                p0 = (3791.0 - 8.0) / 1600.0;
                p1 = 1.0 - 7357.0 * pow(2,-20);
                p2 = 3038.0 * 100.0 * pow(2,-36);

                /*
                Serial.println();
                Serial.print("c3: "); Serial.println(c3);
                Serial.print("c4: "); Serial.println(c4);
                Serial.print("c5: "); Serial.println(c5);
                Serial.print("c6: "); Serial.println(c6);
                Serial.print("b1: "); Serial.println(b1);
                Serial.print("mc: "); Serial.println(mc);
                Serial.print("md: "); Serial.println(md);
                Serial.print("x0: "); Serial.println(x0);
                Serial.print("x1: "); Serial.println(x1);
                Serial.print("x2: "); Serial.println(x2);
                Serial.print("y0: "); Serial.println(y0);
                Serial.print("y1: "); Serial.println(y1);
                Serial.print("y2: "); Serial.println(y2);
                Serial.print("p0: "); Serial.println(p0);
                Serial.print("p1: "); Serial.println(p1);
                Serial.print("p2: "); Serial.println(p2);
                */
                
                // Success!
                return(1);
        }
        else
        {
                // Error reading calibration data; bad component or connection?
                return(0);
        }
}


char SFE_BMP180::readInt(char address, int16_t &value)
// Read a signed integer (two bytes) from device
// address: register to start reading (plus subsequent register)
// value: external variable to store data (function modifies value)
{
        unsigned char data[2];

        data[0] = address;
        if (readBytes(data,2))
        {
                value = (int16_t)((data[0]<<8)|data[1]);
                //if (*value & 0x8000) *value |= 0xFFFF0000; // sign extend if negative
                return(1);
        }
        value = 0;
        return(0);
}


char SFE_BMP180::readUInt(char address, uint16_t &value)
// Read an unsigned integer (two bytes) from device
// address: register to start reading (plus subsequent register)
// value: external variable to store data (function modifies value)
{
        unsigned char data[2];

        data[0] = address;
        if (readBytes(data,2))
        {
                value = (((uint16_t)data[0]<<8)|(uint16_t)data[1]);
                return(1);
        }
        value = 0;
        return(0);
}


char SFE_BMP180::readBytes(unsigned char *values, char length)
// Read an array of bytes from device
// values: external array to hold data. Put starting register in values[0].
// length: number of bytes to read
{
        char x;

        Wire.beginTransmission(BMP180_ADDR);
        Wire.write(values[0]);
        _error = Wire.endTransmission();
        if (_error == 0)
        {
                Wire.requestFrom(BMP180_ADDR,length);
                while(Wire.available() != length) ; // wait until bytes are ready
                for(x=0;x<length;x++)
                {
                        values[x] = Wire.read();
                }
                return(1);
        }
        return(0);
}


char SFE_BMP180::writeBytes(unsigned char *values, char length)
// Write an array of bytes to device
// values: external array of data to write. Put starting register in values[0].
// length: number of bytes to write
{
        char x;
        
        Wire.beginTransmission(BMP180_ADDR);
        Wire.write(values,length);
        _error = Wire.endTransmission();
        if (_error == 0)
                return(1);
        else
                return(0);
}


char SFE_BMP180::startTemperature(void)
// Begin a temperature reading.
// Will return delay in ms to wait, or 0 if I2C error
{
        unsigned char data[2], result;
        
        data[0] = BMP180_REG_CONTROL;
        data[1] = BMP180_COMMAND_TEMPERATURE;
        result = writeBytes(data, 2);
        if (result) // good write?
                return(5); // return the delay in ms (rounded up) to wait before retrieving data
        else
                return(0); // or return 0 if there was a problem communicating with the BMP
}


char SFE_BMP180::getTemperature(double &T)
// Retrieve a previously-started temperature reading.
// Requires begin() to be called once prior to retrieve calibration parameters.
// Requires startTemperature() to have been called prior and sufficient time elapsed.
// T: external variable to hold result.
// Returns 1 if successful, 0 if I2C error.
{
        unsigned char data[2];
        char result;
        double tu, a;
        
        data[0] = BMP180_REG_RESULT;

        result = readBytes(data, 2);
        if (result) // good read, calculate temperature
        {
                tu = (data[0] * 256.0) + data[1];

                //example from Bosch datasheet
                //tu = 27898;

                //example from http://wmrx00.sourceforge.net/Arduino/BMP085-Calcs.pdf
                //tu = 0x69EC;
                
                a = c5 * (tu - c6);
                T = a + (mc / (a + md));

                /*                
                Serial.println();
                Serial.print("tu: "); Serial.println(tu);
                Serial.print("a: "); Serial.println(a);
                Serial.print("T: "); Serial.println(*T);
                */
        }
        return(result);
}


char SFE_BMP180::startPressure(char oversampling)
// Begin a pressure reading.
// Oversampling: 0 to 3, higher numbers are slower, higher-res outputs.
// Will return delay in ms to wait, or 0 if I2C error.
{
        unsigned char data[2], result, delay;
        
        data[0] = BMP180_REG_CONTROL;

        switch (oversampling)
        {
                case 0:
                        data[1] = BMP180_COMMAND_PRESSURE0;
                        delay = 5;
                break;
                case 1:
                        data[1] = BMP180_COMMAND_PRESSURE1;
                        delay = 8;
                break;
                case 2:
                        data[1] = BMP180_COMMAND_PRESSURE2;
                        delay = 14;
                break;
                case 3:
                        data[1] = BMP180_COMMAND_PRESSURE3;
                        delay = 26;
                break;
                default:
                        data[1] = BMP180_COMMAND_PRESSURE0;
                        delay = 5;
                break;
        }
        result = writeBytes(data, 2);
        if (result) // good write?
                return(delay); // return the delay in ms (rounded up) to wait before retrieving data
        else
                return(0); // or return 0 if there was a problem communicating with the BMP
}


char SFE_BMP180::getPressure(double &P, double &T)
// Retrieve a previously started pressure reading, calculate abolute pressure in mbars.
// Requires begin() to be called once prior to retrieve calibration parameters.
// Requires startPressure() to have been called prior and sufficient time elapsed.
// Requires recent temperature reading to accurately calculate pressure.

// P: external variable to hold pressure.
// T: previously-calculated temperature.
// Returns 1 for success, 0 for I2C error.

// Note that calculated pressure value is absolute mbars, to compensate for altitude call sealevel().
{
        unsigned char data[3];
        char result;
        double pu,s,x,y,z;
        
        data[0] = BMP180_REG_RESULT;

        result = readBytes(data, 3);
        if (result) // good read, calculate pressure
        {
                pu = (data[0] * 256.0) + data[1] + (data[2]/256.0);

                //example from Bosch datasheet
                //pu = 23843;

                //example from http://wmrx00.sourceforge.net/Arduino/BMP085-Calcs.pdf, pu = 0x982FC0;        
                //pu = (0x98 * 256.0) + 0x2F + (0xC0/256.0);
                
                s = T - 25.0;
                x = (x2 * pow(s,2)) + (x1 * s) + x0;
                y = (y2 * pow(s,2)) + (y1 * s) + y0;
                z = (pu - x) / y;
                P = (p2 * pow(z,2)) + (p1 * z) + p0;

                /*
                Serial.println();
                Serial.print("pu: "); Serial.println(pu);
                Serial.print("T: "); Serial.println(*T);
                Serial.print("s: "); Serial.println(s);
                Serial.print("x: "); Serial.println(x);
                Serial.print("y: "); Serial.println(y);
                Serial.print("z: "); Serial.println(z);
                Serial.print("P: "); Serial.println(*P);
                */
        }
        return(result);
}


double SFE_BMP180::sealevel(double P, double A)
// Given a pressure P (mb) taken at a specific altitude (meters),
// return the equivalent pressure (mb) at sea level.
// This produces pressure readings that can be used for weather measurements.
{
        return(P/pow(1-(A/44330.0),5.255));
}


double SFE_BMP180::altitude(double P, double P0)
// Given a pressure measurement P (mb) and the pressure at a baseline P0 (mb),
// return altitude (meters) above baseline.
{
        return(44330.0*(1-pow(P/P0,1/5.255)));
}


char SFE_BMP180::getError(void)
        // If any library command fails, you can retrieve an extended
        // error code using this command. Errors are from the wire library: 
        // 0 = Success
        // 1 = Data too long to fit in transmit buffer
        // 2 = Received NACK on transmit of address
        // 3 = Received NACK on transmit of data
        // 4 = Other error
{
        return(_error);
}


// float SFE_BMP180::readTemperature(void) {
//           unsigned char data[2];
//         char result;
//         double tu, a, T;
//         data[0] = BMP180_REG_CONTROL;
//         data[1] = BMP180_COMMAND_TEMPERATURE;
//         writeBytes(data, 2);  //写入指令
//   //
//     data[0] = BMP180_REG_RESULT; //获得RWS值
//         result = readBytes(data, 2);
//         if (result) // good read, calculate temperature
//         {
//                 tu = (data[0] * 256.0) + data[1];
//                 a = c5 * (tu - c6);
//                 T = a + (mc / (a + md));
//         }
//         return(T);
// }        
float SFE_BMP180::readTemperature(void) {  //增加读取温值
  char status;
  double T,P,p0,a;
  status = SFE_BMP180::startTemperature();
  if (status != 0)
  {  
        delay(status);
    status = SFE_BMP180::getTemperature(T);
    if (status != 0)
    {
                return T;
        }
  } 

}
int32_t SFE_BMP180::readPressure(void) {  //增加读取气压
   char status;
   double T,P,p0,a;
    status = SFE_BMP180::startTemperature();
        if (status != 0){
                delay(status);
                status = SFE_BMP180::getTemperature(T);
                if (status != 0)
                {
                        status = SFE_BMP180::startPressure(3);//采样等级3
                        if (status != 0)
                        {
                                delay(status);
                                status = SFE_BMP180::getPressure(P,T);
                                if (status != 0)
                                {
                                //获得正常值
                                return P;
                                }
                        }
                }
        }
}

评分

参与人数 1金钱 +5 收起 理由
szqt + 5 大神666!

查看全部评分

回复

使用道具 举报

4

主题

57

帖子

277

积分

论坛分享达人

积分
277
金钱
220
HASS币
10
 楼主| 发表于 2020-5-21 09:54:15 | 显示全部楼层
四、增加YAML文件
       在以上实例中,另存为dimmer006.yaml(名称随你,不要中文),把 自定义库
  #加入自定义库
  includes:
    - dimmer005_sensor.h
  libraries:
    - "Adafruit BMP085 Library"

修改为:因为CPP文件已经在ESP目录中,所以不需要加入  libraries:
  #加入自定义库
  includes:
    - dimmer006_bmp180_b.h

其它不变。
全文如下:
esphome:
  name: dimmer006
  platform: ESP8266
  board: nodemcuv2
  #加入自定义库
  includes:
    - dimmer006_bmp180_b.h
wifi:
  ssid: "你的路由名称"
  password: "你的路由密码"
  ap:
    ssid: "ap_001"
    password: "12345678"

logger:
  # https://esphome.io/components/logger

api:
  password: "12345678"
  # https://esphome.io/components/api
ota:

  # https://esphome.io/components/ota
i2c:
  sda: D4
  scl: D5
sensor:
  - platform: custom
    lambda: |-
      auto my_sensor = new MyCustomSensor();
      App.register_component(my_sensor);
      return {my_sensor->temperature_sensor, my_sensor->pressure_sensor};

  sensors:
  - name: "Temperature1"
    unit_of_measurement: °C
    accuracy_decimals: 1
  - name: "Pressure Sensor1"
    unit_of_measurement: hPa
    accuracy_decimals: 2
回复

使用道具 举报

4

主题

57

帖子

277

积分

论坛分享达人

积分
277
金钱
220
HASS币
10
 楼主| 发表于 2020-5-21 09:57:23 | 显示全部楼层
五、编译上传与验证
      命令行:Esphome esphome_config\dimmer006.yaml run
    编译上传成功后:
1.  在串口中可以发现ESP有定时打印温度和气压值
2.  在HA中可以搜到设备,并正确显示值
3.  手碰传感器,温度会随之改变。
自鉴定完毕。成功调用本地驱动文件,完成BMP180驱动。ESPHOME真的很方便。
回复

使用道具 举报

5

主题

322

帖子

1345

积分

金牌会员

Rank: 6Rank: 6

积分
1345
金钱
1023
HASS币
0
发表于 2020-5-22 15:39:45 | 显示全部楼层
esphome的确方便又灵活!特别是串口设备驱动起来非常方便!
回复

使用道具 举报

0

主题

220

帖子

1739

积分

金牌会员

Rank: 6Rank: 6

积分
1739
金钱
1519
HASS币
0
QQ
发表于 2021-8-17 15:33:12 | 显示全部楼层
大神厉害学习了,弄这个多少要有点代码基础吧?
回复

使用道具 举报

9

主题

135

帖子

894

积分

高级会员

Rank: 4

积分
894
金钱
759
HASS币
0
发表于 2021-11-6 10:02:29 | 显示全部楼层
lfp136 发表于 2020-5-19 18:27
.h文件内容和 YAML文件内容的解说,在官网文档:https://esphome.io/components/sensor/custom.html 写的很 ...

楼主,我手上有一块st7565/st7567/nt7534驱动的128*64液晶屏,arduino ide的u8g2库还有github上也有支持的驱动可以点亮,但是esphome没有这个型号的屏的支持,我要怎样做才能让esphome使用这个屏幕?移植驱动吗?
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-27 22:37 , Processed in 0.561985 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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