ashirafumiiro / Arduino-File-Upload

Uploading files with some Arduino Supported boards and Sim800l
GNU General Public License v2.0
1 stars 1 forks source link

Problem with client.connect(server,port) #1

Open asadi2021 opened 1 year ago

asadi2021 commented 1 year ago

Hi, I have used your program for uploading a file to server , but connecting to server failed. Serial monitor is here : Initializing modem... Modem: SIM800 R14.18 Waiting for network... OK Connecting to mtnapn OK Local IP:212.235.246.28 Signal quality:23 card initialized. Connecting to teknifa.com fail

ashirafumiiro commented 1 year ago

Hello @asadi2021 The code supports only http. Please confirm that your server is handling http connections. The code does not work with https. You can share your url for the backend if you are not sure about that.

asadi2021 commented 1 year ago

Hello @ashirafumiiro Thank you for your reply My server is handling only http connection but I don't know where is problem , I used your example and I changed these variables :

`#define GSM_PIN 4

define SIM800_RX_PIN 13

define SIM800_TX_PIN 15

include

SoftwareSerial SerialAT(SIM800_RX_PIN, SIM800_TX_PIN); // RX, TX

const char apn[] = "mtnapn"; const char user[] = ""; const char pass[] = ""; const char server[] = "mydomain"; const char resource[] = "/tiny/upload.php"; const int port = 80; char fileName[] = "test.txt";`

ashirafumiiro commented 1 year ago

@asadi2021, from your description, the code is failing while trying to connect to the server. Since the server is using http, the most likely issue to cause the failure is with the GPRS connection. please confirm that your GSM can make a http request by running a simple example from the tinyGSM library.

asadi2021 commented 1 year ago

Hi, @ashirafumiiro , Thank you I try tinyGSM example and connected to gprs , I used simple post command and get success response: String contentType = "application/json"; // "application/x-www-form-urlencoded"; String postData = "{ \"deviceid\": \"1\", \"voltage\": \""+String(voltage)+"\"}"; SerialMon.print(F("Performing HTTP POST request... ")); http.post(resource, contentType, postData);

I still can't send files. I have written the following code using your code: . . `#include

include `

. . . TinyGsmClient client(modem); HttpClient http(client, server, port); //ArduinoHttpClient.h . . .

` SD_init(); readFile(SD_MMC, "/12816_12.jpg"); // uint8_t pix[] //file content; size_t img_len is file size

SerialMon.println("Connecting to server: " + serverName); char fileName[] = "12816_12.jpg"; //"datafile.txt"; String fileType = "multipart/form-data" ; //"text/plain"; String content = "--boundary1\r\n"; content += "Content-Disposition: form-data; name=\"fileToUpload\"; filename="+String(fileName)+"\r\n"; // the fileToUpload is the form parameter content += "Content-Type: "+fileType+"\r\n\r\n"; //after this, post the file data. String closingContent = "\r\n--boundary1--"; http.print(F("POST /PHMS/upload.php HTTP/1.1\r\n")); // replace with your /path/to/uploat/script http.print(String("Host: ") + server + "\r\n"); http.print("Content-Type: multipart/form-data; boundary=boundary1\r\n"); http.print("Content-Length: "+String(content.length()+img_len+closingContent.length())+"\r\n"); //dataFile.size() http.print(F("Connection: close\r\n\r\n")); http.print(content); String data = ""; int i = 0; while (i<img_len) { http.print(pix[i]); i++; }

http.print(closingContent); // Read respnse unsigned long timeout = millis(); while (http.connected() && millis() - timeout < 50000L) { // Print available data while (http.available()) { char c = http.read(); SerialMon.print(c); timeout = millis(); } } SerialMon.println(); // Shutdown http.stop();`

Thank you for guiding me.

ashirafumiiro commented 1 year ago

Sorry @asadi2021. The code in this repository only supports text files. You will have to modify it to be able to send binary data like images. It is not so direct but I know someone built on top of this and was able to send sound files from some MCUs