Links

Food Irradiation Dose Detection

Building a Food Irradiation detection device using a DFRobot ESP32, Geiger Counter, and Visible Light sensor.
Created By: Kutluhan Aktar

Description

Even though food irradiation improves food hygiene, spoilage reduction, and extension of shelf-life, it should be regulated strictly to avoid any health risks and nutritional value drops. However, small businesses in the food industry lack a budget-friendly and simple way to detect food irradiation doses after treating food with ionizing energy, especially for animal (livestock) feed. Therefore, I decided to build an AI-driven IoT device predicting food irradiation doses based on weight, color (visible light), and emitted ionizing radiation.
Ionizing radiation is a nonthermal process utilized to achieve the preservation of food. At a maximum commercial irradiation dose of 10 kGy, irradiation does not impart heat to the food, and the nutritional quality of the food is generally unaffected. The irradiation process can reduce the microbial contamination of food, resulting in improved microbial safety as well as the extended shelf-life of the food[^1]. Irradiation also benefits the consumer by reducing the risk of severe health issues caused by foodborne illnesses. Food irradiation has three categories: low-dose (radurization), medium-dose (radicidation), and high-dose (radappertization). Low dose irradiation (under 1 kGy) inhibits the sprouting of produce (onion, potato, and garlic); retards the ripening and fungi deterioration of fruits and vegetables (strawberry, tomato, etc.), and promotes insect disinfestations in cereals and vegetables. Medium dose irradiation (between 1 and 10 kGy) controls the presence of pathogenic organisms, especially in fruit juices; retards the deterioration of fish and fresh meat; and reduces Salmonella in poultry products, similar to pasteurization. High dose irradiation (over 10 kGy) is rather significant to the sterilization of health and personal hygiene products[^2].
Since foods treated with ionizing radiation should be adequately labeled under the general labeling requirements, consumers can make their own free choice between irradiated and non-irradiated food. However, unfortunately, some countries do not apply strict regulations for irradiated foods, especially for animal feed. Therefore, detecting proper irradiation doses can be arduous for small businesses in the food industry due to governments not incentivizing strictly regulated food irradiation processes. Since irradiation can engender certain alterations that can modify the chemical composition and nutritive values of food, depending on the factors such as irradiation dose, food composition, packaging, and processing conditions such as temperature and atmospheric oxygen saturation[^2], unsupervised food irradiation portends health issues.
After scrutinizing recent research papers on food irradiation, I decided to utilize ionizing radiation, weight, and visible light (color) measurements denoting the applied irradiation dose so as to create a budget-friendly and accessible device to predict food irradiation dose levels in the hope of assisting small businesses in checking compliance with existing regulations on food irradiation.
Although ionizing radiation, weight, and visible light (color) measurements provide insight into detecting food irradiation doses, it is not possible to conclude and interpret food irradiation doses precisely by merely employing limited data without applying complex algorithms since food irradiation dose levels fluctuate depending on processing techniques, food characteristics, and equipment. Therefore, I decided to build and train an artificial neural network model by utilizing the theoretically assigned food irradiation dose classes to predict food irradiation dose levels based on ionizing radiation, weight, and visible light (color) measurements. Since I could not apply ionizing radiation directly to foods by emitting Gamma rays, X-rays, or electron beams, I exposed foods to sun rays as a natural source of radiation for estimated periods.
Since Beetle ESP32-C3 is an ultra-small size development board intended for IoT applications, that can easily collect data and run my neural network model after being trained to predict food irradiation doses, I decided to employ Beetle ESP32-C3 in this project. To obtain the required measurements to train my model, I utilized a Geiger counter module (Gravity), an I2C weight sensor (Gravity), and an AS7341 11-channel visible light sensor (Gravity). Since Beetle ESP32-C3 is equipped with an expansion board providing the GDI display interface, I connected an SSD1309 OLED transparent screen (Fermion) to display the collected data.
After collecting data successfully, I developed a PHP web application that obtains the transmitted data from Beetle ESP32-C3 via HTTP GET requests, logs the received measurements in a given MySQL database table, and lets the user create appropriately formatted samples for Edge Impulse.
After completing my data set and creating samples, I built my artificial neural network model (ANN) with Edge Impulse to make predictions on food irradiation dose levels (classes) based on ionizing radiation, weight, and visible light (color) measurements. Since Edge Impulse is nearly compatible with all microcontrollers and development boards, I had not encountered any issues while uploading and running my model on Beetle ESP32-C3. As labels, I employed the theoretically assigned food irradiation dose classes for each data record while collecting and logging data:
  • Regulated
  • Unsafe
  • Hazardous
