Introduction: Google Search on ESP32
In this tutorial I will show you how to do google searches with an ESP32. The result itself isn't very useful because the search results are in the serial monitor on the computer, but it's a cool thing to do and show the power of ESP32. The code can be improved to create a mini web browser on ESP32 and print result on a LCD screen for example.
In this tutorial, I will use an ESP32 board with 4 MB of PSRAM to be sure to have enough memory. This can be useful to download the html code of sites found.
- ESP32 Board with external RAM like uPesy ESP32 Wrover DevKit
- Arduino IDE or PlatformIO with esp32 extension installed
- A Google Account
Step 1: Download HTML File or JSON File : the Good and the Bad Way
The easiest way to retrieve google searches would be to download the HTML page from the url: https://www.google.com/search?q=esp32, with your query after q=
This is the bad way for few reasons :
- It's difficult to parse (extract data), because there is no HTML parser for ESP32. So you have to find the right HTML tag, extract strings,... : the code will be messy.
- You could be blacklisted by Google : If you do too much research quickly, Google will consider you as a bot and good luck solving a captcha on ESP32.
The good way is to use the Google search API which returns a JSON file. A JSON file could be easily parsed on ESP32 with librairies like ArduinoJson. It will be very easy with this method to excract search results.
Step 2: Create a Search Engine
Step 3: Search Engine Configuration
Go to the control panel of the search engine to modify parameters:
- Enable "Search the entire web"
- You can change language or region, enable images
- Get the Search Engine ID, it will be useful for next steps
Scroll down until "Programmatic Acces" and click on "Get started"
Step 4: Get API Key
You should be now on https://developers.google.com website:
- Click on "Get a Key"
- Enter a Project name
- Copy your API Key
Step 5: Test API
Now we can test the API, URL is as follows :
Replace "YOUR_API_KEY" and "YOUR_SEARCH_ENGINE_ID" by yours.
In your web browser, go to this url, you should see as result a Json file with google searches results like in the screenshot.
The list of all parameters are available here https://developers.google.com/custom-search/v1/ref...
Step 6: Install ArduinoJson Library
To parse JSON file, we will use ArduinoJson library.
Go to the Library Manager in Arduino IDE, and type ArduinoJson. Install the right library"ArduinoJson by Benoit Blanchon".
Congratulations, all configurations is done.
Step 7: Download the Sketch and Search on Google
For this last step :
- Download the sketch.
- Add your WiFi crendentials, your API Key and your Engine ID.
- Compile the sketch and use the serial moniteur to send your query.
More tutorials on my website: upesy.com