[ESPhome] custom sensor geeft altijd afgeronde waarde

Pagina: 1
Acties:

Vraag


Acties:
  • 0 Henk 'm!

  • Recoil
  • Registratie: Februari 2002
  • Niet online
Ik heb een custom sensor aangemaakt in ESPhome voor een MCP9808 temperatuursensor. In principe werkt het geheel, maar de output is altijd een afgeronde waarde. Dus hij gaat bijvoorbeeld van 19.0000 naar 20.0000

Dit is een voorbeeld van de log-output:
code:
1
[20:27:23][D][sensor:092]: 'MCP9808 temperature': Sending state 22.00000 °C with 4 decimals of accuracy


Dit is de custom sensor voor ESPhome (sensor_mcp9808.h):
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "esphome.h"
#include "Adafruit_MCP9808.h"
class MyCustomSensor : public PollingComponent, public Sensor {
 public:
  Adafruit_MCP9808 mcp;

  MyCustomSensor() : PollingComponent(15000) { }

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

  void update() override {
    int temp = mcp.readTempC(); 
    publish_state(temp); 
  }
};


Als library gebruik ik de onaangepaste Adafruit library (Adafruit_MCP9808.h):
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef _ADAFRUIT_MCP9808_H
#define _ADAFRUIT_MCP9808_H

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#include <Wire.h>

#define MCP9808_I2CADDR_DEFAULT 0x18 ///< I2C address
#define MCP9808_REG_CONFIG 0x01      ///< MCP9808 config register

#define MCP9808_REG_CONFIG_SHUTDOWN 0x0100   ///< shutdown config
#define MCP9808_REG_CONFIG_CRITLOCKED 0x0080 ///< critical trip lock
#define MCP9808_REG_CONFIG_WINLOCKED 0x0040  ///< alarm window lock
#define MCP9808_REG_CONFIG_INTCLR 0x0020     ///< interrupt clear
#define MCP9808_REG_CONFIG_ALERTSTAT 0x0010  ///< alert output status
#define MCP9808_REG_CONFIG_ALERTCTRL 0x0008  ///< alert output control
#define MCP9808_REG_CONFIG_ALERTSEL 0x0004   ///< alert output select
#define MCP9808_REG_CONFIG_ALERTPOL 0x0002   ///< alert output polarity
#define MCP9808_REG_CONFIG_ALERTMODE 0x0001  ///< alert output mode

#define MCP9808_REG_UPPER_TEMP 0x02   ///< upper alert boundary
#define MCP9808_REG_LOWER_TEMP 0x03   ///< lower alert boundery
#define MCP9808_REG_CRIT_TEMP 0x04    ///< critical temperature
#define MCP9808_REG_AMBIENT_TEMP 0x05 ///< ambient temperature
#define MCP9808_REG_MANUF_ID 0x06     ///< manufacture ID
#define MCP9808_REG_DEVICE_ID 0x07    ///< device ID
#define MCP9808_REG_RESOLUTION 0x08   ///< resolution

/*!
 *    u/brief  Class that stores state and functions for interacting with
 *            MCP9808 Temp Sensor
 */
class Adafruit_MCP9808 {
public:
  Adafruit_MCP9808();
  bool begin();
  bool begin(TwoWire *theWire);
  bool begin(uint8_t addr);
  bool begin(uint8_t addr, TwoWire *theWire);

  bool init();
  float readTempC();
  float readTempF();
  uint8_t getResolution(void);
  void setResolution(uint8_t value);

  void shutdown_wake(boolean sw);
  void shutdown();
  void wake();

  void write16(uint8_t reg, uint16_t val);
  uint16_t read16(uint8_t reg);

  void write8(uint8_t reg, uint8_t val);
  uint8_t read8(uint8_t reg);

private:
  TwoWire *_wire;
  uint8_t _i2caddr;
};

#endif


En ik gebruik dit in de configuration.yaml om de sensor uiteindelijk aan te kunnen roepen (alleen de relevante code):
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
esphome:
  libraries:
    - "Adafruit MCP9808 Library"
  includes:
    - sensor_mcp9808.h

sensor:    
  - platform: custom
    lambda: |-
      auto my_sensor = new MyCustomSensor();
      App.register_component(my_sensor);
      return {my_sensor};

    sensors:
      name: "MCP9808 temperature"
      unit_of_measurement: °C
      accuracy_decimals: 4


Hoe kan ik ervoor zorgen dat hij dat hij de daadwerkelijke inhoud print in plaats van alleen maar *.0000?

Beste antwoord (via Recoil op 19-12-2020 22:02)


  • SA007
  • Registratie: Oktober 2002
  • Laatst online: 26-09 11:35

SA007

Moderator Tweaking
Heel simpel, je slaap je uitgelezen temperatuur op in een int.
Een int is altijd een geheel getal, daardoor gooi je effectief alle decimalen weg.

In plaats van int Temp wil je een float.

Alle reacties


Acties:
  • Beste antwoord
  • +2 Henk 'm!

  • SA007
  • Registratie: Oktober 2002
  • Laatst online: 26-09 11:35

SA007

Moderator Tweaking
Heel simpel, je slaap je uitgelezen temperatuur op in een int.
Een int is altijd een geheel getal, daardoor gooi je effectief alle decimalen weg.

In plaats van int Temp wil je een float.

Acties:
  • 0 Henk 'm!

  • Recoil
  • Registratie: Februari 2002
  • Niet online
Holy shit, dat heeft een paar uur van mijn leven gekost. Weer wat geleerd, dankjewel!
code:
1
[22:01:11][D][sensor:092]: 'MCP9808 temperature': Sending state 20.93750 °C with 4 decimals of accuracy