Sitemap / Advertise

Introduction

Collate weight, color, and emitted ionizing radiation of foods to train a NN. Then, run it on Beetle C3 to detect food irradiation doses.


Tags

Share

IoT AI-driven Food Irradiation Dose Detector w/ Edge Impulse

Advertisement:


read_later

Read Later



read_later

Read Later

Introduction

Collate weight, color, and emitted ionizing radiation of foods to train a NN. Then, run it on Beetle C3 to detect food irradiation doses.

Tags

Share





Advertisement

Advertisement




    Components :
  • [1]DFRobot Beetle ESP32 - C3
  • [1]Raspberry Pi 3B+ or 4
  • [1]DFRobot Gravity: Geiger Counter Module
  • [1]DFRobot Gravity: I2C 1Kg Weight Sensor
  • [1]DFRobot Fermion: 1.51” OLED Transparent Display
  • [1]DFRobot Gravity: AS7341 11-Channel Visible Light Sensor
  • [1]Creality CR-200B 3D Printer
  • [3]Button (6x6)
  • [1]Xiaomi 20000 mAh 3 Pro Type-C Power Bank
  • [1]USB Buck-Boost Converter Board
  • [1]Breadboard
  • [1]Mini Breadboard
  • [1]Jumper Wires

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:

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.

project-image
Figure - 79.1


project-image
Figure - 79.2


project-image
Figure - 79.3


project-image
Figure - 79.4


project-image
Figure - 79.5


project-image
Figure - 79.6


project-image
Figure - 79.7

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.

project-image
Figure - 79.8


project-image
Figure - 79.9


project-image
Figure - 79.10


project-image
Figure - 79.11


project-image
Figure - 79.12

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.

project-image
Figure - 79.13


project-image
Figure - 79.14


project-image
Figure - 79.15

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:

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.

project-image
Figure - 79.16


project-image
Figure - 79.17

#️⃣ Test the nozzle and hot bed temperatures.

project-image
Figure - 79.18

#️⃣ Go to Settings ➡ Leveling and adjust four predefined points by utilizing the leveling nuts.

project-image
Figure - 79.19


project-image
Figure - 79.20


project-image
Figure - 79.21


project-image
Figure - 79.22

#️⃣ Finally, attach the spool holder and feed the extruder with the filament.

project-image
Figure - 79.23

#️⃣ 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.

project-image
Figure - 79.24


project-image
Figure - 79.25

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.

project-image
Figure - 79.26

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.

project-image
Figure - 79.27

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.

project-image
Figure - 79.28


project-image
Figure - 79.29

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.

project-image
Figure - 79.30


project-image
Figure - 79.31

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.

project-image
Figure - 79.32


project-image
Figure - 79.33


project-image
Figure - 79.34


project-image
Figure - 79.35


project-image
Figure - 79.36


project-image
Figure - 79.37


project-image
Figure - 79.38


project-image
Figure - 79.39

Finally, I affixed the Hulk replica to the top of the structure via the hot glue gun.

project-image
Figure - 79.40


project-image
Figure - 79.41

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:

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:

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");
	}	

project-image
Figure - 79.42


project-image
Figure - 79.43


project-image
Figure - 79.44


project-image
Figure - 79.45


project-image
Figure - 79.46


project-image
Figure - 79.47

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

project-image
Figure - 79.48

#️⃣ 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

project-image
Figure - 79.49


project-image
Figure - 79.50

#️⃣ 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

project-image
Figure - 79.51

#️⃣ 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

project-image
Figure - 79.52

#️⃣ 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:

#️⃣ Then, restart the apache server to activate the installed packages on the web server:

sudo service apache2 restart

project-image
Figure - 79.53

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

project-image
Figure - 79.54

#️⃣ 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!

project-image
Figure - 79.55

#️⃣ 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.

project-image
Figure - 79.56

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.

project-image
Figure - 79.57

#️⃣ 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/

project-image
Figure - 79.58

#️⃣ 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

project-image
Figure - 79.59

💻 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

project-image
Figure - 79.60


project-image
Figure - 79.61

⭐ If the create_table parameter is set as OK, the web application creates the requested database table (entries) and prints: Database Table Created Successfully!

project-image
Figure - 79.62

💻 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).

project-image
Figure - 79.63


project-image
Figure - 79.64


project-image
Figure - 79.65


project-image
Figure - 79.66


project-image
Figure - 79.67

⭐ If the user clicks the Download button, the application downloads all generated CSV files (samples) in the ZIP file format (data.zip).

project-image
Figure - 79.68


project-image
Figure - 79.69

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

project-image
Figure - 79.70


project-image
Figure - 79.71

#️⃣ Then, to install the required core, navigate to Tools ➡ Board ➡ Boards Manager and search for esp32.

project-image
Figure - 79.72


project-image
Figure - 79.73

#️⃣ After installing the core, navigate to Tools > Board > ESP32 Arduino and select ESP32C3 Dev Module.

project-image
Figure - 79.74

#️⃣ To print data on the serial monitor, enable USB CDC On Boot after setting Beetle ESP32-C3.

project-image
Figure - 79.75

#️⃣ 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.

project-image
Figure - 79.76

#️⃣ Go to Image ➡ Mode and select Grayscale.

project-image
Figure - 79.77

#️⃣ Finally, export the image as an XBM file.

project-image
Figure - 79.78


project-image
Figure - 79.79

#️⃣ 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());

project-image
Figure - 79.80


project-image
Figure - 79.81

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.

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.

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:


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");

project-image
Figure - 79.82


project-image
Figure - 79.83


project-image
Figure - 79.84


project-image
Figure - 79.85


project-image
Figure - 79.86

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.

project-image
Figure - 79.87

☢️🍱 Then, the device displays a modicum of the collected data on the SSD1309 OLED transparent screen.

☢️🍱 The device allows the user to collect visible light (color) data at different angles with the moveable handle.

project-image
Figure - 79.88


project-image
Figure - 79.89


project-image
Figure - 79.90

☢️🍱 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.

☢️🍱 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!

project-image
Figure - 79.91

☢️🍱 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.

project-image
Figure - 79.92


project-image
Figure - 79.93

☢️🍱 Also, the device prints notifications and sensor measurements on the serial monitor for debugging.

project-image
Figure - 79.94


project-image
Figure - 79.95

As far as my experiments go, the device operates impeccably while collecting measurements and transmitting data packets to a given web application :)

project-image
Figure - 79.96

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:

project-image
Figure - 79.97


project-image
Figure - 79.98


project-image
Figure - 79.99


project-image
Figure - 79.100


project-image
Figure - 79.101


project-image
Figure - 79.102


project-image
Figure - 79.103

As explained in Step 2, I generated a CSV file (sample) for each data record in the MySQL database by utilizing the web application.

☢️🍱 The web application shows the total number of data records for classes (labels) in the database.

project-image
Figure - 79.104

☢️🍱 If the user clicks the Create Samples button, the web application scales data items and generates a CSV file (sample) for each data record, depending on the selected data type (training or testing).

project-image
Figure - 79.105


project-image
Figure - 79.106


project-image
Figure - 79.107


project-image
Figure - 79.108

☢️🍱 If the user clicks the Download button, the web application downloads all generated CSV files (samples) in the ZIP file format.

📌 Training samples:

project-image
Figure - 79.109


project-image
Figure - 79.110

📌 Testing samples:

project-image
Figure - 79.111


