Read a JSON file on ESP8266

3

I need help to read a JSON file in an esp8266, the esp must open the file by means of a code in LUA, read the data it contains to assign them to the configuration of the static ip. The json file contains the following:

{
 "ip": "192.168.2.252", 
 "password": "contraseña del router", 
 "ide": "1261527000323", 
 "ssid": "SSID de mi router", 
 "gateway": "ip de mi router"
}

The problem is that I am new to LUA so I still can not find information to help me.

    
asked by Eder 19.04.2016 в 01:27
source

2 answers

2

You can use LUA's cjson library, although looking for a bit there are other alternatives such as Jeffery Friedl . The advantage of the first is the speed and the second is only an LUA file.

Example cjson:

-- abre fichero json
file = io.open("fichero.json", "r")

-- indica que vamos a usar file como fichero por defecto 
io.input(file)

-- carga fichero
textojson = io.read("*all")    

-- decodifica  y lo usa
t = cjson.decode( textojson )
for k,v in pairs(t) do print(k,v) end

--cierra fichero
io.close()

Anyway, if the configuration file is yours, you know the format of the lines and it does not vary in time, maybe you can consider doing the parsing by hand:

file.open( "fichero.txt", "r") 
while true 
do 
    line = file.readline() 
    if (line == nil) then break 
    //ToDo: aquí el código
end 
file.close()
    
answered by 06.05.2016 в 13:46
1

I really see better dkjson

You install them with luarocks with sudo luarocks install dkjson or in Debian / Ubuntu with sudo apt-get install lua-dkjson , you have too many utilities for json

json = require 'dkjson'

archivo = io.read("tu.json", 'r')

texto = archivo:read("*all")

diccionario = json.decode(texto)

for i,v in pairs(diccionario) do
    print(i, v)
end
    
answered by 20.03.2017 в 07:41