Home Forum Software How to upload to uRadmonitor from (Arduino) ESP32? Reply To: How to upload to uRadmonitor from (Arduino) ESP32?

#28844
DonZalmrol
Participant

I use WifiServer instead of WebServer which means that the original code from the opensource version uploads it data to uRadmonitor on a different manner then what I’m currently using to do a HTTPS using the WifiServer options.

Below is my current code which works for uploading it to another platform (Radmon) at this moment.
Would love to also getting it to work to upload it uRadmonitor to provide the other sensors that I can monitor already through my build in webserver page.

The json query will take a bit of work but I’m hoping I will be able to do that on my own. My main concern now is how to do the upload to uRadmonitor.

Thx!

#include <Arduino.h>
#include <heltec.h>
#include <WiFi.h>
#include <movingAvg.h>
#include <esp_task_wdt.h>
#include <Preferences.h>
#include <DHTesp.h>
#include <ArduinoJson.h>

Preferences preferences;
DHTesp dht;

#define WDT_TIMEOUT 15 //15 seconds WDT

// Tube dead time in seconds
// SI-22G = 0.000xxx seconds
// SBM-20 = 0.000190 seconds
#define tubeDeadTime 0.000190

// define Radiation DETECTORS
#define GEIGER_TUBE_SI22G 0x6

const char *ssid = "YourSSIDNameHere";
const char *password = "YourSSIDPaswordHere";
const long timeoutTime = 5000;
WiFiServer server(80);

// be.pool.ntp.org

unsigned long currentMillis = 0;
unsigned long previousMillis_1 = 0, previousMillis_2 = 0, previousMillis_3 = 0, previousMillis_4 = 0;
unsigned long ONE_WEEK = 7ul * 24ul * 60ul * 60ul * 1000ul;

unsigned long cps_1 = 0, cps_2 = 0;
unsigned long cpm_1 = 0, cpm_2 = 0;
unsigned long actual_cps_1 = 0, actual_cps_2 = 0;
unsigned long actual_cpm_1 = 0, actual_cpm_2 = 0;
unsigned long totalCount_3 = 0;

int sensorMovingAvg_3 = 0;
int tenSecCount = 0;
int increaseSecCount = 0;
int retry = 0;

unsigned long weekCPMCtr = 0, totCPMCtr = 0, prevcpm_1 = 0, prevcpm_2 = 0, prevACPM = 0, prevMACPM = 0;
float currentTemp = 0.0, currentHumidity = 0.0;

// Set sensor to 120 data points for moving average (2x 60 second data entry points)
movingAvg sensor_3(120);

boolean event1 = false, event2 = false;
boolean uploadOk = false;
boolean buttonShowLCD = false;

// RADMON credentials
const char *UserName = "username";
const char *DataSendingPassWord = "password";