project-image
Figure - 79.112

Step 6: Building a neural network model with Edge Impulse

When I completed collating my food irradiation dose data set and assigning labels, I had started to work on my artificial neural network model (ANN) to make predictions on food irradiation dose levels (classes) based on ionizing radiation, weight, and visible light (color) measurements.

Since Edge Impulse supports almost every microcontroller and development board due to its model deployment options, I decided to utilize Edge Impulse to build my artificial neural network model. Also, Edge Impulse makes scaling embedded ML applications easier and faster for edge devices such as Beetle ESP32-C3.

Even though Edge Impulse supports CSV files to upload samples, the data type should be time series to upload all data records in a single file. Therefore, I needed to follow the steps below to format my data set so as to train my model accurately:

As explained in the previous steps, I utilized the web application to scale (normalize) and preprocess data records to create CSV files (samples) for Edge Impulse.

Since the assigned classes are stored under the class data field in the MySQL database, I preprocessed my data set effortlessly to obtain labels for each data record while generating samples:

Plausibly, Edge Impulse allows building predictive models optimized in size and accuracy automatically and deploying the trained model as an Arduino library. Therefore, after scaling (normalizing) and preprocessing my data set to create samples, I was able to build an accurate neural network model to forecast food irradiation dose levels and run it on Beetle ESP32-C3 effortlessly.

You can inspect my neural network model on Edge Impulse as a public project.

Step 6.1: Uploading samples to Edge Impulse

After generating training and testing samples successfully, I uploaded them to my project on Edge Impulse.

#️⃣ First of all, sign up for Edge Impulse and create a new project.

project-image
Figure - 79.113

#️⃣ Navigate to the Data acquisition page and click the Upload existing data button.

project-image
Figure - 79.114


project-image
Figure - 79.115

#️⃣ Then, choose the data category (training or testing) and select Infer from filename under Label to deduce labels from file names automatically.

#️⃣ Finally, select files and click the Begin upload button.

project-image
Figure - 79.116


project-image
Figure - 79.117


project-image
Figure - 79.118


project-image
Figure - 79.119


project-image
Figure - 79.120


project-image
Figure - 79.121

Step 6.2: Training the model on food irradiation dose levels

After uploading my training and testing samples successfully, I designed an impulse and trained it on food irradiation dose levels (classes).

An impulse is a custom neural network model in Edge Impulse. I created my impulse by employing the Raw Data block and the Classification learning block.

The Raw Data block generate windows from data samples without any specific signal processing.

The Classification learning block represents a Keras neural network model. Also, it lets the user change the model settings, architecture, and layers.

#️⃣ Go to the Create impulse page. Then, select the Raw Data block and the Classification learning block. Finally, click Save Impulse.

project-image
Figure - 79.122

#️⃣ Before generating features for the model, go to the Raw data page and click Save parameters.

project-image
Figure - 79.123

#️⃣ After saving parameters, click Generate features to apply the Raw Data block to training samples.

project-image
Figure - 79.124


project-image
Figure - 79.125

#️⃣ Finally, navigate to the NN Classifier page and click Start training.

project-image
Figure - 79.126


project-image
Figure - 79.127

According to my experiments with my neural network model, I modified classification model settings, architecture, and layers to build a neural network model with high accuracy and validity:

📌 Neural network settings:

📌 Extra layers:

After generating features and training my model with training samples, Edge Impulse evaluated the precision score (accuracy) as 100%.

The precision score is approximately 100% due to the volume and variety of training samples. In technical terms, the model overfits the training data set. Therefore, I am still collecting data to improve my training data set.

project-image
Figure - 79.128


project-image
Figure - 79.129

Step 6.3: Evaluating the model accuracy and deploying the model

After building and training my neural network model, I tested its accuracy and validity by utilizing testing samples.

The evaluated accuracy of the model is 96.30%.

#️⃣ To validate the trained model, go to the Model testing page and click Classify all.

project-image
Figure - 79.130


project-image
Figure - 79.131


project-image
Figure - 79.132

After validating my neural network model, I deployed it as a fully optimized and customizable Arduino library.

#️⃣ To deploy the validated model as an Arduino library, navigate to the Deployment page and select Arduino library.

#️⃣ Then, choose the Quantized (int8) optimization option to get the best performance possible while running the deployed model.

#️⃣ Finally, click Build to download the model as an Arduino library.

project-image
Figure - 79.133


project-image
Figure - 79.134


project-image
Figure - 79.135

Step 7: Setting up the Edge Impulse model on Beetle ESP32-C3

After building, training, and deploying my model as an Arduino library on Edge Impulse, I needed to upload and run the Arduino library on Beetle ESP32-C3 directly so as to create an easy-to-use and capable device operating with minimal latency and power consumption.

Since Edge Impulse optimizes and formats signal processing, configuration, and learning blocks into a single package while deploying models as Arduino libraries, I was able to import my model effortlessly to run inferences.

#️⃣ After downloading the model as an Arduino library in the ZIP file format, go to Sketch > Include Library > Add .ZIP Library...

#️⃣ Then, include the IoT_AI-driven_Food_Irradiation_Classifier_inferencing.h file to import the Edge Impulse neural network model.


#include <IoT_AI-driven_Food_Irradiation_Classifier_inferencing.h>

After importing my model successfully to the Arduino IDE, I employed the control button (B) attached to Beetle ESP32-C3 to run inferences so as to predict food irradiation dose levels:

You can download the IoT_food_irradiation_run_model.ino file to try and inspect the code for running Edge Impulse neural network models on Beetle ESP32-C3.

You can inspect the corresponding functions and settings in Step 5.

⭐ Include the required libraries.


#include <DFRobot_Geiger.h>
#include <DFRobot_HX711_I2C.h>
#include <U8g2lib.h>
#include <SPI.h>
#include "DFRobot_AS7341.h"

// Include the Edge Impulse model converted to an Arduino library:
#include <IoT_AI-driven_Food_Irradiation_Classifier_inferencing.h>

⭐ Define the required parameters to run an inference with the Edge Impulse model.

⭐ Define the features array (buffer) to classify one frame of data.


#define FREQUENCY_HZ        EI_CLASSIFIER_FREQUENCY
#define INTERVAL_MS         (1000 / (FREQUENCY_HZ + 1))

// Define the features array to classify one frame of data.
float features[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE];
size_t feature_ix = 0;

⭐ Define the threshold value (0.60) for the model outputs (predictions).

⭐ Define the food irradiation dose class names:


float threshold = 0.60;

// Define the food irradiation dose (class) names:
String classes[] = {"Hazardous", "Regulated", "Unsafe"};

⭐ Define monochrome graphics.

⭐ Create an array including icons for each food irradiation dose class.


static const unsigned char *class_icons[] U8X8_PROGMEM = {hazardous_bits, regulated_bits, unsafe_bits};

⭐ In the run_inference_to_make_predictions function:

⭐ Scale (normalize) the collected data depending on the given model and copy the scaled data items to the features array (buffer).

⭐ If required, multiply the scaled data items while copying them to the features array (buffer).

⭐ Display the progress of copying data to the features buffer on the serial monitor.

⭐ If the features buffer is full, create a signal object from the features buffer (frame).

⭐ Then, run the classifier.

⭐ Print the inference timings on the serial monitor.

⭐ Read the prediction (detection) result for each food irradiation dose class (label).

⭐ Print the prediction results on the serial monitor.

