ESP8266 如何保存远程的图片到SPIFFS或SD卡呢
楼主有个项目,是需要把远程的图片存到esp8266 的文件系统SPIFFS中比如获取百度的logo https://www.baidu.com/img/bd_logo1.png
使用的是arduino的IDE
程序
File myfile;
void setup( ){
wifi配置等。 就不重复了
SPIFFS.begin();
}
void loop( ) {
HTTPClient http;
http.begin("http://www.baidu.com/img/bd_logo1.png");
int Code = http.GET();
if(Code){
String payload = http.getString();
Serial.print(payload);
myfile=SPIFFS("/bd_logo1.png","w");
myfile.close();
}
}
但IDE反馈的信息,只显示了文件的类型png,文件也没有保存到本地。请问有大神知道错在哪里?
这个我就老熟悉了,debug的语句你自己改下
int downloadFromServer(const char *host, int port, const char *url)
{
char *filename = strrstr(url, "/");
if (SPIFFS.exists(filename))
SPIFFS.remove("fname");
File file = SPIFFS.open(filename, "w");
if (!file) {
debugPrintf("Can't open %s !\r\n", filename);
return -1;
}
debugPrintf("begin %s:%d/%s\n", host, port, url);
HTTPClient http;
http.begin(host, port, url);
debugPrint(" GET...\n");
int httpCode = http.GET();
debugPrintf(" GET... code: %d\n", httpCode);
if (httpCode != HTTP_CODE_OK)
{
debugPrintf(" GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
return -2;
}
int fileLen = http.getSize();
uint8_t buff;
memset(buff, 0, sizeof(buff));
WiFiClient * stream = http.getStreamPtr();
debugPrintf("get File len:%d\r\n", fileLen);
while (http.connected() && (fileLen > 0 || fileLen == -1))
{
size_t size = stream->available();
if (size) {
int l = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
file.write(buff, l);
if (fileLen > 0) {fileLen -= l;}
}
}
file.close();
debugPrint(" connection closed or file end.\n");
http.end();
return 1;
} 话说真的可以保存到8266里面吗? hackxiaoya 发表于 2019-3-9 16:18
话说真的可以保存到8266里面吗?
楼上那位老哥就做过 跑龙套的保 发表于 2018-8-2 17:34
这个我就老熟悉了,debug的语句你自己改下
int downloadFromServer(const char *hos ...
这个只能保存到FLASH里吗?换成SD卡可不可以? a7178636965 发表于 2020-3-10 14:02
这个只能保存到FLASH里吗?换成SD卡可不可以?
可以,SPIFFS换成SD相关的即可 跑龙套的保 发表于 2018-8-2 17:34
这个我就老熟悉了,debug的语句你自己改下
int downloadFromServer(const char *hos ...
下载其它网页的怎么弄,我一直都没有成功
页:
[1]