After training and testing my neural network model, I deployed and uploaded the model on Beetle ESP32-C3. Therefore, the device is capable of detecting precise food irradiation dose levels (classes) by running the model independently without any additional procedures.
Lastly, to make the device as robust and compact as possible while experimenting with a motley collection of foods, I designed a Hulk-inspired structure with a moveable visible light sensor handle (3D printable).
So, this is my project in a nutshell 😃
In the following steps, you can find more detailed information on coding, logging data via a web application, building a neural network model with Edge Impulse, and running it on Beetle ESP32-C3.
🎁
🎨
Huge thanks to DFRobot for sponsoring these products:
Beetle ESP32-C3 | Inspect
Gravity: Geiger Counter Module | Inspect
Gravity: I2C 1Kg Weight Sensor Kit | Inspect
Gravity: AS7341 11-Channel Visible Light Sensor | Inspect
Fermion: 1.51” OLED Transparent Display | Inspect
🎁
🎨
If you want to purchase products from DFRobot, you can use my $5 discount coupon.
🎁
🎨
Also, huge thanks to Creality for sending me a Creality CR-200B 3D Printer.
image
image
image
image
image
image
image

Step 1: Designing and printing a Hulk-inspired structure

Since this project is for detecting irradiation doses of foods treated with ionizing radiation, I got inspired by the most prominent fictional Gamma radiation expert, Bruce Banner (aka, The Incredible Hulk), to design a unique structure so as to create a robust and compact device flawlessly operating while collecting data from foods. To collect data with the visible light sensor at different angles, I added a moveable handle to the structure, including a slot and a hook for hanging the sensor.
I designed the structure and its moveable handle in Autodesk Fusion 360. You can download their STL files below.
image
image
image
image
image
For the Hulk replica affixed to the top of the structure, I utilized this model from Thingiverse:
Then, I sliced all 3D models (STL files) in Ultimaker Cura.
image
image
image
Since I wanted to create a solid structure for this device with a moveable handle and complement the Hulk theme gloriously, I utilized these PLA filaments:
  • eMarble Natural
  • Peak Green
Finally, I printed all parts (models) with my Creality CR-200B 3D Printer. It is my first fully-enclosed FDM 3D printer, and I must say that I got excellent prints effortlessly with the CR-200B :)
If you are a maker planning to print your 3D models to create more complex projects, I highly recommend the CR-200B. Since the CR-200B is fully-enclosed, you can print high-resolution 3D models with PLA and ABS filaments. Also, it has a smart filament runout sensor and the resume printing option for power failures.
According to my experience, there are only two downsides of the CR-200B: relatively small build size (200 x 200 x 200 mm) and manual leveling. Conversely, thanks to the large leveling nuts and assisted leveling, I was able to level the bed and start printing my first model in less than 30 minutes.
#⃣
Before the first use, remove unnecessary cable ties and apply grease to the rails.
image
image
#⃣
Test the nozzle and hot bed temperatures.
image
#⃣
Go to Settings ➡ Leveling and adjust four predefined points by utilizing the leveling nuts.
image
image
image
image
#⃣
Finally, attach the spool holder and feed the extruder with the filament.
image
#⃣
Since the CR-200B is not officially supported by Cura, select the Ender-3 profile and change the build size to 200 x 200 x 200 mm. Also, to compensate for the nozzle placement, set the Nozzle offset X and Y values to -10 mm on the Extruder 1 tab.
image
image

Step 1.1: Assembling the structure and making connections & adjustments

// Connections
// Beetle ESP32-C3 :
// Gravity: Geiger Counter Module
// D5 --------------------------- D
// VCC --------------------------- +
// GND --------------------------- -
// Gravity: I2C 1Kg Weight Sensor Kit - HX711
// VCC --------------------------- VCC
// GND --------------------------- GND
// D9 --------------------------- SCL
// D8 --------------------------- SDA
// Fermion: 1.51” SSD1309 OLED Transparent Display
// D4 --------------------------- SCLK
// D6 --------------------------- MOSI
// D7 --------------------------- CS
// D2 --------------------------- RES
// D1 --------------------------- DC
// AS7341 11-Channel Spectral Color Sensor
// VCC --------------------------- +
// GND --------------------------- -
// D9 --------------------------- C
// D8 --------------------------- D
// Control Button (A)
// D0 --------------------------- +
// Control Button (B)
// D20 --------------------------- +
// Control Button (C)
// D21 --------------------------- +
First of all, I soldered male pin headers to Beetle ESP32-C3 and its expansion board.
image
Then, to collect ionizing radiation, weight, and color (visible light) measurements, I connected a Geiger counter module (Gravity), an I2C HX711 weight sensor (Gravity), and an AS7341 11-channel visible light sensor (Gravity) to Beetle ESP32-C3. Since the expansion board provides the GDI display interface for DFRobot screens, I was able to connect the SSD1309 OLED transparent screen (Fermion) to Beetle ESP32-C3 via the expansion board.
image
After assembling the weight sensor kit, to calibrate the weight sensor in order to get accurate measurements, press the cal button on the adapter board. Then, wait for the indicator LED to turn on and place a 100 g (default value) object on the scale within 5 seconds. When the adapter board completes calibration, the indicator LED blinks three times.
image
image
Since Beetle ESP32-C3 cannot power the Geiger counter module and the weight sensor simultaneously due to its working current, I connected a USB buck-boost converter board to my Xiaomi power bank to elicit stable 3.3V to supply the sensors.
Since the Geiger counter library needs to use an external interrupt pin for counting, the Geiger counter module can only be connected to external interrupt pins. Plausibly, Beetle ESP32-C3 allows the user to define any pin as an external interrupt.
To assign labels while transmitting the collected data and run my neural network model effortlessly, I added three control buttons (6x6), as shown in the schematic below.
After completing sensor connections and adjustments on breadboards successfully, I made the breadboard connection points rigid by utilizing a hot glue gun.
image
image
After printing all parts (models), I fastened all components except the visible light sensor to their corresponding slots on the structure via the hot glue gun.
Then, I attached the visible light sensor to the moveable handle and hung it via its slot in the structure.
image
image
image
image
image
image
image
image
Finally, I affixed the Hulk replica to the top of the structure via the hot glue gun.
image
image