⭐ Obtain the detection result greater than the given threshold (0.60). It represents the most accurate label (food irradiation dose class) predicted by the model.

⭐ Print the detected anomalies on the serial monitor, if any.

⭐ Finally, clear the features buffer (frame).


void run_inference_to_make_predictions(int multiply){
  // Scale (normalize) data items depending on the given model:
  float scaled_weight = weight / 10;
  float scaled_F1 = data1.ADF1 / 100;
  float scaled_F2 = data1.ADF2 / 100;
  float scaled_F3 = data1.ADF3 / 100;
  float scaled_F4 = data1.ADF4 / 100;
  float scaled_F5 = data2.ADF5 / 100;
  float scaled_F6 = data2.ADF6 / 100;
  float scaled_F7 = data2.ADF7 / 100;
  float scaled_F8 = data2.ADF8 / 100;
  float scaled_CPM = geiger.getCPM() / 100;
  float scaled_nSv = geiger.getnSvh() / 100;
  float scaled_uSv = geiger.getuSvh();
  
  // Copy the scaled data items to the features buffer.
  // If required, multiply the scaled data items while copying them to the features buffer.
  for(int i=0; i<multiply; i++){  
    features[feature_ix++] = scaled_weight;
    features[feature_ix++] = scaled_F1;
    features[feature_ix++] = scaled_F2;
    features[feature_ix++] = scaled_F3;
    features[feature_ix++] = scaled_F4;
    features[feature_ix++] = scaled_F5;
    features[feature_ix++] = scaled_F6;
    features[feature_ix++] = scaled_F7;
    features[feature_ix++] = scaled_F8;
    features[feature_ix++] = scaled_CPM;
    features[feature_ix++] = scaled_nSv;
    features[feature_ix++] = scaled_uSv;
  }

  // Display the progress of copying data to the features buffer.
  Serial.print("\nFeatures Buffer Progress: "); Serial.print(feature_ix); Serial.print(" / "); Serial.println(EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE);
  
  // Run inference:
  if(feature_ix == EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE){    
    ei_impulse_result_t result;
    // Create a signal object from the features buffer (frame).
    signal_t signal;
    numpy::signal_from_buffer(features, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);
    // Run the classifier:
    EI_IMPULSE_ERROR res = run_classifier(&signal, &result, false);
    ei_printf("\nrun_classifier returned: %d\n", res);
    if(res != 0) return;

    // Print the inference timings on the serial monitor.
    ei_printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n", 
        result.timing.dsp, result.timing.classification, result.timing.anomaly);

    // Obtain the prediction results for each label (class).
    for(size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++){
      // Print the prediction results on the serial monitor.
      ei_printf("%s:\t%.5f\n", result.classification[ix].label, result.classification[ix].value);
      // Get the predicted label (class).
      if(result.classification[ix].value >= threshold) predicted_class = ix;
    }
    Serial.print("\nPredicted Class: "); Serial.println(predicted_class);

    // Detect anomalies, if any:
    #if EI_CLASSIFIER_HAS_ANOMALY == 1
      ei_printf("Anomaly : \t%.3f\n", result.anomaly);
    #endif

    // Clear the features buffer (frame):
    feature_ix = 0;
  }
}

⭐ If the control button (B) is pressed, start running inference with the Edge Impulse model to predict the food irradiation dose level.

⭐ Wait until the Edge Impulse model predicts a food irradiation dose level (label) successfully.

⭐ Then, display the prediction (detection) result (class) on the SSD1309 OLED transparent screen with its assigned monochrome icon.

⭐ Clear the predicted label (class).

⭐ Finally, stop the running inference and return to the home screen.


  if(!digitalRead(button_B)){
    model_activation = true;
    u8g2.firstPage();  
    do{
      u8g2.setFont(u8g2_font_open_iconic_all_8x_t);
      u8g2.drawGlyph(/* x=*/32, /* y=*/0, /* encoding=*/233);  
    }while(u8g2.nextPage());
  }
  while(model_activation){
    get_Weight();
    get_Visual_Light();
    activate_Geiger_counter();

    // Run inference:
    run_inference_to_make_predictions(1);

    // If the Edge Impulse model predicted a label (class) successfully:
    if(predicted_class != -1){
      // Display the predicted class:
      String c = "Class: " + classes[predicted_class];
      int str_x = c.length() * 4;
      u8g2.firstPage();  
      do{
        //u8g2.setBitmapMode(true /* transparent*/);
        u8g2.drawXBMP( /* x=*/(u8g2.getDisplayWidth()-50)/2 , /* y=*/0 , /* width=*/50 , /* height=*/50 , class_icons[predicted_class]);
        u8g2.setFont(u8g2_font_4x6_tr);
        u8g2.drawStr((u8g2.getDisplayWidth()-str_x)/2, 55, c.c_str());
      }while(u8g2.nextPage());      
      
      // Clear the predicted class (label).
      predicted_class = -1;

      // Stop the running inference and return to the home screen.
      model_activation = false;
    }
  }

project-image
Figure - 79.136


project-image
Figure - 79.137


project-image
Figure - 79.138


project-image
Figure - 79.139


project-image
Figure - 79.140

Step 8: Running the model on Beetle ESP32-C3 to make predictions on food irradiation doses

When the features array (buffer) is full with data items, my Edge Impulse neural network model predicts possibilities of labels (food irradiation dose classes) for the given features buffer as an array of 3 numbers. They represent the model's "confidence" that the given features buffer corresponds to each of the three different food irradiation dose levels (classes) based on ionizing radiation, weight, and visible light (color) measurements [0 - 2], as shown in Step 6:

After executing the IoT_food_irradiation_run_model.ino file on Beetle ESP32-C3:

☢️🍱 The device displays a modicum of the collected data on the SSD1309 OLED transparent screen.

project-image
Figure - 79.141

☢️🍱 If the control button (B) is pressed, the device runs an inference with the Edge Impulse model by filling the features buffer with the recently collected ionizing radiation, weight, and visible light (color) measurements.

☢️🍱 When the device starts filling the features buffer with data items, it shows:

project-image
Figure - 79.142

☢️🍱 Then, the device displays the detection result, which represents the most accurate label (food irradiation dose class) predicted by the model.

☢️🍱 Each food irradiation dose level (class) has a unique monochrome icon to be shown on the SSD1309 OLED transparent screen when being predicted (detected) by the model:

project-image
Figure - 79.143


project-image
Figure - 79.144


project-image
Figure - 79.145

☢️🍱 Also, the device prints notifications and sensor measurements on the serial monitor for debugging.

project-image
Figure - 79.146


project-image
Figure - 79.147

As far as my experiments go, the device predicts food irradiation dose levels (classes) accurately by employing the collected measurements :)

project-image
Figure - 79.148

Videos and Conclusion



After completing all steps above and experimenting, I have employed the device to predict and detect food irradiation dose levels of various foods and food packaging so as to check whether they conform to health and safety standards regarding food irradiation.

project-image
Figure - 79.149

Further Discussions

By applying neural network models trained on ionizing radiation, weight, and visible light (color) measurements in detecting food irradiation dose levels, we can achieve to[3]:

☢️🍱 prevent changes to the packaging that might affect integrity as a barrier to microbial contamination,

☢️🍱 avert producing radiolysis products that could migrate into food, affecting odor, taste, and possibly the safety of the food,

☢️🍱 preclude inadvertent radiation effects on polymers in food packaging due to competing crosslinking or chain scission reactions.

