无法使用esp32和Arduinojson从Googlesheet获取数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用esp32和Arduinojson从Googlesheet获取数据相关的知识,希望对你有一定的参考价值。
我是esp32的新手,现在我正尝试使用ArduinoJson获取数据,在我的Googlesheet中,“ A1”有一个数字,我想让esp32来获取它,我将googlesheet发送到互联网并使用网址:https://spreadsheets.google.com/feeds/cells/1RICYSv0y0wEEu-PhcCL45jZmh7DlFSAsbW8Bie0inbA/1/public/values?alt=json&range=A1。而我的电话号码位于对象>提要>条目> 0>内容> $ t。
[这里是问题:当我使用esp32获取此json数据时,即使我的esp32已连接到googlesheet,我也无法获取它
如果需要,这是我的完整代码:
#include <ArduinoJson.h>
#include <WiFi.h>
#include <SPI.h>
WiFiClient client;
const char* ssid = "wwwwwwwww";
const char* password = "wwwwwwww";
const char* server = "spreadsheets.google.com";
const char* resource = "/feeds/cells/1RICYSv0y0wEEu-PhcCL45jZmh7DlFSAsbW8Bie0inbA/1/public/values?alt=json&range=A1";
const unsigned long HTTP_TIMEOUT = 10000; // max respone time from server
const size_t MAX_CONTENT_SIZE = 1024; // max size of the HTTP response
byte mac[] = 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED;
struct clientData
char item[8];
;
void setup()
Serial.begin(9600);
while (!Serial)
Serial.println("Serial ready");
Serial.print("Connecting to wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
delay(500);
Serial.print(".");
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
void loop()
if(connect(server))
if(sendRequest(server, resource) && skipResponseHeaders())
clientData clientData;
if(readReponseContent(&clientData))
printclientData(&clientData);
disconnect();
wait();
bool connect(const char* hostName)
Serial.print("Connect to ");
Serial.println(hostName);
bool ok = client.connect(hostName, 80);
Serial.println(ok ? "Connected" : "Connection Failed!");
return ok;
bool sendRequest(const char* host, const char* resource)
Serial.print("GET ");
Serial.println(resource);
client.print("GET ");
client.print(resource);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(host);
client.println("Connection: close");
client.println();
return true;
bool skipResponseHeaders()
// HTTP headers end with an empty line
char endOfHeaders[] = "\r\n\r\n";
client.setTimeout(HTTP_TIMEOUT);
bool ok = client.find(endOfHeaders);
if (!ok)
Serial.println("No response or invalid response!");
return ok;
bool readReponseContent(struct clientData* clientData)
const size_t bufferSize = 5*JSON_ARRAY_SIZE(1) +
JSON_ARRAY_SIZE(5) + 10*JSON_OBJECT_SIZE(1) +
6*JSON_OBJECT_SIZE(2) + 7*JSON_OBJECT_SIZE(3) +
JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(7) + JSON_OBJECT_SIZE(15);
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.parseObject(client);
if (!root.success())
Serial.println("JSON parsing failed!");
return false;
strcpy(clientData->item, root["feed"]["entry"][0]["content"]["$t"]);
return true;
void printclientData(const struct clientData* clientData)
Serial.print("Time = ");
Serial.println(clientData->item);
void disconnect()
Serial.println("Disconnect");
client.stop();
void wait()
Serial.println("Wait 20 seconds");
delay(20000);
串行输出:
Connecting to wifi: wwwwwwwwww
..
WiFi connected
IP address:
xxx.xxx.x.xx
Connect to spreadsheets.google.com
Connected
GET /feeds/cells/1RICYSv0y0wEEu-PhcCL45jZmh7DlFSAsbW8Bie0inbA/1/public/values?alt=json&range=A1
JSON parsing failed!
Disconnect
Wait 20 seconds
如果我基于您的json输出并使用ArduinoJson Assistant计算bufferSize,我会得到这个:
const size_t capacity = 5*JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(5) + 10*JSON_OBJECT_SIZE(1) + 6*JSON_OBJECT_SIZE(2) + 7*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(7) + JSON_OBJECT_SIZE(15) + 2270;
末尾省略了+ 2270
?您解析的json失败,因为您的bufferSize不足2270个字节(即,您的bufferSize太小)。
Updte
我没有尝试调试您的代码,但是我可以方便地使用此Web客户端,因此我可以运行它,并且对我有用。
我的代码基于ESP32 Arduino Core的HTTPClient,您可以在https://github.com/espressif/arduino-esp32/tree/master/libraries/HTTPClient上进一步了解它。它基于您使用的Arduino Wificlient,但更容易获得负载。我建议您使用它。
我在代码中添加了ArduinoJson,但是我的是基于v6语法的,如果仍然觉得使用v5更容易,可以将其转换为v5语法。顺便说一句,json文档包含Unicode,我必须在程序的顶部添加#define ARDUINOJSON_DECODE_UNICODE 1
才能使其正常工作,否则我将得到deserializationerror
为NotSupported
。
#define ARDUINOJSON_DECODE_UNICODE 1
#include <ArduinoJson.h>
#include <WiFi.h>
#include <HTTPClient.h>
HTTPClient http;
const char* ssid = "your_SSID";
const char* password = "your_password";
const char* url = "https://spreadsheets.google.com/feeds/cells/1RICYSv0y0wEEu-PhcCL45jZmh7DlFSAsbW8Bie0inbA/1/public/values?alt=json&range=A1";
void setup()
Serial.begin(115200);
while (!Serial)
Serial.print("Connecting to wifi: "); Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
delay(200);
Serial.print(".");
Serial.println();
Serial.print("IP address: "); Serial.println(WiFi.localIP());
http.begin(url);
int httpCode = http.GET(); //send GET request
if (!httpCode == 200)
Serial.print("Error on HTTP request: ");
Serial.println(httpCode);
else
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
// the following is based on ArduinoJson v6 API
StaticJsonDocument<4000> doc;
DeserializationError err = deserializeJson(doc, payload);
if (err)
Serial.print("deserialization error ");
Serial.println(err.c_str());
JsonObject feed = doc["feed"];
const char* clientData = feed["entry"][0]["content"]["$t"];
Serial.println(clientData);
http.end(); //Free the resources
void loop()
以上是关于无法使用esp32和Arduinojson从Googlesheet获取数据的主要内容,如果未能解决你的问题,请参考以下文章
ESP8266在Arduinojson获取网络数据时防止数据为空