Step 2: Developing a web application in PHP to collate data on food irradiation doses

To be able to log and process data packets transmitted by Beetle ESP32-C3, I decided to develop a web application in PHP named food_irradiation_data_logger.
As shown below, the web application consists of two folders and five files:
  • /assets
    • class.php
    • icon.png
    • index.css
  • /data
  • get_data.php
  • index.php
I also employed the web application to scale (normalize) and preprocess my data set so as to create appropriately formatted samples for Edge Impulse.
If the data type is not time series, Edge Impulse requires a CSV file with a header indicating data fields per sample to upload data with CSV files. Since Edge Impulse can infer the uploaded sample's label from its file name, the application reads the given data set in the MySQL database and generate a CSV file (sample) for each data record, named according to the assigned food irradiation dose class. Also, the application utilizes the unique row number under the id data field as the sample number to identify each generated CSV file:
  • Regulated.training.sample_101.csv
  • Unsafe.training.sample_542.csv
  • Hazardous.training.sample_152.csv
You can download and inspect the web application in the ZIP file format below.
📁 class.php
In the class.php file, in order to run all functions successfully, I created two classes named _main and sample: the latter inherits from the former.
Define the _main class and its functions:
In the init function, define the required variables for the MySQL database.
public function __init__($conn, $table){
$this->conn = $conn;
$this->table = $table;
}
In the insert_new_data function, append the given measurements and food irradiation dose class to the given database table.
public function insert_new_data($d1, $d2, $d3, $d4, $d5, $d6, $d7, $d8, $d9, $d10, $d11, $d12, $c){
$sql = "INSERT INTO `$this->table`(`weight`, `f1`, `f2`, `f3`, `f4`, `f5`, `f6`, `f7`, `f8`, `cpm`, `nsv`, `usv`, `class`) VALUES ('$d1', '$d2', '$d3', '$d4', '$d5', '$d6', '$d7', '$d8', '$d9', '$d10', '$d11', '$d12', '$c')";
if(mysqli_query($this->conn, $sql)){ return true; }else { return false; }
}
In the database_create_table function, create the required database table.
public function database_create_table(){
// Create a new database table.
$sql_create = "CREATE TABLE `$this->table`(
id int AUTO_INCREMENT PRIMARY KEY NOT NULL,
weight varchar(255) NOT NULL,
f1 varchar(255) NOT NULL,
f2 varchar(255) NOT NULL,
f3 varchar(255) NOT NULL,
f4 varchar(255) NOT NULL,
f5 varchar(255) NOT NULL,
f6 varchar(255) NOT NULL,
f7 varchar(255) NOT NULL,
f8 varchar(255) NOT NULL,
cpm varchar(255) NOT NULL,
nsv varchar(255) NOT NULL,
usv varchar(255) NOT NULL,
`class` varchar(255) NOT NULL
);";
if(mysqli_query($this->conn, $sql_create)) echo("<br><br>Database Table Created Successfully!");
}
Define the sample class, extending the _main class, and its functions:
Define the food irradiation dose class (label) names.
In the count_samples function, count the registered data records (samples) in the given database table.
public $class_names = ["Regulated", "Unsafe", "Hazardous"];
// Count the registered data records (samples) in the given database table.
public function count_samples(){
$count = [
"total" => mysqli_num_rows(mysqli_query($this->conn, "SELECT * FROM `$this->table`")),
"regulated" => mysqli_num_rows(mysqli_query($this->conn, "SELECT * FROM `$this->table` WHERE class='0'")),
"unsafe" => mysqli_num_rows(mysqli_query($this->conn, "SELECT * FROM `$this->table` WHERE class='1'")),
"hazardous" => mysqli_num_rows(mysqli_query($this->conn, "SELECT * FROM `$this->table` WHERE class='2'")),
];
return $count;
}
In the create_sample_files function:
Obtain the registered data records from the given database table.
Scale (normalize) data items to define appropriately formatted inputs in the range of 0-1.
Define the header indicating data elements.
Create an array with the scaled data items.
For each data record, create a CSV file (sample) named with the assigned irradiation dose class and identified with the unique row number under the id data field.
Each sample includes twelve data items [shape=(12,)]:
[15.877, 0.25, 0.76, 0.57, 0.8, 1.89, 2.85, 4.65, 3.63, 0.8, 5.31, 0.53]
public function create_sample_files($type){
// Obtain the registered data records (samples) from the given database table.
$sql = "SELECT * FROM `$this->table`";
$result = mysqli_query($this->conn, $sql);
$check = mysqli_num_rows($result);
if($check > 0){
while($row = mysqli_fetch_assoc($result)){
// Scale (normalize) data items to define appropriately formatted inputs (samples).
$scaled = [
"weight" => $row["weight"] / 10,
"f1" => $row["f1"] / 100,
"f2" => $row["f2"] / 100,
"f3" => $row["f3"] / 100,
"f4" => $row["f4"] / 100,
"f5" => $row["f5"] / 100,
"f6" => $row["f6"] / 100,
"f7" => $row["f7"] / 100,
"f8" => $row["f8"] / 100,
"cpm" => $row["cpm"] / 100,
"nsv" => $row["nsv"] / 100,
"usv" => $row["usv"]
];
// Add the header as the first row.
$processed_data = [
['weight','f1','f2','f3','f4','f5','f6','f7','f8','cpm','nsv','usv'],
[$scaled["weight"],$scaled["f1"],$scaled["f2"],$scaled["f3"],$scaled["f4"],$scaled["f5"],$scaled["f6"],$scaled["f7"],$scaled["f8"],$scaled["cpm"],$scaled["nsv"],$scaled["usv"]]
];
$filename = "data/".$this->class_names[$row["class"]].".".$type.".sample_".$row["id"].".csv";
$f = fopen($filename, "w");
foreach($processed_data as $r){
fputcsv($f, $r);
}
fclose($f);
}
}
}
In the download_samples function, download all generated CSV files (samples) in the ZIP file format.
public function download_samples($zipname){
if(count(scandir("data")) > 2){
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach(glob("data/*.csv") as $sample){
$zip->addFile($sample);
}
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename='$zipname'");
header('Content-Length: ' . filesize($zipname));
header("Location: $zipname");
}else{
header("Location: .");
exit();
}
}
Define the required MySQL database connection settings for Raspberry Pi.
$server = array(
"name" => "localhost",
"username" => "root",
"password" => "bot",
"database" => "foodirradiation",
"table" => "entries"
);
$conn = mysqli_connect($server["name"], $server["username"], $server["password"], $server["database"]);
📁 get_data.php
Include the class.php file.
Define the food object of the _main class with its required parameters.
include_once "assets/class.php";
// Define the new 'food' object:
$food = new _main();
$food->__init__($conn, $server["table"]);
Obtain the transferred information from Beetle ESP32-C3.
Then, insert the received measurements into the given database table.
if(isset($_GET["weight"]) && isset($_GET["F1"]) && isset($_GET["F2"]) && isset($_GET["F3"]) && isset($_GET["F4"]) && isset($_GET["F5"]) && isset($_GET["F6"]) && isset($_GET["F7"]) && isset($_GET["F8"]) && isset($_GET["CPM"]) && isset($_GET["nSv"]) && isset($_GET["uSv"]) && isset($_GET["class"])){
if($food->insert_new_data($_GET["weight"], $_GET["F1"], $_GET["F2"], $_GET["F3"], $_GET["F4"], $_GET["F5"], $_GET["F6"], $_GET["F7"], $_GET["F8"], $_GET["CPM"], $_GET["nSv"], $_GET["uSv"], $_GET["class"])){
echo("Data received and saved successfully!");
}else{
echo("Database error!");
}
}else{
echo("Waiting Data...");
}
If requested, create the required database table (entries).
if(isset($_GET["create_table"]) && $_GET["create_table"] == "OK") $food->database_create_table();
📁 index.php
Include the class.php file.
Define the sample object of the sample class with its required parameters.
include_once "assets/class.php";
// Define the new 'sample' object:
$sample = new sample();
$sample->__init__($conn, $server["table"]);
Elicit the total number of data records (samples) for classes (labels) in the given database table.
$count = $sample->count_samples();
If the user requests via the HTML form, create a CSV file (sample) for each data record in the given database table, depending on the selected data type: training or testing.
if(isset($_POST["data"]) && $_POST["data"] != ""){
$sample->create_sample_files($_POST["data"]);
}
If the Download button is clicked, download all generated CSV files (samples) in the ZIP file format.
if(isset($_GET["download"])){
$sample->download_samples("data.zip");
}
image
image
image
image
image
image

