#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);
// Initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // Wait two seconds for initializing
oled.clearDisplay(); // Clear display
oled.setTextSize(1); // Set text size
oled.setTextColor(WHITE); // Set text color
oled.setCursor(15, 30); // Set position to display (x,y)
oled.println("Welcome to APEX"); // Set text
oled.display(); // Display on OLED
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); // Wait 2 seconds for loading screen
// Loading animation during the loading screen
for (int i = 0; i < 3; i++) {
displayLoadingAnimation();
delay(500);
}
oled.clearDisplay(); // Clear display
// Connect to WiFi
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(battery, network);
}
String sendCommand(const String &command) { // sends an AT command to a device (assumedly a modem or similar) and returns the response
Serial2.println(command);
delay(100);
return Serial2.readString();
}
String get_network() { //retrieves the signal strength, and maps it to a percentage value
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() { // etrieves the battery level, and returns it.
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(const String &battery, const String &network) {
oled.clearDisplay();
// Display Battery Indicator on the left side
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(2, 5);
oled.print("Bat:");
oled.print(battery);
oled.print("%");
oled.drawRect(14, 20, 10, 20, WHITE);
oled.fillRect(14, 20, (int)(10 * battery.toInt() / 100), 20, WHITE);
// Display WiFi Indicator on the right side
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(110, 5);
oled.print("WiFi");
int net = network.toInt() / 20;
int x1 = 118, y1 = 20, x2 = 10, y2 = 4;
for (int i = 1; i <= net; i++) {
oled.fillRect(x1, y1, x2, y2, WHITE);
y1 += 6;
y2 += 6;
}
oled.display();
delay(2000);
}