-
Notifications
You must be signed in to change notification settings - Fork 0
/
_15_TLi4970.ino
147 lines (130 loc) · 3.83 KB
/
_15_TLi4970.ino
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#if FWTYPE == 15 // esp8266-tli4970
const char str_acLoad[] = "acLoad";
void sensorSetup() {
Tli4970CurrentSensor.begin(SPI, PIN_TLI_CS, PIN_TLI_DIO);
}
void printSensorConfig(const String &cfgStr) {
mqttSend(String(cfgStr + str_acLoad), String(acLoad), true);
}
void sensorImportJSON(JsonObject& json) {
if (json[str_acLoad].is<bool>()) {
acLoad = json[str_acLoad];
}
}
void sensorExportJSON(JsonObject& json) {
json[str_acLoad] = acLoad;
}
bool sensorUpdateConfig(const String &key, const String &value) {
if ( key == str_acLoad ) {
if ( value.equalsIgnoreCase("true") or value.equalsIgnoreCase("yes") or value == "1" ) {
acLoad = true;
} else if ( value.equalsIgnoreCase("false") or value.equalsIgnoreCase("no") or value == "0" ) {
acLoad = false;
} else {
logString = String(F("Unrecognised value for acLoad: ")) + value;
mqttLog(app_name_sensors, logString);
}
} else {
return false;
}
return true;
}
bool sensorRunAction(const String &key, const String &value) {
return false;
}
void calcData() {
if ( currentTime > (sLoopTime) ) { // Check current every ms
sLoopTime = currentTime; // Updates sLoopTime
if (Tli4970CurrentSensor.readOut()) {
tli4970Errors++;
if (tli4970Errors == error_count_log) {
logString = "Tli4970 error";
mqttLog(app_name_sensors, logString);
}
} else {
float thisCurrent = Tli4970CurrentSensor.getCurrent();
if ( acLoad && thisCurrent < 0 )
thisCurrent *= -1;
current += thisCurrent;
readings++;
if (tli4970Errors) {
if (tli4970Errors >= error_count_log) {
logString = "Tli4970 recovered";
mqttLog(app_name_sensors, logString);
}
tli4970Errors = 0;
}
}
}
#ifdef USESSD1306
if ( millis() < (prevDisplay + (displaySleep * 1000) ) ) { // if the display should still be awake
if ( currentTime >= (dLoopTime + 1000) ) {
dLoopTime = currentTime; // Updates dLoopTime for OLED redraw
display.clearDisplay();
char text[7];
float amps = current/readings;
byte places = 1;
if (amps < 1)
places = 3;
else if (amps < 10)
places = 2;
dtostrf(amps, 6, places, text);
display.setCursor(0, 16);
display.setTextSize(3);
display.print(text);
display.setCursor(115, 23);
display.setTextSize(2);
display.print("A");
display.display();
}
}
#endif
}
void sendData() {
char text[7];
float amps = current/readings;
byte places = 1;
// current /= readings; // Return the average of the current readings since the last time data was sent
#ifdef DEBUG
Serial.print("readings: ");
Serial.print(readings);
Serial.print(", current: ");
Serial.println(current);
#endif
if (amps < 1)
places = 3;
else if (amps < 10)
places = 2;
dtostrf(amps, 6, places, text);
mqttSend(String("current/amps"), String(text), false);
current = 0;
readings = 0;
}
String httpSensorData() {
String httpData = tableStart;
httpData += trStart + "Current:" + tdBreak;
httpData += String(current/readings) + " amps";
httpData += trEnd;
httpData += tableEnd;
return httpData;
}
String httpSensorSetup() {
String httpData;
httpData += trStart + F("Load type:") + tdBreak;
httpData += htmlRadio(str_acLoad, str_false, (! acLoad)) + "DC";
httpData += htmlRadio(str_acLoad, str_true, acLoad) + "AC";
httpData += trEnd;
return httpData;
}
String httpSensorConfig() {
if (httpServer.hasArg(str_acLoad)) {
if (httpServer.arg(str_acLoad) == str_true) {
acLoad = true;
} else {
acLoad = false;
}
}
}
void sensorMqttCallback(char* topic, byte* payload, unsigned int length) {}
void sensorMqttSubs() {}
#endif