//Q3
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
// LCD Setup: Adjust I2C address and dimensions if needed
#define LCD_ADDRESS 0x27 // Change this to your LCD's address if different
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
// Button Pins
#define BUTTON_NEXT 14
#define BUTTON_PREV 12
// Variables for network scanning
int currentNetworkIndex = 0;
int totalNetworks = 0;
bool networksUpdated = false;
// Function prototypes
void scanNetworks();
void displayNetworkSSID(int index);
void displayError(String message);
void scanI2CDevices();
void setup() {
Serial.begin(115200);
Wire.begin();
// Scan I2C devices to find the correct address
scanI2CDevices();
// LCD initialization
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.backlight();
lcd.clear();
// Button initialization
pinMode(BUTTON_NEXT, INPUT_PULLUP);
pinMode(BUTTON_PREV, INPUT_PULLUP);
// Connect to WiFi (use a known network or open network)
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(1000);
Serial.println("Connecting to WiFi...");
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi!");
// Initial WiFi scan
scanNetworks();
}
void loop() {
// Handle button presses
if (digitalRead(BUTTON_NEXT) == LOW) {
currentNetworkIndex = (currentNetworkIndex + 1) % totalNetworks;
displayNetworkSSID(currentNetworkIndex);
delay(200); // Simple debounce delay
}
if (digitalRead(BUTTON_PREV) == LOW) {
currentNetworkIndex = (currentNetworkIndex - 1 + totalNetworks) % totalNetworks;
displayNetworkSSID(currentNetworkIndex);
delay(200); // Simple debounce delay
}
// Periodically scan for networks if not already updated
if (!networksUpdated) {
scanNetworks();
}
}
// Function to scan WiFi networks and store the total count
void scanNetworks() {
Serial.println("Scanning WiFi networks...");
totalNetworks = WiFi.scanNetworks();
networksUpdated = true;
Serial.print("Total networks found: ");
Serial.println(totalNetworks);
if (totalNetworks == 0) {
displayError("No networks found");
} else {
// Display the first network on the LCD
currentNetworkIndex = 0;
displayNetworkSSID(currentNetworkIndex);
}
}
// Function to display the SSID of a specific network on the LCD
void displayNetworkSSID(int index) {
lcd.clear();
String ssid = WiFi.SSID(index);
// Display SSID on the LCD
lcd.setCursor(0, 0);
lcd.print("SSID:");
lcd.print(ssid.substring(0, LCD_COLUMNS - 5)); // Display SSID, truncated to fit
// Display full details on the Serial Monitor
Serial.println("Displaying network details...");
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI(index));
Serial.print("Channel: ");
Serial.println(WiFi.channel(index));
Serial.print("Encryption: ");
Serial.println(WiFi.encryptionType(index));
Serial.println("-----------------------------");
}
// Function to display error messages
void displayError(String message) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
Serial.println(message);
}
// Function to scan for I2C devices and print their addresses
void scanI2CDevices() {
Serial.println("Scanning I2C devices...");
byte error, address;
int nDevices = 0;
for (address = 1; address < 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address < 16) Serial.print("0");
Serial.print(address, HEX);
Serial.println("!");
nDevices++;
delay(500); // Small delay to prevent flooding the serial monitor
} else if (error == 4) {
Serial.print("Unknown error at address 0x");
if (address < 16) Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found.");
} else {
Serial.println("Scanning complete.");
}
}