#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
#define HTTP_DOMAIN "https://zbsmf3rk-5000.uks1.devtunnels.ms/" // Put the port forwarded IP here
#define HTTP_ENDPOINT_SUFFIX "specific/"
#define HTTP_PORT 5000
// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Buffer to store the audio data
const int bufferSize = 1024 * 64; // 64KB
uint8_t audioBuffer[bufferSize];
// Refresh Interval for a new guess
int interval = 5000; // every 5 secs
// List of animals
const char *animals[] = {"bird", "cat", "cow", "dog", "lion", "sheep"};
const int animalCount = sizeof(animals) / sizeof(animals[0]);
String selectedAnimal; // Holds the currently selected animal
String httpEndpoint; // Holds the dynamically constructed HTTP endpoint
// Function to connect to Wi-Fi
void connectToWiFi()
{
Serial.print("Connecting to Wi-Fi");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
}
// Function to randomly select an animal
void selectRandomAnimal()
{
int randomIndex = random(animalCount); // Randomly select an index
selectedAnimal = animals[randomIndex]; // Update the selected animal
httpEndpoint = String(HTTP_DOMAIN) + HTTP_ENDPOINT_SUFFIX + selectedAnimal;
Serial.print("Selected animal: ");
Serial.println(selectedAnimal);
Serial.print("HTTP Endpoint: ");
Serial.println(httpEndpoint);
}
// Function to fetch audio from the server
bool fetchAudioFromServer()
{
HTTPClient http;
http.begin(httpEndpoint);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK)
{
// Read the audio data from the response
WiFiClient *stream = http.getStreamPtr();
int contentLength = http.getSize();
if (contentLength > 0)
{
int bytesRead = 0;
while (http.connected() && (contentLength > 0 || contentLength == -1))
{
size_t size = stream->available();
if (size)
{
int len = stream->readBytes(audioBuffer + bytesRead, ((size > sizeof(audioBuffer)) ? sizeof(audioBuffer) : size));
bytesRead += len;
contentLength -= len;
}
}
Serial.println("Audio data fetched");
http.end();
return true;
}
}
else
{
Serial.printf("Error fetching audio: %d\n", httpCode);
}
http.end();
return false;
}
// Function to display text on the OLED display
void displayText(const char *label)
{
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print("Predicted:");
display.setCursor(0, 30);
display.print(label);
display.display(); // Display text
}
void classifyAudio()
{
// This should predict the audio file (in a real implementation)
Serial.println("Prediction not implemented");
// For now, display the selected animal
displayText(selectedAnimal.c_str());
}
void setup()
{
Serial.begin(115200);
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println("SSD1306 allocation failed");
for (;;);
}
display.clearDisplay();
display.display();
// Connect to Wi-Fi
connectToWiFi();
// Initialize random seed
randomSeed(analogRead(0)); // Ensure we get different random numbers
}
void loop()
{
// Select a random animal and update the HTTP endpoint
selectRandomAnimal();
// Fetch and classify audio data
if (fetchAudioFromServer())
{
classifyAudio();
}
else
{
Serial.println("Failed to fetch audio");
}
// Wait 5 seconds before repeating
delay(interval);
}