project-image
Figure - 79.150

References

[1] Vanee Komolprasert. "CHAPTER 6: PACKAGING FOR FOODS TREATED BY IONIZING RADIATION." Packaging for Nonthermal Processing of Food. Blackwell Publishing, First edition, 2007. 87 - 88.

[2] Ana Paula Dionísio, Renata Takassugui Gomes, and Marília Oetterer. Ionizing Radiation Effects on Food Vitamins – A Review. Braz. Arch. Biol. Technol. v.52 n.5: pp. 1267-1278, Sept/Oct 2009

[3] Kim M. Morehouse and Vanee Komolprasert. Overview of Irradiation of Food and Packaging. ACS Symposium Series 875, Irradiation of Food and Packaging, 2004, Chapter 1, Pages 1-11. https://www.fda.gov/food/irradiation-food-packaging/overview-irradiation-food-and-packaging.

Code

IoT_food_irradiation_data_collect.ino

Download



         /////////////////////////////////////////////  
        //   IoT AI-driven Food Irradiation Dose   // 
       //        Detector w/ Edge Impulse         //
      //           -----------------             //
     //            (Beetle ESP32-C3)            //           
    //             by Kutluhan Aktar           // 
   //                                         //
  /////////////////////////////////////////////

// 
// Collate weight, color, and emitted ionizing radiation of foods to train a NN. Then, run it on Beetle C3 to detect food irradiation doses.
//
// For more information:
// https://www.theamplituhedron.com/projects/IoT_AI_driven_Food_Irradiation_Dose_Detector_w_Edge_Impulse
//
//
// 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  --------------------------- +


// 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"

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.
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” OLED transparent display (SSD1309).
#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:
static const unsigned char error_bits[] U8X8_PROGMEM = {
   0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xf0, 0xff, 0x0f, 0x00, 0x00, 0xfc,
   0xff, 0x3f, 0x00, 0x00, 0xfe, 0xff, 0x7f, 0x00, 0x80, 0xff, 0xff, 0xff,
   0x01, 0xc0, 0xff, 0x81, 0xff, 0x03, 0xe0, 0xff, 0x00, 0xff, 0x07, 0xf0,
   0xff, 0x00, 0xff, 0x0f, 0xf0, 0x7f, 0x00, 0xfe, 0x0f, 0xf8, 0x7f, 0x00,
   0xfe, 0x1f, 0xfc, 0x7f, 0x00, 0xfe, 0x3f, 0xfc, 0xff, 0x00, 0xff, 0x3f,
   0xfe, 0xff, 0x00, 0xff, 0x7f, 0xfe, 0xff, 0x00, 0xff, 0x7f, 0xfe, 0xff,
   0x00, 0xff, 0x7f, 0xfe, 0xff, 0x00, 0xff, 0x7f, 0xff, 0xff, 0x00, 0xff,
   0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff,
   0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0x81,
   0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0xff,
   0xfe, 0xff, 0xc3, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0x7f, 0xfe, 0xff,
   0xff, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0x7f, 0xfc, 0xff, 0xc3, 0xff,
   0x3f, 0xfc, 0xff, 0x81, 0xff, 0x3f, 0xf8, 0xff, 0x81, 0xff, 0x1f, 0xf0,
   0xff, 0x81, 0xff, 0x0f, 0xf0, 0xff, 0x81, 0xff, 0x0f, 0xe0, 0xff, 0xc3,
   0xff, 0x07, 0xc0, 0xff, 0xff, 0xff, 0x03, 0x80, 0xff, 0xff, 0xff, 0x01,
   0x00, 0xfe, 0xff, 0x7f, 0x00, 0x00, 0xfc, 0xff, 0x3f, 0x00, 0x00, 0xf0,
   0xff, 0x0f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00
};
static const unsigned char data_colllect_bits[] U8X8_PROGMEM = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x0f, 0xf0, 0x3f, 0x00,
   0x1f, 0x00, 0x70, 0x1c, 0x30, 0x30, 0x80, 0x39, 0x00, 0x18, 0x30, 0x10,
   0x20, 0xc0, 0x20, 0x00, 0x8c, 0x63, 0xf0, 0x3f, 0x40, 0x60, 0x00, 0xc6,
   0xc6, 0x10, 0x20, 0x70, 0xf0, 0x00, 0x46, 0xcc, 0x10, 0x20, 0x38, 0x98,
   0x01, 0x42, 0x8c, 0xf0, 0x3f, 0x18, 0x08, 0x01, 0xc2, 0x86, 0xf0, 0x3f,
   0x18, 0x80, 0x01, 0xe2, 0x8f, 0x10, 0x20, 0xf0, 0xff, 0x01, 0x76, 0x9c,
   0xf0, 0x3f, 0xe0, 0xff, 0x00, 0x1e, 0xf0, 0xe0, 0x1f, 0xe0, 0x00, 0x00,
   0x1c, 0x60, 0x00, 0x03, 0x70, 0x00, 0x00, 0x18, 0x70, 0x00, 0x03, 0x38,
   0x00, 0x00, 0x70, 0xf8, 0x00, 0x03, 0x1c, 0x00, 0x00, 0xe0, 0xcf, 0xe1,
   0x1f, 0x0e, 0x00, 0x00, 0x00, 0x80, 0xfb, 0x7f, 0x07, 0x00, 0x00, 0x00,
   0x00, 0xdf, 0xec, 0x03, 0x00, 0x00, 0x00, 0x00, 0x66, 0x98, 0x01, 0x00,
   0x00, 0x00, 0x00, 0x67, 0x98, 0x03, 0x00, 0x00, 0xfe, 0x07, 0xff, 0xff,
   0x83, 0xff, 0x01, 0x06, 0x8c, 0x21, 0x10, 0xc6, 0x80, 0x01, 0x06, 0x8c,
   0x31, 0x30, 0xc6, 0x80, 0x01, 0xfe, 0x8f, 0x31, 0x30, 0xc6, 0xff, 0x01,
   0x06, 0xfc, 0xff, 0xff, 0xff, 0x80, 0x01, 0x06, 0xfc, 0xff, 0xff, 0xff,
   0x80, 0x01, 0xfe, 0x8f, 0x31, 0x30, 0xc6, 0xff, 0x01, 0x06, 0x8c, 0x31,
   0x30, 0xc6, 0x80, 0x01, 0x06, 0x8c, 0x21, 0x10, 0xc6, 0x80, 0x01, 0xfe,
   0x07, 0xff, 0xff, 0x83, 0xff, 0x01, 0x00, 0x00, 0x67, 0x98, 0x03, 0x00,
   0x00, 0x00, 0x00, 0x66, 0x98, 0x01, 0x00, 0x00, 0x00, 0x00, 0xdf, 0xec,
   0x03, 0x00, 0x00, 0x00, 0x80, 0xfb, 0x7f, 0x07, 0x00, 0x00, 0x00, 0xc0,
   0xe1, 0x1f, 0x0e, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x03, 0x1c, 0x00, 0x00,
   0x00, 0x70, 0x00, 0x03, 0x38, 0x00, 0x00, 0x00, 0x20, 0x00, 0x03, 0xfc,
   0xff, 0x01, 0xe0, 0x01, 0xe0, 0x1f, 0x0c, 0x80, 0x01, 0xf8, 0x07, 0xf0,
   0x3f, 0x04, 0x00, 0x01, 0x0e, 0x1c, 0x10, 0x20, 0x24, 0x00, 0x01, 0xc4,
   0x00, 0xf0, 0x3f, 0x24, 0x08, 0x01, 0xf0, 0x03, 0xf0, 0x3f, 0x24, 0x18,
   0x01, 0x10, 0x02, 0x10, 0x20, 0x24, 0x19, 0x01, 0x00, 0x00, 0x10, 0x20,
   0xe4, 0x1b, 0x01, 0xe0, 0x01, 0xf0, 0x3f, 0xe4, 0x3f, 0x01, 0x20, 0x01,
   0x10, 0x20, 0x04, 0x00, 0x01, 0xe0, 0x01, 0x30, 0x30, 0x0c, 0x80, 0x01,
   0xe0, 0x01, 0xf0, 0x3f, 0xfc, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00
};

