-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarBatt_Monitor-oversamples.ino
More file actions
161 lines (120 loc) · 4.58 KB
/
Copy pathCarBatt_Monitor-oversamples.ino
File metadata and controls
161 lines (120 loc) · 4.58 KB
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
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
This example runs directly on ESP32 chip.
Note: This requires ESP32 support package:
https://github.com/espressif/arduino-esp32
Please be sure to select the right ESP32 module
in the Tools -> Board menu!
Change WiFi ssid, pass, and Blynk auth token to run :)
Feel free to apply it to any other example. It's simple!
************************************************************
:: Car Battery Monitor v1.0
:: by
:: @CraigMarston
:: This uses the ESP32's ADC which is only 12-bit
:: and is not very linear, hence the polynomial
:: which is derived from actual measurements taken.
:: A lead-acid car battery is 100% charged at 12.6 V
:: it is 75% charged at 12.4 V
:: it is 50% charged at 12.2 V
:: and at 12.0V it is only 25% charged and probably knackered!!
*/
// Node 32s board, Hold down the boot button!!
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ThingSpeak.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "-------------"; // Voltage Monitor - Craig's Blynk
//Thing Speak
unsigned long myChannelNumber = ----------;
const char * myWriteAPIKey = "-------------";
// Your WiFi credentials.
char ssid[] = "---------";
char pass[] = "----------";
uint16_t ADC_val0;
uint16_t ADC_val1;
uint16_t ADC_val2;
uint16_t ADC_val3;
uint16_t ADC_val4;
uint16_t ADC_val5;
uint16_t ADC_val6;
uint16_t ADC_val7;
uint16_t ADC_val8;
uint16_t ADC_val9;
uint16_t ADC_val_mean;
const float coeffA = -9.433922E-7; // these coefficients are
const float coeffB = 9.282729E-3; // worked out from calibration data
const float coeffC = -6.640744E0; // using https://arachnoid.com/polysolve/ 2-degree!
float BattVolts;
const uint8_t sleepyTime = 30; // ------------------------::--::--::--> sleep duration in mins, max 70
const uint32_t time_in_us = (sleepyTime * 60000000); // multiple of microseconds to seconds
WiFiClient client;
void setup()
{
//Debug console
Serial.begin(115200);
pinMode(2, OUTPUT); // sets the digital pin 2 as output
digitalWrite(2, HIGH); // puts the LED on so we can see activity
Blynk.begin(auth, ssid, pass);
ThingSpeak.begin(client); // Initialize ThingSpeak
esp_sleep_enable_timer_wakeup(time_in_us); // limited to 70 mins due to variable maxing out
Blynk.run(); // all the Blynk magic happens here
ADC_val0 = analogRead(32); //reading the sensor on ...
delay(333);
ADC_val1 = analogRead(32);
delay(333);
ADC_val2 = analogRead(32);
delay(333);
ADC_val3 = analogRead(32);
delay(333);
ADC_val4 = analogRead(32);
delay(333);
ADC_val5 = analogRead(32);
delay(333);
ADC_val6 = analogRead(32);
delay(333);
ADC_val7 = analogRead(32);
delay(333);
ADC_val8 = analogRead(32);
delay(333);
ADC_val9 = analogRead(32);
ADC_val_mean = (ADC_val0+ADC_val1+ADC_val2+ADC_val3+ADC_val4+ADC_val5+ADC_val6+ADC_val7+ADC_val8+ADC_val9) / 10 ;
// f(x) = Ax^2 + Bx + C the polynomial equation for calibration
BattVolts = (coeffA*sq(ADC_val_mean)) + (coeffB*ADC_val_mean) + coeffC ;
Blynk.virtualWrite(V5, ADC_val_mean); //sending to Blynk
Blynk.virtualWrite(V6, BattVolts); // conversion to voltage
Serial.print("ADC: ");
Serial.println(ADC_val_mean);
Serial.print("V = ");
Serial.println(BattVolts);
;
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
//setField (field, value)
ThingSpeak.setField (1, ADC_val_mean);
ThingSpeak.setField (2, BattVolts); // disable while calibrating
//writeFields (channelNumber, writeAPIKey)
ThingSpeak.writeFields (myChannelNumber, myWriteAPIKey);
digitalWrite(2, LOW);
esp_deep_sleep_start();
}
void loop()
{
// nowt happens here!
}