#include <WiFi.h>
#include <WebServer.h>
#include "RTClib.h"
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
#define led 2
const char* SSID = "Wokwi-GUEST";
const char* password = "";
const char* openaiApiKey = "b453666eb3d04c1fa7c608375d88f7fc";
RTC_DS1307 rtc;
WebServer server(80);
String dataValue = "";
#define analog_In 12
const int buttonPin = 13;
const float GAMMA = 0.7;
const float RL10 = 50;
void handleRoot() {
server.send(200, "text/plain", "Поточні дані: \n" + dataValue);
}
void handleUpdate() {
if (server.hasArg("value")) {
String new_data = server.arg("value");
if(new_data=="rec"){
request_gpt("Analyze the time the lamp turns on and off, and draw certain conclusions about when and under what brightness conditions the light turns on."+ dataValue);
}
} else {
server.send(400, "text/plain", "Немає параметра 'value'");
}
}
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
void setup() {
Serial.begin(115200);
pinMode(analog_In, INPUT);
pinMode(led, OUTPUT);
pinMode(buttonPin, INPUT);
WiFi.begin(SSID, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/update", handleUpdate);
rtc.begin();
LCD.init();
LCD.backlight();
}
void request_gpt(String promt){
DynamicJsonDocument jsonDocument(1024);
jsonDocument["model"] = "gpt-3.5-turbo";
JsonArray messages = jsonDocument.createNestedArray("messages");
JsonObject systemMessage = messages.createNestedObject();
systemMessage["role"] = "system";
systemMessage["content"] = "You are a smart lamp. Analyze available data and predict the next lamp turns on and off";
JsonObject userMessage = messages.createNestedObject();
userMessage["role"] = "user";
userMessage["content"] = promt;
HTTPClient http;
String apiUrl = "https://artificialintelligence.openai.azure.com/openai/deployments/test/chat/completions?api-version=2023-05-15";
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("api-key", openaiApiKey);
String requestBody;
serializeJson(jsonDocument, requestBody);
http.setTimeout(15000);
int httpCode = http.POST(requestBody);
Serial.println(httpCode);
if (httpCode == 200) {
const String& response = http.getString();
Serial.println(response);
DeserializationError error = deserializeJson(jsonDocument, response);
if (error) {
Serial.print("Помилка розбору JSON: ");
Serial.println(error.c_str());
return;
}
String content = jsonDocument["choices"][0]["message"]["content"].as<String>();
Serial.println("Відповідь GPT:\n" + content);
} else {
Serial.println("Помилка HTTP-запиту");
}
http.end();
}
void loop() {
server.handleClient();
DateTime now = rtc.now();
String Time = "Час: " + String(now.hour())+":" + String(now.minute())+":" + String(now.second());
dataValue = dataValue+'\n'+'\n'+Time + '\n';
int analogValue = analogRead(analog_In);
Serial.println(analogValue);
float voltage = analogValue / 4096. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux_a = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
Serial.println(lux_a);
String Lux = "Освітленість: " + String(lux_a);
if (lux_a < 100) {
digitalWrite(led, HIGH);
} else{
digitalWrite(led, LOW);
}
dataValue = dataValue + Lux + '\n';
Serial.println("Зібрані дані: \n" + dataValue);
LCD.clear();
LCD.setCursor(0, 0);
LCD.print(Time);
LCD.setCursor(0, 1);
LCD.print(Lux);
delay(150);
int currentButtonState = digitalRead(buttonPin);
if(currentButtonState == HIGH) {
Serial.println("Кнопка натиснута");
request_gpt("Analyze the time the lamp turns on and off, and draw certain conclusions about when and under what brightness conditions the light turns on."+ dataValue);
}
delay(500);
}