-1

How do I receive and display the JSON data sent from a local server? I mean the script in the Lua fired at ESP8266 version 01. Ultimately, I want to display the data on an attached LCD 2x16 to ESP8266 by I2C.

I want to connect the ESP - 01 to a webapi server, which sends data, which contains different information, dynamically in JSON. I want this information to be displayed on the LCD connected to the ESP. I do not know how to decode the JSON data. The server address is 192.168.1.8:8057/api.

OK. I try this:

sk=net.createConnection(net.TCP, 0) 
sk:on("receive", function(sck, c) 
d = c
end )
sk:connect(8095,"192.168.1.8") 
sk:send("GET /api/ HTTP/1.1\r\nHost: 192.168.1.8\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

print(d)

and it display result:

{"lowVersion":1,"highVersion":3}

If I try:

sk=net.createConnection(net.TCP, 0) 
sk:on("receive", function(sck, c) 
d = c
end )
sk:connect(8095,"192.168.1.8") 
sk:send("GET /api/ HTTP/1.1\r\nHost: 192.168.1.8\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

local t = cjson.decode(d) -- this is line 10
print(t.lowVersion)

is display:

test.lua:10: Expected value but found invalid token at character 1

How to display the value "lowVersion" or "highVersion" from JSON?

Rafik73
  • 51
  • 1
  • 2
  • 13
  • 2
    StackOverflow is about asking specific questions to a specific problem. Yours is way to broad. Do some research and ask specific questions when you're stuck. Which module do you use? There's no such thing as ESP8266-01. There's ESP-01 (only 2 pins) or the NodeMCU dev kit v1.0 - and [many more](http://frightanic.com/iot/comparison-of-esp8266-nodemcu-development-boards/). – Marcel Stör May 23 '16 at 18:29
  • Have a look here: http://lua-users.org/wiki/JsonModules – Thomas W May 24 '16 at 05:28
  • Please, look again on my post. Please help. – Rafik73 May 25 '16 at 17:00
  • Do you need any more feedback? If not please [close](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it. – Marcel Stör May 29 '16 at 13:06

1 Answers1

0

So, this whole question boils down to why your cjson.decode() fails? It's because your server doesn't seem to return valid JSON.

Both of the following examples work just fine on a recent NodeMCU firmware from the dev branch.

local t = cjson.decode('{"lowVersion":1,"highVersion":3}')
print(t.lowVersion)

http.get("http://httpbin.org/get", nil, function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      local t = cjson.decode(data)
      for k,v in pairs(t) do print(k,v) end
    end
  end)

Yields

1

args    table: 3fff0618
url http://httpbin.org/get
origin  xxx.71.91.xxx
headers table: 3fff0a88

So, your server doesn't seem to consistently return {"lowVersion":1,"highVersion":3} or it may return certain invisible but invalid characters.

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198