#include <Wire.h>
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#define I2C_SDA 21
#define I2C_SCL 22
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* host = "api.thingspeak.com";
const int httpPort = 80;
const String channelID = "2729122";
const String writeApiKey = "WD7ZUL3QG8U6LKXY";
const String readApiKey = "IKO6E1P45OADOVCA";
int field1 = 0;
int numberOfResults = 3;
int fieldNumber = 1;
// Підключення датчика температури LM35 до аналогового входу A0
const int temperaturePin = 34;
const int servoPin = 16;
Servo myservo;
const int ledPin1 = 2; // Перший світлодіод підключений до піна 2
const int ledPin2 = 4; // Другий світлодіод підключений до піна 4
const int tempLowerLimit = 0;
const int tempUpperLimit = 10;
LiquidCrystal_I2C LCD(0x27, 16, 2);
int dl = 1000;
float prevTemp = 0;
const float nominalResistance = 10000;
const float nominalTemperature = 25;
const float bCoefficient = 3950;
const float seriesResistor = 10000;
// Cтворення кастомного символу
byte ch[] = {
B00010,
B00101,
B00010,
B00000,
B00000,
B00000,
B00000,
B00000
};
void setup() {
Wire.begin(I2C_SDA, I2C_SCL);
// Встановлення швидкості передачі даних для Serial Monitor
Serial.begin(115200);
if (Serial)
Serial.println("Serial available");
Serial.println();
Serial.println("******************************************************");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Налаштування пінів
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
myservo.attach(servoPin, 500, 2400);
// Створення кастомного символу
LCD.begin(16, 2);
LCD.backlight();
LCD.createChar(0, ch);
}
void loop() {
float temp = gettingTemp();
if (temp != prevTemp) {
LCD.clear();
controlLEDs(temp);
monitorControltemp(temp);
monitorExtraTemp(temp);
controlMicroServo(temp);
sendGetWrite(temp);
prevTemp = temp;
}
// Затримка
delay(dl);
}
void sendGetWrite(float temperature) {
WiFiClient client;
String footer = String(" HTTP/1.1\r\n") + "Host: " + String(host) + "\r\n" + "Connection: close\r\n\r\n";
if (!client.connect(host, httpPort)) {
return;
}
client.print("GET /update?api_key=" + writeApiKey + "&field1=" + temperature + footer);
readResponse(&client);
}
void controlLEDs(float temperature) {
if (temperature < tempLowerLimit - 10) {
digitalWrite(ledPin1, HIGH); // Запалюємо перший світодіод
digitalWrite(ledPin2, LOW); // Гасимо другий світодіод
} else if (temperature > tempUpperLimit + 10) {
digitalWrite(ledPin1, LOW); // Гасимо перший світодіод
digitalWrite(ledPin2, HIGH); // Запалюємо другий світодіод
} else {
digitalWrite(ledPin1, LOW); // Гасимо обидва світодіода
digitalWrite(ledPin2, LOW);
}
}
void monitorControltemp(float temperature) {
LCD.setCursor(0, 0);
LCD.print("Temp: ");
LCD.print(temperature);
LCD.write(byte(0));
LCD.print("C");
}
float gettingTemp() {
int adcValue = analogRead(temperaturePin);
// Convert the ADC reading to voltage (ESP32 ADC is 12-bit: 0-4095)
float voltage = adcValue / 4095.0 * 3.3;
// Calculate the resistance of the thermistor
float resistance = seriesResistor / ((3.3 / voltage) - 1);
// Calculate temperature in Kelvin using the Beta parameter equation
float temperatureK = 1.0 / (1.0 / (nominalTemperature + 273.15) + (1.0 / bCoefficient) * log(resistance / nominalResistance));
// Convert Kelvin to Celsius
float temperatureC = temperatureK - 273.15;
return temperatureC;
}
void controlMicroServo(float temperature) {
if (temperature >= tempUpperLimit) {
myservo.write(180);
} else if (temperature <= tempLowerLimit) {
myservo.write(0);
} else {
myservo.write(90);
}
}
void monitorExtraTemp(float temperature) {
if (temperature >= tempUpperLimit) {
LCD.setCursor(0, 1);
LCD.print("Status: HOT");
} else if (temperature <= tempLowerLimit) {
LCD.setCursor(0, 1);
LCD.print("Status: COLD");
} else {
LCD.setCursor(0, 1);
LCD.print("Status: OK");
}
}
void readResponse(WiFiClient* client) {
unsigned long timeout = millis();
while (client->available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client->stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while (client->available()) {
String line = client->readStringUntil('\r');
Serial.print(line);
}
Serial.printf("\nClosing connection\n\n");
}