ARDUINO UNO with itegrated WiFi ESP8266

More
11 months 1 week ago - 11 months 1 week ago #6855 by Vladimirovic
For sending data to radmon.org I am using ARDUINO UNO with integrated WiFi ESP8266. There is needed two codes, one for ATMEGA (arduino part) and second for ESP8266 part.
As a Geiger-Counter I am using Radiation Detector RH Electronics, but is possible to use also other counter (see details in code comments).


 

/*


 *
 * This code is written for Arduino clone Uno with integrated WiFi ESP8266 for sending CPM to radmon.org via WiFi
 *
 * This code is for ATmega328 (another one is for ESP8266)
 *
 * I am using "Radiation Detector Arduino Compatible DIY kit ver.3 from rhelectronics.net - connected D2 pin as input to Arduino - but it will
 * work also for another "Geiger Detectors" - just use D2 pin as input (LOW)
 *
 * As reference I used code from RHelectronics page (Thank You!):
 * https://www.rhelectronics.store/radiation-detector-geiger-counter-diy-kit-second-edition
 * and add communication between ATmega328 and ESP8266
 *
 * Vladimirovic
 *
 */

#include <ArduinoJson.h>
#include <SPI.h>
#define LOG_PERIOD 60000     //Logging period in milliseconds, recommended value 15000-60000.
#define MAX_PERIOD 60000    //Maximum logging period
unsigned long counts;             //variable for GM Tube events
unsigned long cpm;                 //variable for CPM
unsigned int multiplier;             //variable for calculation CPM in this sketch
unsigned long previousMillis;      //variable for time measurement

void tube_impulse(){               //procedure for capturing events from Geiger Kit
  counts++;
}

void setup()
{
  counts = 0;
  cpm = 0;
  multiplier = MAX_PERIOD / LOG_PERIOD;      //calculating multiplier, depend on your log period
  Serial.begin(9600);                                    // start serial monitor
 // uncomment if you have time-out problem to connect with Radiation Logger
 //  delay(2000);
 //  Serial.write('0');                                      // sending zero to avoid connection time out with radiation logger
 //  delay(2000);
 //  Serial.write('0');                                     // sending zero to avoid connection time out with radiation logger
  pinMode(2, INPUT);                                   // set pin INT0 input for capturing GM Tube events
  digitalWrite(2, HIGH);                                 // turn on internal pullup resistors, solder C-INT on the PCB
  attachInterrupt(0, tube_impulse, FALLING);  //define external interrupts
}

void loop(){                                               //main cycle
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > LOG_PERIOD){
    previousMillis = currentMillis;
    cpm = counts * multiplier;
    counts = 0;
  }


// SEND DATA TO THE ESP8266 //////////////////////////////////////////////////////////////////////
{
StaticJsonDocument<256> message;
message["radiation"] = cpm; // setting up the variable radiation
serializeJson(message, Serial);
Serial.println();
delay(10000); // wait 10 sec between each send to the ESP
}
}
 

/*


 *
 * This code is written for Arduino clone Uno with integrated WiFi ESP8266 for sending CPM to radmon.org via WiFi
 *
 * This code is for ESP8266 (another one is for ATmega328)
 *
 * I am using "Radiation Detector Arduino Compatible DIY kit ver.3 from rhelectronics.net - connected D2 pin as input to Arduino - but it will
 * work also for another "Geiger Detectors" - just use D2 pin as input (LOW)
 *
 * As reference I used code from Simomax (ESP8266 - Wemos D1 Mini - Arduino Code) (Thank You!):
 * https://radmon.org/index.php/forum/arduino/1225-esp8266-wemos-d1-mini-arduino-code-bare-bones-code-verified-working
 * and add communication between ATmega328 and ESP8266
 *
 * Vladimirovic
 *
 */
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
WiFiClient client;
// Your WiFi Network SSID and Password.
#define WIFI_SSID             "your_WiFi_login"                  //Your WiFi login
#define WIFI_PASS             "your_WiFi_password"        //Your WiFi password
// Your Radmon.org username and submission password.
const char* RadmonHost = "radmon.org";              // No need to change this
const char* UserName = "your_radmon_username";              //Your Radmon.org user name
const char* PassWord = "your_radmon_password";       //Your Radmon.org submission password - Make sure this is the same as your 'data sending password' on your Radmon.org profile page.
String 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 cpmTime=millis();             // And another for the 60 second timer for Radmon.org submission.
int radmonMillis = 300000;                     // 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.
int lastConnectionAttempt = millis();
int connectionDelay = 5000;                   // Try to reconnect WiFi every 5 seconds if connection lost.
void setup()
{
  Serial.begin(9600);
  delay(500);
  WiFi.mode(WIFI_STA);
}
void updateRadmon() {
  WiFiClient clientGet;
  const int httpGetPort = 80;
  String urlGet = "/radmon.php";
  urlGet += "?function=submit&user=";
  urlGet += UserName;
  urlGet += "&password=";
  urlGet += PassWord;
  urlGet += "&value=";
  urlGet += String(CPM);
  urlGet += "&unit=CPM";
  if (!clientGet.connect(RadmonHost, httpGetPort)) {
  } else {                                            // Connected - Success!
    clientGet.println("GET " + urlGet + " HTTP/1.1"); // Send the datary goodness to Radmon.org.
    clientGet.print("Host: ");
    clientGet.println(RadmonHost);
    clientGet.println("User-Agent: ESP8266/1.0");
    clientGet.println("Connection: close\r\n\r\n");
    unsigned long timeoutP = millis();        // We check the connection time. If longer than 10 seconds then stop the connection and exit the function - timeout.
    while (clientGet.available() == 0) {
      if (millis() - timeoutP > 10000) {
        clientGet.stop();
        return;
      }
    }
    while (clientGet.available()) {          // We got a response so just check the 1st line of the server response. Could be expanded if needed.
      String retLine = clientGet.readStringUntil('\r');
      break;
    }
  }                                         // End client connection as we are done.
  clientGet.stop();
}

void loop()
{    

// receive data from ATmega328 //////////////
bool StringReady;
String json;
while (Serial.available()){
json=Serial.readString();
StringReady = true;
}
if (StringReady){
delay(1);
StaticJsonDocument<256> message;
auto error = deserializeJson(message, json);
if (error) {
return;
}
String temporary = message["radiation"];
CPM = temporary;
}
/////////////////////////////////////////////

  if ( (millis()-cpmTime) >= radmonMillis) {        // Check our 60 second interval.
    cpmTime = millis();
    updateRadmon();                         // 60 Seconds has surpassed so we submit the CPM to Radmon.org.
  }
 
  if (WiFi.status() != WL_CONNECTED)        // check WiFi connection:
  {
    // (optional) "offline" part of code - you can add code here to signal when WiFi is disconnected, such as lighting an LED.
    if (millis() - lastConnectionAttempt >= connectionDelay)
    {
      lastConnectionAttempt = millis();
      if (WIFI_PASS && strlen(WIFI_PASS))   // attempt to connect to Wifi network:
      {
        WiFi.begin((char*)WIFI_SSID, (char*)WIFI_PASS);
      }
      else
      {
        WiFi.begin((char*)WIFI_SSID);
      }
    }
  }
  else
  {
    // We are connected - you can add code here to signal when WiFi is connected, such as lighting an LED.
  }
}

Attachments:
Last edit: 11 months 1 week ago by Vladimirovic.
The following user(s) said Thank You: Simomax

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

Moderators: Gamma-Man
Time to create page: 0.208 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 29% Memory 14% Swap 17% CPU temp=50.1'C Uptime 9 Days