5

I'm stumped with sending data to a remote server , I'm able to send a post request but not sure how to add data which is then received by the server.

I've went through the datasheet http://www.jarzebski.pl/datasheets/SIM900_https-121018-1.00.pdf

tried

# usual at+sapbr=1,1 set up
+HTTPINIT
+HTTPPARA = “CID”,1
+HTTPPARA="URL","IP-ADDRESS:PORT"
+httpdata=100,10000
# Where do I add the post data ?
+httpaction=1

which sends the http post request. But how do I add data - I've tried adding it to the url ?key=val but no joy - any help here will be appreciated

trojan_spike
  • 187
  • 1
  • 1
  • 9
  • Good on you for finding a solution! Could you please put the content of your latest edit into an answer instead? Answering your own question is perfectly ok and the right thing to do in this case. – hlovdal Oct 27 '15 at 17:40

2 Answers2

15

httpdata=100,10000 means that SIM800 should expect 100 bytes within 10 seconds.

This is how I accomplished this using the HTTP client:

AT+HTTPINIT
AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","http://url.com/endPoint"
AT+HTTPPARA="CONTENT","application/json"
AT+HTTPDATA=40,10000

At this point, the SIM800 should respond with "DOWNLOAD". Which means it's expecting your data. Send in your data; in my case:

{"location_id": 238, "fill_percent": 90}

Wait 10 seconds to send the rest of the commands. Then:

AT+HTTPACTION=1
AT+HTTPREAD
AT+HTTPTERM

That did it for me. Hope it helps.

This is where I got the information from: http://www.raviyp.com/embedded/194-sim900-gprs-http-at-commands

In the backend, using Python Flask, this is the code I used

@app.route('/reportTrashLevel', methods=['POST'])
def report_trash_level():
    data = request.get_json()
    database.insert_trash_level(data)

    return Response(status=200)
Alan Alvarez
  • 253
  • 3
  • 10
  • would you please provide me the code written in server side? – alex Jul 13 '17 at 21:27
  • 1
    @alex I've edited the answer to reflect the code I used – Alan Alvarez Jul 14 '17 at 22:43
  • Per the documents, there are a restricted number of headers you can use with this method, so if you have something like a authorization header token, then you'll want to use the TCP connection style that @trojan_spike used. – Ross Rogers Oct 05 '18 at 20:12
8

I managed to get it to do what I need, this code-snippet will likely help others

AT+CGATT=1                  # enter GPRS configuration mode 
AT+CIPMUX=0                 # Disable multi IP connection mode.  Single IP cnxn only
AT+CSTT="APN","USER","PASS"
AT+CIICR                    # bring up wireless connection with GPRS and CSD 
AT+CIFSR                    # ip up - gprs working
AT+CIPSHUT                  # Exit GPRS configuration mode

# Now do a post request to remote server api in json format. 
# Change IP_ADDR|DOMAIN for the IP or domain name of your server.  
# Change 2000 to its port
AT+CIPSTART="TCP","IP_ADDR|DOMAIN","2000"

AT+CIPSEND=119 # Num of char in tcp/ip data, \r & \n count as 1 char
POST /post HTTP/1.1
Host: **.**.***.***
Content-Type: application/json
Content-Length:23

{"postkey":"postvalue"}

Hope this helps the next person stuck on it.

Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
trojan_spike
  • 187
  • 1
  • 1
  • 9
  • It did a couple of years later – thank you! The only thing that I don't understand is AT+CIPSHUT (close connections). Is that necessary to do before opening a new connection? In another example that I've seen, it was used after sending the data, which is more understandable. – ximo May 14 '18 at 21:28
  • BTW, this works on SIM7000, and possibly other more recent SIMCOM chips. I don't think the AT+HTTP… commands mentioned in the other answers are supported anymore. – ximo May 14 '18 at 21:28
  • Also, `AT+CIFSR` is a bit wonky, it doesn't respond with `OK` or `+CIFSR: ` as one might expect. I've found `AT+CIFSREX` much more reliable (and faster for some reason). It is supported by SIM7000 at least. – ximo May 20 '18 at 11:44