SIM800 post method sequence (python and PHP)


#1

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


#2

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.