void setup()
{
  // True = Display, False = LoRa, True = Serial
  Heltec.begin(true, false, true);
  Heltec.display->flipScreenVertically();
  Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
  Heltec.display->setFont(ArialMT_Plain_10);
  Heltec.display->clear();
  Heltec.display->drawString(0, 0, "GM SI-22G");
  Heltec.display->drawString(0, 10, "Stationary Logger");
  Heltec.display->drawString(0, 20, "V5");
  Heltec.display->drawString(0, 40, "By Don Zalmrol");
  Heltec.display->display();
  delay(3000);
  Heltec.display->clear();

  // Connect to the WiFi
  WiFiSetup();

  delay(10);

  // Initialize variables
  cps_1 = 0, cps_2 = 0;
  cpm_1 = 0, cpm_2 = 0;
  actual_cps_1 = 0, actual_cps_2 = 0;
  sensorMovingAvg_3 = 0;
  tenSecCount = 0;
  increaseSecCount = 0;

  // Initialize sensor
  sensor_3.begin();

  // Initialize Arduino pins
  pinMode(0, INPUT_PULLUP);   // Set pin0 (GPIO0) input for button (PCB and external)
  pinMode(25, OUTPUT);        // Set pin25 (GPIO25) as output for LED upload = OK
  pinMode(34, INPUT_PULLUP);  // Set pin12 (GPIO12) input for capturing GM Tube events
  pinMode(35, INPUT_PULLUP);  // Set pin14 (GPIO14) input for capturing GM Tube events
  pinMode(21, OUTPUT);        // Set pin21 (GPIO21) as output for OLED
  pinMode(26, OUTPUT);        // Set pin04 (GPIO26) as output for LED
  
  // Allow external interrupts on INT0 and INT1
  attachInterrupt(digitalPinToInterrupt(34), tube_impulse1, FALLING); //define external interrupts
  attachInterrupt(digitalPinToInterrupt(35), tube_impulse2, FALLING); //define external interrupts

  // Set pin13 (GPIO13) as input for DHT sensor
  dht.setup(13, DHTesp::DHT11);

  Serial.println(F("--- Start program ---"));
  Serial.println(F("\n"));
  
  // Initialize UART on 115200 baud rate for ethernet shield
  Serial.begin(115200);
  delay(1000);

  // Initialize watchdog
  Serial.println(F("\n"));
  Serial.println(F("Initializing WatchDogTimer (WDT)."));
  Serial.println(F("\n"));
  esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
  esp_task_wdt_add(NULL); //add current thread to WDT watch

  // Open Preferences with my-app namespace. Each application module, library, etc
  // has to use a namespace name to prevent key name collisions. We will open storage in
  // RW-mode (second parameter has to be false).
  // Note: Namespace name is limited to 15 chars.
  //preferences.begin("GM-SI-22G", false);

  // Remove all preferences under the opened namespace
  //preferences.clear();

  // Or remove the counter key only
  //preferences.remove("weekCPMCtr");

  // Close the Preferences
  //preferences.end();
}