Step 3: Setting up a LAMP web server on Raspberry Pi

Since I decided to host my web application on a Raspberry Pi 3, I needed to set up a LAMP web server.
#⃣
First of all, open a terminal window by selecting Accessories ➡ Terminal from the menu.
#⃣
Then, install the apache2 package by typing the following command into the terminal and pressing Enter:
sudo apt-get install apache2 -y
image
#⃣
After installing the apache2 package successfully, open Chromium Web Browser and navigate to localhost so as to test the web server.
#⃣
Then, enter the command below to the terminal to obtain the Raspberry Pi's IP address:
hostname -I
image
image
#⃣
To install the latest package versions successfully, update the Pi. Then, download the PHP package by entering these commands below to the terminal:
sudo apt-get update
sudo apt-get install php -y
image
#⃣
To be able to create files in the ZIP file format with the web application, install the php-zip package:
sudo apt install php7.3-zip
image
#⃣
Since the web application creates a large ZIP file with the generated CSV files (samples), open the php.ini file in order to modify these configurations:
  • upload_max_filesize
  • max_file_uploads
#⃣
Then, restart the apache server to activate the installed packages on the web server:
sudo service apache2 restart
image

Step 3.1: Creating a MySQL database in MariaDB

Since I needed to log measurements transmitted by Beetle ESP32-C3 so as to create appropriately formatted samples for Edge Impulse, I also set up a MariaDB server on Raspberry Pi 3.
#⃣
First of all, install the MariaDB (MySQL) server and PHP-MySQL packages by entering the following command into the terminal:
sudo apt-get install mariadb-server php-mysql -y
image
#⃣
To create a new user, run the MySQL secure installation command in the terminal window:
sudo mysql_secure_installation
#⃣
When requested, type the current password for the root user (enter for none). Then, press Enter.
#⃣
Type in Y and press Enter to set the root password.
#⃣
Type in bot at the New password: prompt, and press Enter.
#⃣
Type in Y to remove anonymous users.
#⃣
Type in Y to disallow root login remotely.
#⃣
Type in Y to remove the test database and its access permissions.
#⃣
Type in Y to reload privilege tables.
#⃣
After successfully setting the MariaDB server, the terminal prints: All done! Thanks for using MariaDB!
image
#⃣
Finally, to create a new database in the MariaDB server, run the MySQL interface in the terminal:
sudo mysql -uroot -p
#⃣
Then, enter the recently changed root password - bot.
#⃣
When the terminal shows the MariaDB [(none)]> prompt, create the new database (foodirradiation) by utilizing these commands below:
create database foodirradiation;
GRANT ALL PRIVILEGES ON foodirradiation.* TO 'root'@'localhost' IDENTIFIED BY 'bot';
FLUSH PRIVILEGES;
#⃣
Press Ctrl + D to exit the MariaDB [(none)]> prompt.
image

