As the title says... Chat on...

User avatar
By zooto68
#10958 My code works, I just can't get my head around why (.-) works. I still don't get it.

Code: Select allconn=net.createConnection(net.TCP, 0)

-- Once connected to wensite send request
conn:on("connection",function(conn, payload)
            conn:send("GET /api/yourAPIkey/astronomy/q/UK/Gravesend.json"
                        .." HTTP/1.1\r\n"
                        .."Host: api.wunderground.com\r\n"
                        .."Connection: close\r\n"
                        .."Accept: */*\r\n"
                        .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; )\r\n"
                        .."\r\n"
                        )
end)

            -- What to do once data is received back from the website
conn:on("receive", function(conn, payload)
  success = true
  process(payload)
  conn:send("ok\r\n") 
end)

--t = tmr.now()   
conn:connect(80,'api.wunderground.com')

function process(payload)
     s = payload 
     s=(string.gsub(s, "%c+", "")) -- strip out the newline and carriage return characters
     print()
     -- TODO check valid APi return here
     
     -- sunrise
     sunriseHour, sunriseMinute = string.match(s, [["sunrise": {"hour":"(%d+)","minute":"(%d+)"]])
     print("Sunrise:\t"..sunriseHour..":"..sunriseMinute)
     -- sunset
     sunsetHour, sunsetMinute = string.match(s, [["sunset": {"hour":"(%d+)","minute":"(%d+)"]])
     print("Sunset:\t"..sunsetHour..":"..sunsetMinute)

     -- percentage of moon illuminated
     percentIlluminated = string.match(s, [["percentIlluminated":"(%d+)"]])
     print("Percent:\t"..percentIlluminated.."%")

     -- age of moon since last new moon
     age = string.match(s, [["ageOfMoon":"(%d+)"]])
     print("Age:\t\t"..age)

     -- Phase of the moon
     moonPhase = string.match(s, [["phaseofMoon":"(.-)"]])
     print("Phase:\t"..moonPhase)
end
User avatar
By zooto68
#10959
joe wrote:Because (%a+) fails at the space between the first and second words. It never gets beyond that. It only matches letters, not letters and spaces.


So why doesn't it return the word "Waxing" ?

joe wrote:P.S. Given the constrained memory on this device, I think you are going to have difficulties doing this kind of work on text in Lua. I don't know what your application is, but if it were me I'd look to be using a server as an intermediate processor to do the hard work for me and just send me the results.


Probably, but i'm just looking at taking some of the processing away from an Arduino. As much that the ESP8266 can do I will get it to do. I only bought one a few days ago so in that short space of time have had to learn Lua too. I think i'm doing OK so far.

The application is to simply gather some info from the internet (time,weather, etc.) for displaying on a dot matrix or TFT display in the home.

Mike
User avatar
By zooto68
#10964 OK now I understand why it works. I had a chat with a guy on the Lua IRC channel who was very helpful.

The " at the end of the [["phaseofMoon":"(.-)"]]) is part of the pattern. That is what was not clear to me. I was under the impression that it was what was inside the quotes that was important.

So it is now clear to me and reads as:

Find "phaseofMoon":"
Find the shortest string of any characters after this and up to the next "

:)

It's easy once it's explained properly.