// Define the control button pins:
#define button_A 0
#define button_B 20
#define button_C 21

// Define the data holders:
float weight;

void setup() {
  Serial.begin(9600);

  pinMode(button_A, INPUT_PULLUP);
  pinMode(button_B, INPUT_PULLUP);
  pinMode(button_C, INPUT_PULLUP);

  // Initialize the SSD1309 transparent display.
  u8g2.begin();
  u8g2.setFontPosTop();
  //u8g2.setDrawColor(0);
  
  // Check the connection status between the weight (HX711) sensor and the 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) to calibrate the weight sensor automatically.
  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 the Beetle ESP32-C3.
  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);

  // Connect to WPA/WPA2 network. Change this line if using open or WEP 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);
}

void loop() {
  get_Weight();
  get_Visual_Light();
  activate_Geiger_counter();

  // Show the collected data on the screen.
  home_screen(8, 90, 20);

  // Transmit the collected data to the PHP web application with the selected 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");
}

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());
  }
}

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());
}

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());
}

void get_Weight(){
  weight = MyScale.readWeight();
  if(weight < 0.5) weight = 0;
  Serial.print("\nWeight: "); Serial.print(weight); Serial.println(" g");
  delay(1000);
}

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);
}

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());
}

void drawNumber(int x, int y, int __){
    char buf[7];
    u8g2.drawStr(x, y, itoa(__, buf, 10));
}


IoT_food_irradiation_run_model.ino

Download



         /////////////////////////////////////////////  
        //   IoT AI-driven Food Irradiation Dose   // 
       //        Detector w/ Edge Impulse         //
      //           -----------------             //
     //            (Beetle ESP32-C3)            //           
    //             by Kutluhan Aktar           // 
   //                                         //
  /////////////////////////////////////////////

// 
// Collate weight, color, and emitted ionizing radiation of foods to train a NN. Then, run it on Beetle C3 to detect food irradiation doses.
//
// For more information:
// https://www.theamplituhedron.com/projects/IoT_AI_driven_Food_Irradiation_Dose_Detector_w_Edge_Impulse
//
//
// 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  --------------------------- +


// Include the required libraries:
#include <DFRobot_Geiger.h>
#include <DFRobot_HX711_I2C.h>
#include <U8g2lib.h>
#include <SPI.h>
#include "DFRobot_AS7341.h"

// Include the Edge Impulse model converted to an Arduino library:
#include <IoT_AI-driven_Food_Irradiation_Classifier_inferencing.h>

// Define the required parameters to run an inference with the Edge Impulse model.
#define FREQUENCY_HZ        EI_CLASSIFIER_FREQUENCY
#define INTERVAL_MS         (1000 / (FREQUENCY_HZ + 1))

// Define the features array to classify one frame of data.
float features[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE];
size_t feature_ix = 0;

// Define the threshold value for the model outputs (predictions).
float threshold = 0.60;

// Define the food irradiation dose (class) names:
String classes[] = {"Hazardous", "Regulated", "Unsafe"};

