Skip to content

Instantly share code, notes, and snippets.

@mKeRix mKeRix/d6t_44l_06.h
Last active May 18, 2019

Embed
What would you like to do?
ESPHome D6T Custom Component
#include "esphome.h"
#include "math.h"
using namespace esphome;
#define I2C_ADDR_D6T 0x0a
#define CMD 0x4c
static const int SOBEL_X[3][3] =
{ { -1, 0, 1 },
{ -2, 0, 2 },
{ -1, 0, 1 } };
static const int SOBEL_Y[3][3] =
{ { -1, -2, -1 },
{ 0, 0, 0 },
{ 1, 2, 1 } };
static const float THRESHOLD = 4.0;
class D6T44L06BinarySensor : public PollingComponent, public binary_sensor::BinarySensor {
public:
D6T44L06BinarySensor() : PollingComponent(500) {}
void setup() override {
Wire.begin();
}
void update() override {
float temps[4][4];
float gradients[4][4];
bool state;
readTemps(temps);
applySobel(temps, gradients);
state = meetsThreshold(gradients);
publish_state(state);
}
private:
void readTemps(float temps[4][4]) {
uint8_t buf[35];
int i = 0;
Wire.beginTransmission(I2C_ADDR_D6T);
Wire.write(CMD);
Wire.endTransmission();
Wire.requestFrom(I2C_ADDR_D6T, 35);
while (Wire.available() && i < 35){
buf[i++] = Wire.read();
}
Wire.endTransmission();
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
i = x + (y * 4);
temps[y][x] = (buf[(i * 2 + 2)] + (buf[(i * 2 + 3)] << 8)) * 0.1;
}
}
}
void applySobel(float temps[4][4], float gradients[4][4]) {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
float magX = 0;
float magY = 0;
float gradient = 0;
for (int yk = 0; yk < 3; yk++) {
for (int xk = 0; xk < 3; xk++) {
magX += temps[clamp(y + yk - 1, 0, 3)][clamp(x + xk - 1, 0, 3)] * SOBEL_X[yk][xk];
magY += temps[clamp(y + yk - 1, 0, 3)][clamp(x + xk - 1, 0, 3)] * SOBEL_Y[yk][xk];
}
}
gradients[y][x] = sqrt(pow(magX, 2) + pow(magY, 2));
}
}
}
bool meetsThreshold(float values[4][4]) {
for (int y = 0; y < 4; y++){
for (int x = 0; x < 4; x++){
if (values[y][x] >= THRESHOLD) {
return true;
}
}
}
return false;
}
int clamp(int d, int min, int max) {
const int t = d < min ? min : d;
return t > max ? max : t;
}
};
esphome:
includes:
- d6t_44l_06.h
name: d6t_sensor
platform: ESP32
board: nodemcu-32s
i2c:
sda: 21
scl: 22
scan: False
binary_sensor:
- platform: custom
lambda: |-
auto d6t_sensor = new D6T44L06BinarySensor();
App.register_component(d6t_sensor);
return {d6t_sensor};
binary_sensors:
name: "D6T Sensor"
device_class: occupancy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.