-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32-geigercounter.ino
328 lines (241 loc) · 8 KB
/
esp32-geigercounter.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
#include <SPI.h>
// WiFi
#include <WiFi.h>
// NTP
#include "time.h"
//MQTT
#include <PubSubClient.h> //MQTT
//Display
#include <SSD1306.h> //Display
//BME280
#include <BME280I2C.h>
#include <Wire.h>
// WiFi Config
const char *ssid = "******************";
const char *password = "******************";
// NTP Config
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
// MQTT Config
const char *mqttServer = "192.168.3.5";
const char *mqttClientName = "Geigercounter";
const char *mqttTopicCpm = "/backyard/geigercounter/cpm";
const char *mqttTopicuSvh = "/backyard/geigercounter/usvh";
const char *mqttTopicTemperature = "/backyard/geigercounter/temperature";
const char *mqttTopicPressure = "/backyard/geigercounter/pressure";
const char *mqttTopicHumidity = "/backyard/geigercounter/humidity";
// Geigercounter Config
const int clickImputPin = 15;
const float tubeCpmOffset = 7.2; //Correct Ghost counts create by the tube itself without radiation (M4011: 0,2/s = 12/min)
const float conversionIndex = 153.8; //how many cpm are 1 uSv/h?
volatile long counts = 0; // Tube events
float cpm = 0; // CPM
float uSvh = 0; // Micro Sievert per hour
unsigned long previousMillis = 0; // Time measurement
unsigned long currentMillis = millis();
unsigned long start = 0;
#define MINUTE_PERIOD 60000 // One Minute in ms
//BME280 Config (0x76)
#define BME_SDA 5 //19
#define BME_SCL 4 //18
float temperature(NAN), humidity(NAN), pressure(NAN);
BME280I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
WiFiClient espClient;
PubSubClient mqttClient(espClient);
SSD1306 display(0x3c, 5, 4);
// Method definitions
void connectWiFi();
void disconnectWiFi();
void connectMqtt();
void getNtpTime();
void ISR_impulse();
void sendGeigercounterData(double, double);
void updateBmeData();
void sendBmeData(double, double, double);
void updateDisplayValues();
void displayString(String dispString, int x, int y);
void displayInt(int dispInt, int x, int y);
void displayInit();
// initializes everything
void setup() {
Serial.begin(115200);
Wire.begin(BME_SDA, BME_SCL);
pinMode(clickImputPin, INPUT); // Set pin for capturing Tube events
displayInit();
displayString("Welcome", 64, 15);
connectWiFi(); // Connect WiFi
getNtpTime(); // Get Time
//Start MQTT
connectMqtt();
//Start BME280
if (!bme.begin()) {
Serial.println("Could not find a valid BME280 !");
displayString("No BME280!", 64, 15);
}
//Start counting
interrupts(); // Enable interrupts // Enable interrupts
attachInterrupt(digitalPinToInterrupt(clickImputPin), ISR_impulse, FALLING); // Define interrupt on falling edge
unsigned long clock1 = millis();
start = clock1;
unsigned long previousMillis = 0; // Time measurement
unsigned long currentMillis = millis();
unsigned long start = 0;
}
void loop() {
currentMillis = millis();
//1 Minute over
if (currentMillis - previousMillis >= MINUTE_PERIOD) {
previousMillis = currentMillis;
cpm = counts;
if (cpm >= tubeCpmOffset) {
cpm = cpm - tubeCpmOffset;
counts = 0;
} else {
cpm = 0;
counts = counts - tubeCpmOffset;
}
uSvh = cpm / conversionIndex;
Serial.println("CPM: " + String(cpm));
Serial.println("uSvh: " + String(uSvh));
sendGeigercounterData(cpm, uSvh);
//Update also BME280 data
updateBmeData();
updateDisplayValues();
} else if ((currentMillis - previousMillis) >= (MINUTE_PERIOD / 10)) {
updateDisplayValues();
}
cpm = counts;
}
/**
*
* Sub methods
*
**/
void ISR_impulse() { // Captures count of events from Geiger counter board
Serial.println("Click!");
Serial.println(millis());
Serial.println(currentMillis);
Serial.println(previousMillis);
counts++;
//updateDisplayValues();
}
//Connect to WiFi. Without WiFi no time
void connectWiFi() {
WiFi.mode(WIFI_STA);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
display.clear();
displayString(String(WiFi.status()), 64, 15);
delay(10);
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
display.clear();
displayString(WiFi.localIP().toString(), 64, 15);
}
void connectMqtt() {
mqttClient.setServer(mqttServer, 1883);
mqttClient.setKeepAlive(90);
if (mqttClient.connect(mqttClientName)) {
Serial.println("MQTT connected");
} else {
Serial.print("Error while connecting to MQTT");
}
}
void disconnectWiFi() {
Serial.println("Disconnect WiFi");
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void getNtpTime() {
Serial.println("Get NTP Time");
Serial.println(ntpServer);
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
void sendGeigercounterData(double cpmValue, double usvhValue) {
Serial.println("Start converting Geigercounter Data");
char cpmString[10];
sprintf(cpmString, "%lf", cpmValue);
char usvhString[10];
sprintf(usvhString, "%lf", usvhValue);
Serial.println("Start pushing Data");
Serial.println(usvhString);
if (!mqttClient.connected()) {
mqttClient.connect(mqttClientName);
}
mqttClient.publish(mqttTopicCpm, cpmString);
mqttClient.publish(mqttTopicuSvh, usvhString);
}
/**
* Update data read from the BME
*/
void updateBmeData() {
if(!bme.begin())
{
Serial.println("Could not find BME280 sensor!");
return;
}
BME280::TempUnit temperatureUnit(BME280::TempUnit_Celsius);
BME280::PresUnit pressureUnit(BME280::PresUnit_hPa);
bme.read(pressure, temperature, humidity, temperatureUnit, pressureUnit);
Serial.println(temperature);
Serial.println(pressure);
Serial.println(humidity);
sendBmeData(temperature, pressure, humidity);
}
void sendBmeData(float temperature, float pressure, float humidity) {
Serial.println("Start converting BME Data");
char temperatureString[20];
sprintf(temperatureString, "%lf", temperature);
char pressureString[20];
sprintf(pressureString, "%lf", pressure);
char humidityString[20];
sprintf(humidityString, "%lf", humidity);
Serial.println("Publish BME Data");
mqttClient.publish(mqttTopicTemperature, temperatureString);
mqttClient.publish(mqttTopicPressure, pressureString);
mqttClient.publish(mqttTopicHumidity, humidityString);
}
// Display stuff
void displayInit() {
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
}
void displayInt(int dispInt, int x, int y) {
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(x, y, String(dispInt));
display.setFont(ArialMT_Plain_10);
display.display();
}
void displayString(String dispString, int x, int y) {
display.setColor(WHITE);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(x, y, dispString);
display.setFont(ArialMT_Plain_24);
display.display();
}
void updateDisplayValues() {
display.clear();
display.setColor(WHITE);
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 0, "µSv/h");
display.drawString(40, 0, String(uSvh));
display.drawString(80, 0, String((int) roundf(cpm))+" CpM");
display.drawString(0, 15, "Temperatur");
display.drawString(80, 15, String(temperature)+" °C");
display.drawString(0, 30, "Luftfeuchtigkeit");
display.drawString(80, 30, String(humidity)+" %");
display.drawString(0, 45, "Luftdruck");
display.drawString(55, 45, String(pressure)+" hPa");
display.setFont(ArialMT_Plain_10);
display.display();
}