SIM800 post method sequence (python and PHP)

Hi,
I’m trying to complete the sequence to upload by post method a text file between a SIM800L, controlled by python, and the server with a PHP script
The php script is tested by Fiddler program (data lenght calc to upload) and seems work.
python code:

gsm = serial.Serial("/dev/ttyS2", baudrate=115200, timeout=15)
gsm.write(‘AT+SAPBR=3,1,“CONTYPE”,“GPRS”\r\n’)
reply_gsm() # wait OK
gsm.write(‘AT+SAPBR=3,1,“APN”,"’ + apn + ‘"\r\n’)
reply_gsm()
gsm.write(‘AT+SAPBR=1,1\r\n’) # Initiate the IP connection
reply_gsm()

sleep(2)
gsm.write(‘AT+HTTPINIT\r\n’)
reply_gsm()
gsm.write(‘AT+HTTPPARA=“CID”,1\r\n’)
reply_gsm()
gsm.write(‘AT+HTTPPARA=“URL”,“http://www.mysite.com/upload.php”\r\n’)
reply_gsm()
gsm.write(‘AT+HTTPPARA=“CONTENT”,“multipart/form-data; boundary=-------------------------acebdf13572468”\r\n’)
reply_gsm()
gsm = serial.Serial("/dev/ttyS2", baudrate=115200, timeout=50)
gsm.write(‘AT+HTTPDATA=196,10000\r\n’)
rcv = gsm.readline()
rcv = gsm.readline()
if “DOWNLOAD” in rcv:
gsm.write(’-------------------------acebdf13572468\n’)
gsm.write(‘Content-Disposition: form-data; name=“filename”; filename=“data.txt”\n’)
gsm.write(‘Content-Type: text/plain\n\n’)
gsm.write(‘hallo guys\n’)
gsm.write(’-------------------------acebdf13572468\n’)
rcv = gsm.readline()
rcv = gsm.readline()
gsm.write(‘AT+HTTPACTION=1\r\n’)
rcv = gsm.readline()
rcv = gsm.readline()
rcv = gsm.readline()
rcv = gsm.readline()
if “+HTTPACTION” in rcv: # waiting reply
print "SIM800 reply: ", rcv

	gsm.write('AT+HTTPREAD\r\n')
	rcv = gsm.readline()
	rcv = gsm.readline()
	rcv = gsm.readline()
	print rcv

gsm.write(‘AT+SAPBR=0,1\r\n’) #close connection now
reply_gsm()

################################################################
PHP code on the server: upload.php

<?php $target_dir = "test/"; $target_file = $target_dir . basename($_FILES["filename"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check file size if ($_FILES["filename"]["size"] > 150000) { echo "Sorry, your file is too large. "; $uploadOk = 0; } if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; } else { if (move_uploaded_file($_FILES["filename"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["filename"]["name"]). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file. "; } } ?>

any ideas?
cheers
Roberto

p.s. sorry for the not correct indent

Most of the time the problem is with datalength. The multipart form data needs extra new lines at the end of the data. Try adding and see. Also check if the bytes sent out are the same numbers as the count mentioned.
Capture data on UART in HEX mode and see the data length.

Its works, tested on SIM800 and SIM808

Arduino

gsm.print(F(“AT+HTTPPARA=“URL”,“http://…/post_check.php””));
gsm.print(RN);
response_ok(1000);
gsm.print(F(“AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded”));
gsm.print(RN);
response_ok(1000);
gsm.print(F(“AT+HTTPDATA=100,10000”)); // Start Download
gsm.print(RN);
response_ok(1000); // Warte auf OK, ERROR oder DOWNLOAD
gsm.print(F("name=Max "));
response_ok(1000);

gsm.print(F(“AT+HTTPACTION=1”));
gsm.print(RN);
response_ok(1000);

byte response_ok(int d) // Ausgabe von GSM bis OK oder ERROR kommt
{
byte check = 0; // 0= keine Antwort, 1 = OK, 2 = ERROR
char string[30] = “”;
int count = 0;
byte k = 0;
char z;
while(1)
{
while (gsm.available())
{
z = gsm.read();
if(z == 13 || z == 10) k = 0;
Serial.write(z);
string[k] = z;
if(k < 29) k++;
string[k] = ‘\0’;
if(strstr(string, OK)) {count = d; check = 1;}
if(strstr(string, “DOWNLOAD”)) {count = d; check = 3;}
if(strstr(string, “ERROR”)) {count = d; check = 2;}
}
delay(10);
count++;
if(count > d)
{
//Serial.println();
break;
}
}
return check;
}

PHP script post_check.php

<?php if (isset($_POST["name"])) { $name = $_POST["name"]; echo $name; } ?>

Gruss
Vlad

1 Like