Step 3.2: Setting and running the web application on Raspberry Pi

As discussed above, I set up a LAMP web server on my Raspberry Pi 3 to run the web application, but you can run it on any server as long as it is a PHP server.
#⃣
First of all, install and extract the food_irradiation_data_logger.zip folder.
image
#⃣
Then, move the application folder (food_irradiation_data_logger) to the Apache server (/var/www/html) by using the terminal since the Apache server is a protected location.
sudo mv /home/pi/Downloads/food_irradiation_data_logger /var/www/html/
image
#⃣
Since the Apache server is a protected location, it throws an error while attempting to modify the files and folders in it. Therefore, before utilizing the web application to create CSV files (samples) and download them in the ZIP file format, change the web application's folder permission by using the terminal:
sudo chmod -R 777 /var/www/html/food_irradiation_data_logger
image
💻 On the get_data.php file:
If the web application did not receive measurements from Beetle ESP32-C3 via an HTTP GET request, it prints: Waiting Data...
Otherwise, the web application prints: Data received and saved successfully!
localhost/food_irradiation_data_logger/get_data.php
image
image
If the create_table parameter is set as OK, the web application creates the requested database table (entries) and prints: Database Table Created Successfully!
image
💻 On the index.php file:
The application interface shows created sample names and data record numbers for each class in the MySQL database.
If the user clicks the Create Samples submit button on the HTML form, the web application generates CSV files (samples) for Edge Impulse, depending on the selected data type (training or testing).
image
image
image
image
image
If the user clicks the Download button, the application downloads all generated CSV files (samples) in the ZIP file format (data.zip).
image
image

Step 4: Setting up Beetle ESP32-C3 on the Arduino IDE

Before proceeding with the following steps, I needed to set up Beetle ESP32-C3 on the Arduino IDE and install the required libraries for this project.
If your computer cannot recognize Beetle ESP32-C3 when plugged in via a USB cable, connect Pin 9 to GND (pull-down) and try again.
#⃣
To add the ESP32-C3 board package to the Arduino IDE, navigate to File ➡ Preferences and paste the URL below under Additional Boards Manager URLs.
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
image
image
#⃣
Then, to install the required core, navigate to Tools ➡ Board ➡ Boards Manager and search for esp32.
image
image
#⃣
After installing the core, navigate to Tools > Board > ESP32 Arduino and select ESP32C3 Dev Module.
image
#⃣
To print data on the serial monitor, enable USB CDC On Boot after setting Beetle ESP32-C3.
image
#⃣
Finally, download the required libraries for the Geiger counter module, the I2C HX711 weight sensor, the AS7341 visible light sensor, and the SSD1309 OLED transparent screen:
DFRobot_Geiger | Download DFRobot_HX711_I2C | Download DFRobot_AS7341 | Download U8g2_Arduino | Download

