- Forum
- Software discussions
- Arduino Code
- Arduino Uno, Mega2560 & Pro Mini + ENC28J60 ethernet adapter - Bare-bones code
Arduino Uno, Mega2560 & Pro Mini + ENC28J60 ethernet adapter - Bare-bones code
1 year 11 months ago - 1 year 11 months ago #6319
by Simomax
Arduino Uno, Mega2560 & Pro Mini + ENC28J60 ethernet adapter - Bare-bones code was created by Simomax
Here is some Arduino code for the Arduino Uno, Mega2560 and Pro Mini with ENC28J60 ethernet adapter. 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 ENC28J60) 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 ENC28J60.
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.
There are some differences between Uno/Pro Mini and Mega 2560. The pins for MOSI, MISO, SCK and SS are different on the Mega 2560. Make sure you connect up to the correct pins and also comment/uncomment the specific line in void setup() for the particular Arduino you are using.
Some thoughts on the ENC28J60 - Personally, I would not use this for radmon.org. That's just my opinion, but after many many pitfalls whilst trying to get this to work with all three boards, I nearly gave up. I didn't and persevered and did finally get it working. The ENC28J60 does not have it's own TCP stack (unlike the Wiznet 5100 which has it's own in hardware). This means that the library takes up a large amount of memory on the Uno and Pro Mini - 24k used out of 32k available. This would mean that adding things such as a screen or another 'thing' will cause the Arduino to run out of memory, fast. The library I have used is a new library for the ENC28J60 and can be downloaded in the Adruino library manager (pictured below) and the github page is https://github.com/JAndrassy/EthernetENC .
The ENC28J60 requires 3.3v for operation, but is 5v tolerant. In testing I just connected it to 5v on the Pro Mini (that has no 3.3v pin) and it worked fine for testing.
In all it was a massive PITA to get working and left me with little memory on the Adruino Uno and Pro Mini for anything else. I really wouldn't use this adaptor, unless it was an absolute must for one reason or another. I won't be supporting this code at all. Not in the slightest. I suggest you use something else that is not the ENC28J60.
Here is the code:
When running properly you should see it startup and start to detect events in the debug log like this:
When it submits the CPM to Radmon.org, you should see this in the debug log:
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:
Again, use this at your peril. It does work just like the code for the WizNet W5100 (it is in fact the same code with a couple of small alterations) but having little memory left and the PITA it was to get ti working just puts me off using it for anything solid.
Cheers!
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.
There are some differences between Uno/Pro Mini and Mega 2560. The pins for MOSI, MISO, SCK and SS are different on the Mega 2560. Make sure you connect up to the correct pins and also comment/uncomment the specific line in void setup() for the particular Arduino you are using.
Some thoughts on the ENC28J60 - Personally, I would not use this for radmon.org. That's just my opinion, but after many many pitfalls whilst trying to get this to work with all three boards, I nearly gave up. I didn't and persevered and did finally get it working. The ENC28J60 does not have it's own TCP stack (unlike the Wiznet 5100 which has it's own in hardware). This means that the library takes up a large amount of memory on the Uno and Pro Mini - 24k used out of 32k available. This would mean that adding things such as a screen or another 'thing' will cause the Arduino to run out of memory, fast. The library I have used is a new library for the ENC28J60 and can be downloaded in the Adruino library manager (pictured below) and the github page is https://github.com/JAndrassy/EthernetENC .
The ENC28J60 requires 3.3v for operation, but is 5v tolerant. In testing I just connected it to 5v on the Pro Mini (that has no 3.3v pin) and it worked fine for testing.
In all it was a massive PITA to get working and left me with little memory on the Adruino Uno and Pro Mini for anything else. I really wouldn't use this adaptor, unless it was an absolute must for one reason or another. I won't be supporting this code at all. Not in the slightest. I suggest you use something else that is not the ENC28J60.
Here is the code:
/*
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| ArduinoBB Giraffe ENC 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 both Uno and Mega2560. The the ethernet shield was plugged onto the Uno and Mega2560 during tests. |
| The ENC28J60 uses pins (Uno & Pro Mini) 10 - CS, 11 - SI, 12 - SO, 13 - SCK. (Mega) 53 - CS, 51 - SI, 50 - SO, 52= - SCK. Voltage is 3.3v |
| TTL Geiger counter input on pin 2 |
| Comment/uncomment the line in void setup() depending if you are running an Uno/Pro Mini or a Mega 2560 |
| |
| 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 <EthernetENC.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 = true;
// 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 = false;
// 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 = 30000; // 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.
// Choose one of the following lines and comment the other out.
Ethernet.init(10); // Initialize ethernet on pin 10 (Uno & Pro Mini)
//Ethernet.init(53); // Initialize ethernet on pin 10 (Mega 2560)
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.
Ethernet.maintain(); // Maintain teh ethernet connection. if the cable has been unplugged and plugged back in this will renew DHCP IP address.
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 Giraffe ENC 1.0"); // Let the web server know what we are.
if (debug) {
Serial.println("User-Agent: ArduinoBB Giraffe ENC 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
while (client.connected()) {
if (client.available()) {
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; // 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:
18:18:23.792 -> Initialize Ethernet.
18:18:25.144 -> << Got Event >>
18:18:26.315 -> Ethernet cable connected.
18:18:26.315 -> DHCP assigned IP 172.16.100.132
18:18:26.363 -> Running!
18:18:26.363 ->
18:18:26.363 -> Calculating CPM: 1
18:18:26.456 -> << Got Event >>
18:18:26.735 -> << Got Event >>
18:18:27.295 -> Calculating CPM: 3
18:18:28.182 -> << Got Event >>
18:18:28.322 -> Calculating CPM: 4
18:18:29.302 -> Calculating CPM: 4
18:18:29.394 -> << Got Event >>
18:18:30.282 -> Calculating CPM: 5
18:18:31.308 -> Calculating CPM: 5
18:18:32.294 -> Calculating CPM: 5
18:18:33.317 -> Calculating CPM: 5
18:18:34.309 -> Calculating CPM: 5
18:18:35.325 -> Calculating CPM: 5
18:18:36.308 -> Calculating CPM: 5
18:18:37.285 -> Calculating CPM: 5
18:18:37.845 -> << Got Event >>
18:18:38.314 -> Calculating CPM: 6
18:18:39.292 -> Calculating CPM: 6
When it submits the CPM to Radmon.org, you should see this in the debug log:
18:18:52.306 -> Calculating CPM: 14
18:18:53.330 -> Calculating CPM: 14
18:18:54.308 -> Calculating CPM: 14
18:18:55.335 -> Calculating CPM: 14
18:18:56.033 -> << Got Event >>
18:18:56.314 ->
18:18:56.314 -> Updating Radmon.
18:18:56.314 -> Connecting.
18:18:57.948 -> Connected to radmon.org at 80.229.27.195
18:18:57.993 -> Host: radmon.org
18:18:58.040 -> User-Agent: ArduinoBB Giraffe ENC 1.0
18:18:58.086 -> Connection: close
18:18:58.086 ->
18:18:58.086 ->
18:18:59.906 -> << Got Event >>
18:19:00.280 -> Response: OK
18:19:00.280 ->
18:19:00.280 -> Calculating CPM: 16
18:19:00.747 -> << Got Event >>
18:19:01.262 -> Calculating CPM: 17
18:19:01.730 -> << Got Event >>
18:19:02.286 -> Calculating CPM: 18
18:19:03.262 -> Calculating CPM: 18
18:19:03.308 -> << Got Event >>
18:19:04.287 -> Calculating CPM: 19
18:19:05.264 -> Calculating CPM: 19
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
Again, use this at your peril. It does work just like the code for the WizNet W5100 (it is in fact the same code with a couple of small alterations) but having little memory left and the PITA it was to get ti working just puts me off using it for anything solid.
Cheers!
Attachments:
Last edit: 1 year 11 months ago by Simomax.
Please Log in or Create an account to join the conversation.
Moderators: Gamma-Man
- Forum
- Software discussions
- Arduino Code
- Arduino Uno, Mega2560 & Pro Mini + ENC28J60 ethernet adapter - Bare-bones code
Time to create page: 0.225 seconds