// Define the Geiger counter module.
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” OLED transparent display (SSD1309).
#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:
static const unsigned char error_bits[] U8X8_PROGMEM = {
   0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xf0, 0xff, 0x0f, 0x00, 0x00, 0xfc,
   0xff, 0x3f, 0x00, 0x00, 0xfe, 0xff, 0x7f, 0x00, 0x80, 0xff, 0xff, 0xff,
   0x01, 0xc0, 0xff, 0x81, 0xff, 0x03, 0xe0, 0xff, 0x00, 0xff, 0x07, 0xf0,
   0xff, 0x00, 0xff, 0x0f, 0xf0, 0x7f, 0x00, 0xfe, 0x0f, 0xf8, 0x7f, 0x00,
   0xfe, 0x1f, 0xfc, 0x7f, 0x00, 0xfe, 0x3f, 0xfc, 0xff, 0x00, 0xff, 0x3f,
   0xfe, 0xff, 0x00, 0xff, 0x7f, 0xfe, 0xff, 0x00, 0xff, 0x7f, 0xfe, 0xff,
   0x00, 0xff, 0x7f, 0xfe, 0xff, 0x00, 0xff, 0x7f, 0xff, 0xff, 0x00, 0xff,
   0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff,
   0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0x81,
   0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0xff,
   0xfe, 0xff, 0xc3, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0x7f, 0xfe, 0xff,
   0xff, 0xff, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0x7f, 0xfc, 0xff, 0xc3, 0xff,
   0x3f, 0xfc, 0xff, 0x81, 0xff, 0x3f, 0xf8, 0xff, 0x81, 0xff, 0x1f, 0xf0,
   0xff, 0x81, 0xff, 0x0f, 0xf0, 0xff, 0x81, 0xff, 0x0f, 0xe0, 0xff, 0xc3,
   0xff, 0x07, 0xc0, 0xff, 0xff, 0xff, 0x03, 0x80, 0xff, 0xff, 0xff, 0x01,
   0x00, 0xfe, 0xff, 0x7f, 0x00, 0x00, 0xfc, 0xff, 0x3f, 0x00, 0x00, 0xf0,
   0xff, 0x0f, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00
};
static const unsigned char regulated_bits[] U8X8_PROGMEM = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x80, 0x04, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x40, 0x04, 0x00, 0x00, 0x00,
   0x34, 0x00, 0x30, 0x84, 0x03, 0x00, 0x00, 0x62, 0x12, 0x50, 0x44, 0x02,
   0x00, 0x00, 0x42, 0x36, 0x28, 0x88, 0x03, 0x00, 0x00, 0x8a, 0x36, 0x28,
   0x08, 0x00, 0x00, 0x00, 0x8a, 0x3e, 0x14, 0x08, 0x00, 0x00, 0x00, 0x92,
   0x3e, 0x14, 0x08, 0x30, 0x00, 0x00, 0xd4, 0x7c, 0x14, 0x08, 0x2f, 0x00,
   0x40, 0x5c, 0x9c, 0x15, 0xcc, 0x20, 0x00, 0xa0, 0x70, 0x04, 0x09, 0x74,
   0x42, 0x00, 0xa0, 0x20, 0x02, 0x0a, 0x5a, 0x42, 0x00, 0xe0, 0x40, 0x02,
   0x92, 0x89, 0x41, 0x00, 0x00, 0x40, 0x8c, 0x53, 0x04, 0x43, 0x00, 0x00,
   0x8e, 0x04, 0x72, 0x14, 0x4c, 0x00, 0x00, 0x19, 0x08, 0x02, 0x12, 0x70,
   0x00, 0x80, 0xf0, 0x38, 0x02, 0x12, 0x40, 0x00, 0xe0, 0x00, 0x11, 0x72,
   0x0a, 0x40, 0x00, 0x10, 0x00, 0xb1, 0x52, 0x0a, 0x20, 0x00, 0x08, 0x80,
   0x60, 0x52, 0x0a, 0x20, 0x00, 0x48, 0x40, 0x40, 0x22, 0x02, 0x10, 0x00,
   0xd0, 0x43, 0x5c, 0x02, 0x02, 0x08, 0x00, 0x10, 0x40, 0x96, 0x02, 0x03,
   0x06, 0x00, 0x10, 0x92, 0x94, 0x83, 0x81, 0x01, 0x00, 0x10, 0xb7, 0x08,
   0xe3, 0xe0, 0x00, 0x00, 0xe0, 0xa5, 0x00, 0xc0, 0x37, 0x00, 0x00, 0x00,
   0xff, 0x01, 0x80, 0x3f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x03,
   0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00,
   0x00, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00, 0x00, 0x11,
   0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x03, 0x00,
   0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x46, 0x00, 0x00, 0x80,
   0x01, 0x00, 0x00, 0x84, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x08, 0x03,
   0x00, 0x40, 0x00, 0x00, 0x00, 0x10, 0x06, 0x00, 0x20, 0x00, 0x00, 0x00,
   0x20, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x0c, 0x00,
   0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00
};
static const unsigned char unsafe_bits[] U8X8_PROGMEM = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x7f, 0x00,
   0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x03, 0x00, 0x00, 0x00, 0xc0, 0xff,
   0xff, 0x0f, 0x00, 0x00, 0x00, 0xf0, 0x1f, 0xe0, 0x3f, 0x00, 0x00, 0x00,
   0xf8, 0x01, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0xf8, 0x01,
   0x00, 0x00, 0x3f, 0x00, 0x01, 0xf0, 0x03, 0x00, 0x00, 0x0f, 0x80, 0x01,
   0xc0, 0x03, 0x00, 0xc0, 0x07, 0x80, 0x00, 0xc0, 0x07, 0x00, 0xc0, 0x03,
   0x80, 0x00, 0xe0, 0x0f, 0x00, 0xe0, 0x01, 0x40, 0x00, 0xf0, 0x1f, 0x00,
   0xe0, 0x00, 0x00, 0x00, 0x78, 0x3c, 0x00, 0xf0, 0x58, 0x6b, 0x03, 0x38,
   0x3c, 0x00, 0x78, 0xfc, 0xff, 0x07, 0x1e, 0x78, 0x00, 0x78, 0xfc, 0xff,
   0x07, 0x0f, 0x70, 0x00, 0x38, 0x00, 0x00, 0x80, 0x07, 0xf0, 0x00, 0x3c,
   0x74, 0x77, 0xc7, 0x03, 0xf0, 0x00, 0x1c, 0xfc, 0xff, 0xe7, 0x01, 0xe0,
   0x00, 0x1c, 0xfc, 0xff, 0xf7, 0x00, 0xe0, 0x00, 0x1c, 0xf8, 0xff, 0x7b,
   0x00, 0xe0, 0x01, 0x1e, 0xf8, 0xff, 0x38, 0x00, 0xc0, 0x01, 0x0e, 0xf8,
   0xff, 0x1e, 0x00, 0xc0, 0x01, 0x0e, 0xf8, 0x3f, 0xdf, 0x01, 0xc0, 0x01,
   0x1e, 0xf8, 0xbf, 0x07, 0x0c, 0xc0, 0x01, 0x0e, 0xf0, 0xcf, 0x23, 0x21,
   0xc0, 0x01, 0x1e, 0xf0, 0xef, 0x01, 0x40, 0xc0, 0x01, 0x1e, 0xf0, 0xf3,
   0x90, 0x94, 0xe0, 0x01, 0x1c, 0xf0, 0x7b, 0x02, 0x00, 0xc0, 0x01, 0x1c,
   0xf0, 0x7c, 0x02, 0x80, 0xe0, 0x00, 0x1c, 0xe0, 0x9e, 0xff, 0xff, 0xe0,
   0x00, 0x3c, 0x20, 0xdf, 0xff, 0xff, 0xe0, 0x00, 0x38, 0xa0, 0xe7, 0xff,
   0xff, 0xf0, 0x00, 0x38, 0xc0, 0x47, 0x55, 0x95, 0x70, 0x00, 0x78, 0xe0,
   0x01, 0x00, 0x00, 0x78, 0x00, 0x70, 0xe0, 0x04, 0x00, 0x80, 0x38, 0x00,
   0xf0, 0x78, 0x28, 0x49, 0x52, 0x3c, 0x00, 0xe0, 0x3d, 0x00, 0x00, 0x00,
   0x1e, 0x00, 0xc0, 0x1f, 0x00, 0x00, 0x00, 0x1e, 0x00, 0xc0, 0x0f, 0x00,
   0x00, 0x80, 0x0f, 0x00, 0x80, 0x0f, 0x00, 0x00, 0x80, 0x07, 0x00, 0x00,
   0x1f, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0x7e, 0x00, 0x00, 0xf0, 0x01,
   0x00, 0x00, 0xfc, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0xf0, 0x07, 0x80,
   0x3f, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xfd, 0x1f, 0x00, 0x00, 0x00, 0x80,
   0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00
};
static const unsigned char hazardous_bits[] U8X8_PROGMEM = {
   0x00, 0x00, 0xa0, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfd, 0xff, 0x02,
   0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0xe0, 0x17,
   0xa0, 0x1f, 0x00, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x7c, 0x00, 0x00, 0x00,
   0x7c, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0xe0, 0x03,
   0x00, 0x80, 0x07, 0x00, 0x01, 0x80, 0x03, 0x00, 0xc0, 0x03, 0xe0, 0x1f,
   0x00, 0x07, 0x00, 0xc0, 0x01, 0xf0, 0x3f, 0x00, 0x0e, 0x00, 0xe0, 0x00,
   0xfc, 0xff, 0x00, 0x1c, 0x00, 0x70, 0x00, 0xfe, 0xff, 0x01, 0x38, 0x00,
   0x70, 0x00, 0xff, 0xff, 0x03, 0x38, 0x00, 0x38, 0x00, 0xff, 0xff, 0x03,
   0x70, 0x00, 0x18, 0x00, 0xff, 0xff, 0x03, 0x60, 0x00, 0x1c, 0x80, 0xff,
   0xff, 0x07, 0xe0, 0x00, 0x1c, 0x80, 0xff, 0xff, 0x07, 0xc0, 0x00, 0x0c,
   0x80, 0xff, 0xff, 0x07, 0xc0, 0x00, 0x0e, 0x80, 0x03, 0x83, 0x07, 0xc0,
   0x01, 0x0e, 0x80, 0x01, 0x01, 0x07, 0xc0, 0x01, 0x0e, 0x80, 0x01, 0x03,
   0x06, 0x80, 0x01, 0x06, 0x00, 0x03, 0x03, 0x03, 0x80, 0x03, 0x06, 0x00,
   0x83, 0x07, 0x03, 0x80, 0x01, 0x07, 0x00, 0xd6, 0xef, 0x01, 0x80, 0x03,
   0x07, 0x00, 0xfc, 0xff, 0x00, 0x80, 0x03, 0x07, 0x10, 0x78, 0x38, 0x00,
   0x80, 0x03, 0x06, 0x30, 0x70, 0x3b, 0x70, 0x80, 0x03, 0x06, 0x78, 0xf0,
   0x3f, 0x70, 0x80, 0x01, 0x06, 0x78, 0xd0, 0x2f, 0xf8, 0x80, 0x03, 0x0e,
   0xfc, 0xc1, 0x0f, 0xfc, 0xc0, 0x01, 0x06, 0xfc, 0x43, 0x0b, 0xff, 0x80,
   0x01, 0x0e, 0x80, 0x0f, 0xc0, 0x03, 0xc0, 0x01, 0x0c, 0x00, 0x1e, 0xe0,
   0x01, 0xc0, 0x01, 0x0e, 0x00, 0x7c, 0xfc, 0x00, 0xc0, 0x00, 0x1c, 0x00,
   0xf0, 0x3f, 0x00, 0xe0, 0x00, 0x18, 0x00, 0xc0, 0x0f, 0x00, 0x60, 0x00,
   0x38, 0x00, 0xc0, 0x1f, 0x00, 0x70, 0x00, 0x78, 0x00, 0xfe, 0xff, 0x01,
   0x38, 0x00, 0x70, 0x00, 0x7e, 0xf8, 0x01, 0x38, 0x00, 0xe0, 0x01, 0x1e,
   0xf0, 0x01, 0x1c, 0x00, 0xc0, 0x03, 0x1c, 0xe0, 0x00, 0x0f, 0x00, 0xc0,
   0x07, 0x1c, 0xc0, 0x00, 0x07, 0x00, 0x80, 0x07, 0x00, 0x00, 0xc0, 0x03,
   0x00, 0x00, 0x0f, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x7c, 0x00, 0x00,
   0xf0, 0x00, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x7e, 0x00, 0x00, 0x00, 0xe0,
   0x0f, 0xa0, 0x1f, 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0x07, 0x00, 0x00,
   0x00, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x27, 0x00,
   0x00, 0x00
};

