Nodemcu/LUA/ESP8266 - extract JSON data received
Posted: Thu Jul 09, 2015 5:29 pm
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?
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?