DS1631 Temperature Sensor with Tasmota

So, I’m trying to get a DS1631 temperature sensor working with Tasmota, and thereby transmit its data through to Home Assistant. Why a DS1631 you ask? Because thats what I have.

So a DS1631 uses I2C for communication. I’ve written some code to work with it directly, but I’d like to make it work under Tasmota for all the extra goodies Tasmota offers. Googling sensor driver development has me a wee bit overwhelmed though. Anyone know of a good basic tutorial to help? (Its a pretty basic protocol, so it should be fairly easy to get going…)

Also, would be nice to be able to test in the tasmota console, which I’m assuming is possible, but can’t figure out how to read/write to the bus from the console

I know both should be easy, but I’m getting bogged down with too much when I start reading manuals.

If they admits binary has support for that sensor and i2c you should see the sensor value in the console. Provided as well you have selected the right module/template to configure i2c… it’s pretty straightforward when you set the GPIOs for i2c

I can see the sensor if I run i2cscan, but it doesn’t know what the sensor is, and I don’t know how to tell it its that sensor at the given address.

Found so far — looking at the supported sensors, DS1631 isn’t on the list of supported sensors.

Looks like I should be able to customize https://git.dahwa.fr/meuliere/Sonoff-Tasmota/src/889b01779dc01ca7a389fb13e8f211ed59197ea6/sonoff/xsns_26_lm75ad.ino , but I have to figure out all the details of how…

Sensor needs to have configuration messages sent initially, then messages sent to read the temperature back. Haven’t quite figured out some of the nitty gritty yet.

Documenting for any who come after… this looks like an important page…

Datasheet for the ds1631

Spent a few hours on it today. Doesn’t seem like its actually using the code I wrote…

Came back to this for a different reason, and I decided to hook to esphome instead.


#include "esphome.h"

const int I2C_address = 0x4e;  // I2C write address
int16_t ds1631_temperature();

class ds1631: public PollingComponent, public Sensor {
 public:
  // constructor
  ds1631() : PollingComponent(15000) {}
  void setup() override {
  Wire.begin();           // join i2c bus
  // initialize DS1631 sensor
  Wire.beginTransmission(I2C_address); // connect to DS1631 (send DS1631 address)
  Wire.write(0xAC);                       // send configuration register address (Access Config)
  Wire.write(0x0C);                       // perform continuous conversion with 12-bit resolution
  Wire.beginTransmission(I2C_address); // send repeated start condition
  Wire.write(0x51);                       // send start temperature conversion command
  Wire.endTransmission();     
  }
  void update() override {
    // This will be called every "update_interval" milliseconds.
  publish_state(float(ds1631_temperature())/float(256));
  
  }
};




int16_t ds1631_temperature() {
  Wire.beginTransmission(I2C_address); // connect to DS1631 (send DS1631 address)
  Wire.write(0xAA);                       // read temperature command
  Wire.endTransmission(false);            // send repeated start condition
  Wire.requestFrom(I2C_address, 2);    // request 2 bytes from DS1631 and release I2C bus at end of reading
  uint8_t t_msb = Wire.read();            // read temperature MSB register
  uint8_t t_lsb = Wire.read();            // read temperature LSB register

  // calculate full temperature (raw value)
  int16_t raw_t = (int8_t)t_msb<<8 | t_lsb;
  //raw_t >>= 4;
  return raw_t;
}

// end of code.

Then the yaml


esphome:
  name: temperature-board
  platform: ESP8266
  board: d1_mini_lite
  includes:
    - ds1631.h
# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: ""

wifi:
  ssid: ""
  password: ""

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

captive_portal:
i2c:
     sda: D2
     scl: D1
     scan: true
     

sensor:
 - platform: custom
   lambda: |-
     auto airytemp = new ds1631();
     App.register_component(airytemp);
     return {airytemp};
   sensors:
    name: "Airy Temperature"
    unit_of_measurement: °C
    accuracy_decimals: 1