#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Change to -1 for no reset pin
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* password = ""; // Replace with your WiFi password
void setup() {
Serial.begin(115200);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Failed to start SSD1306 OLED"));
while (1);
}
delay(2000);
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(15, 30);
oled.println("Welcome to APEX");
oled.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println(WiFi.localIP());
delay(2000);
for (int i = 0; i < 3; i++) {
displayLoadingAnimation();
delay(500);
}
oled.clearDisplay();
Serial.print("Connecting to ");
Serial.println(ssid);
}
void displayLoadingAnimation() {
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(40, 30);
oled.print("Loading");
for (int i = 0; i < 3; i++) {
oled.print(".");
oled.display();
delay(500);
}
}
void loop() {
String network = get_network();
String battery = get_battery();
display_battery_and_network_animation(battery, network);
displaySymbolsAnimation();
delay(100); // Adjust delay as needed
}
String sendCommand(const String &command) {
Serial2.println(command);
delay(100);
return Serial2.readString();
}
String get_network() {
String response = sendCommand("AT+CSQ");
Serial.println(response);
int index1 = response.indexOf(":");
int index2 = response.indexOf(",");
String result = response.substring(index1 + 1, index2);
result.trim();
int signalStrength = result.toInt();
if (signalStrength == 99) {
return "0";
} else if (signalStrength >= 2 && signalStrength <= 9) {
return "20";
} else if (signalStrength >= 10 && signalStrength <= 14) {
return "40";
} else if (signalStrength >= 15 && signalStrength <= 19) {
return "60";
} else if (signalStrength >= 20 && signalStrength <= 31) {
return "99";
}
return "0";
}
String get_battery() {
String response = sendCommand("AT+CBC");
Serial.println(response);
int index1 = response.indexOf(",");
int index2 = response.indexOf(",", index1 + 1);
String result = response.substring(index1 + 1, index2);
result.trim();
return result;
}
void display_battery_and_network_animation(const String &battery, const String &network) {
oled.clearDisplay();
// Display Battery Indicator Animation on the top right corner
for (int i = 0; i <= battery.toInt(); i += 10) {
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(SCREEN_WIDTH - 40, 5);
oled.print("Bat:");
oled.print(i);
oled.print("%");
oled.drawRect(SCREEN_WIDTH - 52, 20, 10, 20, WHITE);
oled.fillRect(SCREEN_WIDTH - 52, 20, (int)(10 * i / 100), 20, WHITE);
oled.display();
delay(100);
}
// Display WiFi Indicator Animation next to Battery
for (int i = 0; i <= network.toInt(); i += 20) {
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(SCREEN_WIDTH - 40, 35);
oled.print("WiFi");
int net = i / 20;
int x1 = SCREEN_WIDTH - 30, y1 = 20, x2 = 10, y2 = 4;
for (int j = 1; j <= net; j++) {
oled.fillRect(x1, y1, x2, y2, WHITE);
y1 += 6;
y2 += 6;
}
oled.display();
delay(100);
}
delay(2000); // Adjust delay as needed
}
void displaySymbolsAnimation() {
//oled.clearDisplay();
// Display Heart Symbol
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(5, 5);
oled.print("❤");
// Display Temperature Symbol
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(40, 5);
oled.print("°C");
// Display Percentage Symbol
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(75, 5);
oled.print("%");
oled.display();
}