i notified that strange issue in my similar codes, both load the information from the same json file:
Example one:
Code: Select all
with open('document.json') as data_file:
# pay attention, load NOT loads
data = ujson.load(data_file)
data_file.close()
yourWifiSSID = data["wifi"]["SSD"]
yourWifiPassword = data["wifi"]["pass"]
print('SSD: {} & pass: {}'.format(yourWifiSSID,yourWifiPassword))
sta_if = network.WLAN(network.STA_IF) # create station interface
sta_if.active(True) # activate the interface
sta_if.connect(yourWifiSSID, yourWifiPassword) # connect to an AP
while not sta_if.isconnected():
pass
Example two, i spilt json load and the connection in two functions:
Code: Select all
global data
def json_load():
global data
# read standard json configuration
with open('document.json') as data_file:
# pay attention, load NOT loads
data = ujson.load(data_file)
data_file.close()
print("json loaded.")
def do_connection():
global data
yourWifiSSID = data["wifi"]["SSD"]
yourWifiPassword = data["wifi"]["pass"]
print('SSD: {} & pass: {}'.format(yourWifiSSID,yourWifiPassword))
# connect the ESP8266 to local wifi network
sta_if = network.WLAN(network.STA_IF) # create station interface
sta_if.active(True) # activate the interface
sta_if.connect(yourWifiSSID, yourWifiPassword) # connect to an AP
while not sta_if.isconnected():
pass
# ------ MAIN
json_load()
do_connection()
What i'm doing wrong?