Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- // if testing over 1000 cpm turn off debugging or the serial buffer will overload
- boolean debugmode = true;
- const char* ssid = "";
- const char* password = "";
- const char* mqtt_server = "";
- const int interruptPin = D2; // ~D2 // connected to + LED
- int totalHitCount = 0;
- int currentHitCount = 0;
- int lastMinCount = 0;
- double startMillis;
- double seconds;
- double minutes;
- double CPM = 0.0;
- double uSv = 0.0;
- double lastcSv = 0.0;
- int currentTimeSlot = 0;
- int eventsArray[60];
- int WiFiStrength = 0;
- int minHolder = 0;
- int minuteHits = 0;
- char msg[512];
- boolean WiFiConnected = true;
- const unsigned long REFRESH_INTERVAL = 10000; // ms
- double lastRefreshTime = 0;
- const unsigned long LAST_MINUTE_COUNT_INTERVAL = 60000; // ms
- double lastMinuteCountTime = 0;
- const unsigned long EVERY_SECOND_INTERVAL = 1000; // ms
- double everySecondCountTime = 0;
- WiFiServer server(80);
- WiFiClient espClient;
- PubSubClient client(espClient);
- //////////////////////////////////////////////////////////////////////////////////////
- // this function executes everytime there is hit to the Geiger counter ///
- //////////////////////////////////////////////////////////////////////////////////////
- void pinChanged()
- {
- detachInterrupt(interruptPin);
- seconds = (millis() - startMillis) / 1000;
- minutes = seconds / 60;
- totalHitCount++;
- currentHitCount++;
- minuteHits++;
- // increase the event count by 1 for the current time slot
- eventsArray[currentTimeSlot]++;
- // sum up all the event counts in our eventsArray to get a running sum
- CPM = 0;
- for (int i = 0; i < 60; i++) {
- CPM += eventsArray[i];
- }
- uSv = CPM * 0.0057;
- if (debugmode) {
- // Good for debugging
- Serial.print(" Total hit Count: "); Serial.println(totalHitCount);
- Serial.print(" Current hit Count: "); Serial.println(currentHitCount);
- Serial.print(" Last minute count: "); Serial.println(lastMinCount);
- Serial.print(" Time seconds: "); Serial.println(seconds);
- Serial.print(" Time minutes: "); Serial.println(minutes);
- Serial.print(" CPM: "); Serial.println(CPM);
- Serial.print(" uSv/hr: "); Serial.println(uSv);
- Serial.print(" Slot: "); Serial.println((millis() / 1000) % 60);
- Serial.println(" ");
- }
- attachInterrupt(interruptPin, pinChanged, RISING);
- }
- void sendStats(int totalHitCounter, int upseconds, int upminutes, double cpm, double sv) {
- sprintf(msg, "{\"totalHitCounter\": %d, \"upseconds\": %d, \"upminutes\": %d, \"cpm\": %0.2f, \"sv\": %0.2f}", (int)totalHitCounter, (int)(upseconds), (int)upminutes, (double)cpm, (double)sv);
- Serial.print("Publish message: ");
- Serial.println(msg);
- client.publish("home/sensors/geiger/input", msg);
- }
- //////////////////////////////////////////////////////////////////
- // function used to reset variables: used for data clearing ///
- // session reset ///
- //////////////////////////////////////////////////////////////////
- void resetVariables()
- {
- //reset varialbles
- totalHitCount = 0;
- startMillis = millis();
- seconds = 0.0;
- minutes = 0.0;
- CPM = 0.0;
- uSv = 0.0;
- minHolder = 0;
- minuteHits = 0;
- lastcSv = 0;
- //
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Create a random client ID
- String clientId = "ESP8266Client-";
- clientId += String(random(0xffff), HEX);
- // Attempt to connect
- if (client.connect(clientId.c_str())) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- client.publish("home/sensors/geiger/input", "geiger meter connected");
- // ... and resubscribe
- client.subscribe("home/sensors/geiger/output");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(1000);
- }
- }
- }
- void callback(char* topic, byte* payload, int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- }
- //////////////////////////////////
- // main setup function ///
- //////////////////////////////////
- void setup() {
- pinMode(interruptPin, INPUT);
- startMillis = millis();
- attachInterrupt(interruptPin, pinChanged, RISING); // start counting Geiger hits
- if (debugmode)
- {
- Serial.begin(115200);
- delay(10);
- }
- // Connect to WiFi network
- if (debugmode)
- {
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- }
- WiFi.begin(ssid, password);
- // Set the ip address of the webserver
- // WiFi.config(WebServerIP, Gatway, Subnet)
- // or comment out the line below and DHCP will be used to obtain an IP address
- // which will be displayed via the serial console
- //WiFi.config(IPAddress(192, 168, 1, 220), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
- // connect to WiFi router
- int waitCount = 0;
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- if (debugmode)
- Serial.print(".");
- waitCount++;
- if (waitCount > 30)
- {
- WiFiConnected = false;
- break;
- }
- }
- if (WiFiConnected) {
- if (debugmode) {
- Serial.println("");
- Serial.println("WiFi connected");
- }
- // Start the server
- server.begin();
- if (debugmode) {
- Serial.println("Server started");
- // Print the IP address
- Serial.print("Use this URL to connect: ");
- Serial.print("http://");
- Serial.print(WiFi.localIP());
- Serial.println("/");
- }
- } else {
- if (debugmode)
- Serial.println("WiFi NOT connected");
- }
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- // clean up eventsArray
- for (int i = 0; i < 60; i++) {
- eventsArray[i] = 0;
- }
- }
- ////////////////////////////////////
- // main loop function ///
- ////////////////////////////////////
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- if((millis() - lastRefreshTime) > REFRESH_INTERVAL) {
- lastRefreshTime = millis();
- sendStats(totalHitCount, seconds, minutes, CPM, uSv);
- }
- // hold the event count value every minune
- if((millis() - lastMinuteCountTime) > LAST_MINUTE_COUNT_INTERVAL) {
- lastMinuteCountTime = millis();
- lastMinCount = currentHitCount;
- currentHitCount = 0;
- }
- // run every second
- if((millis() - everySecondCountTime) > EVERY_SECOND_INTERVAL) {
- everySecondCountTime = millis();
- currentTimeSlot = (int)(everySecondCountTime / 1000) % 60;
- eventsArray[currentTimeSlot] = 0;
- /*
- for (int i = 0; i < 60; i++) {
- Serial.print(eventsArray[i]); Serial.print(", ");
- }
- Serial.println();
- */
- }
- // check to for any web server requests. ie - browser requesting a page from the webserver
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- // Wait until the client sends some data
- WiFiStrength = WiFi.RSSI();
- if (debugmode) {
- Serial.println("new client");
- }
- /*
- double analogValue = analogRead(A0); // read the analog signal
- // convert the analog signal to voltage
- // the ESP2866 A0 reads between 0 and ~3 volts, producing a corresponding value
- // between 0 and 1024. The equation below will convert the value to a voltage value.
- double analogVolts = (analogValue * 3.12) / 1024;
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement