#include <Arduino.h>
#include "HX711.h"
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// ====== WiFi ======
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const char* API_KEY = "b453666eb3d04c1fa7c608375d88f7fc"; // замінити своїм
// ====== Web server ======
WebServer server(80);
// ====== HX711 ======
const int PIN_DOUT = 16;
const int PIN_SCK = 4;
HX711 scale;
// ====== LCD ======
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ====== Temperature sensor DS18B20 ======
const int ONE_WIRE_PIN = 2;
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature tempSensor(&oneWire);
// ====== Data ======
float currentWeight = 0;
float foodTemperature = 0;
String adviceText = "Натисни кнопку для поради...";
// ---------------- HTML ----------------
void handleHomePage() {
String page = R"html(
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>🥗 Kitchen Dashboard</title>
<style>
body { font-family: Verdana, sans-serif; background:#f0fff4; color:#222; text-align:center; }
h2 { color:#2e7d32; }
.box { display:inline-block; border-radius:14px; padding:20px; margin:10px; background:#e8f5e9; box-shadow:0 4px 6px rgba(0,0,0,0.1); width:220px; }
.label { font-size:14px; color:#555; }
.value { font-size:22px; font-weight:bold; margin-top:8px; }
button { margin-top:20px; padding:12px 24px; background:#43a047; border:none; border-radius:10px; color:white; font-size:16px; cursor:pointer; }
button:hover { background:#2e7d32; }
</style>
<script>
function updateData(){
fetch('/status').then(r=>r.json()).then(j=>{
document.getElementById("w").innerText=j.weight+" g";
document.getElementById("t").innerText=j.temp+" °C";
document.getElementById("advice").innerText=j.ai;
});
}
function askAI(){
fetch('/ask').then(r=>r.text()).then(alert);
setTimeout(updateData,2000);
}
setInterval(updateData,3000);
</script>
</head>
<body>
<h2>🥗 Kitchen Dashboard</h2>
<div class="box">
<div class="label">Вага</div>
<div class="value" id="w">--</div>
</div>
<div class="box">
<div class="label">Температура</div>
<div class="value" id="t">--</div>
</div>
<div class="box" style="width:90%">
<div class="label">AI Порада</div>
<div class="value" id="advice">Завантаження...</div>
</div>
<br/>
<button onclick="askAI()">Отримати пораду</button>
</body>
</html>
)html";
server.send(200, "text/html", page);
}
void handleStatus() {
DynamicJsonDocument doc(512);
doc["weight"] = currentWeight;
doc["temp"] = foodTemperature;
doc["ai"] = adviceText;
String json; serializeJson(doc, json);
server.send(200, "application/json", json);
}
void handleAskAI() {
adviceText = "Очікуйте відповідь...";
DynamicJsonDocument req(1024);
req["model"]="gpt-3.5-turbo";
JsonArray msgs=req.createNestedArray("messages");
JsonObject sys = msgs.createNestedObject();
sys["role"] = "system";
sys["content"] = "You are a cooking assistant. Give short and practical kitchen advice.";
JsonObject usr = msgs.createNestedObject();
usr["role"] = "user";
usr["content"] = "Я маю " + String(currentWeight,1) + " грамів інгредієнтів з температурою " + String(foodTemperature,1) + "°C. Дай пораду.";
HTTPClient http;
http.begin("https://artificialintelligence.openai.azure.com/openai/deployments/test/chat/completions?api-version=2023-05-15");
http.addHeader("Content-Type","application/json");
http.addHeader("api-key",API_KEY);
String body; serializeJson(req,body);
int code=http.POST(body);
if(code==200){
DynamicJsonDocument resp(2048);
deserializeJson(resp,http.getString());
adviceText=resp["choices"][0]["message"]["content"].as<String>();
Serial.println("AI: "+adviceText);
} else {
adviceText="Помилка AI ("+String(code)+")";
}
http.end();
server.send(200,"text/plain","AI request sent!");
}
// ---------------- Setup ----------------
void setup() {
Serial.begin(115200);
// LCD init
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Kitchen Monitor");
// WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status()!=WL_CONNECTED) {
delay(500); Serial.print(".");
}
Serial.println(WiFi.localIP());
// HX711
scale.begin(PIN_DOUT, PIN_SCK);
scale.set_scale(0.42);
// Temp sensor
tempSensor.begin();
// Routes
server.on("/", handleHomePage);
server.on("/status", handleStatus);
server.on("/ask", handleAskAI);
server.begin();
}
// ---------------- Loop ----------------
void loop() {
server.handleClient();
// Обновляем сенсоры
currentWeight = scale.get_units(10);
tempSensor.requestTemperatures();
foodTemperature = tempSensor.getTempCByIndex(0);
// LCD
lcd.clear();
lcd.setCursor(0,0); lcd.print("W:" + String(currentWeight,1) + "g");
lcd.setCursor(0,1); lcd.print("T:" + String(foodTemperature,1) + "C");
delay(1000);
}