#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_PASS = "";
const char* OPENAI_KEY = "b453666eb3d04c1fa7c608375d88f7fc"; // свій ключ
// ===== Server =====
WebServer server(80);
// ===== HX711 =====
#define PIN_DT 16
#define PIN_SCK 4
HX711 hx;
// ===== LCD =====
LiquidCrystal_I2C screen(0x27, 16, 2);
// ===== Temp Sensor =====
#define PIN_ONEWIRE 2
OneWire onewire(PIN_ONEWIRE);
DallasTemperature ds(&onewire);
// ===== Global State =====
float g_weight = 0;
float g_temp = 0;
String g_tip = "Натисніть для поради";
// ---------------- API ----------------
void routeRoot() {
extern const char index_html[] PROGMEM;
server.send(200,"text/html",index_html);
}
void routeMetrics() {
DynamicJsonDocument doc(512);
doc["weight"] = g_weight;
doc["temp"] = g_temp;
doc["ai"] = g_tip;
String json; serializeJson(doc,json);
server.send(200,"application/json",json);
}
void routeAsk() {
g_tip = "Зачекайте...";
DynamicJsonDocument req(1024);
req["model"]="gpt-3.5-turbo";
auto msgs=req.createNestedArray("messages");
JsonObject sys=msgs.createNestedObject();
sys["role"]="system";
sys["content"]="You are a cooking assistant. Give very short kitchen tips.";
JsonObject usr=msgs.createNestedObject();
usr["role"]="user";
usr["content"]="В мене " + String(g_weight,1) + "г продуктів при " + String(g_temp,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",OPENAI_KEY);
String body; serializeJson(req,body);
int code=http.POST(body);
if(code==200){
DynamicJsonDocument resp(2048);
deserializeJson(resp,http.getString());
g_tip=resp["choices"][0]["message"]["content"].as<String>();
Serial.println("AI => " + g_tip);
} else {
g_tip="AI error " + String(code);
}
http.end();
server.send(200,"text/plain","Запит надіслано");
}
// ---------------- Setup ----------------
void setup() {
Serial.begin(115200);
screen.init();
screen.backlight();
screen.setCursor(0,0); screen.print("Kitchen Ready");
WiFi.begin(WIFI_SSID,WIFI_PASS);
while(WiFi.status()!=WL_CONNECTED){ delay(300); Serial.print("."); }
Serial.println("\nIP: "+WiFi.localIP().toString());
hx.begin(PIN_DT,PIN_SCK);
hx.set_scale();
ds.begin();
server.on("/",routeRoot);
server.on("/metrics",routeMetrics);
server.on("/ask",routeAsk);
server.begin();
}
// ---------------- Loop ----------------
void loop() {
server.handleClient();
g_weight = hx.get_units(5);
ds.requestTemperatures();
g_temp = ds.getTempCByIndex(0);
screen.clear();
screen.setCursor(0,0); screen.print("W:" + String(g_weight,1)+" g");
screen.setCursor(0,1); screen.print("T:" + String(g_temp,1)+" C");
delay(1000);
}
// ---------------- HTML ----------------
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>🍳 Smart Kitchen Assistant</title>
<style>G
body { font-family: Arial; background:#fff8f0; color:#333; text-align:center; }
h2 { color:#c0392b; }
.card { border:1px solid #ccc; border-radius:12px; padding:15px; margin:10px; background:#fff3e6; }
button { padding:10px 20px; background:#e67e22; border:none; border-radius:8px; color:white; font-size:16px; }
button:hover { background:#d35400; }
</style>
<script>
function refresh(){
fetch('/metrics').then(r=>r.json()).then(j=>{
document.getElementById("weight").innerText=j.weight+" g";
document.getElementById("temp").innerText=j.temp+" °C";
document.getElementById("ai").innerText=j.ai;
});
}
function ask(){
fetch('/ask').then(r=>r.text()).then(alert);
setTimeout(refresh,1500);
}
setInterval(refresh,3000);
</script>
</head>
<body>
<h2>🍳 Smart Kitchen Assistant</h2>
<div class="card"><h3>Вага продуктів:</h3><p id="weight">--</p></div>
<div class="card"><h3>Температура продуктів:</h3><p id="temp">--</p></div>
<div class="card"><h3>AI Порада:</h3><p id="ai">Завантаження...</p></div>
<button onclick="ask()">Отримати пораду від ШІ</button>
</body>
</html>
)rawliteral";