What if we could build a small search engine that fits inside our hands, using nothing more than an ESP32, a tiny OLED display, and three push buttons?
Sounds interesting, right?
Recently, I worked on a small embedded project where I used an ESP32 to create a basic search interface. The idea was simple: enter a search query using physical buttons, send it over Wi-Fi, fetch a search result from DuckDuckGo, and display the response on a 128×64 OLED screen.
This project is a good example of how embedded systems can interact with web services using HTTP requests.
Let's understand how it works.
1. Hardware and Software Requirements
Hardware:
- ESP32 development board
- 0.96-inch SSD1306 OLED display (128×64)
- Three push buttons
- Connecting wires
- USB power supply
Software and libraries:
- Arduino IDE
- WiFi.h – For wireless connectivity
- HTTPClient.h – For making HTTP requests
- Wire.h – For I2C communication
- Adafruit_GFX.h – Graphics library
- Adafruit_SSD1306.h – OLED display driver
2. Understanding the System Architecture
Before looking at the code, let's understand the overall working principle.
The project follows a simple sequence:
- ESP32 initializes the OLED display.
- It connects to the configured Wi-Fi network.
- The user enters a search query using three buttons.
- The ESP32 converts the query into a URL-compatible format.
- An HTTPS GET request is sent to DuckDuckGo.
- The ESP32 receives the HTML response.
- It extracts a search-result snippet from the received HTML.
- The result is displayed on the OLED.
The user can scroll through the result using the buttons.
The ESP32 essentially acts as a small web client.
3. Initializing the OLED Display
The first important part is configuring the OLED display.
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(
SCREEN_WIDTH,
SCREEN_HEIGHT,
&Wire,
OLED_RESET
);Here:
- SCREEN_WIDTH defines the horizontal resolution.
- SCREEN_HEIGHT defines the vertical resolution.
- Wire initializes the I2C communication interface.
- OLED_RESET is set to -1 because the display does not use a separate reset pin.
The display is initialized inside setup():
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for (;;);
}The address 0x3C is a commonly used I2C address for SSD1306 displays.
If initialization fails, the ESP32 stops execution.
4. Using Three Buttons as a Keyboard
One interesting part of this project is that we don't use a conventional keyboard.
Instead, three physical buttons are used to navigate through a character set.
#define BTN_UP 2
#define BTN_SELECT 0
#define BTN_DOWN 15The character set is defined as:
const char charSet[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_ <*";Each button has a specific purpose:
- UP: Moves forward through the available characters.
- DOWN: Moves backward through the characters.
- SELECT: Adds the selected character to the query.
There are also three special characters:
_ represents a space.< represents backspace.* represents search/submit.
For example, to enter ESP32, we navigate through the characters and select them one by one.
The selected character is stored using an index:
int selectedCharIdx = 0;The index is updated using modular arithmetic:
selectedCharIdx =
(selectedCharIdx + 1) % totalChars;This ensures that once we reach the last character, navigation starts again from the beginning.
Similarly, the DOWN button moves backward.
The buttons are configured using INPUT_PULLUP, meaning a pressed button reads LOW and a released button reads HIGH.
5. Implementing Two Different Operating Modes
Instead of handling all operations in one place, the program uses a simple state machine.
enum AppState {
TYPING_MODE,
SCROLL_MODE
};
AppState currentState = TYPING_MODE;There are two operating modes:
Typing Mode
The user enters a search query and navigates through the character selection interface.
Scroll Mode
After submitting a query, the search result is displayed. The user can scroll through the text.
The main loop decides which function should execute:
void loop() {
if (currentState == TYPING_MODE) {
handleTypingControls();
}
else if (currentState == SCROLL_MODE) {
handleScrollControls();
}
}This approach keeps the input-handling logic separated and makes the program easier to understand and maintain.
6. Handling User Input
The handleTypingControls() function manages all three buttons.
Whenever UP or DOWN is pressed, the selected character index changes.
When SELECT is pressed, the program checks which character is currently selected.
char chosen = charSet[selectedCharIdx];Then it performs one of four operations:
- If the character is
*, it starts the search. - If the character is
<, it removes the last character. - If the character is
_, it inserts a space. - Otherwise, it appends the selected character to the query.
The query length is limited to 18 characters:
if (customQuery.length() < 18) {
customQuery += chosen;
}This restriction helps keep the input manageable on the small display.
7. Connecting ESP32 to Wi-Fi
The Wi-Fi connection is established inside setup():
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(400);
}The ESP32 continuously checks whether it has successfully connected to the network.
Once connected, it can communicate with external web servers.
For an actual deployment, it is better to add a connection timeout and display an error message if Wi-Fi is unavailable.
8. Sending an HTTP Request to DuckDuckGo
This is where the project becomes interesting.
The main search functionality is implemented inside:
void performPublicSearch(String query)First, the program checks whether Wi-Fi is connected.
Then it prepares the search query:
String encodedQuery = query;
encodedQuery.replace(" ", "+");For example:
ESP32 embedded programming
becomes:
ESP32+embedded+programming
The search URL is constructed:
String url =
"https://html.duckduckgo.com/html/?q=" + encodedQuery;Next, the HTTP client is initialized:
HTTPClient http;
http.begin(url);
http.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
);
int httpCode = http.GET();Here:
HTTPClient handles the HTTP communication.begin() initializes the request URL.setUserAgent() sets the user-agent header.GET() sends an HTTPS GET request.
The ESP32 then receives the server's response.
9. Extracting Search Results from HTML
A search engine does not directly return plain text in this implementation.
Instead, DuckDuckGo returns an HTML page.
The program retrieves the complete response:
String payload = http.getString();It then searches for the HTML element containing the result snippet:
int snippetStart =
payload.indexOf("class=\"result__snippet\"");If the element is found, the code locates its starting and ending positions:
int textStart =
payload.indexOf(">", snippetStart) + 1;
int textEnd =
payload.indexOf("</a>", textStart);The extracted content is stored:
searchResult =
payload.substring(textStart, textEnd);Some basic HTML entities and tags are removed:
searchResult.replace("<b>", "");
searchResult.replace("</b>", "");
searchResult.replace(""", "\"");
searchResult.replace("&", "&");
searchResult.replace("'", "'");This gives us readable text that can be displayed on the OLED.
An important limitation: This is a basic HTML-parsing approach. Search engine HTML structures can change, and the first matching snippet may not always represent the complete result. A proper HTML parser or a supported search API would be more reliable for a production application.
Also, replacing spaces with + is only basic query encoding. A robust implementation should URL-encode all special characters.
10. Creating a Scrolling Text Interface
A 128×64 OLED cannot display a large amount of text at once.
To overcome this limitation, the project implements vertical scrolling.
int scrollOffset = 0;
const int lineSpacing = 12;
int maxScroll = 0;The search result is divided into lines:
int charsPerLine = 19;Each line is displayed at a different vertical position:
int startY = 16 + scrollOffset;Whenever the user presses UP or DOWN, the scroll offset changes.
For example:
scrollOffset -= lineSpacing;moves the displayed content upward, revealing the next lines.
The maximum scrolling distance is calculated based on the estimated number of lines:
int totalLines =
(searchResult.length() / 19) + 2;
int visibleLines = 4;
maxScroll = max(
0,
(totalLines - visibleLines) * lineSpacing
);A small scrollbar is also drawn on the right side of the display.
This gives the user a visual indication of their position within the result.
11. Designing the OLED User Interface
The project uses the Adafruit GFX library to create a simple graphical interface.
Instead of displaying plain text, it uses:
- Rounded rectangles
- Inverted header bars
- Horizontal separator lines
- Character selection indicators
- Search progress animation
- Scrollbar indicators
Three major display functions are implemented.
drawSplashScreen()
Displays the initial welcome screen while the ESP32 connects to Wi-Fi.
drawSearchAnimation()
Displays a simple loading animation when a search request is initiated.
updateDisplay()
Refreshes the OLED interface according to the current operating mode.
One useful technique is using:
display.fillRoundRect();to highlight the currently selected character.
The interface is completely drawn using software, without requiring any additional graphical hardware.
12. What Happens When We Press the Search Button?
Let's put everything together.
Suppose the user enters:
ESP32
After selecting *, the following sequence takes place:
- The program checks whether the query is empty.
- A search animation is displayed.
- The ESP32 sends the query to DuckDuckGo.
- The server returns an HTML response.
- The program extracts the first matching result snippet.
- The result is stored in
searchResult. - The scrolling offset is reset.
- The application switches to
SCROLL_MODE. - The OLED displays the extracted result.
Pressing UP and DOWN scrolls through the result, while SELECT returns to the typing interface.
That's the complete working principle of this mini search engine.
13. Possible Improvements
Although this project is relatively simple, there are several ways to improve it.
Some ideas for the next version:
- Use an HTML parser instead of manually searching for HTML tags.
- Implement proper URL encoding.
- Display multiple search results.
- Add automatic text wrapping instead of splitting text after a fixed number of characters.
- Replace blocking delays with non-blocking button debouncing.
- Add a Wi-Fi connection timeout and reconnection mechanism.
- Use a JSON-based search API instead of scraping HTML.
- Add a battery indicator and low-power sleep mode.
- Store Wi-Fi credentials securely instead of hardcoding them.
- Use dynamic memory carefully to avoid heap fragmentation caused by repeated String operations.
One more important point: the current implementation extracts HTML content using string searching. It does not validate the complete HTML structure, handle every possible HTML entity, or guarantee that the result will always fit the display.
These are good areas to explore when moving from a prototype to a more reliable embedded product.
14. What I Learned from This Project
This project demonstrates that an embedded device does not have to be limited to reading sensors, controlling motors, or switching GPIO pins.
With an ESP32 and a Wi-Fi connection, we can build interactive devices that communicate with web services and present useful information through a very small interface.
It also brings together several important embedded concepts:
- GPIO input handling
- I2C communication
- State machine implementation
- Wi-Fi connectivity
- HTTPS communication
- String manipulation
- Basic HTML parsing
- OLED graphics and UI development
- Memory and resource management
The interesting part is not just making a search request. It's bringing together hardware, firmware, networking, and user interaction into one small embedded system.
That's what makes embedded development exciting!
#ESP32 #EmbeddedSystems #EmbeddedC #Arduino #IoT #Microcontrollers #FirmwareDevelopment #Electronics #OLEDDisplay #WiFi #HardwareDevelopment #EngineeringProjects
Full Code:
//-------------------------------------------------------------------