void loop()
{
  newclient();

  currentMillis = millis();

  // if button is pressed set LCD bool to true
  // turn on LCD
  if((digitalRead(0)== LOW) && (buttonShowLCD == false))
  {
    buttonShowLCD = true;
    Serial.println("button pushed = " + String(digitalRead(0)));
  }

  // turn off LCD
  if(buttonShowLCD == false)
  {
    Heltec.display->clear();
    Heltec.display->setContrast(0);
    Heltec.display->display();
  }

  if ((event1 == true) || (event2 == true))
  {
    // 1 blink for 50 ms
    blinkLed(1, 50);
    
    event1 = false, event2 = false;
  }

  // If reached 01 seconds
  if (currentMillis - previousMillis_1 > 1000)
  {
    previousMillis_1 = currentMillis;
    increaseSecCount++;

    // Recalute with tube dead time accounted for actual CPS
    actual_cps_1 = (cps_1 / (1- (cps_1 * tubeDeadTime)));
    actual_cps_2 = (cps_2 / (1- (cps_2 * tubeDeadTime)));

    actual_cpm_1 = (cpm_1 / (1- (cpm_1 * tubeDeadTime)));
    actual_cpm_2 = (cpm_2 / (1- (cpm_2 * tubeDeadTime)));

    // Add a new datapoint to the moving average array
    // Add both tubes sensors to the combination
    sensorMovingAvg_3 = sensor_3.reading(actual_cps_1);
    sensorMovingAvg_3 = sensor_3.reading(actual_cps_2);

    Serial.println(F("--- 01 sec ---"));
    Serial.println("| Tube 1 " + String(increaseSecCount) + " sec current count (cps_1) = " + String(actual_cps_1));
    Serial.println("| Tube 2 " + String(increaseSecCount) + " sec current count (cps_2) = " + String(actual_cps_2));
    Serial.println("| Tubes current total mavg " + String(increaseSecCount) + " sec current count = " + String(sensorMovingAvg_3));
    Serial.println(F("--- 01 sec ---"));

    // Print to LCD
    if(buttonShowLCD)
    {
      Heltec.display->clear();
      Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
      Heltec.display->drawString(0, 0, "Current sec. = " + String(increaseSecCount) + " of 60");
      Heltec.display->drawString(0, 10, "CPS 1 = " + String(actual_cps_1) + " | CPS 2 = " + String(actual_cps_2));
      Heltec.display->drawString(0, 20, "MA-CPS = " + String(sensorMovingAvg_3));
      Heltec.display->drawString(0, 30, "Current uSv = " + String(outputSieverts(sensorMovingAvg_3)));
      Heltec.display->drawString(0, 40, "Tubes voltage = " + String(displayTubeVoltage()) + " V");
      Heltec.display->drawString(0, 50, "Last Upload = " + String(uploadOk) + " | retry = " + String(retry));
      Heltec.display->display();
    }
  }

  // If reached 10 seconds
  if (currentMillis - previousMillis_2 > 10000)
  {
    previousMillis_2 = currentMillis;
    tenSecCount++;

    // Print out for closing the 01 sec group
    Serial.println(F("--- 01 sec ---"));

    // Print new line
    Serial.println(F("\n"));

    Serial.println(F("--- 10 sec ---"));
    Serial.println("| count " + String(tenSecCount) + " of 6 x 10 seconds");
    Serial.println("| Tubes voltage = " + String(displayTubeVoltage()) + " V");
    Serial.println("| Resetting WDT");
    Serial.println(F("--- 10 sec ---"));

    // Print new line
    Serial.println(F("\n"));

    // Reset WatchDogTimer
    esp_task_wdt_reset();
  }

  // If reached 60 seconds
  if (currentMillis - previousMillis_3 > 60000)
  {
    previousMillis_3 = currentMillis;

    totalCount_3 = sensor_3.getAvg();

    // Print new line
    Serial.println(F("\n"));

    Serial.println(F("--- 60 sec ---"));
    Serial.println("| Tube 1 current 60 sec total count = " + String(actual_cpm_1) + " cpm_1");
    Serial.println("| Tube 2 current 60 sec total counts = " + String(actual_cpm_2) + " cpm_2");
    Serial.println("| Total current 60 sec mavg count = " + String(totalCount_3) + " counts");
    Serial.println("| uSV = " + String(outputSieverts(totalCount_3)) + " uSV");
    Serial.println("| Temperature = " + String(dht.getTemperature()) + "°C");
    Serial.println("| Humidity = " + String(dht.getHumidity()) + "%");
    Serial.println(F("--- 60 sec ---"));

    // Print new line
    Serial.println(F("\n"));

    // Print to LCD
    if(buttonShowLCD)
    {
      Heltec.display->clear();
      Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
      Heltec.display->drawString(0, 0, "Current second = " + String(increaseSecCount));
      Heltec.display->drawString(0, 10, "CPS 1 = " + String(actual_cps_1) + " | CPS 2 = " + String(actual_cps_2));
      Heltec.display->drawString(0, 20, "MA-CPS total = " + String(totalCount_3));
      Heltec.display->drawString(0, 30, "Current uSv = " + String(outputSieverts(totalCount_3)));
      Heltec.display->drawString(0, 40, "Tubes voltage = " + String(displayTubeVoltage()) + " V");
      Heltec.display->drawString(0, 50, "Last upload = " + String(uploadOk) + " | retry = " + String(retry));
      Heltec.display->display();
    }

    Serial.println(F("\n"));

    // Store vars into Flash memory of ESP32
    // Minute CPM tube 1, Minute CPM tube 2, total count
    storeInFlash(actual_cpm_1, actual_cpm_2, totalCount_3);

    // Connect to RadMon.org
    connecToRadMonLogger();
    
    // Reset variables 
    cps_1 = 0, cps_2 = 0;
    cpm_1 = 0, cpm_2 = 0;
    actual_cps_1 = 0, actual_cps_2 = 0;
    actual_cpm_1 = 0, actual_cpm_2 = 0;
    totalCount_3 = 0;
    sensorMovingAvg_3 = 0;
    
    tenSecCount = 0;
    increaseSecCount = 0;
    
    sensor_3.reset();
    buttonShowLCD = false;

    // Print new line
    Serial.println(F("\n"));
  }

  // If one week uptime has been reached, reboot device for maintenance.
  if (currentMillis - previousMillis_4 >= ONE_WEEK)
  {
    previousMillis_4 = currentMillis;
    Serial.println(F("Weekly reboot"));

    // Clear from Flash Memory
    clearFromFlash();
    
    // Restart ESP32
    ESP.restart();
  }
}

