-->
Page 1 of 2

Parsing Reply as HTTP Client

PostPosted: Tue Mar 15, 2016 5:19 pm
by Sid351
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:

Code: Select allfunction 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.

Re: Parsing Reply as HTTP Client

PostPosted: Tue Mar 22, 2016 12:52 pm
by Sid351
I feel like this should be something pretty basic that I'm just not getting.

Has anyone got any links I could read as reference please?

Re: Parsing Reply as HTTP Client

PostPosted: Tue Mar 22, 2016 1:42 pm
by Geert
Take a look at how LUA uses string handling:

http://lua-users.org/wiki/StringLibraryTutorial
Code: Select all> str = "Hello Lua user"
> = str:sub( 7)      -- from character 7 until the end
Lua user


Code: Select all> str = "Hello Lua user"
> = str:.find("Lua")
7

You can use this example to work on your code:
Code: Select allstr = "this is <html>another string</html> blablabla"
newstr = str:sub(str:find("<html>") + 6, str:find("</html>")-1)

newstr contains now 'another string'

Code: Select allfunction sendHttp(host,requestString)
    sk=net.createConnection(net.TCP, 0)
    sk:on("receive", function(sck, c) print(c:sub(c:find("<cammode>") + 8, c:find("</cammode>")-1)) 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)

Re: Parsing Reply as HTTP Client

PostPosted: Tue Mar 22, 2016 3:30 pm
by Sid351
Geert wrote:Take a look at how LUA uses string handling:


Thanks for the examples. How do I go about capturing the output that is currently being displayed on the console in order to interpret it?