// Create an array including icons for labels (classes).
static const unsigned char *class_icons[] U8X8_PROGMEM = {hazardous_bits, regulated_bits, unsafe_bits}; 

// Define the control button pins:
#define button_A 0
#define button_B 20
#define button_C 21

// Define the data holders:
float weight;
volatile boolean model_activation = false; 
int predicted_class = -1;

void setup() {
  Serial.begin(9600);

  pinMode(button_B, INPUT_PULLUP);

  // Initialize the SSD1309 transparent display.
  u8g2.begin();
  u8g2.setFontPosTop();
  //u8g2.setDrawColor(0);
  
  // Check the connection status between the weight (HX711) sensor and the 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) to calibrate the weight sensor automatically.
  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 the Beetle ESP32-C3.
  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);

  delay(1000);
}

void loop() {
  get_Weight();
  get_Visual_Light();
  activate_Geiger_counter();

  // Show the collected data on the screen.
  home_screen(8, 90, 20);

  // Execute the Edge Impulse model to make predictions on the food irradiation doses (classes).
  if(!digitalRead(button_B)){
    model_activation = true;
    u8g2.firstPage();  
    do{
      u8g2.setFont(u8g2_font_open_iconic_all_8x_t);
      u8g2.drawGlyph(/* x=*/32, /* y=*/0, /* encoding=*/233);  
    }while(u8g2.nextPage());
  }
  while(model_activation){
    get_Weight();
    get_Visual_Light();
    activate_Geiger_counter();

    // Run inference:
    run_inference_to_make_predictions(1);

    // If the Edge Impulse model predicted a label (class) successfully:
    if(predicted_class != -1){
      // Display the predicted class:
      String c = "Class: " + classes[predicted_class];
      int str_x = c.length() * 4;
      u8g2.firstPage();  
      do{
        //u8g2.setBitmapMode(true /* transparent*/);
        u8g2.drawXBMP( /* x=*/(u8g2.getDisplayWidth()-50)/2 , /* y=*/0 , /* width=*/50 , /* height=*/50 , class_icons[predicted_class]);
        u8g2.setFont(u8g2_font_4x6_tr);
        u8g2.drawStr((u8g2.getDisplayWidth()-str_x)/2, 55, c.c_str());
      }while(u8g2.nextPage());      
      
      // Clear the predicted class (label).
      predicted_class = -1;

      // Stop the running inference and return to the home screen.
      model_activation = false;
    }
  }
}

void run_inference_to_make_predictions(int multiply){
  // Scale (normalize) data items depending on the given model:
  float scaled_weight = weight / 10;
  float scaled_F1 = data1.ADF1 / 100;
  float scaled_F2 = data1.ADF2 / 100;
  float scaled_F3 = data1.ADF3 / 100;
  float scaled_F4 = data1.ADF4 / 100;
  float scaled_F5 = data2.ADF5 / 100;
  float scaled_F6 = data2.ADF6 / 100;
  float scaled_F7 = data2.ADF7 / 100;
  float scaled_F8 = data2.ADF8 / 100;
  float scaled_CPM = geiger.getCPM() / 100;
  float scaled_nSv = geiger.getnSvh() / 100;
  float scaled_uSv = geiger.getuSvh();
  
  // Copy the scaled data items to the features buffer.
  // If required, multiply the scaled data items while copying them to the features buffer.
  for(int i=0; i<multiply; i++){  
    features[feature_ix++] = scaled_weight;
    features[feature_ix++] = scaled_F1;
    features[feature_ix++] = scaled_F2;
    features[feature_ix++] = scaled_F3;
    features[feature_ix++] = scaled_F4;
    features[feature_ix++] = scaled_F5;
    features[feature_ix++] = scaled_F6;
    features[feature_ix++] = scaled_F7;
    features[feature_ix++] = scaled_F8;
    features[feature_ix++] = scaled_CPM;
    features[feature_ix++] = scaled_nSv;
    features[feature_ix++] = scaled_uSv;
  }

  // Display the progress of copying data to the features buffer.
  Serial.print("\nFeatures Buffer Progress: "); Serial.print(feature_ix); Serial.print(" / "); Serial.println(EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE);
  
  // Run inference:
  if(feature_ix == EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE){    
    ei_impulse_result_t result;
    // Create a signal object from the features buffer (frame).
    signal_t signal;
    numpy::signal_from_buffer(features, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);
    // Run the classifier:
    EI_IMPULSE_ERROR res = run_classifier(&signal, &result, false);
    ei_printf("\nrun_classifier returned: %d\n", res);
    if(res != 0) return;

    // Print the inference timings on the serial monitor.
    ei_printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n", 
        result.timing.dsp, result.timing.classification, result.timing.anomaly);

    // Obtain the prediction results for each label (class).
    for(size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++){
      // Print the prediction results on the serial monitor.
      ei_printf("%s:\t%.5f\n", result.classification[ix].label, result.classification[ix].value);
      // Get the predicted label (class).
      if(result.classification[ix].value >= threshold) predicted_class = ix;
    }
    Serial.print("\nPredicted Class: "); Serial.println(predicted_class);

    // Detect anomalies, if any:
    #if EI_CLASSIFIER_HAS_ANOMALY == 1
      ei_printf("Anomaly : \t%.3f\n", result.anomaly);
    #endif

    // Clear the features buffer (frame):
    feature_ix = 0;
  }
}

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());
}

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());
}

