/*
Project: ESP32-C3 Aquarium Controller
Description: See ReadMe.txt
Author: AnonEngineering
Date: 9/23/26
License: Beerware
*/
#include <SPI.h>
#include <WiFi.h>
#include <TimeLib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#include "secrets.h"
#define ONE_WIRE_BUS 3
#define TFT_DC 5
#define TFT_CS 7
const int SCREEN_WIDTH = 320;
const int SCREEN_HEIGHT = 240;
const int ONE_SECOND = 1000; // time interval
const int DL_OFFSET_SEC = 3600; // daylight savings offset
const long GMT_OFFSET_SEC = -(5 * 60) * 60; // GMT offset (EST is -5)
const char* NTP_ADDR = "pool.ntp.org";
unsigned long lastTime = 0;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void centerText(const char* text, int y) {
int16_t x1, y1;
uint16_t textWidth, textHeight;
tft.getTextBounds(text, 0, y, &x1, &y1, &textWidth, &textHeight);
int16_t x = (SCREEN_WIDTH - textWidth) / 2;
tft.setCursor(x, y);
tft.print(text);
}
void connectWiFi() {
static bool symbol = false;
char buffer[24];
strncpy(buffer, "Connecting to", 24);
//centerText(buffer, 10);
Serial.print(buffer);
Serial.print(" ");
//centerText(ssid, 30);
Serial.println(ssid);
//oled.display();
WiFi.begin(ssid, pass, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
//Serial.print(WiFi.status());
symbol = !symbol;
//oled.setCursor(63, 50);
//oled.print(symbol ? "X" : "+");
//oled.display();
}
Serial.println("");
//oled.clearDisplay();
strncpy(buffer, "WiFi connected.", 24);
//centerText(buffer, 32);
Serial.println(buffer);
//oled.display();
delay(1000);
//oled.clearDisplay();
}
void printLocalTime(bool mode24) {
char buffer[24];
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
if (!getLocalTime(timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
if (mode24) {
strftime (buffer, 24, " %H:%M:%S ", timeinfo);
} else {
strftime (buffer, 24, "%I:%M:%S%p", timeinfo);
}
//Serial.print(buffer);
centerText(buffer, 30);
strftime (buffer, 16, "%A", timeinfo);
centerText(buffer, 5);
//Serial.print("\t");
//Serial.print(buffer);
//Serial.print(", ");
strftime (buffer, 24, "%B %d, %Y", timeinfo);
//Serial.println(buffer);
centerText(buffer, 55);
}
void setup() {
Serial.begin(115200);
sensors.begin();
SPI.begin();
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK); // White text on a black background
tft.setTextSize(2);
// Connect to Wi-Fi
connectWiFi();
configTime(GMT_OFFSET_SEC, DL_OFFSET_SEC, NTP_ADDR);
}
void loop() {
//bool is24h = digitalRead(MODE_SW_PIN);
if (millis() - lastTime >= ONE_SECOND) {
lastTime = millis();
printLocalTime(true);
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println(" °C");
//delay(1000);
}
}
Heater
Air
Feeder
Water
level
Water temp
Tank lights