Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions TP3/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// !!! WARNING !!! AUTO-GENERATED FILE!
// PLEASE DO NOT MODIFY IT AND USE "platformio.ini":
// https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags
//
{
"configurations": [
{
"name": "PlatformIO",
"includePath": [
"D:/UniverStudy/ML/AIoT-2025/TP3/src",
"D:/UniverStudy/ML/AIoT-2025/TP3/.pio/libdeps/uno/DHT sensor library",
"D:/UniverStudy/ML/AIoT-2025/TP3/.pio/libdeps/uno/Adafruit Unified Sensor",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/cores/arduino",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/variants/standard",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/libraries/HID/src",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
""
],
"browse": {
"limitSymbolsToIncludedHeaders": true,
"path": [
"D:/UniverStudy/ML/AIoT-2025/TP3/src",
"D:/UniverStudy/ML/AIoT-2025/TP3/.pio/libdeps/uno/DHT sensor library",
"D:/UniverStudy/ML/AIoT-2025/TP3/.pio/libdeps/uno/Adafruit Unified Sensor",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/cores/arduino",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/variants/standard",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/libraries/HID/src",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
"C:/Users/GHE.INFO/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
""
]
},
"defines": [
"PLATFORMIO=60118",
"ARDUINO_AVR_UNO",
"F_CPU=16000000L",
"ARDUINO_ARCH_AVR",
"ARDUINO=10808",
"__AVR_ATmega328P__",
""
],
"cStandard": "gnu11",
"cppStandard": "gnu++11",
"compilerPath": "C:/Users/GHE.INFO/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe",
"compilerArgs": [
"-mmcu=atmega328p",
""
]
}
],
"version": 4
}
10 changes: 10 additions & 0 deletions TP3/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
44 changes: 44 additions & 0 deletions TP3/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// AUTOMATICALLY GENERATED FILE. PLEASE DO NOT MODIFY IT MANUALLY
//
// PlatformIO Debugging Solution
//
// Documentation: https://docs.platformio.org/en/latest/plus/debugging.html
// Configuration: https://docs.platformio.org/en/latest/projectconf/sections/env/options/debug/index.html

{
"version": "0.2.0",
"configurations": [
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug",
"executable": "D:/UniverStudy/ML/AIoT-2025/TP3/.pio/build/uno/firmware.elf",
"projectEnvName": "uno",
"toolchainBinDir": "C:/Users/GHE.INFO/.platformio/packages/toolchain-atmelavr/bin",
"internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": {
"type": "PlatformIO",
"task": "Pre-Debug"
}
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug (skip Pre-Debug)",
"executable": "D:/UniverStudy/ML/AIoT-2025/TP3/.pio/build/uno/firmware.elf",
"projectEnvName": "uno",
"toolchainBinDir": "C:/Users/GHE.INFO/.platformio/packages/toolchain-atmelavr/bin",
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "platformio-debug",
"request": "launch",
"name": "PIO Debug (without uploading)",
"executable": "D:/UniverStudy/ML/AIoT-2025/TP3/.pio/build/uno/firmware.elf",
"projectEnvName": "uno",
"toolchainBinDir": "C:/Users/GHE.INFO/.platformio/packages/toolchain-atmelavr/bin",
"internalConsoleOptions": "openOnSessionStart",
"loadMode": "manual"
}
]
}
69 changes: 21 additions & 48 deletions TP3/diagram.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,23 @@
{
"version": 1,
"author": "AIoT2025-TP3",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-uno",
"id": "uno",
"top": 0,
"left": 0,
"attrs": {}
},
{
"type": "wokwi-dht22",
"id": "dht1",
"top": -201.3,
"left": 23.4,
"attrs": {}
}
],
"connections": [
[
"dht1:VCC",
"uno:5V",
"red",
[
"v316.8",
"h124.8"
]
],
[
"dht1:SDA",
"uno:2",
"green",
[
"v57.6",
"h192.1"
]
],
[
"dht1:GND",
"uno:GND.1",
"black",
[
"v0"
]
]
],
"dependencies": {}
"version": 1,
"author": "AIoT2025-TP3",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0.6, "left": -0.6, "attrs": {} },
{ "type": "wokwi-dht22", "id": "dht1", "top": -162.9, "left": -82.2, "attrs": {} },
{ "type": "wokwi-gas-sensor", "id": "mq2", "top": -150.9, "left": 83.8, "attrs": {} },
{ "type": "wokwi-flame-sensor", "id": "flame1", "top": -149, "left": 405.8, "attrs": {} }
],
"connections": [
[ "dht1:VCC", "uno:5V", "red", [ "v316.8", "h220.8", "v-76.7" ] ],
[ "dht1:SDA", "uno:2", "green", [ "v38.4", "h294.2" ] ],
[ "dht1:GND", "uno:GND.1", "black", [ "v19.2", "h153.9" ] ],
[ "mq2:VCC", "uno:5V", "red", [ "h96", "v315.9", "h-157.4" ] ],
[ "mq2:AOUT", "uno:A0", "blue", [ "h115.2", "v383.5" ] ],
[ "flame1:VCC", "uno:5V", "red", [ "h9.6", "v403.2", "h-455" ] ],
[ "flame1:GND", "uno:GND.3", "black", [ "h38.4", "v355.1", "h-349" ] ],
[ "flame1:DOUT", "uno:3", "orange", [ "v86.2", "h-377.8" ] ],
[ "mq2:GND", "uno:GND.1", "black", [ "h28.8", "v47.2", "h-134.4", "v9.6" ] ]
],
"dependencies": {}
}
136 changes: 88 additions & 48 deletions TP3/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,69 +1,109 @@