//------------------------//
//--- START functions ---//
//----------------------//
void WiFiSetup()
{
  Serial.println(F("\n"));
  Serial.println("Connecting to = " + String(ssid));
  
  WiFi.begin(ssid, password);
  
  // Set up Wifi connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  if(WiFi.status() == WL_CONNECTED)
  {
    Serial.println(F("\n"));
    Serial.println(F("WiFi connected"));
    Serial.println("IP address = " +  String(WiFi.localIP().toString()));
    Serial.println("Gateway = " + String(WiFi.gatewayIP().toString()));
    Serial.println("SubnetMask = " + String(WiFi.subnetMask().toString()));
    Serial.println("DNS 1 = " + String(WiFi.dnsIP(0).toString()));
    Serial.println("DNS 2 = " + String(WiFi.dnsIP(1).toString()));
    Serial.println("MAC = " + String(WiFi.macAddress()));
    server.begin();
    Serial.println(F("HTTP server started"));
    Serial.println(F("\n"));
  }

  // Wifi is not connected
  else
  {
      Serial.println(F("WiFi NOT connected"));
      Serial.println(F("HTTP server NOT started"));
      Serial.println(F("\n"));
      WiFiSetup(); // Try again
  }
}

// Start webserver client
void newclient()
{
  WiFiClient client = server.available();   // listen for incoming clients
  
  // if you get a client connection
  if (client)
  {
    Serial.println(F("New remote client connection."));
    
    while (client.connected() && currentMillis - previousMillis_1 <= timeoutTime)
    {
      currentMillis = millis();

      String uploadOkStatus = "Failed";
      if(uploadOk == 1)
      {
        uploadOkStatus = "Succeeded";
      }
      else
      {
        uploadOkStatus = "Failed";
      }

      // Get vars from Flash memory of ESP32
      readFromFlash();

      String color = "#00FF00";
      if(outputSieverts(weekCPMCtr) <= 1000)
      {
        color = "#00FF00";
      }
      else if(outputSieverts(weekCPMCtr) >= 1001 & outputSieverts(weekCPMCtr) <= 3000)
      {
        color = "#FFFF00";
      }
      else
      {
        color = "#FF0000";
      }

      String ptr = "HTTP/1.1 200 OK;\n";
      ptr += "Content-type:text/html\n";
      ptr += "\n";
      ptr += "<!DOCTYPE html lang='en'>\n";
      ptr += "<html>\n";
      ptr += "<head>\n";
      ptr += "<title>GM SI-22G : Stationary Logger</title>\n";
      ptr += "<style>\n";
      ptr += "table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; }\n";
      ptr += "td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; }\n";
      ptr += "tr:nth-child(even) { background-color: #dddddd; }\n";
      ptr += "</style>\n";
      ptr += "</head>\n"; 
      ptr += "<body>\n";
      
      ptr += "<center><h2>GM SI-22G : Stationary Logger</h2></center>\n";

      ptr += "<table>\n";
      ptr += "<caption>60 seconds results</caption>\n";
      ptr += "<tr>\n";
      ptr += "<th>Description</th>\n";
      ptr += "<th>Value</th>\n";
      ptr += "<th>Symbol</th>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Counts Per Second Tube 1</td>\n";
      ptr += "<td>" + String(actual_cps_1) + "</td>\n";
      ptr += "<td>CPS</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Counts Per Second Tube 2</td>\n";
      ptr += "<td>" + String(actual_cps_2) + "</td>\n";
      ptr += "<td>CPS</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Moving Average Counts Per Second Tubes 1+2</td>\n";
      ptr += "<td>" + String(sensorMovingAvg_3) + "</td>\n";
      ptr += "<td>maCPS</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>&micro;Sieverts</td>\n";
      ptr += "<td>" + String(outputSieverts(sensorMovingAvg_3)) + "</td>\n";
      ptr += "<td>&micro;SV ☢</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Tubes High Voltage</td>\n";
      ptr += "<td>" + String(displayTubeVoltage()) + "</td>\n";
      ptr += "<td>V</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Temperature</td>\n";
      ptr += "<td>" + String(dht.getTemperature()) + "</td>\n";
      ptr += "<td>℃</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Humidity</td>\n";
      ptr += "<td>" + String(dht.getHumidity()) + "</td>\n";
      ptr += "<td>%</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td colspan='3'><center>Current second <b>" + String(increaseSecCount) + "</b> of <b>60</b> results</center></td>\n";
      ptr += "</tr>\n";
      ptr += "</table>\n";

      ptr += "<br />&nbsp;\n";

      ptr += "<table>\n";
      ptr += "<caption>Previous minute results</caption>\n";
      ptr += "<tr>\n";
      ptr += "<th>Description</th>\n";
      ptr += "<th>Value</th>\n";
      ptr += "<th>Symbol</th>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Counts Per Minute Tube 1</td>\n";
      ptr += "<td>" + String(prevcpm_1) + "</td>\n";
      ptr += "<td>CPM</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Counts Per Minute Tube 2</td>\n";
      ptr += "<td>" + String(prevcpm_2) + "</td>\n";
      ptr += "<td>CPM</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Moving Average Counts Per Minute Tubes 1+2</td>\n";
      ptr += "<td>" + String(prevMACPM) + "</td>\n";
      ptr += "<td>maCPM</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>&micro;Sieverts</td>\n";
      ptr += "<td>" + String(outputSieverts(prevMACPM)) + "</td>\n";
      ptr += "<td>&micro;SV ☢</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Last upload</td>\n";
      ptr += "<td>" + String(uploadOkStatus) + "</td>\n";
      ptr += "<td>Succeeded or Failed</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Retry count</td>\n";
      ptr += "<td>" + String(retry) + "</td>\n";
      ptr += "<td>of 3</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td colspan='3'><center>Previous minute results</center></td>\n";
      ptr += "</tr>\n";
      ptr += "</table>\n";

      ptr += "<br />&nbsp;\n";

      ptr += "<table>\n";
      ptr += "<caption>Week results</caption>\n";
      ptr += "<tr>\n";
      ptr += "<th>Description</th>\n";
      ptr += "<th>Value</th>\n";
      ptr += "<th>Symbol</th>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Total Counts this week</td>\n";
      ptr += "<td>" + String(weekCPMCtr) + "</td>\n";
      ptr += "<td>CPM</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Total &micro;Sieverts max dosage this week</td>\n";
      ptr += "<td><progress value=" + String(outputSieverts(weekCPMCtr)) + " max='5000'>" + String(outputSieverts(weekCPMCtr)) + "</progress></td>\n";
      ptr += "<td>&micro;SV ☢</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td style='background-color:" + String(color) + "'>Total &micro;Sieverts max this week</td>\n";
      ptr += "<td style='background-color:" + String(color) + "'>" + String(outputSieverts(weekCPMCtr)) + "</td>\n";
      ptr += "<td style='background-color:" + String(color) + "'>Of max 5000 &micro;SV (5 mSV) ☢</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td colspan='3'><center><b>7 day results</b></center></td>\n";
      ptr += "</tr>\n";
      ptr += "</table>\n";

      ptr += "<br />&nbsp;\n";

      ptr += "<table>\n";
      ptr += "<caption>Total results since start of counting on 2021-05-01</caption>\n";
      ptr += "<tr>\n";
      ptr += "<th>Description</th>\n";
      ptr += "<th>Value</th>\n";
      ptr += "<th>Symbol</th>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Total Counts</td>\n";
      ptr += "<td>" + String(totCPMCtr) + "</td>\n";
      ptr += "<td>CPM</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td>Total &micro;Sieverts</td>\n";
      ptr += "<td>" + String(outputSieverts(totCPMCtr)) + "</td>\n";
      ptr += "<td>&micro;SV ☢</td>\n";
      ptr += "</tr>\n";
      ptr += "<tr>\n";
      ptr += "<td colspan='3'><center><b>Total counting results</b></center></td>\n";
      ptr += "</tr>\n";
      ptr += "</table>\n";

      ptr += "</body>\n";
      ptr += "</html>\n";
      ptr += "\n";

      // Print out pointer (ptr)
      client.print(ptr);

      // Flush connection
      client.flush(); 

      // break out of the while loop:
      break;
    }
    client.flush(); // Flush connection again
    client.stop(); // Stop client
    delay(5);// Give some time to stop
    Serial.println(F("Remote client disconnected."));
  }
}

