我尝试使用API从Arduino上传图像(JPG)到Google驱动器。 有时它可以工作(我按照指南中的说明执行POST请求并发送所有位),并且收到200 OK,但有时,在发送所有字节后,Google驱动器没有任何响应。 有什么建议吗?
这里有一部分代码用于上传(请注意,我仅粘贴了“问题”部分)。 -有时由于我收到200响应,所以周期结束。 -有时我没有收到回复,所以我再次开始发送图像。
在
while (millis() - startTime < 15000 && !received)
我试图等待长达十分钟,但没有回应
Serial.println("Token request has been succesful. Starting upload");
bool succesful = false;
// I have obtained the uploadID, now I start uploading
String location = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=" + uploadID;
received = true;
while(!succesful){ // I upload until it is successful
// I stop the previous client session, because now I start a new one, to do the PUT request and upload the file
client.stop();
// Upload request
if (client.connect("www.googleapis.com", 443)) {
client.println("PUT " + location + " HTTP/1.1");
client.println("User-Agent: Arduino Camera");
client.println("Content-Length: " + String(image.size()));
client.println("Connection: close");
client.println();
while (image.available()) {
client.write(image.read()); // Here I send the bytes of the image
}
Serial.println(".");
image.close();
received = false;
} else {
Serial.println("Connection failed");
received = true;
}
// Listening to the response
startTime = millis();
String code = "";
while (millis() - startTime < 15000 && !received) { //try to listen for 5 seconds
int i = 0;
while (client.available() && i < 12) {
received = true;
char c = client.read();
Serial.write(c);
code = code + c;
i++;
}
// HTTP 200 OK
if (code == "HTTP/1.1 200" || code == "HTTP/1.1 201")
{
while(client.available()) {
char c = client.read();
Serial.write(c);
}
Serial.println("\nUpload succesful");
succesful = true;
// client.stop();
return succesful;
}
if (!received) {
client.stop();
Serial.println("\nUpload interrupted. Starting a new session");
// I have to open image again
image = SD.open(filepath, FILE_READ);
}
}