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
918 changes: 918 additions & 0 deletions TP1/Solution/ai-for-io-t-fire-alarm-detection.ipynb

Large diffs are not rendered by default.

1,423 changes: 1,423 additions & 0 deletions TP2/Solution/ai-for-io-t-fire-alarm-detection.ipynb

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions TP3/src/solotion/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <Arduino.h>
#include "DHT.h"
#include <math.h>

#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

const int N_FEATURES = 12;

const float MEAN[N_FEATURES] = {24.5, 55.0, 0, 400, 12300, 18500, 940.0, 0, 0, 0, 0, 0};
const float STD[N_FEATURES] = {2.3, 6.1, 1, 50, 1000, 2000, 50.0, 1, 1, 1, 1, 1};
const float WEIGHTS[N_FEATURES] = {0.85, -0.47, 0.12, 0.03, -0.01, 0.02, 0.05, 0, 0, 0, 0, 0};
const float BIAS = -0.22;

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_raw, int idx)
{
return (x_raw - MEAN[idx]) / STD[idx];
}

float sigmoid(float z)
{
return 1.0 / (1.0 + exp(-z));
}

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

void setup()
{
Serial.begin(9600);
Serial.println(F("DHTxx + Logistic Regression Test"));
dht.begin();
}

void loop()
{
delay(2000);

float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);

if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println(F("Failed to read from DHT sensor!"));
return;
}

X[0] = t;
X[1] = h;

float X_scaled[N_FEATURES];
for (int i = 0; i < N_FEATURES; i++)
{
X_scaled[i] = standardize(X[i], i);
}

float y_pred = predict(X_scaled);

Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" °C | Humidity: ");
Serial.print(h);
Serial.print(" % | Predicted Probability: ");
Serial.println(y_pred, 4);

if (y_pred >= 0.5)
Serial.println("Predicted Class: 1");
else
Serial.println("Predicted Class: 0");

Serial.println("-------------------------------");
}
1 change: 0 additions & 1 deletion TP3/wokwi.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[wokwi]
version = 1
mcu = "esp32"
elf = ".pio/build/uno/firmware.elf"
firmware = ".pio/build/uno/firmware.hex"

Expand Down
3 changes: 2 additions & 1 deletion TP4/ai_logic/mqtt_ai_subscriber.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json, paho.mqtt.client as mqtt
import json
import paho.mqtt.client as mqtt

def on_message(client, userdata, msg):
data = json.loads(msg.payload)
Expand Down
1 change: 1 addition & 0 deletions TP4/ai_logic/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
paho-mqtt~=2.1.0
3 changes: 2 additions & 1 deletion TP4/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ monitor_speed = 115200
lib_deps =
adafruit/DHT sensor library
knolleary/PubSubClient
marcoschwartz/LiquidCrystal_I2C
marcoschwartz/LiquidCrystal_I2C
bblanchon/ArduinoJson
165 changes: 165 additions & 0 deletions TP4/src/solotion/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>

#define DHTPIN 15
#define DHTTYPE DHT22
#define LED_PIN 2

const char *ssid = "Wokwi-GUEST";
const char *password = "";

const char *mqtt_server = "broker.mqtt.cool";
const int mqtt_port = 1883;

const char *USER_NAMESPACE = "aiot_lab_yourname";
const char *DEVICE_ID = "esp32-01";

String topic_data = String(USER_NAMESPACE) + "/" + DEVICE_ID + "/data";
String topic_control = String(USER_NAMESPACE) + "/" + DEVICE_ID + "/control";

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
String currentCommand = "---";

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

void setup_wifi()
{
Serial.print("Connecting to WiFi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected! IP address:");
Serial.println(WiFi.localIP());
}

void callback(char *topic, byte *message, unsigned int length)
{
String msg;
for (int i = 0; i < length; i++)
msg += (char)message[i];
msg.trim();

Serial.print("Received command on [");
Serial.print(topic);
Serial.print("]: ");
Serial.println(msg);

if (msg.equalsIgnoreCase("ON"))
{
digitalWrite(LED_PIN, HIGH);
currentCommand = "ON";
}
else if (msg.equalsIgnoreCase("OFF"))
{
digitalWrite(LED_PIN, LOW);
currentCommand = "OFF";
}

lcd.setCursor(0, 1);
lcd.print("CMD:");
lcd.print(currentCommand);
lcd.print(" ");
}

void reconnect()
{
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
if (client.connect(DEVICE_ID))
{
Serial.println("connected");
client.subscribe(topic_control.c_str());
Serial.print("Subscribed to: ");
Serial.println(topic_control);
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" retry in 5s");
delay(5000);
}
}
}

void setup()
{
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print("Starting...");
dht.begin();

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

unsigned long lastMsg = 0;
const long interval = 3000;

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

unsigned long now = millis();
if (now - lastMsg > interval)
{
lastMsg = now;

float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}

X[0] = t;
X[1] = h;

lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(t, 1);
lcd.print("C H:");
lcd.print(h, 0);
lcd.print("% ");

lcd.setCursor(0, 1);
lcd.print("CMD:");
lcd.print(currentCommand);
lcd.print(" ");

JsonDocument doc;
doc["device_id"] = DEVICE_ID;
doc["temperature"] = t;
doc["humidity"] = h;
doc["timestamp"] = (long)(millis() / 1000);

char payload[256];
serializeJson(doc, payload);

Serial.print("Publishing to ");
Serial.print(topic_data);
Serial.print(": ");
Serial.println(payload);

client.publish(topic_data.c_str(), payload);
}
}
Loading