-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmc5883l.h
77 lines (63 loc) · 2.03 KB
/
hmc5883l.h
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
67
68
69
70
71
72
73
74
75
76
77
#include "esphome.h"
#include "Wire.h"
class WaterMeterSensor : public PollingComponent , public Sensor {
public:
private:
unsigned long last_publish = 0;
unsigned long now = 0;
unsigned int crossings = 0;
int new_val = 0;
int old_val = 0;
#define hmc5883l_address 0x1E
#define publish_delay 5000
boolean changed = false;
boolean previousChanged = false;
int16_t y;
void setup() override {
set_update_interval(10);
Wire.begin();
Wire.beginTransmission(hmc5883l_address);
Wire.write(0x00); // Select Configuration Register A
Wire.write(0x38); // 2 Averaged Samples at 75Hz
Wire.endTransmission();
Wire.beginTransmission(hmc5883l_address);
Wire.write(0x01); // Select Configuration Register B
Wire.write(0x20); // Set Default Gain
Wire.endTransmission();
Wire.beginTransmission(hmc5883l_address);
Wire.write(0x02); // Select Mode Register
Wire.write(0x00); // Continuous Measurement Mode
Wire.endTransmission();
}
void update() override {
now = millis();
Wire.beginTransmission(hmc5883l_address);
Wire.write(0x03); // Select register 3, X MSB Register
Wire.endTransmission();
Wire.requestFrom(hmc5883l_address, 6); delay(4);
if(Wire.available() >= 6) {
Wire.read(); Wire.read(); Wire.read(); Wire.read(); // Ignore X and Z Registers
y = Wire.read() << 8; // Y MSB
y |= Wire.read(); // Y LSB
}
old_val = new_val;
new_val = map(y, -1000, -250, -350, 350);
changed = (old_val < 0 && new_val > 0) || (old_val > 0 && new_val < 0);
if(changed) {
crossings += 1;
}
char s[30];
if((crossings > 0 || previousChanged) && (now - last_publish) >= publish_delay) {
sprintf(s,"WaterValue: %u,%d,%d,%d",crossings,y,new_val,old_val);
publish_state(crossings);
ESP_LOGD("info","%s",s);
Serial.println(s);
if (crossings > 0)
previousChanged=true;
else
previousChanged=false;
crossings = 0;
last_publish = now;
}
}
};