ESP8266 上不稳定的 Micropython
Unstable Micropython on ESP8266
我在 ESP8266 上迈出了我在 Micropython 中的第一步。我上传了最新版本 1.13,当我尝试在 AP 模式下 运行 wifi 时 - 奇怪的事情发生了。每隔几个 wifi 运行s 就会工作一次,但大多数情况下没有响应(我无法从我的计算机上看到 wifi 网络)。那是我的代码:
boot.py
# Libraries
import uos, machine, ujson
import gc
# Changing CPU from 80MHz to 160MHz
machine.freq(160000000)
# Load and check the config file
print('BOOT: Loading database.json...')
data = open('database.json', 'r')
config = ujson.load(data)
data.close()
# Config is corrupted
if config['Device']['Manufacture'] != 'ACME' or config['Checksum'] != '18caE':
print('BOOT: database.json is corrupted! Uploading from backup.')
# Copy clean from backup
dataBackup = open('database-backup.json', 'r')
configBackup = ujson.load(dataBackup)
dataBackup.close()
if configBackup:
data = open('database.json', 'w')
ujson.dump(configBackup, data)
data.close()
print('BOOT: Uploading from backup completed! Rebooting system...')
# Reboot OS
machine.reset()
# Config is correct
else:
print('BOOT: database.json loading completed!')
# Clean memory
gc.collect()
main.py
# Libraries
import network, servers
print('MAIN: Loading module...')
servers.py
import network
print('SERVER: Loading module...')
# Setup Wi-Fi card
# As Access Point
if config['Configuration']['WiFiMode'] == 'Access Point':
print('SERVER: Setup as Access Point')
wifi = network.WLAN(network.AP_IF)
wifi.active(True)
wifi.config(essid = config['Device']['Name'] + ' ' + config['Device']['SerialNumber'], password = config['Configuration']['WiFiAPPassword'])
wifi.ifconfig(('192.168.0.1','255.255.255.0','192.168.0.1','192.168.0.1'))
# As Wi-Fi client
elif config['Configuration']['WiFiMode'] == 'Client':
print('SERVER: Setup as client ' + config['Configuration']['WiFiClientSSID'] + ' with password ' + config['Configuration']['WiFiClientPassword'])
else:
machine.reset()
database.json
{
"Device": {
"Manufacture": "ACME",
"Name": "My device",
"SerialNumber": "M0J4"
},
"Configuration": {
"WiFiAPPassword": "test1test",
"WiFiAPIP": "192.168.0.1",
"WiFiAPMask": "255.255.255.0",
"WiFiClientSSID": "Network name",
"WiFiClientPassword": "Network password",
"WiFiMode": "Access Point",
},
"Checksum": "18caE"
}
有时这段代码开始,但通常在 boot.py 之后停止。我崩溃了,因为我写了很多没有 ESP8266 的代码,从 12 个小时开始我就厌倦了开始...
“全局”变量仅在模块内是全局的。您的 config
变量在 main.py
、boot.py
和 REPL 中可见,因为它们都在相同的上下文(隐式 __main__
模块)中执行,但它不会在其他模块中可见。
一个解决方案是将您的配置处理代码移动到另一个模块中。例如,创建 config.py
为:
import gc
import json
import machine
config = {}
def load_config():
global config
# Load and check the config file
print("BOOT: Loading database.json...")
try:
with open("database.json", "r") as fd:
config = json.load(fd)
except OSError:
# failed to open file (maybe it didn't exist)
config = {}
# Config is corrupted
if (
config.get("Device", {}).get("Manufacture") != "ACME"
or config.get("Checksum") != "18caE"
):
print("BOOT: database.json is corrupted! Uploading from backup.")
# Copy clean from backup
with open('database-backup.json', 'r') as fd:
configBackup = json.load(fd)
with open('database.json', 'w') as fd:
json.dump(configBackup, fd)
print("BOOT: Uploading from backup completed! Rebooting system...")
# Reboot OS
machine.reset()
# Config is correct
else:
print("BOOT: database.json loading completed!")
# Clean memory
gc.collect()
然后你的boot.py
变成:
import machine
import gc
# Changing CPU from 80MHz to 160MHz
machine.freq(160000000)
gc.collect()
而 servers.py
看起来像:
import config
import machine
import network
print("SERVER: Loading module...")
config.load_config()
# Setup Wi-Fi card
# As Access Point
if config.config["Configuration"]["WiFiMode"] == "Access Point":
print("SERVER: Setup as Access Point")
wifi = network.WLAN(network.AP_IF)
wifi.active(True)
wifi.config(
essid="{} {}".format(
config.config["Device"]["Name"],
config.config["Device"]["SerialNumber"],
),
password=config.config["Configuration"]["WiFiAPPassword"],
)
wifi.ifconfig(("192.168.0.1", "255.255.255.0", "192.168.0.1", "192.168.0.1"))
# As Wi-Fi client
elif config.config["Configuration"]["WiFiMode"] == "Client":
print(
"SERVER: Setup as client {} with password {}".format(
config["Configuration"]["WiFiClientSSID"],
config["Configuration"]["WiFiClientPassword"],
)
)
else:
machine.reset()
我在 ESP8266 上迈出了我在 Micropython 中的第一步。我上传了最新版本 1.13,当我尝试在 AP 模式下 运行 wifi 时 - 奇怪的事情发生了。每隔几个 wifi 运行s 就会工作一次,但大多数情况下没有响应(我无法从我的计算机上看到 wifi 网络)。那是我的代码:
boot.py
# Libraries
import uos, machine, ujson
import gc
# Changing CPU from 80MHz to 160MHz
machine.freq(160000000)
# Load and check the config file
print('BOOT: Loading database.json...')
data = open('database.json', 'r')
config = ujson.load(data)
data.close()
# Config is corrupted
if config['Device']['Manufacture'] != 'ACME' or config['Checksum'] != '18caE':
print('BOOT: database.json is corrupted! Uploading from backup.')
# Copy clean from backup
dataBackup = open('database-backup.json', 'r')
configBackup = ujson.load(dataBackup)
dataBackup.close()
if configBackup:
data = open('database.json', 'w')
ujson.dump(configBackup, data)
data.close()
print('BOOT: Uploading from backup completed! Rebooting system...')
# Reboot OS
machine.reset()
# Config is correct
else:
print('BOOT: database.json loading completed!')
# Clean memory
gc.collect()
main.py
# Libraries
import network, servers
print('MAIN: Loading module...')
servers.py
import network
print('SERVER: Loading module...')
# Setup Wi-Fi card
# As Access Point
if config['Configuration']['WiFiMode'] == 'Access Point':
print('SERVER: Setup as Access Point')
wifi = network.WLAN(network.AP_IF)
wifi.active(True)
wifi.config(essid = config['Device']['Name'] + ' ' + config['Device']['SerialNumber'], password = config['Configuration']['WiFiAPPassword'])
wifi.ifconfig(('192.168.0.1','255.255.255.0','192.168.0.1','192.168.0.1'))
# As Wi-Fi client
elif config['Configuration']['WiFiMode'] == 'Client':
print('SERVER: Setup as client ' + config['Configuration']['WiFiClientSSID'] + ' with password ' + config['Configuration']['WiFiClientPassword'])
else:
machine.reset()
database.json
{
"Device": {
"Manufacture": "ACME",
"Name": "My device",
"SerialNumber": "M0J4"
},
"Configuration": {
"WiFiAPPassword": "test1test",
"WiFiAPIP": "192.168.0.1",
"WiFiAPMask": "255.255.255.0",
"WiFiClientSSID": "Network name",
"WiFiClientPassword": "Network password",
"WiFiMode": "Access Point",
},
"Checksum": "18caE"
}
有时这段代码开始,但通常在 boot.py 之后停止。我崩溃了,因为我写了很多没有 ESP8266 的代码,从 12 个小时开始我就厌倦了开始...
“全局”变量仅在模块内是全局的。您的 config
变量在 main.py
、boot.py
和 REPL 中可见,因为它们都在相同的上下文(隐式 __main__
模块)中执行,但它不会在其他模块中可见。
一个解决方案是将您的配置处理代码移动到另一个模块中。例如,创建 config.py
为:
import gc
import json
import machine
config = {}
def load_config():
global config
# Load and check the config file
print("BOOT: Loading database.json...")
try:
with open("database.json", "r") as fd:
config = json.load(fd)
except OSError:
# failed to open file (maybe it didn't exist)
config = {}
# Config is corrupted
if (
config.get("Device", {}).get("Manufacture") != "ACME"
or config.get("Checksum") != "18caE"
):
print("BOOT: database.json is corrupted! Uploading from backup.")
# Copy clean from backup
with open('database-backup.json', 'r') as fd:
configBackup = json.load(fd)
with open('database.json', 'w') as fd:
json.dump(configBackup, fd)
print("BOOT: Uploading from backup completed! Rebooting system...")
# Reboot OS
machine.reset()
# Config is correct
else:
print("BOOT: database.json loading completed!")
# Clean memory
gc.collect()
然后你的boot.py
变成:
import machine
import gc
# Changing CPU from 80MHz to 160MHz
machine.freq(160000000)
gc.collect()
而 servers.py
看起来像:
import config
import machine
import network
print("SERVER: Loading module...")
config.load_config()
# Setup Wi-Fi card
# As Access Point
if config.config["Configuration"]["WiFiMode"] == "Access Point":
print("SERVER: Setup as Access Point")
wifi = network.WLAN(network.AP_IF)
wifi.active(True)
wifi.config(
essid="{} {}".format(
config.config["Device"]["Name"],
config.config["Device"]["SerialNumber"],
),
password=config.config["Configuration"]["WiFiAPPassword"],
)
wifi.ifconfig(("192.168.0.1", "255.255.255.0", "192.168.0.1", "192.168.0.1"))
# As Wi-Fi client
elif config.config["Configuration"]["WiFiMode"] == "Client":
print(
"SERVER: Setup as client {} with password {}".format(
config["Configuration"]["WiFiClientSSID"],
config["Configuration"]["WiFiClientPassword"],
)
)
else:
machine.reset()