|
| 1 | +#include <SPI.h> |
| 2 | +// WiFi |
| 3 | +#include <WiFi.h> |
| 4 | + |
| 5 | +// NTP |
| 6 | +#include "time.h" |
| 7 | + |
| 8 | +//MQTT |
| 9 | +#include <PubSubClient.h> //MQTT |
| 10 | + |
| 11 | +//Display |
| 12 | +#include <SSD1306.h> //Display |
| 13 | + |
| 14 | +//BME280 |
| 15 | +#include <BME280I2C.h> |
| 16 | +#include <Wire.h> |
| 17 | + |
| 18 | + |
| 19 | +// WiFi Config |
| 20 | +const char *ssid = "******************"; |
| 21 | +const char *password = "******************"; |
| 22 | + |
| 23 | +// NTP Config |
| 24 | +const char *ntpServer = "pool.ntp.org"; |
| 25 | +const long gmtOffset_sec = 3600; |
| 26 | +const int daylightOffset_sec = 3600; |
| 27 | + |
| 28 | +// MQTT Config |
| 29 | +const char *mqttServer = "192.168.3.5"; |
| 30 | +const char *mqttClientName = "Geigercounter"; |
| 31 | +const char *mqttTopicCpm = "/backyard/geigercounter/cpm"; |
| 32 | +const char *mqttTopicuSvh = "/backyard/geigercounter/usvh"; |
| 33 | +const char *mqttTopicTemperature = "/backyard/geigercounter/temperature"; |
| 34 | +const char *mqttTopicPressure = "/backyard/geigercounter/pressure"; |
| 35 | +const char *mqttTopicHumidity = "/backyard/geigercounter/humidity"; |
| 36 | + |
| 37 | +// Geigercounter Config |
| 38 | +const int clickImputPin = 15; |
| 39 | +const float tubeCpmOffset = 7.2; //Correct Ghost counts create by the tube itself without radiation (M4011: 0,2/s = 12/min) |
| 40 | +const float conversionIndex = 153.8; //how many cpm are 1 uSv/h? |
| 41 | + |
| 42 | +volatile long counts = 0; // Tube events |
| 43 | +float cpm = 0; // CPM |
| 44 | +float uSvh = 0; // Micro Sievert per hour |
| 45 | +unsigned long previousMillis = 0; // Time measurement |
| 46 | +unsigned long currentMillis = millis(); |
| 47 | +unsigned long start = 0; |
| 48 | +#define MINUTE_PERIOD 60000 // One Minute in ms |
| 49 | + |
| 50 | +//BME280 Config (0x76) |
| 51 | +#define BME_SDA 5 //19 |
| 52 | +#define BME_SCL 4 //18 |
| 53 | +float temperature(NAN), humidity(NAN), pressure(NAN); |
| 54 | + |
| 55 | +BME280I2C bme; // Default : forced mode, standby time = 1000 ms |
| 56 | + // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off, |
| 57 | + |
| 58 | + |
| 59 | +WiFiClient espClient; |
| 60 | + |
| 61 | +PubSubClient mqttClient(espClient); |
| 62 | + |
| 63 | +SSD1306 display(0x3c, 5, 4); |
| 64 | + |
| 65 | +// Method definitions |
| 66 | +void connectWiFi(); |
| 67 | + |
| 68 | +void disconnectWiFi(); |
| 69 | + |
| 70 | +void connectMqtt(); |
| 71 | + |
| 72 | +void getNtpTime(); |
| 73 | + |
| 74 | +void ISR_impulse(); |
| 75 | + |
| 76 | +void sendGeigercounterData(double, double); |
| 77 | + |
| 78 | +void updateBmeData(); |
| 79 | + |
| 80 | +void sendBmeData(double, double, double); |
| 81 | + |
| 82 | +void updateDisplayValues(); |
| 83 | + |
| 84 | +void displayString(String dispString, int x, int y); |
| 85 | + |
| 86 | +void displayInt(int dispInt, int x, int y); |
| 87 | + |
| 88 | +void displayInit(); |
| 89 | + |
| 90 | +// initializes everything |
| 91 | +void setup() { |
| 92 | + Serial.begin(115200); |
| 93 | + Wire.begin(BME_SDA, BME_SCL); |
| 94 | + pinMode(clickImputPin, INPUT); // Set pin for capturing Tube events |
| 95 | + |
| 96 | + displayInit(); |
| 97 | + displayString("Welcome", 64, 15); |
| 98 | + |
| 99 | + connectWiFi(); // Connect WiFi |
| 100 | + |
| 101 | + getNtpTime(); // Get Time |
| 102 | + |
| 103 | + //Start MQTT |
| 104 | + connectMqtt(); |
| 105 | + |
| 106 | + //Start BME280 |
| 107 | + if (!bme.begin()) { |
| 108 | + Serial.println("Could not find a valid BME280 !"); |
| 109 | + displayString("No BME280!", 64, 15); |
| 110 | + } |
| 111 | + |
| 112 | + //Start counting |
| 113 | + interrupts(); // Enable interrupts // Enable interrupts |
| 114 | + attachInterrupt(digitalPinToInterrupt(clickImputPin), ISR_impulse, FALLING); // Define interrupt on falling edge |
| 115 | + unsigned long clock1 = millis(); |
| 116 | + start = clock1; |
| 117 | + |
| 118 | + unsigned long previousMillis = 0; // Time measurement |
| 119 | + unsigned long currentMillis = millis(); |
| 120 | + unsigned long start = 0; |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +void loop() { |
| 125 | + currentMillis = millis(); |
| 126 | + |
| 127 | + //1 Minute over |
| 128 | + if (currentMillis - previousMillis >= MINUTE_PERIOD) { |
| 129 | + previousMillis = currentMillis; |
| 130 | + cpm = counts; |
| 131 | + |
| 132 | + if (cpm >= tubeCpmOffset) { |
| 133 | + cpm = cpm - tubeCpmOffset; |
| 134 | + counts = 0; |
| 135 | + } else { |
| 136 | + cpm = 0; |
| 137 | + counts = counts - tubeCpmOffset; |
| 138 | + } |
| 139 | + uSvh = cpm / conversionIndex; |
| 140 | + |
| 141 | + |
| 142 | + Serial.println("CPM: " + String(cpm)); |
| 143 | + Serial.println("uSvh: " + String(uSvh)); |
| 144 | + |
| 145 | + sendGeigercounterData(cpm, uSvh); |
| 146 | + |
| 147 | + //Update also BME280 data |
| 148 | + updateBmeData(); |
| 149 | + |
| 150 | + updateDisplayValues(); |
| 151 | + } else if ((currentMillis - previousMillis) >= (MINUTE_PERIOD / 10)) { |
| 152 | + updateDisplayValues(); |
| 153 | + } |
| 154 | + cpm = counts; |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | +* |
| 159 | +* Sub methods |
| 160 | +* |
| 161 | +**/ |
| 162 | + |
| 163 | +void ISR_impulse() { // Captures count of events from Geiger counter board |
| 164 | + Serial.println("Click!"); |
| 165 | + Serial.println(millis()); |
| 166 | + Serial.println(currentMillis); |
| 167 | + Serial.println(previousMillis); |
| 168 | + counts++; |
| 169 | + |
| 170 | + //updateDisplayValues(); |
| 171 | + |
| 172 | +} |
| 173 | + |
| 174 | +//Connect to WiFi. Without WiFi no time |
| 175 | +void connectWiFi() { |
| 176 | + WiFi.mode(WIFI_STA); |
| 177 | + Serial.print("Connecting to "); |
| 178 | + Serial.println(ssid); |
| 179 | + WiFi.begin(ssid, password); |
| 180 | + while (WiFi.status() != WL_CONNECTED) { |
| 181 | + display.clear(); |
| 182 | + displayString(String(WiFi.status()), 64, 15); |
| 183 | + delay(10); |
| 184 | + } |
| 185 | + Serial.println(""); |
| 186 | + Serial.println("WiFi connected."); |
| 187 | + Serial.println("IP address: "); |
| 188 | + Serial.println(WiFi.localIP()); |
| 189 | + display.clear(); |
| 190 | + displayString(WiFi.localIP().toString(), 64, 15); |
| 191 | +} |
| 192 | + |
| 193 | +void connectMqtt() { |
| 194 | + mqttClient.setServer(mqttServer, 1883); |
| 195 | + mqttClient.setKeepAlive(90); |
| 196 | + if (mqttClient.connect(mqttClientName)) { |
| 197 | + Serial.println("MQTT connected"); |
| 198 | + } else { |
| 199 | + Serial.print("Error while connecting to MQTT"); |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +void disconnectWiFi() { |
| 204 | + Serial.println("Disconnect WiFi"); |
| 205 | + WiFi.disconnect(true); |
| 206 | + WiFi.mode(WIFI_OFF); |
| 207 | +} |
| 208 | + |
| 209 | +void getNtpTime() { |
| 210 | + Serial.println("Get NTP Time"); |
| 211 | + Serial.println(ntpServer); |
| 212 | + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); |
| 213 | +} |
| 214 | + |
| 215 | +void sendGeigercounterData(double cpmValue, double usvhValue) { |
| 216 | + Serial.println("Start converting Geigercounter Data"); |
| 217 | + |
| 218 | + char cpmString[10]; |
| 219 | + sprintf(cpmString, "%lf", cpmValue); |
| 220 | + |
| 221 | + char usvhString[10]; |
| 222 | + sprintf(usvhString, "%lf", usvhValue); |
| 223 | + |
| 224 | + Serial.println("Start pushing Data"); |
| 225 | + |
| 226 | + Serial.println(usvhString); |
| 227 | + |
| 228 | + if (!mqttClient.connected()) { |
| 229 | + mqttClient.connect(mqttClientName); |
| 230 | + } |
| 231 | + |
| 232 | + mqttClient.publish(mqttTopicCpm, cpmString); |
| 233 | + mqttClient.publish(mqttTopicuSvh, usvhString); |
| 234 | + |
| 235 | +} |
| 236 | + |
| 237 | +/** |
| 238 | + * Update data read from the BME |
| 239 | + */ |
| 240 | +void updateBmeData() { |
| 241 | + |
| 242 | + if(!bme.begin()) |
| 243 | + { |
| 244 | + Serial.println("Could not find BME280 sensor!"); |
| 245 | + return; |
| 246 | + } |
| 247 | + |
| 248 | + BME280::TempUnit temperatureUnit(BME280::TempUnit_Celsius); |
| 249 | + BME280::PresUnit pressureUnit(BME280::PresUnit_hPa); |
| 250 | + |
| 251 | + bme.read(pressure, temperature, humidity, temperatureUnit, pressureUnit); |
| 252 | + |
| 253 | + Serial.println(temperature); |
| 254 | + Serial.println(pressure); |
| 255 | + Serial.println(humidity); |
| 256 | + sendBmeData(temperature, pressure, humidity); |
| 257 | +} |
| 258 | + |
| 259 | +void sendBmeData(float temperature, float pressure, float humidity) { |
| 260 | + Serial.println("Start converting BME Data"); |
| 261 | + |
| 262 | + char temperatureString[20]; |
| 263 | + sprintf(temperatureString, "%lf", temperature); |
| 264 | + |
| 265 | + char pressureString[20]; |
| 266 | + sprintf(pressureString, "%lf", pressure); |
| 267 | + |
| 268 | + char humidityString[20]; |
| 269 | + sprintf(humidityString, "%lf", humidity); |
| 270 | + |
| 271 | + Serial.println("Publish BME Data"); |
| 272 | + mqttClient.publish(mqttTopicTemperature, temperatureString); |
| 273 | + mqttClient.publish(mqttTopicPressure, pressureString); |
| 274 | + mqttClient.publish(mqttTopicHumidity, humidityString); |
| 275 | +} |
| 276 | + |
| 277 | + |
| 278 | + |
| 279 | +// Display stuff |
| 280 | +void displayInit() { |
| 281 | + display.init(); |
| 282 | + display.flipScreenVertically(); |
| 283 | + display.setFont(ArialMT_Plain_10); |
| 284 | +} |
| 285 | + |
| 286 | +void displayInt(int dispInt, int x, int y) { |
| 287 | + display.setColor(WHITE); |
| 288 | + display.setTextAlignment(TEXT_ALIGN_CENTER); |
| 289 | + display.drawString(x, y, String(dispInt)); |
| 290 | + display.setFont(ArialMT_Plain_10); |
| 291 | + display.display(); |
| 292 | +} |
| 293 | + |
| 294 | +void displayString(String dispString, int x, int y) { |
| 295 | + display.setColor(WHITE); |
| 296 | + display.setTextAlignment(TEXT_ALIGN_CENTER); |
| 297 | + display.drawString(x, y, dispString); |
| 298 | + display.setFont(ArialMT_Plain_24); |
| 299 | + display.display(); |
| 300 | +} |
| 301 | + |
| 302 | + |
| 303 | +void updateDisplayValues() { |
| 304 | + display.clear(); |
| 305 | + display.setColor(WHITE); |
| 306 | + display.setFont(ArialMT_Plain_10); |
| 307 | + display.setTextAlignment(TEXT_ALIGN_LEFT); |
| 308 | + |
| 309 | + display.drawString(0, 0, "µSv/h"); |
| 310 | + display.drawString(40, 0, String(uSvh)); |
| 311 | + display.drawString(80, 0, String((int) roundf(cpm))+" CpM"); |
| 312 | + |
| 313 | + display.drawString(0, 15, "Temperatur"); |
| 314 | + display.drawString(80, 15, String(temperature)+" °C"); |
| 315 | + |
| 316 | + display.drawString(0, 30, "Luftfeuchtigkeit"); |
| 317 | + display.drawString(80, 30, String(humidity)+" %"); |
| 318 | + |
| 319 | + display.drawString(0, 45, "Luftdruck"); |
| 320 | + display.drawString(55, 45, String(pressure)+" hPa"); |
| 321 | + |
| 322 | + |
| 323 | + |
| 324 | + |
| 325 | + display.setFont(ArialMT_Plain_10); |
| 326 | + display.display(); |
| 327 | + |
| 328 | +} |
0 commit comments