Created
June 18, 2018 13:16
-
-
Save youjunjer/1caa0f51b16819f97121a6406006bf28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//you need to connect to wifi first | |
void wifisendfile() | |
{ | |
String host="xxx.xxx.xxx.xxx";//your host name or ip | |
//prepare httpclient | |
Serial.println("Starting connection to server..."); | |
Serial.println(host); | |
WiFiClient client; | |
//start http sending | |
if (client.connect(host.c_str(), 80)) | |
{ | |
//open file | |
myFile = SD.open("/pic.jpg");//change to your file name | |
int filesize=myFile.size(); | |
Serial.print("filesize="); | |
Serial.println(filesize); | |
String fileName = myFile.name(); | |
String fileSize = String(myFile.size()); | |
Serial.println("reading file"); | |
if (myFile) | |
{ | |
String boundary = "CustomizBoundarye----"; | |
String contentType = "image/jpeg";//change to your file type | |
// prepare http post data(generally, you dont need to change anything here) | |
String postHeader = "POST " + url + " HTTP/1.1\r\n"; | |
postHeader += "Host: " + host + ":80 \r\n"; | |
postHeader += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n"; | |
postHeader += "Accept-Charset: utf-8;\r\n"; | |
String keyHeader = "--" + boundary + "\r\n"; | |
keyHeader += "Content-Disposition: form-data; name=\"key\"\r\n\r\n"; | |
String requestHead = "--" + boundary + "\r\n"; | |
requestHead += "Content-Disposition: form-data; name=\"\"; filename=\"" + fileName + "\"\r\n"; | |
requestHead += "Content-Type: " + contentType + "\r\n\r\n"; | |
// post tail | |
String tail = "\r\n--" + boundary + "--\r\n\r\n"; | |
// content length | |
int contentLength = keyHeader.length() + requestHead.length() + myFile.size() + tail.length(); | |
postHeader += "Content-Length: " + String(contentLength, DEC) + "\n\n"; | |
// send post header | |
char charBuf0[postHeader.length() + 1]; | |
postHeader.toCharArray(charBuf0, postHeader.length() + 1); | |
client.write(charBuf0); | |
//Serial.print("send post header="); | |
//Serial.println(charBuf0); | |
// send key header | |
char charBufKey[keyHeader.length() + 1]; | |
keyHeader.toCharArray(charBufKey, keyHeader.length() + 1); | |
client.write(charBufKey); | |
//Serial.print("send key header="); | |
//Serial.println(charBufKey); | |
// send request buffer | |
char charBuf1[requestHead.length() + 1]; | |
requestHead.toCharArray(charBuf1, requestHead.length() + 1); | |
client.write(charBuf1); | |
//Serial.print("send request buffer="); | |
//Serial.println(charBuf1); | |
// create file buffer | |
const int bufSize = 2048; | |
byte clientBuf[bufSize]; | |
int clientCount = 0; | |
// send myFile: | |
while (myFile.available()) | |
{ | |
clientBuf[clientCount] = myFile.read(); | |
clientCount++; | |
if (clientCount > (bufSize - 1)) | |
{ | |
client.write((const uint8_t *)clientBuf, bufSize); | |
clientCount = 0; | |
} | |
} | |
if (clientCount > 0) | |
{ | |
client.write((const uint8_t *)clientBuf, clientCount); | |
//Serial.println("Sent LAST buffer"); | |
} | |
// send tail | |
char charBuf3[tail.length() + 1]; | |
tail.toCharArray(charBuf3, tail.length() + 1); | |
client.write(charBuf3); | |
//Serial.print(charBuf3); | |
} | |
} | |
//print response | |
while(client.connected()) | |
{ | |
while(client.available()) | |
{ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
} | |
myFile.close(); | |
Serial.println("closing connection"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment