#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// Create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* password = ""; // Replace with your WiFi password
void setup() {
Serial.begin(9600);
// 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
oled.clearDisplay(); // Clear display
oled.setCursor(15, 30); // Set position to display (x,y)
oled.println("Loading...");
oled.display(); // Display on OLED
// Connect to WiFi
Serial.print("Connecting to ");
Serial.println(ssid);
}
// Add your main loop code here
void loop() {
// Read battery level (0-100)
int batteryLevel = analogRead(A0); // Connect your battery sensor to A0
// Map the analog reading to a percentage value
int batteryPercent = map(batteryLevel, 0, 1023, 0, 100);
// Display battery percentage
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0);
oled.print("Battery Level:");
oled.setTextSize(2);
oled.setCursor(0, 16);
oled.print(batteryPercent);
oled.print("%");
oled.display();
delay(1000); // You can adjust the delay according to your needs
}
void drawWiFiIndicator() {
// Draw WiFi symbol (for simplicity, just a basic shape)
oled.drawLine(125, 0, 120, 5, WHITE);
oled.drawLine(120, 5, 125, 10, WHITE);
oled.drawLine(122, 0, 122, 10, WHITE);
}