ESP32调用Google Vision人脸检测接口返回错误码-5如何解决
问题排查与解决方案
错误码含义说明
ESP32的Arduino HTTPClient库返回值 -5 代表HTTPS连接建立失败,绝大多数情况是SSL证书校验不通过,你当前用的普通WiFiClient不支持HTTPS站点的SSL握手逻辑,是问题的核心诱因。
修复步骤
- 第一步:引入SSL客户端依赖
在代码头部新增WiFiClientSecure头文件:#include <WiFiClientSecure.h> - 第二步:替换连接客户端为SSL版本
把loop函数里的WiFiClient client;替换为WiFiClientSecure client;,并且在http.begin之前新增跳过SSL验证的配置(临时调试用,生产环境建议导入Google API的根证书提升安全性):WiFiClientSecure client; HTTPClient http; // 新增这行跳过SSL证书校验 client.setInsecure(); http.begin(client, serverName); - 第三步:验证基础配置正确性
- 确认你填写的
MyAPIKey是Google Cloud控制台已开通Vision API权限的有效密钥,没有额度不足、IP限制等问题 - 确认当前WiFi网络可以正常访问Google服务,没有防火墙或DNS拦截
- 你当前的请求体JSON结构和官方测试一致,不需要调整
- 确认你填写的
核心修改后的代码片段
// 头部新增 #include <WiFiClientSecure.h> // loop函数内修改部分 if(WiFi.status()== WL_CONNECTED){ // 替换为SSL客户端 WiFiClientSecure client; HTTPClient http; // 跳过SSL证书验证,快速调试用 client.setInsecure(); http.begin(client, serverName); Serial.print("Conected to Server: "); Serial.println(serverName); http.addHeader("Content-Type", "application/json"); Serial.print("Posting request: "); Serial.println(_request); int httpResponseCode = http.POST(_request); // 后续逻辑保持原有代码不变
内容的提问来源于stack exchange,提问作者Paulo Borges
相关产品推荐
相关产品推荐

