Parsing Reply as HTTP Client
Posted: Tue Mar 15, 2016 5:19 pm
I'm trying to build some conditional logic based on the reply to a HTTP GET request I'm sending from the ESP8266, but I'm struggling to understand how to interrogate what is returned.
I'm running NodeMCU on a LiLon development board.
Essentially what I'm failing to understand is how I can catch the HTTP response in to a variable that I can then manually parse.
For example, the ESP8266 runs this code:
When running from ESPlorer (or I suppose just in general connected to the console) I see the following displayed on the console:
What I would now like to do is parse that reply and get the value between the <cammode> tags. I know it's XML and in other situations I could parse that, but I think with the memory limits on the ESP8266 I'll have to do it "by hand" (which is fine in this case).
I've shamelessly copied the HTTP client code above from somewhere on the internet (in my searches I don't know exactly where to credit) so I'll hold my hands up to not understanding it 100%.
If anyone could shed some light on how I can "capture" the response I'd be grateful, as I can handle the substring'ing from there.
I'm running NodeMCU on a LiLon development board.
Essentially what I'm failing to understand is how I can catch the HTTP response in to a variable that I can then manually parse.
For example, the ESP8266 runs this code:
Code: Select all
function sendHttp(host,requestString)
sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end)
sk:on("connection", function(sck)
sck:send("GET /" .. requestString .." HTTP/1.1\r\nHost: " .. host .. "\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end)
sk:connect(80,host)
end
camera = "192.168.54.1"
getCamState = "/cam.cgi?mode=getstate"
sendHttp(camera, getCamState)
When running from ESPlorer (or I suppose just in general connected to the console) I see the following displayed on the console:
Code: Select all
> HTTP/1.1 200 OK
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/xml
f9
<?xml version="1.0" encoding="UTF-8"?>
<camrply><result>ok</result><state><batt>3/3</batt><cammode>play</cammode><sdcardstatus>write_enable</sdcardstatus><sd_memory>set</sd_memory><sd_access>off</sd_access><version>D2.20</version></state></camrply>
0
What I would now like to do is parse that reply and get the value between the <cammode> tags. I know it's XML and in other situations I could parse that, but I think with the memory limits on the ESP8266 I'll have to do it "by hand" (which is fine in this case).
I've shamelessly copied the HTTP client code above from somewhere on the internet (in my searches I don't know exactly where to credit) so I'll hold my hands up to not understanding it 100%.
If anyone could shed some light on how I can "capture" the response I'd be grateful, as I can handle the substring'ing from there.