Read a JSON file on ESP8266


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": "", 
 "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

2 answers


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 ="fichero.json", "r")

-- indica que vamos a usar file como fichero por defecto 

-- carga fichero
textojson ="*all")    

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

--cierra fichero

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: "fichero.txt", "r") 
while true 
    line = file.readline() 
    if (line == nil) then break 
    //ToDo: aquí el código
answered by 06.05.2016 в 13:46

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 ="tu.json", 'r')

texto = archivo:read("*all")

diccionario = json.decode(texto)

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