138 lines
2.8 KiB
C++
138 lines
2.8 KiB
C++
#define DEBUG_MODE
|
|
#define pwm_pin 2
|
|
|
|
#include <FS.h>
|
|
#include <LittleFS.h>
|
|
#include <ArduinoJson.h>
|
|
#include <WiFi.h>
|
|
#include <GxEPD2.h>
|
|
#include <GxEPD2_BW.h>
|
|
#include <Fonts/FreeMonoBold9pt7b.h>
|
|
#include "GxEPD2_display_selection_new_style.h"
|
|
|
|
// GLOBAL VARS
|
|
String ssid;
|
|
String wifiPass;
|
|
int loop_count;
|
|
|
|
void setup() {
|
|
ledcAttach(pwm_pin, 200, 8);
|
|
ledcWrite(pwm_pin, 50);
|
|
|
|
// Initialize Serial only in debug mode
|
|
#ifdef DEBUG_MODE
|
|
Serial.begin(115200);
|
|
while (!Serial) {
|
|
delay(5000);
|
|
}
|
|
delay(5000);
|
|
Serial.println("Debug mode enabled");
|
|
Serial.flush();
|
|
#endif
|
|
|
|
display.init(115200, true, 2, false);
|
|
|
|
helloWorld();
|
|
|
|
display.hibernate();
|
|
|
|
// Initialize the file system
|
|
if (!LittleFS.begin()) {
|
|
logError("Failed to mount FS");
|
|
} else {
|
|
logError("FS Mounted");
|
|
}
|
|
|
|
#ifdef DEBUG_MODE
|
|
listFiles();
|
|
#endif
|
|
|
|
// Open config file
|
|
File file = LittleFS.open("/config.json", "r");
|
|
if (!file) {
|
|
logError("Failed to open config file");
|
|
}
|
|
|
|
// Parse JSON
|
|
StaticJsonDocument<200> doc;
|
|
DeserializationError error = deserializeJson(doc, file);
|
|
if (error) {
|
|
logError("Failed to parse config file");
|
|
logError(error.c_str());
|
|
}
|
|
|
|
// Extract values
|
|
ssid = doc["ssid"].as<String>();
|
|
wifiPass = doc["wifiPass"].as<String>();
|
|
|
|
// Debug output
|
|
#ifdef DEBUG_MODE
|
|
Serial.print("SSID: ");
|
|
Serial.println(ssid);
|
|
scanNetworks();
|
|
#endif
|
|
}
|
|
|
|
void loop() {
|
|
// put your main code here, to run repeatedly:
|
|
ledcChangeFrequency(pwm_pin, 10, 8);
|
|
ledcWrite(pwm_pin, 10);
|
|
#ifdef DEBUG_MODE
|
|
Serial.print("Loop Count: ");
|
|
Serial.println(loop_count);
|
|
#endif
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(ssid, wifiPass);
|
|
int maxAttempts = 50;
|
|
int attempts = 0;
|
|
|
|
while (WiFi.status() != WL_CONNECTED && attempts < maxAttempts) {
|
|
delay(100);
|
|
#ifdef DEBUG_MODE
|
|
Serial.print("\nAttempt ");
|
|
Serial.print(attempts + 1);
|
|
Serial.print(": Status = ");
|
|
Serial.println(WiFi.status());
|
|
#endif
|
|
attempts++;
|
|
}
|
|
|
|
updateTime();
|
|
|
|
#ifdef DEBUG_MODE
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
Serial.println("WiFi Connected!");
|
|
} else {
|
|
Serial.println("WiFi Connection Failed!");
|
|
}
|
|
#endif
|
|
WiFi.disconnect(true);
|
|
WiFi.mode(WIFI_OFF);
|
|
ledcWrite(pwm_pin, 0);
|
|
delay(3600000);
|
|
loop_count++;
|
|
return;
|
|
}
|
|
|
|
const char HelloWorld[] = "Hello World!";
|
|
|
|
void helloWorld() {
|
|
display.setRotation(1);
|
|
display.setFont(&FreeMonoBold9pt7b);
|
|
display.setTextColor(GxEPD_BLACK);
|
|
int16_t tbx, tby; uint16_t tbw, tbh;
|
|
display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
|
|
uint16_t x = ((display.width() - tbw) /2 ) - tbx;
|
|
uint16_t y = ((display.height() - tby) / 2) - tby;
|
|
display.setFullWindow();
|
|
display.firstPage();
|
|
do
|
|
{
|
|
display.fillScreen(GxEPD_WHITE);
|
|
display.setCursor(x, y);
|
|
display.print(HelloWorld);
|
|
}
|
|
while (display.nextPage());
|
|
|
|
}
|