// GM Tube fastpulse 01 counting
void tube_impulse1()
{
  cps_1++;
  cpm_1++;
  event1 = true;
}

// GM Tube fastpulse 02 counting
void tube_impulse2()
{
  cps_2++;
  cpm_2++;
  event2 = true;
}

// Convert cpm_1 to uSV
float outputSieverts(float x) 
{
  // 0.001714 value for SI-22G
  float y = x * 0.001714;
  return y;
}

// Measure Tube voltage through A0
float displayTubeVoltage()
{
  // read the input on analog pin 27:
  float adcInput = analogRead(27);

  // Convert the analog reading (12bit resolution which goes from 0 - 4095) to a voltage (0 - 3.3V):
  // actual lowVoltage = ( adcInput x 3.3V ) / 4095.0 Resolution
  float lowVoltage = ((adcInput * 3.3) / 4095.0);

  // Rule of 3 to calculate approx HV
  //float highVoltage = ((lowVoltage * 400) / 2);
  
  // ~190-195 conversion factor according to Alex - RH Electronics 2021-03-15
  // ~171 calculated on 2021-04-01 for Arduino
  // 123.15 calcultated on 2021-05-04 for ESP32
  float highVoltage = (lowVoltage * 123.15);

  // Print out voltage
  //Serial.println("| Tubes HV = " + String(highVoltage) + " Volts, A0 LV = " + String(lowVoltage));

  return highVoltage;
}

