[Solved] How to send HTTP POST request via Lua Script?

Discussion on the Lua Scripting capabilities for RaceCapture/Pro. Also see the <a href="http://autosportlabs.net/RaceCapturePro_Lua_Scripting">Lua Scripting Guide</a>

Moderators: JeffC, stieg

Post Reply
GTIspirit
Posts: 249
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

[Solved] How to send HTTP POST request via Lua Script?

Post by GTIspirit »

How can we send an HTTP POST request to connected WiFi client via Lua Script?

So if I want to say use Chrome extension SERVISTATE to send commands to a WiFi client via POST request, how do we do it?

Here's another example of what it looks like:
https://forums.garmin.com/apps-software ... reless-api

@brentp this is all it takes to Start and Stop recording of Garmin Virb. Oh so close.....
Attachments
startRecording.JPG
startRecording.JPG (113.94 KiB) Viewed 5279 times
Last edited by GTIspirit on Fri May 15, 2020 12:02 pm, edited 1 time in total.

brentp
Site Admin
Posts: 6274
Joined: Wed Jan 24, 2007 6:36 am

Post by brentp »

To do this you'd have to turn off WiFi (so RCP isn't trying to control it) and then issue direct AT commands to the WiFi module in RC.

See the bottom of the page here:
https://wiki.autosportlabs.com/AutomaticCameraControl

The WiFi module is an ESP8266, so you'll be able to find information on the web on how to perform HTTP POST commands.

Let us know what you find out!
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

GTIspirit
Posts: 249
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Post by GTIspirit »

Boo ya, got it!

to start recording:

Code: Select all

function startRecording&#40;&#41;
  sendAt&#40;'AT+CIPSTART="TCP","192.168.0.1",80'&#41;
  sleep&#40;2000&#41;
  local crlf = string.char&#40;13&#41; ..string.char&#40;10&#41;
  local post = 'POST /virb HTTP/1.1\r\nHost&#58; 192.168.0.1\r\nAccept&#58; */*\r\nContent-Length&#58; 28\r\nContent-Type&#58; application/x-www-form-urlencoded\r\n\r\n&#123;"command"&#58;"stopRecording"&#125;\r\n' ..crlf ..crlf
  sendAt&#40;'AT+CIPSEND=' ..toInt&#40;#post&#41;&#41;
  sleep&#40;1500&#41;
  sendRaw&#40;post&#41;
println&#40;post&#41;
  sleep&#40;5000&#41;
  sendAt&#40;'AT+CIPCLOSE'&#41; 
end
to stop recording:

Code: Select all

function startRecording&#40;&#41;
  sendAt&#40;'AT+CIPSTART="TCP","192.168.0.1",80'&#41;
  sleep&#40;2000&#41;
  local crlf = string.char&#40;13&#41; ..string.char&#40;10&#41;
  local post = 'POST /virb HTTP/1.1\r\nHost&#58; 192.168.0.1\r\nAccept&#58; */*\r\nContent-Length&#58; 28\r\nContent-Type&#58; application/x-www-form-urlencoded\r\n\r\n&#123;"command"&#58;"stopRecording"&#125;\r\n' ..crlf ..crlf
  sendAt&#40;'AT+CIPSEND=' ..toInt&#40;#post&#41;&#41;
  sleep&#40;1500&#41;
  sendRaw&#40;post&#41;
println&#40;post&#41;
  sleep&#40;5000&#41;
  sendAt&#40;'AT+CIPCLOSE'&#41; 
end
Turns out, the Virb is picky about character indents at the start of each subsequent line. I had followed some examples found by Googling "esp8266 http post request at command" and had an extra space between \r\n and start of next line. I saw this with the println command, but didn't realize this makes a difference. Took out that space and voila, it works!

Now to skinny up the code since the Garmin API syntax is the same up to the command. According to API, this can be "status" "startRecording" "stopRecording" etc.

brentp
Site Admin
Posts: 6274
Joined: Wed Jan 24, 2007 6:36 am

Post by brentp »

That's great. Would you be interested in updating our wiki docs with this information?
https://wiki.autosportlabs.com/AutomaticCameraControl

If so, we'll open up wiki access to allow you to edit. email [email protected] to coordinate.
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

GTIspirit
Posts: 249
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Post by GTIspirit »

Sure, happy to contribute and update the Wiki.

Meanwhile, here's some more info on the POST command.

The Postman program was super helpful to figuring this out. The easy part is simply sending the command via the Postman command line.

What was really helpful was using the Postman feature to view the "code"

Then as @brentp suggested, I Googled "esp8266 http post request at command" Careful with the results, because some of them pertain to GET command, not POST command. (For reference, GET is what the GoPro Hero uses.)

These were the two most helpful hits for me to get this figured out.
https://stackoverflow.com/questions/406 ... an-esp8266
https://stackoverflow.com/questions/406 ... an-esp8266

After getting the above fixed command to work I came up with this more generic POST function.

Code: Select all

function postCommand&#40;cmd&#41;
  sendAt&#40;'AT+CIPSTART="TCP","192.168.0.1",80'&#41;
  sleep&#40;500&#41;
  local crlf = string.char&#40;13&#41; ..string.char&#40;10&#41;
  local post = 'POST /virb HTTP/1.1\r\nHost&#58; 192.168.0.1\r\nAccept&#58; */*\r\nContent-Length&#58; ' ..toInt&#40;#cmd&#41;..'\r\nContent-Type&#58; application/x-www-form-urlencoded\r\n\r\n' ..cmd ..'\r\n' ..crlf ..crlf
  sendAt&#40;'AT+CIPSEND=' ..toInt&#40;#post&#41;&#41;
  sleep&#40;100&#41;
  sendRaw&#40;post&#41;
  println&#40;post&#41;
  sleep&#40;100&#41;
  sendAt&#40;'AT+CIPCLOSE'&#41; 
end
It works by passing the required arguments to the postCommand function, e.g.:

Code: Select all

postCommand&#40;'&#123;"command"&#58;"startRecording"&#125;'&#41;
or

Code: Select all

postCommand&#40;'&#123;"command"&#58;"stopRecording"&#125;'&#41;
The Virb didn't seem to care if the command length was correct or not. e.g.
{"command":"startRecording"} is 28 characters, and
{"command":"stopRecording"} is 27 characters.

This corresponds to the Content-Length: value

But I kept this in there in case it matters for other devices. So now you can have fun trying this out to send info to other WiFi connected devices. :D
Attachments
Garmin Virb startRecording command via Postman
Garmin Virb startRecording command via Postman
startRecording-command.PNG (15.58 KiB) Viewed 5263 times
Garmin Virb startRecording HTTP code via Postman
Garmin Virb startRecording HTTP code via Postman
startRecording-code.PNG (17.09 KiB) Viewed 5263 times

Post Reply