MQTT receiver for die hard Radlog Pro users

  • Simomax
  • Simomax's Avatar Topic Author
  • Offline
  • Moderator
  • Moderator
  • Nuts about radioactive everything!
More
3 days 4 hours ago - 2 days 7 hours ago #7574 by Simomax
 

I wrote Grok wrote me a small Arduino sketch for receiving the CPM from an ESPGeiger and sending it to the serial port on a PC/server. This can be used to get the CPM from any ESPGeiger sending to a MQTT server, or modified for any counter using MQTT. It's just a case of change the details to suit your own WiFi/MQTT server etc and the topic, load onto a Wemos D1 Mini, or other flavour of ESP8266 (with serial connection), and plug it into whatever PC/server you are running Radlog Pro on. Set Radlog to use the com port of the Wemos and baud to 9600. The code sends the CPM to Radlog every 5 seconds, however the default in ESPGeiger firmware settings is 60 seconds. I changed ESPGeiger to send to MQTT every 5 seconds and this made Radlog a little livelier, and a higher resolution for HA too.

Here is the code. Change the settings to suit and upload on an ESP8266. Plug in and go.

***This uses a depreciated method, use the code on the next post***
#include <ESP8266WiFi.h>
#include <PubSubClient.h> // by Nick O'Leary

const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
const char* mqtt_server = "your_mqtt_broker_ip";  // Often Home Assistant's IP
const char* mqtt_user = "optional";
const char* mqtt_pass = "optional";

const char* topic = "ESPGeiger-######/stat/CPM"; // Root topic is ESPGeiger-{id}. Adjust the id for your own counter

WiFiClient espClient;
PubSubClient client(espClient);

String latestCPM = "0";

void setup() {
  Serial.begin(9600);  // Matches common Geiger/RadLog baud
  WiFi.begin(ssid, password);
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void callback(char* topic, byte* payload, unsigned int length) {
  String message;
  for (int i = 0; i < length; i++) message += (char)payload[i];
  latestCPM = message;  // e.g., "42.50"
}

void reconnect() {
  if (client.connect("WemosGeigerBridge", mqtt_user, mqtt_pass)) {
    client.subscribe(topic);
  }
}

void loop() {
  if (!client.connected()) reconnect();
  client.loop();

  // Send to PC every few seconds (RadLog expects periodic data)
  static unsigned long lastSend = 0;
  if (millis() - lastSend > 5000) {  // Adjust as needed
    Serial.println(latestCPM);  // Simple: just the number
    // Serial.print("CPM: "); Serial.println(latestCPM); // Or mimic MightyOhm style
    lastSend = millis();
  }
}

Long live Radlog Pro! 
Attachments:
Last edit: 2 days 7 hours ago by Simomax.
The following user(s) said Thank You: steadramon

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 days 7 hours ago - 2 days 7 hours ago #7575 by Simomax
Steadramon mentioned the method I was using (MQTT stat/* (stat/CPM) etc) are depreciated, so I have Grok has updated this to use the proper JSON method. New code (don't use the old code):
#include <ESP8266WiFi.h>
#include <PubSubClient.h> // (by Nick O'Leary)
#include <ArduinoJson.h> // (by Benoit Blanchon)

const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
const char* mqtt_server = "your_mqtt_broker_ip";   // Home Assistant / MQTT broker IP
const int   mqtt_port = 1883;
const char* mqtt_user = "optional";      // Leave empty if no auth
const char* mqtt_pass = "optional";

const char* jsonTopic = "ESPGeiger-######/tele/sensor"; // JSON topic is ESPGeiger-{id}/tele/sensor. Adjust the id for your own counter

WiFiClient espClient;
PubSubClient client(espClient);

String latestCPM = "0";

void setup() {
  Serial.begin(9600);           // RadLog Pro usually expects 9600 baud
  delay(100);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected");

  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
}

void callback(char* topic, byte* payload, unsigned int length) {
  // Parse JSON
  StaticJsonDocument<256> doc;   // Should be plenty for this payload
  DeserializationError error = deserializeJson(doc, payload, length);

  if (error) {
    Serial.print("JSON parse failed: ");
    Serial.println(error.c_str());
    return;
  }

  // Extract cpm value
  if (doc.containsKey("cpm")) {
    float cpm = doc["cpm"];
    latestCPM = String(cpm, 2);   // Keep 2 decimal places like the firmware
    // Serial.print("Updated CPM: "); Serial.println(latestCPM); // Debug
  }
}

void reconnect() {
  while (!client.connected()) {
    String clientId = "WemosGeigerBridge-" + String(random(0xffff), HEX);
    if (client.connect(clientId.c_str(), mqtt_user, mqtt_pass)) {
      client.subscribe(jsonTopic);
      Serial.println("MQTT connected & subscribed");
    } else {
      delay(2000);
    }
  }
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  // Send latest CPM to PC periodically
  static unsigned long lastSend = 0;
  if (millis() - lastSend > 5000) {        // Every 5 seconds - adjust as needed
    if (latestCPM != "0") {
      // Simple number (works for many programs)
      Serial.println(latestCPM);

      // Uncomment one of these alternatives if RadLog needs a specific format:

      // MightyOhm-style CSV example:
      // Serial.print("CPS, 0, CPM, ");
      // Serial.print(latestCPM);
      // Serial.println(", uSv/hr, 0.00");

      // Or plain labeled:
      // Serial.print("CPM: ");
      // Serial.println(latestCPM);
    }
    lastSend = millis();
  }
}
Last edit: 2 days 7 hours ago by Simomax.

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

Moderators: Gamma-Man
Time to create page: 0.130 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 56% Memory 14% Swap 14% CPU temp=61.8'C Uptime 10 Days