-->
Page 1 of 1

Nodemcu/LUA/ESP8266 - extract JSON data received

PostPosted: Thu Jul 09, 2015 5:29 pm
by fixingthingsguy
Here's a problem that I have been struggling with.
I have a simple door open/ close app for the ESP8266 that sends open or close to the cloud.
What I can do:
- Send data to the cloud, verify it got there. All seems well
What I want to do and I can do
-Use Json to receive last entry of data, the data is received and printed for me.
as follows: {"s3":"Open"} s3 is the data stream and the status is OPEN as retrieved from the cloud.
What I want do, but don't know how!!
-I want to read the data that says OPEN, and turn on a LED by making a GPIO port :High.
-I somehow have to extract the OPEN and make the required LED Port HIGH. How do I
get that data assigned to a variable?
-I have thought of scanning the entire "payload" and looking for OPEN. Is there another way? using the CJSON?
And how do I do that.

Here are code and output extracts.


[code][/code]
-----------------code segment begin
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)print("Payload ") print(payload) end)
conn:connect(80,'xxx.xxx.xx.xx')
conn:send("PUT /api/feed?ompTmplId=xy&compId=12:xx:be:ef:fe:ed&compName=www&api_key=xxxxxxxxxxxxxxxxxxxxxx&freq=11000&s3="..value1.."&rsid=s3 HTTP/1.0\r\n\r\n")
conn:send("Host: http://www.grovestreams.com \r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")

----------------code segment end



OUTPUT BELOW::::::::::::::::::::::


HTTP/1.1 200 OK
Date: Thu, 09 Jul 2015 00:41:35 GMT
Server: Apache
Accept-Ranges: bytes
Vary: Accept-Charset,Accept-Encoding,Accept-Language,Accept
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: accept, origin, x-requested-with, content-type
Access-Control-Max-Age: 86400
Content-Length: 13
Access-Control-Allow-Credentials: true
Connection: close
Content-Type: application/json;charset=UTF-8

{"s3":"Open"} >>>>>>>>>>>how do I access this value so I can decide to set a GPIO port?

Re: Nodemcu/LUA/ESP8266 - extract JSON data received

PostPosted: Tue Jul 14, 2015 9:49 pm
by fixingthingsguy
Hi = I figured out how to do this, using as a guide this link https://github.com/squix78/esp8266-projects/blob/master/weather-station/weatherStation.lua
which was very helpful.
Here's the relevant changes, of course this is only a snippet
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
local payload = string.match(payload, "{.*}")
[i]print(payload)
t=nil
t=string.sub(payload,8,8)
print (t)
payload = nil
conn:close()
conn = nil

end )
conn:connect(80,'173.236.12.163')
conn:send("PUT /api/feed?ompTmplId= mysensor&compId=xxxxxxxxxx&compName=NNN&api_key=xxxxxx-xxx-xxxxxxxx&freq=11000&s3="..value1.."&rsid=s5 HTTP/1.0\r\n\r\n")

conn:send("Host: http://www.grovestreams.com \r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")


end


OUTPUT
{"s3":"R"}
R >>>>>>>>>>>>>this is the value you can then use to set a LED or whatever.