Arduino Uno, Mega2560 & Pro Mini + WizNet 5100 ethernet shield - Bare-bones code

  • Simomax
  • Simomax's Avatar Topic Author
  • Offline
  • Moderator
  • Moderator
  • Nuts about radioactive everything!
More
2 years 4 months ago - 2 years 4 months ago #6315 by Simomax
     

Here is some Arduino code for the Arduino Uno, Mega2560 and Pro Mini with WizNet W5100 ethernet shield. This is bare-bones code only. It does only these things: receives pulses on pin 2 (Uno, Mega & Pro Mini), calculates CPM and submits it to Radmon.org (via ethernet shield) every 60 seconds. It has a little debugging that can print to serial and I have also added CPM print to serial (much like the NetIO GC-10) that can be used with the Radlog windows logging software - and that is it. Nothing more, nothing less. It works. I used my own custom code I had previously written for other devices and pulled snippets from the internet for the ethernet shield.
To use this you simply connect pin 2 to your Geiger detector, load the code and change your radmon.org username and submission password, and that is all.
NOTE: Make sure the pulses coming from your detector are logic level <= 5v or you may destroy your Arduino, or damage your detector circuit, or both. In testing I used my Geiger Counter development (bread)board which is basically a copy of an early GK geiger counter (thank you Brohogan for making it open source - much appreciated!) You may need additional electronics to interface your detector to the Arduino which is not covered here.