// Blink the LED (blinks, time)
void blinkLed(int blinks, int time)
{                        
    for (int i = 0; i < blinks; i++)
    {
      digitalWrite(26, HIGH);
      delay(time);
      digitalWrite(26, LOW);
      delay(time);
    }
}

// Blink the LED (blinks, time)
void blinkLedUpload(int blinks, int time)
{                        
    for (int i = 0; i < blinks; i++)
    {
      digitalWrite(25, HIGH);
      delay(time);
      digitalWrite(25, LOW);
      delay(time);
    }
}

// Read from Flash Memory
void readFromFlash()
{
  // Open namespace with RW-mode enabled (false)
  preferences.begin("GM-SI-22G", false);

  // Get the counter value, if the key does not exist, return a default value of 0
  // Note: Key name is limited to 15 chars.
  weekCPMCtr = preferences.getULong("weekCPMCtr", 0);
  totCPMCtr = preferences.getULong("totCPMCtr", 0);
  
  prevcpm_1 = preferences.getULong("prevcpm_1", 0);
  prevcpm_2 = preferences.getULong("prevcpm_2", 0);
  prevMACPM = preferences.getULong("prevMACPM", 0);
  
  // Close the Preferences
  preferences.end();
}

// Store into Flash Memory
// void storeInFlash(unsigned long totalCountVar)
void storeInFlash(unsigned long actual_cpm_1, unsigned long actual_cpm_2, unsigned long totalCount_3)
{
  // Open namespace with RW-mode enabled (false)
  preferences.begin("GM-SI-22G", false);

  // Increment weekly counts and usv with 60 second and store data
  weekCPMCtr = preferences.getULong("weekCPMCtr", 0) + totalCount_3;
  preferences.putULong("weekCPMCtr", weekCPMCtr);

  // Increment total counts and usv with 60 second and store data
  totCPMCtr = preferences.getULong("totCPMCtr", 0) + totalCount_3;
  preferences.putULong("totCPMCtr", totCPMCtr);

  // Store the counters to the Preferences
  preferences.putULong("prevcpm_1", actual_cpm_1);
  preferences.putULong("prevcpm_2", actual_cpm_2);
  preferences.putULong("prevMACPM", totalCount_3);

  // Close the Preferences
  preferences.end();
}

