Hi there,
So I went back and created a custom search in the Google API console. It worked very well, returns a Json list of Data, Now I can can build on this by:
-
Parsing the JSON response: If you want to extract specific data like URLs or descriptions from the search results,
-
Handling different queries: I can dynamically change the search query based on input, sensors, or other conditions in the project.
HTH
GL PJ
#include <WiFi.h>
#include <HTTPClient.h>
const char *ssid = "GlassSurf-2.4";
const char *password = "SeeedStudio :-p ";
// Google API Key and CSE ID
const String apiKey = "REPLACE with YOUR KEY "; // Your API key
const String searchEngineId = "REPLACEwithYOURs"; // Your CSE ID
// Search query
const String searchQuery = "Arduino project"; // you can set this to whatever your searching for
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Perform Google search
googleSearch(searchQuery);
}
void googleSearch(String query) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Make sure to replace spaces with '%20' for URL encoding
String encodedQuery = query;
encodedQuery.replace(" ", "%20");
// Correct URL formatting for Google's Custom Search API
String url = "https://www.googleapis.com/customsearch/v1?key=" + apiKey + "&cx=" + searchEngineId + "&q=" + encodedQuery;
Serial.print("Requesting URL: ");
Serial.println(url);
// Make GET request to Google Custom Search API
http.begin(url);
int httpCode = http.GET();
// If the GET request was successful
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Search Result:");
Serial.println(payload); // Print the raw JSON result from the API
} else {
Serial.print("Error on HTTP request: ");
Serial.println(httpCode);
}
http.end(); // Free resources
} else {
Serial.println("WiFi not connected");
}
}
void loop() {
// Nothing to
}
The output is Long so I’ll only add some of it.
Connecting to WiFi...
Connected to WiFi!
IP Address: 192.168.1.187
Requesting URL: https://www.googleapis.com/customsearch/v1?key=YOMOMA...&cx=NO_MOMA&q=Arduino%20project
Search Result:
{
"kind": "customsearch#search",
"url": {
"type": "application/json",
"template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"
},
"queries": {
"request": [
{
"title": "Google Custom Search - Arduino project",
"totalResults": "42000000",
"searchTerms": "Arduino project",
"count": 10,
"startIndex": 1,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "b7801a19959ec4def"
}
],
"nextPage": [
{
"title": "Google Custom Search - Arduino project",
"totalResults": "42000000",
"searchTerms": "Arduino project",
"count": 10,
"startIndex": 11,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "b7801a19959ec4def"
}
]
},
"context": {
"title": "SeeedTest"
},
"searchInformation": {
"searchTime": 0.350992,
"formattedSearchTime": "0.35",
"totalResults": "42000000",
"formattedTotalResults": "42,000,000"
},
"items": [
{
"kind": "customsearch#result",
"title": "Arduino Project Hub",
"htmlTitle": "\u003cb\u003eArduino Project\u003c/b\u003e Hub",
"link": "https://projecthub.arduino.cc/",
"displayLink": "projecthub.arduino.cc",
"snippet": "Arduino Project Hub is a website for sharing tutorials and descriptions of projects made with Arduino boards.",
"htmlSnippet": "\u003cb\u003eArduino Project\u003c/b\u003e Hub is a website for sharing tutorials and descriptions of projects made with Arduino boards.",
"formattedUrl": "https://projecthub.arduino.cc/",
"htmlFormattedUrl": "https://\u003cb\u003eproject\u003c/b\u003ehub.\u003cb\u003earduino\u003c/b\u003e.cc/",
"pagemap": {
"cse_thumbnail": [
{
"src": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQVpvlC8drwI09Tpqidcqz-ZuCn9_wLIkhBcwCM8pZenZLcrjEqLOkW0dQ&s",
"width": "259",
"height": "194"
}
],
"metatags": [
{
"next-head-count": "10",
"twitter:card": "summary_large_image",
"og:site_name": "Arduino Project Hub",
"viewport": "width=device-width",
"og:title": "Arduino Project Hub",
"title": "Arduino Project Hub"
}
],
"cse_image": [
{
"src": "https://projects.arduinocontent.cc/thumbnails/resized-mobile-e457819e-a3e1-4e2a-b906-0b6434bbb2d7.png"
}
Works GREAT!