Radiation Ticker

Wait, what, why?

It all started in a very normal way. As anything in life everything started by surfing through all the perks in the Aliexpress (the Chinese Amazon). More specifically I happen to stumble upon this piece of hardware. Since it was on discount I got this marvelous and mostly useless tool for just 25 euros. Yes, it is an offer you can not refuse..

So what now? Well documentation for this piece of hardware is kind of shitty. But after some hours looking into the board I notice that I could plugin three cables.

Vin    -> 5v
Ground
Data

Without any other kind of information I just assume that the data would basically send some kind of signal every time he detects some radiation.

int current_state = digitalRead(RADIATION_PORT);
if(current_state != last_state){
        //Tick received
    }
}
last_state=current_state;

The above piece of code was the first attempt to catch information getting from the RADIATION_PORT which was the port 13 of the ESP 32 microcontroller. Unfortunately for every radiation tick I received we entered twice inside the if statement. So something was happening there. The problem was that for each radiation tick we got the following transitions LOW -> HIGH -> LOW. To circumvent this we need to discard one of the transitions. For that we can just do a modulo 2 operation and the code becomes

    void loopRadiation(){
        int current_state = digitalRead(RADIATION_PORT);
        if(current_state != last_state){
            debounce=debounce+1;
            if(debounce % 2==0){
                //TICK FOUND
                debounce=0;
            }
        }
        last_state=current_state;
    }

This solves our problem now we just need to get this values outside the micro controller and put them in any form we can read them. But before what is an esp 32 micro controller. In a nutshell it is a very low consumption electronic device with all these features

Esp 32 Architecture

In particular we have a Wi-Fi hardware device which we can use to connect to our Wi-Fi network. Some questions may arise at this moment. How are we programming the esp 32 micro controller? Glad you ask! We are using a very cool framework called Platform IO. Platform IO is a platform that has a collaboration of most of micro controllers manufactures. It provides an IDE, based on Code (in my particular case I just installed the plugin for an already version of Visual Code). And a build tool called pio.

To program the Esp 32 micro controller you just need to follow the instructions on the IDE and select expressif esp 32 board as your development device and if all went well you'll end up with a directory filled with all the needed files and in particular the platformio.ini which should seem like this

    [env:hornbill32dev]
    platform = espressif32
    board = hornbill32dev
    framework = arduino

Here you could choose another framework, like the expressif idf library.

To publish data gathered from the radiation board you'll need to connect to the WiFi network and have some kind of protocol to export the radiation data.

Here the protocol chosen was the MQTT which is the protocol of choice for IoT architectures.

The first thing you'll need to do is the inclusion of the header files

    #include <WiFi.h>
    #include <PubSubClient.h>

The first will enable you to do WiFi related tasks, and in this particular this means connect to our local network.

    //WIFI setup
    const char* ssid="NetWorkSSID";
    const char* pass="networkpassword";

    //MQTT Setup
    const char* mqtt_broker_host = "network_mqtt_ip";
    int mqtt_broker_port = 32333;
    const char* mqtt_client_id = "ESP32 Demo Client";
    const char* radiation_topic="topic.esp32.radiation";

In the previous code we have all the constant values needed to setup our radiation publisher application. These values are used to set our WiFi connection and our MQTT client. The code needed to connect to our WiFi network is as follows

    WiFiClient esp_client;
    PubSubClient client(esp_client);

    void setupWifi(){
        delay(100);
        Serial.printf("\nConnection to:");
        Serial.printf(ssid);
        Serial.printf(pass);

        WiFi.begin(ssid,pass);

        //Waiting for connection
        while(WiFi.status() != WL_CONNECTED){
            delay(100);
            Serial.printf("-");
        }

        Serial.printf("\nWiFi Connected to");
        Serial.printf(ssid);
    }

After the initialization of WiFi and the MQTT publisher we can enrich the loop method with the publisher mechanism and the radiationLoop becomes

    void loopRadiation(){
        int current_state = digitalRead(RADIATION_PORT);
        if(current_state != last_state){
            debounce=debounce+1;
            if(debounce % 2==0){
                client.publish(radiation_topic,"{radiation_tick: true}");
                debounce=0;
            }
        }
        last_state=current_state;
    }

But, what do we mean by publish data with MQTT? Well technically we assume an MQTT message broker for which we publish our data. In this particular case we created a Kubernetes container of the Eclipse Mosquitto MQTT broker and we deployed it on my personal raspberry pi kubernetes cluster.

The physical end result can be seen bellow

Radiation Ticker ESP32

And the values published in our the smart-phone through a MQTT client application that connects to our MQTT broker

Smartphone Radiation MQTT