Step 4.1: Displaying images on the SSD1309 transparent OLED screen

To display images (monochrome) on the SSD1309 transparent OLED screen successfully, I needed to convert PNG or JPG files into the XBM (X Bitmap Graphic) file format.
#⃣
First of all, download GIMP.
#⃣
Then, upload an image (black and white) and go to Image ➡ Scale Image... to resize the uploaded image.
image
#⃣
Go to Image ➡ Mode and select Grayscale.
image
#⃣
Finally, export the image as an XBM file.
image
image
#⃣
After exporting the image, add the generated data array to the code and print it on the screen.
u8g2.firstPage();
do{
//u8g2.setBitmapMode(true /* transparent*/);
u8g2.drawXBMP( /* x=*/36 , /* y=*/0 , /* width=*/50 , /* height=*/50 , data_colllect_bits);
}while(u8g2.nextPage());
image
image

Step 5: Collecting and storing food irradiation data w/ Beetle ESP32-C3

After setting up Beetle ESP32-C3 and installing the required libraries, I programmed Beetle ESP32-C3 to collect ionizing radiation, weight, and visible light (color) measurements in order to store them on the MySQL database and create appropriately formatted samples for Edge Impulse.
  • CPM (Counts per Minute)
  • nSv/h (nanoSieverts per hour)
  • μSv/h (microSieverts per hour)
  • Weight (g)
  • F1 (405 - 425 nm)
  • F2 (435 - 455 nm)
  • F3 (470 - 490 nm)
  • F4 (505 - 525 nm)
  • F5 (545 - 565 nm)
  • F6 (580 - 600 nm)
  • F7 (620 - 640 nm)
  • F8 (670 - 690 nm)
Since I needed to assign food irradiation dose levels (classes) theoretically as labels for each data record while collecting data from foods to create a valid data set, I utilized the control buttons attached to Beetle ESP32-C3 so as to choose among irradiation dose classes. After selecting an irradiation dose class, Beetle ESP32-C3 appends the selected class to the collected data and then transmits that data packet to the web application.
  • Control Button (A) ➡ Regulated
  • Control Button (B) ➡ Unsafe
  • Control Button (C) ➡ Hazardous
You can download the IoT_food_irradiation_data_collect.ino file to try and inspect the code for collecting ionizing radiation, weight, and visible light (color) measurements and for transferring information to a given web application.
Include the required libraries.
#include <WiFi.h>
#include <DFRobot_Geiger.h>
#include <DFRobot_HX711_I2C.h>
#include <U8g2lib.h>
#include <SPI.h>
#include "DFRobot_AS7341.h"
Define the Wi-Fi network settings and use the WiFiClient class to create TCP connections.
char ssid[] = "<_SSID_>"; // your network SSID (name)
char pass[] = "<_PASSWORD_>"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
// Define the server (Raspberry Pi).
char server[] = "192.168.1.20";
// Define the web application path.
String application = "/food_irradiation_data_logger/get_data.php";
// Initialize the WiFi client library.
WiFiClient client; /* WiFiSSLClient client; */
Define the Geiger counter module.
Define the I2C HX711 weight sensor.
Define the AS7341 visible light sensor settings and objects.
DFRobot_Geiger geiger(5);
// Define the HX711 weight sensor.
DFRobot_HX711_I2C MyScale;
// Define the AS7341 object.
DFRobot_AS7341 as7341;
// Define AS7341 data objects:
DFRobot_AS7341::sModeOneData_t data1;
DFRobot_AS7341::sModeTwoData_t data2;
Define the 1.51” SSD1309 OLED transparent display settings.
#define OLED_DC 1
#define OLED_CS 7
#define OLED_RST 2
U8G2_SSD1309_128X64_NONAME2_1_4W_HW_SPI u8g2(/* rotation=*/U8G2_R0, /* cs=*/ OLED_CS, /* dc=*/ OLED_DC,/* reset=*/OLED_RST);
Define monochrome graphics.
Initialize the SSD1309 OLED transparent display.
u8g2.begin();
u8g2.setFontPosTop();
//u8g2.setDrawColor(0);
In the err_msg function, display the error message on the SSD1309 OLED transparent screen.
void err_msg(){
// Show the error message on the SSD1309 transparent display.
u8g2.firstPage();
do{
//u8g2.setBitmapMode(true /* transparent*/);
u8g2.drawXBMP( /* x=*/44 , /* y=*/0 , /* width=*/40 , /* height=*/40 , error_bits);
u8g2.setFont(u8g2_font_4x6_tr);
u8g2.drawStr(0, 47, "Check the serial monitor to see");
u8g2.drawStr(40, 55, "the error!");
}while(u8g2.nextPage());
}
Check the connection status between the weight (HX711) sensor and Beetle ESP32-C3.
while (!MyScale.begin()) {
Serial.println("HX711 initialization is failed!");
err_msg();
delay(1000);
}
Serial.println("HX711 initialization is successful!");
Set the calibration weight (g) and threshold (g) to calibrate the weight sensor automatically.
Display the current calibration value on the serial monitor.
MyScale.setCalWeight(100);
// Set the calibration threshold (g).
MyScale.setThreshold(30);
// Display the current calibration value.
Serial.print("\nCalibration Value: "); Serial.println(MyScale.getCalibration());
MyScale.setCalibration(MyScale.getCalibration());
delay(1000);
Check the connection status between the AS7341 visible light sensor and Beetle ESP32-C3. Then, enable the built-in LED on the AS7341 sensor.
while (as7341.begin() != 0) {
Serial.println("AS7341 initialization is failed!");
err_msg();
delay(1000);
}
Serial.println("AS7341 initialization is successful!");
// Enable the built-in LED on the AS7341 sensor.
as7341.enableLed(true);
Initialize the Wi-Fi module.
Attempt to connect to the given Wi-Fi network.
WiFi.begin(ssid, pass);
// Attempt to connect to the WiFi network:
while(WiFi.status() != WL_CONNECTED){
// Wait for the connection:
delay(500);
Serial.print(".");
}
// If connected to the network successfully:
Serial.println("Connected to the WiFi network successfully!");
u8g2.firstPage();
do{
u8g2.setFont(u8g2_font_open_iconic_all_8x_t);
u8g2.drawGlyph(/* x=*/32, /* y=*/0, /* encoding=*/247);
}while(u8g2.nextPage());
delay(2000);
In the get_Weight function, obtain the weight (g) measurement generated by the I2C HX711 weight sensor.
void get_Weight(){
weight = MyScale.readWeight();
if(weight < 0.5) weight = 0;
Serial.print("\nWeight: "); Serial.print(weight); Serial.println(" g");
delay(1000);
}
In the get_Visual_Light function, start spectrum measurement with the AS7341 sensor and read the value of sensor data channel 0~5 under these channel mapping modes:
  • eF1F4ClearNIR
  • eF5F8ClearNIR
