私はesp32が初めてで、ArduinoJsonを使用してデータを取得しようとしています。Googleシート「A1」に番号があり、esp32にそれを取得させたいので、Googleシートをインターネットに送信し、URLを使用します: https: //spreadsheets.google.com/feeds/cells/1RICYSv0y0wEEu-PhcCL45jZmh7DlFSAsbW8Bie0inbA/1/public/values?alt=json&range=A1 . 私の番号は object > feed > entry > 0 > content > $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