Here is the code:
/*
  -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  | ArduinoBB WizNet Pumpkin 1.0                                                                                                                                                                            |
  | Bare-bones Arduino counter for Geiger counter and submission to Radmon.org written by Simomax.                                                                                                          |
  | This is very basic code. It will detect pulses on pin 2 and calculate the CPM every second. It will then submit the CPM to Radmon.org every 60 seconds.                                                 |
  | It has a debug option and also option to print the CPM to serial every second, much like the NetIO- GC-10, which can also be used with the Radlog windows software.                                     |
  |                                                                                                                                                                                                         |
  | You are free to do anything you want with this code. There is no license. You are allowed to use/copy/change/share the code without having to attribute myself,                                         |
  | although it would be appreciated if you did. You are also free to use this in any commercial setting.                                                                                                   |
  | Radmon.org is free and always has been and in that sentiment, so is this code, as is all of my code that I share on the Radmon.org forums unless specifically stated.                                   |
  | If you would like to give something back then please consider a small donation to Radmon.org or even better, become a regular user of the Radmon.org forums. It has become a little quiet of late       |
  | and some new users to the forum would be welcomed, by everyone I am sure.                                                                                                                               |
  |                                                                                                                                                                                                         |
  | This was tested on Uno, Mega2560 and Pro Mini. The the ethernet shield was plugged onto the Uno and Mega2560 and the Pro Mini was mounted on a small breadboard and jumper cables were used during      |
  | tests. The ethernet shield uses pins 10,11,12,13. TTL Geiger counter input on pin 2                                                                                                                     |
  |                                                                                                                                                                                                         |
  | This code comes without warranty or support. I am happy to answer questions in the Radmon forums and chat, but try and keep it within the code. I may not be able to help with your own code            |
  | outside my own.                                                                                                                                                                                         |
  | If it breaks you get to keep all the pieces!                                                                                                                                                            |
  |                                                                                                                                                                                                         |
  | Have fun and happy counting!                                                                                                                                                                            |
  | ~Simonmax                                                                                                                                                                                               |
  -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/

#include <SPI.h>
#include <Ethernet.h>
EthernetClient client;

byte mac = { 0x2E, 0x3D, 0x4E, 0x5F, 0x6E, 0x7D }; // Network adapter MAC Address.

// Your Radmon.org username and submission password.
char RadmonHost = "radmon.org";             // No need to change this
const char* UserName = "changeme";             //Your radmon.org user name
const char* PassWord = "changeme";          //Your radmon.org submission password - Make sure this is the same as your 'data sending password' on your radmon.org profile page.

// Debug - True prints information to serial.
int debug = false;

// Option to enable CPM to be printed to serial - When enables this emulates the NetIO GC-10 serial output - Doesn't work when debug = true
int printCPM = true;




// Variables used - You shouldn't need to change anything below this line unless customizing. --------------------------------------------------------------------------------
long CPM;
volatile unsigned long totalCounts = 0;       // Total counts - May be useful, but for now is only printed when debug enabled.
int cpsCount = 0;                             // Used for calculating CPM. This var counts the amount of interrupt events in one second.
int cpmArray[60];                             // Array used for storing CPS over 60 seconds. Each index equals the CPS over a given second.
int cpmArrayStep = 0;                         // Used for stepping the index of above array when calculating CPM.
static uint32_t cpsTime = millis();           // Used for the 1 second timer. We count millis in the loop as using a delay would impact other functions and delays should generally not be used.
static uint32_t submissionCounter;            // And another for the 60 second timer for Radmon.org submission.
int cpmMillis = 1000;                         // How often to calculate CPM (1000ms = 1 second.)
static uint32_t radmonInterval = 60000;       // How often to submit to Radmon.org (60000ms = 60 seconds.) Please don't set this to less than 60000 as recommended minimum submission time for Radmon.org is 60 seconds.
const int inputPin = 2;                       // TTL input from Geiger counter.
unsigned long response_count;
byte response;


void setup()
{
  Serial.begin(9600);                         // Set serial baud rate to 9600.
  delay(500);
  pinMode(inputPin, INPUT_PULLUP);            // Set pin GPIO 2 (Pin 2) as input and enable internal pullup.
  attachInterrupt(digitalPinToInterrupt(inputPin), GetEvent, FALLING);     // Attach interrupt on GPIO 13.
  Ethernet.init(10); // Initialize ethernet on pin 10 (default)
  delay(1000);
  if (debug) {
    Serial.println("Initialize Ethernet.");
  }
  if (Ethernet.begin(mac) == 0) {
    Serial.println("DHCP failure. Could not optain IP address by DHCP.");
  }
  delay(1000);
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet adapter was not found.");
  }
  if (Ethernet.hardwareStatus() == EthernetW5500) {
    if (debug) {
      Serial.println("W5500 Ethernet adapter was found.");
    }
  }
  if (Ethernet.linkStatus() == LinkON) {
    if (debug) {
      Serial.println("Ethernet cable connected.");
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable not connected.");
  }
  if (debug) {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  if (debug) {
    Serial.println("Running!\r\n");
  }
  submissionCounter = millis();
}

void updateRadmon() {                        // Submit the reading Radmon.org.
  if (debug) {
    Serial.println("\r\nUpdating Radmon.");
    Serial.println("Connecting.");
  }
  if (client.connect(RadmonHost, 80)) {
    if (debug) {
      Serial.print("Connected to ");
      Serial.print(RadmonHost);
      Serial.print(" at ");
      Serial.println(client.remoteIP());
    }
    client.print("GET /radmon.php?function=submit&user="); // Print the URL for Radmon.org CPM submission.
    client.print(UserName);
    client.print("&password=");
    client.print(PassWord);
    client.print("&value=");
    client.print(CPM);
    client.println("&unit=CPM HTTP/1.1");
    client.print("Host: ");
    client.println(RadmonHost);
    if (debug) {
      Serial.println("Host: " + String(RadmonHost));
    }
    client.println("User-Agent: ArduinoBB WizNet Pumpkin 1.0"); // Let the web server know what we are.
    if (debug) {
      Serial.println("User-Agent: ArduinoBB WizNet Pumpkin 1.0");
    }
    client.println("Connection: close\r\n\r\n"); // Finally, close the connection
    if (debug) {
      Serial.println("Connection: close\r\n\r\n");
    }
    delay(2000); // Wait a little and give the server time to respond.
    response_count = 0;
    response = 0;
    while (client.available()) { // Read response from server
      char c = client.read();
      if (response_count > 15 && response == 0 && c == 'O') {
        response = 1;
      } else {
        if (response_count > 15 && response == 1) {
          if (c == 'K') {
            response = 2;
          } else {
            response = 0;
          }
        }
      }
      response_count += 1;
    }
    if (response == 2) {
      if (debug) {
        Serial.println("Response: OK");
        Serial.println();
      }
      client.stop();
    } else {
      if (debug) {
        Serial.println("Unable to connect.");
        Serial.println();
      }
      client.stop();
    }
  }
}    // End client connection as we are done.

void calculateCPM()                         // Calculate the CPM.
{
  if (debug) {
    Serial.print("Calculating CPM: ");
  }
  cpmArray[cpmArrayStep] = cpsCount;        // Set the index in the CPM array to that of the current CPS. The index initially starts at 0 when powered on.
  cpmArrayStep++;                           // The next index we want to record.
  if (cpmArrayStep >= 60)                   // There are 60 indexes, one for every second in a minute. If the index goes out of the bounds of our 60 indexes then cycle back to index 0.
  {
    cpmArrayStep = 0;
  }
  CPM = 0;                                  // Var used to temporarily calculate CPM.
  unsigned int i;
  for (i = 0; i < 60; i++)                  // Get the value at each index of the CPM array.
  {
    CPM += cpmArray[i];                     // Add each index together to give a total over 60 seconds.
  }
  cpsCount = 0;                             // Reset the CPS variable ready for sampling the next second.
  if (printCPM) {
    Serial.println(CPM);                    // Print the current CPM to serial every interval if enabled - Much like the NetIO GC-10.
  }
  else if (debug) {
    Serial.println(CPM);
  }
}

void GetEvent() {                           // ISR triggered for each new event (count).
  if (debug) {
    Serial.println("<< Got Event >>");
  }
  totalCounts ++;                           // Increase total counts each time the interrupt is fired.
  cpsCount ++;                              // Increase var each time the interrupt is fired.
}

void loop()
{
  if ( (millis() - cpsTime) >= cpmMillis) {      // Check our 1 second interval.
    cpsTime = millis();
    calculateCPM(); // 1 Second has surpassed so we calculate our CPM.
  }
  if ( (millis() - submissionCounter) >= radmonInterval) {      // Check our Radmon timer submission second interval.
    submissionCounter = millis();
    updateRadmon();                         // XX Seconds has surpassed so we submit the CPM to Radmon.org.
  }
}

When running properly you should see it startup and start to detect events in the debug log like this:
Initialize Ethernet.
  DHCP assigned IP 172.16.100.132
Running!

Calculating CPM: 0
<< Got Event >>
Calculating CPM: 1
Calculating CPM: 1
Calculating CPM: 1
Calculating CPM: 1
<< Got Event >>
Calculating CPM: 2
Calculating CPM: 2
Calculating CPM: 2
<< Got Event >>
Calculating CPM: 3
Calculating CPM: 3
Calculating CPM: 3
Calculating CPM: 3
Calculating CPM: 3
Calculating CPM: 3
Calculating CPM: 3
Calculating CPM: 3
Calculating CPM: 3
Calculating CPM: 3
Calculating CPM: 3
<< Got Event >>
Calculating CPM: 4
Calculating CPM: 4
Calculating CPM: 4
<< Got Event >>
Calculating CPM: 5
<< Got Event >>
<< Got Event >>
Calculating CPM: 7
Calculating CPM: 7
Calculating CPM: 7
Calculating CPM: 7

When it submits the CPM to Radmon.org, you should see this in the debug log:
<< Got Event >>
Calculating CPM: 18
<< Got Event >>
Calculating CPM: 19
Calculating CPM: 19
Calculating CPM: 19
<< Got Event >>

Updating Radmon.
Connecting.
Connected to radmon.org at 80.229.27.195
Host: radmon.org
User-Agent: ArduinoBB WizNet Pumpkin 1.0
Connection: close


Response: OK

Calculating CPM: 20
Calculating CPM: 19
Calculating CPM: 19
Calculating CPM: 19
<< Got Event >>
<< Got Event >>
Calculating CPM: 21

If all is well and you want to use the serial CPM output then simply turn off debugging (debug = false) and enable the printCPM (printCPM = true). You will then get a serial output that can be used with other equipment, or the Radlog windows software, and should look like this:
0
1
1
1
3
4
4
4
6
7
7
8
8
8
8
9
9
10
11
11
11
11
11
11
11
12
12
12
12
12
13

Be Happy! 
Attachments:
Last edit: 2 years 4 months ago by Simomax.

Please Log in or Create an account to join the conversation.

  • Simomax
  • Simomax's Avatar Topic Author
  • Offline
  • Moderator
  • Moderator
  • Nuts about radioactive everything!
More
2 years 4 months ago - 2 years 4 months ago #6318 by Simomax
I have just tested this with an Arduino Pro Mini and it works well. 

The ethernet shield I am using is a cheap clone. The only pin 10 is connected on the side connectors. Pins 11, 12 and 13 go to the ICSP connector on the shield. This caused me much confusion for far too long! I'm not sure if the original is the same as this, but something to look out for. I also ran the 5v and ground into the ICSP connector, so just pin 10 was connected on the side connectors.

Now on to Arduino and Mini ENC28J60 ethernet adapter! 
Last edit: 2 years 4 months ago by Simomax.

Please Log in or Create an account to join the conversation.

More
1 week 5 days ago - 1 week 5 days ago #7274 by ChrisLX200
Old thread but nice work anyway. I just tarted up my Cajoe with an Arduino Nano + LCD, and cut a black acrylic cover using my 10W laser engraver. Still looks messy with bits hanging in the breeze but I've already changed my mind about using the Nano and ordered some ESP32 boards with built-in OLED display. That can connect via WiFi without further hassle.

 
 
Attachments:
Last edit: 1 week 5 days ago by ChrisLX200.

Please Log in or Create an account to join the conversation.

  • Simomax
  • Simomax's Avatar Topic Author
  • Offline
  • Moderator
  • Moderator
  • Nuts about radioactive everything!
More
1 week 4 days ago - 1 week 4 days ago #7276 by Simomax
If you are going to use ESP8266 or ESP32 then take a look at ESPGeiger:  https://radmon.org/index.php/forum/arduino/1312-espgeiger  - It rocks. I have worked with Steadramon quite a lot with ESPGeiger, although more with the ESPGeiger HW hardware side of things, especially it reading the tube voltage on the fly. Only the ESPGeiger HW does this though, not the other flavours of firmware. If there are no special needs cases for your counters, then it is a very simple drop-in-place solution for submitting to Radmon, MQTT and a couple of other things. Sadly, I do have a special needs case so before I can use it on all of my counters, I need to wait for some extra code to be written for it, although it seems development has slowed to a crawl. I may end up just hacking my own code onto it somehow if Steadramon doesn't include a generic web hook.
Last edit: 1 week 4 days ago by Simomax.

Please Log in or Create an account to join the conversation.

More
1 week 4 days ago - 6 days 1 hour ago #7280 by ChrisLX200
Thinking about it I have a couple of Heltec and Lillygo ESP32 boards in a drawer. These were originally set up for Meshtastic coms but I appear to be located in a blackspot as far as reception is concerned. No matter what antenna I used. Talking to myself from one node to the other kinda got old after a while so I took them down. Anyway, they are ESP32 boards so maybe I can re-purpose them.. They also have GPS modules.

Edit: Those boards are apparently incompatible according to the error the ESPGeiger Web app.
Last edit: 6 days 1 hour ago by ChrisLX200.

Please Log in or Create an account to join the conversation.

More
6 days 10 hours ago - 6 days 1 hour ago #7301 by ChrisLX200

If you are going to use ESP8266 or ESP32 then take a look at ESPGeiger:  https://radmon.org/index.php/forum/arduino/1312-espgeiger  - It rocks. I have worked with Steadramon quite a lot with ESPGeiger, although more with the ESPGeiger HW hardware side of things, especially it reading the tube voltage on the fly. Only the ESPGeiger HW does this though, not the other flavours of firmware. If there are no special needs cases for your counters, then it is a very simple drop-in-place solution for submitting to Radmon, MQTT and a couple of other things. Sadly, I do have a special needs case so before I can use it on all of my counters, I need to wait for some extra code to be written for it, although it seems development has slowed to a crawl. I may end up just hacking my own code onto it somehow if Steadramon doesn't include a generic web hook.




 

I've got my ESP32 boards now so am attempting to connect the one functional CAJOE board I have. Still waiting on delivery of the other two. There seems to be a lot firmware(s) available for different devices but I assume the one I want is the Pulse variant for the Cajoe. I already tried a few others before I figured that out! Anyway, I can't seem to get past the 'prepraring the installation' screen in the Chrome web install. It just sits there. The install routine has found the ESP32 ok (COM21 on my setup) and I've tried holding the Boot button down while clicking on install. I;ve also tried resetting the ESP32. I may try installing the downloaded bin file with a dfferent utility if this doesn't work. It looks good though if I can get it to work and will make life a lot easier uploading the data to Radmon.

Edit: OK, I got it to upload the firmware - I wasn't holding down the boot button long enough.

However, after reporting success the ESP32 remains apparently lifeless. Nothjing on the OLED. Looking at the console I see it is just looping due to not having a WiFi connection? The ESP32 is not creating a hotspot like it's supposed to... I'm using ESP-WROOM-32 devices...

 

Edit: Wits end on this one. Beginning to think these cheap AE ESP32 boards are garbage. I have tried all options posted about getting this working - including cloning the github repository and compiling the source myself. No luck. I'll order a name-brand ESP32 board off Amazon to try, but if anyone has recommendations as to which boards will actually work (and are available) please post.
Attachments:
Last edit: 6 days 1 hour ago by ChrisLX200.

Please Log in or Create an account to join the conversation.

Moderators: Gamma-Man
Time to create page: 0.224 seconds
Powered by Kunena Forum
Everything's free. Please support us by considering a donation. Log in first!
Solar powered Raspberry Pi 4 server stats: CPU 47% Memory 12% Swap 7% CPU temp=57.4'C Uptime 2 Days