-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp_battery_sensor.cpp
214 lines (167 loc) · 4.61 KB
/
esp_battery_sensor.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Do not remove the include below
#include "esp_battery_sensor.h"
#include <string>
//#include "OneWire.h"
#include "DallasTemperature.h"
#define ONE_WIRE_BUS 13 // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long bootTime;
DeviceAddress devices[10];
int sensorsFound = 0;
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
//The setup function is called once at startup of the sketch
void setup()
{
bootTime = millis();
Serial.begin(115200);
//pinMode(13, OUTPUT);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// Initialize the LED_BUILTIN pin as an output// Add your initialization code here
}
bool setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
// static ip
WiFi.config(
IPAddress(192,168,0,241),
IPAddress(192,168,0,1),
IPAddress(255,255,255,0),
IPAddress(8,8,8,8)
);
WiFi.begin(wifi_ssid, wifi_password);
unsigned long s = millis();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if ((millis() > (s + 20000)) || (millis() < s)) {
Serial.println("Giving up on WIFI");
return false;
}
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
return true;
}
bool connect_mqtt() {
// Loop until we're reconnected
unsigned long s = millis();
while (!client.connected()) {
if ((millis() > (s + 20000)) || (millis() < s)) {
Serial.println("Giving up on MQTT connection");
return false;
}
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
return true;
}
float getTemperature(DeviceAddress address) {
float temp;
unsigned long s = millis();
int xtry = 0;
do {
xtry += 1;
if (xtry % 3 == 0) {
DS18B20.requestTemperatures();
}
temp = DS18B20.getTempC(address);
if ((millis() > (s + 1000)) || (millis() < s)) {
Serial.print("timeout temp");
return -127.0;
}
} while (((temp > 84.0) && (temp < 86.0)) || temp < (-126.0));
return temp;
}
void goToSleep() {
delay(100);
Serial.print("Entering deep sleep mode for ");
Serial.print(SLEEP_DELAY_IN_SECONDS);
Serial.print(" seconds after ");
Serial.print((millis() - bootTime)/1000);
Serial.println("seconds");
ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
delay(500);
}
void setup_ds18b20() {
DS18B20.begin();
sensorsFound = DS18B20.getDeviceCount();
for (int i = 0; i < sensorsFound; i++)
if (!DS18B20.getAddress(devices[i], i))
Serial.println("Unable to find address for Device" + i);
for (int i = 0; i < sensorsFound; i++)
DS18B20.setResolution(devices[i], 12);
}
void blockTillConversionComplete(){
int delms = 750;
if (DS18B20.getCheckForConversion() && !DS18B20.isParasitePowerMode()){
unsigned long now = millis();
while(!DS18B20.isConversionComplete() && (millis() - delms < now));
} else {
delay(delms);
}
}
// The loop function is called in an endless loop
void loop()
{
delay(100);
float volts = 1.0 * analogRead(A0) / 1024; // 1024 = 1v
float v_in = volts * (2000 + 330) / 330; // voltage divider: vin --> 2Mohm --> A0 --> 330Kohm --> gnd
Serial.print("battery voltage: v_in ");
Serial.print(v_in);
Serial.print(" V (adc: ");
Serial.print(volts);
Serial.println(")");
setup_ds18b20();
DS18B20.setWaitForConversion(false);
DS18B20.requestTemperatures();
if (!setup_wifi()) {
goToSleep();
return;
}
if (!connect_mqtt()) {
goToSleep();
return;
}
blockTillConversionComplete();
for (int i =0; i < sensorsFound; i++) {
float temp = getTemperature(devices[i]);
String s = String(mqtt_topic_temp) + "/";
for (unsigned int c=0; c<sizeof(devices[i]);c++) {
s += String(devices[i][c],HEX);
}
String value = String(temp);
client.publish(s.c_str(), value.c_str());
Serial.println(s + " " + temp + " °C");
}
client.publish(mqtt_topic_volt, String(v_in).c_str());
Serial.println(String(mqtt_topic_volt) + " " + v_in);
client.disconnect();
WiFi.disconnect();
goToSleep();
}