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
1 year 4 months ago - 1 year 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: 1 year 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
1 year 4 months ago - 1 year 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: 1 year 4 months ago by Simomax.

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

Moderators: Gamma-Man
Time to create page: 0.223 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 78% Memory 13% Swap 17% CPU temp=62.3'C Uptime 61 Days