void get_Visual_Light(){
// Start spectrum measurement:
// Channel mapping mode: 1.eF1F4ClearNIR
as7341.startMeasure(as7341.eF1F4ClearNIR);
// Read the value of sensor data channel 0~5, under eF1F4ClearNIR
data1 = as7341.readSpectralDataOne();
// Channel mapping mode: 2.eF5F8ClearNIR
as7341.startMeasure(as7341.eF5F8ClearNIR);
// Read the value of sensor data channel 0~5, under eF5F8ClearNIR
data2 = as7341.readSpectralDataTwo();
// Print data:
Serial.print("\nF1(405-425nm): "); Serial.println(data1.ADF1);
Serial.print("F2(435-455nm): "); Serial.println(data1.ADF2);
Serial.print("F3(470-490nm): "); Serial.println(data1.ADF3);
Serial.print("F4(505-525nm): "); Serial.println(data1.ADF4);
Serial.print("F5(545-565nm): "); Serial.println(data2.ADF5);
Serial.print("F6(580-600nm): "); Serial.println(data2.ADF6);
Serial.print("F7(620-640nm): "); Serial.println(data2.ADF7);
Serial.print("F8(670-690nm): "); Serial.println(data2.ADF8);
// CLEAR and NIR:
Serial.print("Clear_1: "); Serial.println(data1.ADCLEAR);
Serial.print("NIR_1: "); Serial.println(data1.ADNIR);
Serial.print("Clear_2: "); Serial.println(data2.ADCLEAR);
Serial.print("NIR_2: "); Serial.println(data2.ADNIR);
delay(1000);
}
In the activate_Geiger_counter function:
Initialize the Geiger counter module and enable the external interrupt.
Every three seconds, pause the count to turn off the external interrupt trigger.
Evaluate the current CPM (Counts per Minute) by dropping the edge pulse within three seconds: the error is ±3CPM.
Obtain the current nSv/h (nanoSieverts per hour).
Obtain the current μSv/h (microSieverts per hour).
void activate_Geiger_counter(){
// Initialize the Geiger counter module and enable the external interrupt.
geiger.start();
delay(3000);
// If necessary, pause the count and turn off the external interrupt trigger.
geiger.pause();
// Evaluate the current CPM (Counts per Minute) by dropping the edge pulse within 3 seconds: the error is ±3CPM.
Serial.print("\nCPM: "); Serial.println(geiger.getCPM());
// Get the current nSv/h (nanoSieverts per hour).
Serial.print("nSv/h: "); Serial.println(geiger.getnSvh());
// Get the current μSv/h (microSieverts per hour).
Serial.print("μSv/h: "); Serial.println(geiger.getuSvh());
}
In the drawNumber function, convert numbers to char arrays with the itoa function so as to display them on the SSD1309 OLED transparent screen.
void drawNumber(int x, int y, int __){
char buf[7];
u8g2.drawStr(x, y, itoa(__, buf, 10));
}
In the home_screen function, display the collected data on the SSD1309 OLED transparent screen.
void home_screen(int y, int x, int s){
u8g2.firstPage();
do{
u8g2.setFont(u8g2_font_open_iconic_all_2x_t);
u8g2.drawGlyph(/* x=*/0, /* y=*/y-3, /* encoding=*/142);
u8g2.drawGlyph(/* x=*/0, /* y=*/y+s-3, /* encoding=*/259);
u8g2.drawGlyph(/* x=*/0, /* y=*/y+(2*s)-3, /* encoding=*/280);
u8g2.setFont(u8g2_font_freedoomr10_mu);
u8g2.drawStr(25, y, "WEIGHT:"); drawNumber(x, y, weight);
u8g2.drawStr(25, y+s, "F1:"); drawNumber(x, y+s, data1.ADF1);
u8g2.drawStr(25, y+(2*s), "CPM:"); drawNumber(x, y+(2*s), geiger.getCPM());
}while(u8g2.nextPage());
}
In the make_a_get_request function:
Connect to the web application named food_irradiation_data_logger.
Create the query string with the collected data.
Make an HTTP GET request with the data parameters to the web application.
Wait until the client is available, then fetch the response from the web application.
If there is a response from the server and the web application appends the transferred data packet to the MySQL database successfully, print Data registered successfully! on the serial monitor and the SSD1309 screen.
void make_a_get_request(String _class){
// Connect to the web application named food_irradiation_data_logger. Change '80' with '443' if you are using SSL connection.
if (client.connect(server, 80)){
// If successful:
Serial.println("\nConnected to the web application successfully!");
// Create the query string:
String query = application+"?weight="+String(weight)+"&F1="+data1.ADF1+"&F2="+data1.ADF2+"&F3="+data1.ADF3+"&F4="+data1.ADF4+"&F5="+data2.ADF5+"&F6="+data2.ADF6+"&F7="+data2.ADF7+"&F8="+data2.ADF8;
query += "&CPM="+String(geiger.getCPM())+"&nSv="+String(geiger.getnSvh())+"&uSv="+String(geiger.getuSvh());
query += "&class="+_class;
// Make an HTTP Get request:
client.println("GET " + query + " HTTP/1.1");
client.println("Host: 192.168.1.20");
client.println("Connection: close");
client.println();
}else{
Serial.println("\nConnection failed to the web application!");
err_msg();
}
delay(2000); // Wait 2 seconds after connecting...
// If there are incoming bytes available, get the response from the web application.
String response = "";
while (client.available()) { char c = client.read(); response += c; }
if(response != "" && response.indexOf("Data received and saved successfully!") > 0){
Serial.println("Data registered successfully!");
u8g2.firstPage();
do{
//u8g2.setBitmapMode(true /* transparent*/);
u8g2.drawXBMP( /* x=*/36 , /* y=*/0 , /* width=*/50 , /* height=*/50 , data_colllect_bits);
u8g2.setFont(u8g2_font_4x6_tr);
u8g2.drawStr(6, 55, "Data registered successfully!");
}while(u8g2.nextPage());
}
}
According to the pressed control button (A, B, or C), transmit the data packet to the given web application, including the selected food irradiation dose class.
if(!digitalRead(button_A)) make_a_get_request("0");
if(!digitalRead(button_B)) make_a_get_request("1");
if(!digitalRead(button_C)) make_a_get_request("2");
image
image
image
image
image