void get_Weight(){
  weight = MyScale.readWeight();
  if(weight < 0.5) weight = 0;
  Serial.print("\nWeight: "); Serial.print(weight); Serial.println(" g");
  delay(1000);
}

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);
}

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());
}

void drawNumber(int x, int y, int __){
    char buf[7];
    u8g2.drawStr(x, y, itoa(__, buf, 10));
}


class.php

Download



<?php

// Define the _main class and its functions:
class _main {
	public $conn, $table;
	
	public function __init__($conn, $table){
		$this->conn = $conn;
		$this->table = $table;
	}
	
    // Database -> Insert Data:
	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; }
	}

	// Database -> Create 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 and its functions:
class sample Extends _main {
	// Define the irradiation dose class (label) names.
	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;
	}
	
	// Create a CSV file for each data record (sample) identified with the sample number.
	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);
			}
		}
	}
	
	// Download all generated CSV sample files 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 database and server settings:
$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

Download



<?php

include_once "assets/class.php";

// Define the new 'food' object:
$food = new _main();
$food->__init__($conn, $server["table"]); 

// Obtain the transferred information from the Beetle ESP32-C3.
// Then, insert the received information 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 a new database table.
if(isset($_GET["create_table"]) && $_GET["create_table"] == "OK") $food->database_create_table();

?>


index.php

Download



<?php
	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();

    // Create a CSV file for each data record (sample) in the given database table.
    if(isset($_POST["data"]) && $_POST["data"] != ""){
		$sample->create_sample_files($_POST["data"]);
	}
	
    // Download all generated CSV sample files in the ZIP file format.
    if(isset($_GET["download"])){
		$sample->download_samples("data.zip");
	}	
?>
<!DOCTYPE html>
<html>
<head>
<title>Food Irradiation Data Logger</title>

<!--link to index.css-->
<link rel="stylesheet" type="text/css" href="assets/index.css"></link>

<!--link to favicon-->
<link rel="icon" type="image/png" sizes="36x36" href="assets/icon.png">

<!-- link to FontAwesome-->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.1.1/css/all.css">
 
<!-- link to font -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Oswald&display=swap" rel="stylesheet">

</head>
<body>
<?php ini_set('display_errors',1);?> 
<h1><i class="fa-solid fa-circle-radiation"></i> Food Irradiation Data Logger</h1>

<div class="container">
<section>
<h2>Created Samples:</h2>
<table>
  <tr>
    <th>Samples</th>
    <th>Download</th>
  </tr>
  <tr>
	<td>Download all samples.</td>
	<td><a href="?download"><button><i class="fa-solid fa-cloud-arrow-down"></i></button></a></td>
  </tr>
<?php
  foreach(glob("data/*.csv") as $sample){
	  echo '
        <tr>
           <td>'.explode("/", $sample)[1].'</td>
        </tr>		   
	  ';
  }
?>
</table>
</section>
<section>
<div>
<h2><i class="fa-solid fa-database"></i> Database Status:</h2>
<p>Total Samples: <span><?php echo $count["total"]; ?></span></p>
<p>Regulated: <span><?php echo $count["regulated"]; ?></span></p>
<p>Unsafe: <span><?php echo $count["unsafe"]; ?></span></p>
<p>Hazardous: <span><?php echo $count["hazardous"]; ?></span></p>
<form method="post">
<fieldset>
<legend>Data Type</legend>
<br>
<label><input type="radio" name="data" value="training" /><span class="mark"></span> Training</label>
<label><input type="radio" name="data" value="testing" /><span class="mark"></span> Testing</label>
<br><br>
</fieldset>
<br>
<button type="submit"><i class="fa-solid fa-folder-plus"></i> Create Samples</button>
</form>
</div>
</section>
</div>
</body>
</html>


index.css

Download



html{background-color:#eb2e00;font-family: 'Oswald', sans-serif;}
h1{color:#002699;text-align:center;font-weight:bold;user-select:none;}
h2{color:white;font-weight:bold;}
p, button{line-height:normal;font-weight:bold;color:white;}
fieldset{border: 3px solid #F3D060;margin:auto;width:80%;text-align:center;}
legend{color:#F3D060;font-size:20px;user-select:none;}
input[type="radio"]{position:absolute;height:0;width:0;user-select:none;}
label{position:relative;font-size:15px;padding-left:50px;margin-left:40px;color:#F3D060;font-size:15px;cursor:pointer;}
.mark{position:absolute;top:0;left:0;height:40px;width:40px;border-radius:50%;background-color:#A5282C;margin-right:10px;}
.mark:after{content:'';position:absolute;top:10px;left:10px;width:20px;height:20px;border-radius:50%;background-color:#EE7762;}
label:hover .mark{background-color:#F3D060;}
input[type="radio"]:checked ~ .mark{background-color:#F3D060;} 
input[type="radio"]:checked ~ .mark:after{background-color:#5EB0E5;} 

.container{position:relative;background-color:none;width:90%;height:500px;margin:auto;margin-top:40px;}
.container section:nth-of-type(1){position:absolute;background-color:#2E3033;top:0;left:0;width:50%;height:100%;overflow-y:auto;border-radius:35px 0 0 35px;}
.container section:nth-of-type(1) h2{color:#F3D060;padding-left:45px;}
.container section:nth-of-type(1) table{border-collapse: collapse;width:90%;margin-left:45px;margin-bottom:20px;}
.container section:nth-of-type(1) table, .container section:nth-of-type(1) td, .container section:nth-of-type(1) th{border:2px solid white;color:white;}
.container section:nth-of-type(1) td{padding-left:20px;padding-top:5px;padding-bottom:5px;}
.container section:nth-of-type(1) th{color:#F3D060;background-color:#A5282C;}
.container section:nth-of-type(1) button{color:white;background-color:#A5282C;border:none;border-radius:5px;cursor:pointer;width:75%;}
.container section:nth-of-type(1) button:hover{background-color:white;color:#A5282C;}
.container section:nth-of-type(2){position:absolute;background-color:#ff5c33;top:0;right:0;width:50%;height:100%;border-radius:0 30px 30px 0;}
.container section:nth-of-type(2) div{position:relative;width:90%;height:100%;background-color:none;overflow-y:auto;margin:auto;}
.container section:nth-of-type(2) button{display:block;color:#F3D060;font-size:25px;background-color:#A5282C;border:5px solid #F3D060;border-radius:10px;cursor:pointer;width:75%;margin:auto;}
.container section:nth-of-type(2) button:hover{background-color:#EE7762;}
.container section:nth-of-type(2) span{color:#002699;}

::selection {color: #ff5c33; background: #1a53ff;}
/* Width */
::-webkit-scrollbar {width: 10px;height: 10px;}
/* Track */
::-webkit-scrollbar-track {background-color: #4d79ff; }
/* Button */
::-webkit-scrollbar-button{background-color:#ffc2b3;height:10px;width:10px;}
::-webkit-scrollbar-button:hover{background-color:white;}
/* Handle */
::-webkit-scrollbar-thumb {background-color: #ff9980; }
::-webkit-scrollbar-thumb:hover {background-color: #ffd6cc;}
/* Corner */
::-webkit-scrollbar-corner{background-color:#4d79ff;}

Schematics

project-image
Schematic - 79.1

Downloads

IoT_Food_Irradiation_Detector_case_v1.stl

Download


IoT_Food_Irradiation_Detector_handle_v1.stl

Download


Edge Impulse Model (Arduino Library)

Download


food_irradiation_data_logger.zip

Download