#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#include <math.h>

#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

#define MQ2_PIN A0
#define FLAME_PIN 3

const int N_FEATURES = 12;
const float MEAN[N_FEATURES] = {/* μ_Temperature, μ_Humidity */};
const float STD[N_FEATURES] = {/* σ_Temperature, σ_Humidity */};
const float WEIGHTS[N_FEATURES] = {/* W_Temperature, W_Humidity */};
const float BIAS = 0; /* b */

float X[N_FEATURES] = {20.0, 57.36, 0, 400, 12306, 18520, 939.735, 0.0, 0.0, 0.0, 0.0, 0.0}; // Input features
const float MEAN[N_FEATURES] = {
31297.363165, 1654791844.021415, 15.953115, 48.574302,
1922.884341, 667.743913, 12942.300974, 19754.250459,
938.631948, 100.887285, 186.073921, 490.209271
};

const float STD[N_FEATURES] = {
18071.764509, 109669.276420, 14.358038, 8.821188,
7755.895054, 1903.782213, 271.168609, 606.975952,
1.324514, 925.303530, 1991.527889, 4259.415670
};

const float WEIGHTS[N_FEATURES] = {
-1.373408, -0.342746, -0.212932, 1.180184,
-22.874995, 0.849484, -0.068718, -3.561514,
-2.865623, 1.926110, 0.944893, 2.861934
};

const float BIAS = 17.515389;

float X[N_FEATURES] = {
20.0, 57.36, 0, 400,
12306, 18520, 939.735, 0.0,
0.0, 0.0, 0.0, 0.0
};

float standardize(float x, int idx) {
return (x - MEAN[idx]) / STD[idx];
}

float sigmoid(float z) {
if (z > 10) z = 10;
if (z < -10) z = -10;
return 1.0 / (1.0 + exp(-z));
}

float predict(float features[]) {
float z = 0.0;
for (int i = 0; i < N_FEATURES; i++) {
float x_std = standardize(features[i], i);
z += WEIGHTS[i] * x_std;
}
z += BIAS;
return sigmoid(z);
}

void setup()
{
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
Serial.println(F("🔥 Fire Detection System — Based on Professor's Model"));
dht.begin();
pinMode(MQ2_PIN, INPUT);
pinMode(FLAME_PIN, INPUT);
}

void loop()
{
void loop() {
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// add data to input array
X[0] = t;
X[1] = h;

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println(F("Failed to read from DHT sensor!"));
return;

if (isnan(h) || isnan(t)) {
Serial.println(F("⚠️ Failed to read from DHT sensor, using previous values."));
} else {
X[0] = t;
X[1] = h;
}

// TODO: Add code to standardize the inputs
int mq2_value = analogRead(MQ2_PIN);
int flame_value = digitalRead(FLAME_PIN);

X[2] = mq2_value;
X[3] = flame_value * 500;


// TODO: Add code to compute the output of wx + b
float prob = predict(X);

// TODO: Add code to apply the sigmoid function
bool alarm = false;
if (prob >= 0.6) {
if (mq2_value > 400 || flame_value == 1 || t > 45.0) {
alarm = true;
}
}

// TODO: Add code to print the result to the serial monitor
Serial.println("🌡️ Reading Sensors:");
Serial.print("Temperature: "); Serial.print(t); Serial.print(" °C | ");
Serial.print("Humidity: "); Serial.print(h); Serial.println(" %");
Serial.print("MQ2: "); Serial.print(mq2_value);
Serial.print(" | Flame: "); Serial.println(flame_value);
Serial.print("Predicted probability: "); Serial.println(prob, 6);

// Compute heat index in Fahrenheit (the default)
// float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
// float hic = dht.computeHeatIndex(t, h, false);
if (alarm)
Serial.println("🔥 FIRE ALARM: ON");
else
Serial.println("✅ FIRE ALARM: OFF");

Serial.print("Humidity: ");
Serial.print(h);
Serial.print("% Tempeature: ");
Serial.print(t);
Serial.print("°C ");
Serial.println(f);
// Serial.print(F("°F Heat index: "));
// Serial.print(hic);
// Serial.print(F("°C "));
// Serial.print(hif);
// Serial.println(F("°F"));
}
Serial.println("--------------------------------------------");
}
2 changes: 1 addition & 1 deletion TP3/wokwi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ firmware = ".pio/build/uno/firmware.hex"
[connections.phantomio]
# Enable PhantomIO for serial and telemetry
enabled = true
port = "serial"
port = "serial"
Loading