This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Geiger_Counter_BT.ino
385 lines (324 loc) · 10.4 KB
/
Geiger_Counter_BT.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/****************************************************************************************************************************
Geiger_Counter_BT.ino
For ESP32 using WiFi along with BlueTooth BLE
BlynkESP32_BT_WF is a library for inclusion of both ESP32 Blynk BT/BLE and WiFi libraries.
Then select either one or both at runtime.
Based on and modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases
Built by Khoi Hoang https://github.com/khoih-prog/BlynkESP32_BT_WF
Licensed under MIT license
*****************************************************************************************************************************/
/****************************************************************************************************************************
Important Notes:
1) Sketch is ~0.9MB of code because only 1 instance of Blynk if #define BLYNK_USE_BT_ONLY => true
2) Sketch is very large (~1.3MB code) because 2 instances of Blynk if #define BLYNK_USE_BT_ONLY => false
3) To conmpile, use Partition Scheem with large APP size, such as
a) 8MB Flash (3MB APP, 1.5MB FAT) if use EEPROM
b) No OTA (2MB APP, 2MB SPIFFS)
c) No OTA (2MB APP, 2MB FATFS) if use EEPROM
d) Huge APP (3MB No OTA, 1MB SPIFFS) <===== Preferable if use SPIFFS
e) Minimal SPIFFS (1.9MB APP with OTA, 190KB SPIFFS)
*****************************************************************************************************************************/
#include "defines.h"
#include "Credentials.h"
#include "dynamicParams.h"
bool USE_BT = true;
#define WIFI_BT_SELECTION_PIN 14 //Pin D14 mapped to pin GPIO14/HSPI_SCK/ADC16/TOUCH6/TMS of ESP32
#define GEIGER_INPUT_PIN 18 // Pin D18 mapped to pin GPIO18/VSPI_SCK of ESP32
#define VOLTAGER_INPUT_PIN 36 // Pin D36 mapped to pin GPIO36/ADC0/SVP of ESP32
#define CONV_FACTOR 0.00658
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET 4
#define DEBOUNCE_TIME_MICRO_SEC 4200L
#define MEASURE_INTERVAL_MS 20000L
#define COUNT_PER_MIN_CONVERSION (60000 / MEASURE_INTERVAL_MS)
#define VOLTAGE_FACTOR ( ( 4.2 * (3667 / 3300) ) / 4096 )
float voltage = 0;
long countPerMinute = 0;
long timePrevious = 0;
long timePreviousMeassure = 0;
long _time = 0;
long countPrevious = 0;
float radiationValue = 0.0;
float radiationDose = 0;
void IRAM_ATTR countPulse();
volatile unsigned long last_micros;
volatile long count = 0;
BlynkTimer timer;
#include <Ticker.h>
Ticker led_ticker;
#define BLYNK_PIN_FORCED_CONFIG V10
#define BLYNK_PIN_FORCED_PERS_CONFIG V20
// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal
BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG)
{
if (param.asInt())
{
Serial.println( F("\nCP Button Hit. Rebooting") );
// This will keep CP once, clear after reset, even you didn't enter CP at all.
Blynk.resetAndEnterConfigPortal();
}
}
// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal
BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG)
{
if (param.asInt())
{
Serial.println( F("\nPersistent CP Button Hit. Rebooting") );
// This will keep CP forever, until you successfully enter CP, and Save data to clear the flag.
Blynk.resetAndEnterConfigPortalPersistent();
}
}
void IRAM_ATTR countPulse()
{
if ((long)(micros() - last_micros) >= DEBOUNCE_TIME_MICRO_SEC)
{
count++;
last_micros = micros();
}
}
void sendDatatoBlynk()
{
#if BLYNK_USE_BT_ONLY
Blynk_BT.virtualWrite(V1, countPerMinute);
Blynk_BT.virtualWrite(V3, radiationValue);
Blynk_BT.virtualWrite(V5, radiationDose);
Blynk_BT.virtualWrite(V7, voltage);
#else
if (USE_BT)
{
Blynk_BT.virtualWrite(V1, countPerMinute);
Blynk_BT.virtualWrite(V3, radiationValue);
Blynk_BT.virtualWrite(V5, radiationDose);
Blynk_BT.virtualWrite(V7, voltage);
}
else
{
Blynk_WF.virtualWrite(V1, countPerMinute);
Blynk_WF.virtualWrite(V3, radiationValue);
Blynk_WF.virtualWrite(V5, radiationDose);
Blynk_WF.virtualWrite(V7, voltage);
}
#endif
}
void Serial_Display()
{
Serial.print(F("cpm = "));
Serial.printf("%4lu", countPerMinute);
Serial.print(F(" - "));
Serial.print(F("RadiationValue = "));
Serial.printf("%5.3f", radiationValue);
Serial.print(F(" uSv/h"));
Serial.print(F(" - "));
Serial.print(F("Equivalent RadiationDose = "));
Serial.printf("%6.4f", radiationDose);
Serial.println(F(" uSv"));
}
void set_led(byte status)
{
digitalWrite(LED_BUILTIN, status);
}
void heartBeatPrint(void)
{
static int num = 1;
if (Blynk.connected())
{
set_led(HIGH);
led_ticker.once_ms(111, set_led, (byte) LOW);
Serial.print(F("B"));
}
else
{
Serial.print(F("F"));
}
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
}
#define USE_SIMULATION true
//#define USE_SIMULATION false
void checkStatus()
{
if (millis() - timePreviousMeassure > MEASURE_INTERVAL_MS)
{
if (!USE_BT)
{
if (Blynk.connected())
Serial.println(F("B"));
else
Serial.println(F("F"));
}
timePreviousMeassure = millis();
noInterrupts();
countPerMinute = COUNT_PER_MIN_CONVERSION * count;
interrupts();
radiationValue = countPerMinute * CONV_FACTOR;
radiationDose = radiationDose + (radiationValue / float(240.0));
// can optimize this calculation
voltage = (float) analogRead(VOLTAGER_INPUT_PIN) * VOLTAGE_FACTOR;
if (radiationDose > 99.999)
{
radiationDose = 0;
}
Serial_Display();
#if USE_SIMULATION
count += 10;
if (count >= 1000)
count = 0;
#else
count = 0;
#endif
// report Blynk connection
heartBeatPrint();
}
}
char BT_Device_Name[] = "GeigerCounter-BT";
#if USING_CUSTOMS_STYLE
const char NewCustomsStyle[] /*PROGMEM*/ = "<style>div,input{padding:5px;font-size:1em;}input{width:95%;}body{text-align: center;}\
button{background-color:blue;color:white;line-height:2.4rem;font-size:1.2rem;width:100%;}fieldset{border-radius:0.3rem;margin:0px;}</style>";
#endif
void setup()
{
Serial.begin(115200);
while (!Serial);
#if (USE_LITTLEFS)
Serial.print(F("\nStarting ESP32_BLE_WF using LITTLEFS"));
#elif (USE_SPIFFS)
Serial.print(F("\nStarting ESP32_BLE_WF using SPIFFS"));
#else
Serial.print(F("\nStarting ESP32_BLE_WF using EEPROM"));
#endif
#if USE_SSL
Serial.print(F(" with SSL on "));
#else
Serial.print(F(" without SSL on "));
#endif
Serial.println(ARDUINO_BOARD);
Serial.println(BLYNK_ESP32_BT_WF_VERSION);
#if USE_BLYNK_WM
Serial.println(ESP_DOUBLE_RESET_DETECTOR_VERSION);
#endif
pinMode(GEIGER_INPUT_PIN, INPUT);
attachInterrupt(GEIGER_INPUT_PIN, countPulse, HIGH);
#if BLYNK_USE_BT_ONLY
Blynk_BT.setDeviceName(BT_Device_Name);
Blynk_BT.begin(auth);
#else
if (digitalRead(WIFI_BT_SELECTION_PIN) == HIGH)
{
USE_BT = false;
Serial.println(F("GPIO14 HIGH, Use WiFi"));
#if USE_BLYNK_WM
// Set config portal SSID and Password
Blynk.setConfigPortal("TestPortal-ESP32", "TestPortalPass");
// Set config portal IP address
Blynk.setConfigPortalIP(IPAddress(192, 168, 232, 1));
// Set config portal channel, default = 1. Use 0 => random channel from 1-13 to avoid conflict
Blynk_WF.setConfigPortalChannel(0);
// From v1.0.6, select either one of these to set static IP + DNS
Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 232), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0));
//Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 232), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
// IPAddress(192, 168, 2, 1), IPAddress(8, 8, 8, 8));
//Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 232), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
// IPAddress(4, 4, 4, 4), IPAddress(8, 8, 8, 8));
//////////////////////////////////////////////
#if USING_CUSTOMS_STYLE
Blynk.setCustomsStyle(NewCustomsStyle);
#endif
#if USING_CUSTOMS_HEAD_ELEMENT
Blynk.setCustomsHeadElement("<style>html{filter: invert(10%);}</style>");
#endif
#if USING_CORS_FEATURE
Blynk.setCORSHeader("Your Access-Control-Allow-Origin");
#endif
//////////////////////////////////////////////
// Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX
//Blynk.begin();
// Use this to personalize DHCP hostname (RFC952 conformed)
// 24 chars max,- only a..z A..Z 0..9 '-' and no '-' as last char
//Blynk.begin("ESP32-BT-WM");
Blynk_WF.begin(BT_Device_Name);
#else
//Blynk_WF.begin(auth, ssid, pass);
Blynk_WF.begin(auth, ssid, pass, cloudBlynkServer.c_str(), BLYNK_SERVER_HARDWARE_PORT);
#endif
}
else
{
USE_BT = true;
Serial.println(F("GPIO14 LOW, Use BT"));
Blynk_BT.setDeviceName(BT_Device_Name);
#if USE_BLYNK_WM
if (Blynk_WF.getBlynkBTToken() == NO_CONFIG) //String("blank"))
{
Serial.println(F("No valid stored BT auth. Have to run WiFi then enter config portal"));
USE_BT = false;
Blynk_WF.begin(BT_Device_Name);
}
String BT_auth = Blynk_WF.getBlynkBTToken();
#else
String BT_auth = auth;
#endif
if (USE_BT)
{
Serial.print(F("Connecting Blynk via BT, using auth = "));
Serial.println(BT_auth);
Blynk_BT.begin(BT_auth.c_str());
}
}
#endif
timer.setInterval(5000L, sendDatatoBlynk);
}
#if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS)
void displayCredentials()
{
Serial.println(F("\nYour stored Credentials :"));
for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++)
{
Serial.print(myMenuItems[i].displayName);
Serial.print(F(" = "));
Serial.println(myMenuItems[i].pdata);
}
}
void displayCredentialsInLoop()
{
static bool displayedCredentials = false;
if (!displayedCredentials)
{
for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++)
{
if (!strlen(myMenuItems[i].pdata))
{
break;
}
if ( i == (NUM_MENU_ITEMS - 1) )
{
displayedCredentials = true;
displayCredentials();
}
}
}
}
#endif
void loop()
{
#if BLYNK_USE_BT_ONLY
Blynk_BT.run();
#else
if (USE_BT)
Blynk_BT.run();
else
Blynk_WF.run();
#endif
timer.run();
checkStatus();
#if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS)
displayCredentialsInLoop();
#endif
}