// Clear from the Flash Memory
void clearFromFlash()
{
  // Open namespace with RW-mode enabled (false)
  preferences.begin("GM-SI-22G", false);

  // Remove all preferences under the opened namespace
  //preferences.clear();

  // Or remove the counter key only
  preferences.remove("weekCPMCtr");

  // Close the Preferences
  preferences.end();
}

// Upload totalCount_3 to the RadMon.org server
void connecToRadMonLogger()
{
  WiFiClient client;

  if(retry < 3)
  {
    //if (client.connect(radmonServerIP, radmonServerPort))
    if (client.connect("radmon.org", 80))
    {
      if(client.connected())
      {
        Serial.println(F("Connection to radmon monitoring platform succeeded!"));

        client.print(F("GET /radmon.php?function=submit&user="));
        client.print(UserName);
        client.print(F("&password="));
        client.print(DataSendingPassWord);
        client.print(F("&value="));
        client.print(totalCount_3);
        client.print(F("&unit=CPM"));
        client.println(F("HTTP/1.0"));
        client.println(F("HOST: radmon.org"));
        //client.println();
        client.println(F(""));

        Serial.println("Uploaded cpm = " + String(totalCount_3));
        
        uploadOk = true;
        retry = 0;
        // 5 blinks for 50 ms
        blinkLedUpload(5, 50);
        
        client.flush(); // Flush connection
        client.stop(); // Stop client
        delay(5);// Give some time to stop
        Serial.println(F("Connection to radmon monitoring platform Disconnected."));
      }

      else
      {
        // you didn't get a connection to the Radmon server:
        Serial.println(F("Connection to radmon monitoring platform failed!"));

        client.flush(); // Flush connection
        client.stop(); // Stop client
        delay(1);// Give some time to stop
        
        uploadOk = false;
        retry++;
      }
    }
    else
    {
      // you didn't get a connection to the Radmon server:
      Serial.println(F("Connection to radmon monitoring platform failed!"));

      client.flush(); // Flush connection
      client.stop(); // Stop client
      delay(1);// Give some time to stop
      
      uploadOk = false;
      retry++;
    }
  }
  else
  {
    Serial.println("Reboot");
    ESP.restart();
  }
}

// Upload totalCount_3 to the uradmonitor.com server
void connecToURadMonLogger()
{
  WiFiClient client;

  if(retry < 3)
  {
    if (client.connect("data.uradmonitor.com", 443))
    {
      if(client.connected())
      {
        Serial.println(F("Connection to urad monitoring platform succeeded!"));

        

        // Below does not work
        String ptr = "GET /data.uradmonitor.com/api/v1/upload/exp/01/";
        ptr += increaseSecCount;
        ptr += totalCount_3;
        ptr += GEIGER_TUBE_SI22G;
        ptr += "\n";

        // Print out pointer (ptr)
        client.print(ptr);

        Serial.println("Uploaded cpm  = " + String(totalCount_3));
        
        uploadOk = true;
        retry = 0;
        // 5 blinks for 50 ms
        blinkLedUpload(5, 50);
        
        client.flush(); // Flush connection
        client.stop(); // Stop client
        delay(5);// Give some time to stop
        Serial.println(F("Connection to urad monitoring platform Disconnected."));
      }

      else
      {
        // you didn't get a connection to the Radmon server:
        Serial.println(F("Connection to urad monitoring platform failed!"));

        client.flush(); // Flush connection
        client.stop(); // Stop client
        delay(1);// Give some time to stop
        
        uploadOk = false;
        retry++;
      }
    }
    else
    {
      // you didn't get a connection to the Radmon server:
      Serial.println(F("Connection to urad monitoring platform failed!"));

      client.flush(); // Flush connection
      client.stop(); // Stop client
      delay(1);// Give some time to stop
      
      uploadOk = false;
      retry++;
    }
  }
  else
  {
    Serial.println("Reboot");
    ESP.restart();
  }
}