Step 5.1: Logging the collected data into the MySQL database

After uploading and running the code for collecting data and transmitting data packets to the web application on Beetle ESP32-C3:
🍱
The device waits for the Wi-Fi module to connect to the given Wi-Fi network.
image
🍱
Then, the device displays a modicum of the collected data on the SSD1309 OLED transparent screen.
  • WEIGHT (g)
  • F1 (405 - 425 nm)
  • CPM (Counts per Minute)
🍱
The device allows the user to collect visible light (color) data at different angles with the moveable handle.
image
image
image
🍱
If one of the control buttons (A, B, or C) is pressed, the device transmits the recently collected data by adding the selected food irradiation dose class to the given web application.
  • Control Button (A) ➡ Regulated [0]
  • Control Button (B) ➡ Unsafe [1]
  • Control Button (C) ➡ Hazardous [2]
🍱
Then, if the web application appends the transferred data packet to the MySQL database successfully, the device shows this message on the SSD1309 OLED transparent screen: Data registered successfully!
image
🍱
If Beetle ESP32-C3 throws an error while operating, the device shows the error message on the SSD1309 OLED transparent screen and prints the error details on the serial monitor.
image
image
🍱
Also, the device prints notifications and sensor measurements on the serial monitor for debugging.
image
image
As far as my experiments go, the device operates impeccably while collecting measurements and transmitting data packets to a given web application :)
image

Step 5.2: Creating samples from data records with the web application

After logging ionizing radiation, weight, and visible light (color) measurements in the MySQL database from a motley collection of foods, exposed to sun rays as a natural source of radiation for estimated periods, I elicited my data set with eminent validity.
📌Foods:
  • Pasta
  • Corn kernel
  • Herb
  • Apple
  • Wheat
  • Animal (livestock) feed
image
image
image
image