Initial code commit

This commit is contained in:
Lexi / Zoe 2021-12-28 22:10:27 +01:00
parent c9eba67347
commit 167847cd28
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
5 changed files with 138 additions and 0 deletions

12
.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
# IDE / editor files
.idea/
.vscode/
.*.swp
# General
/_tmp
# Python
__pycache__
*.py[cod]
/venv

26
src/boot.py Normal file
View File

@ -0,0 +1,26 @@
# boot.py -- run on boot-up
import network
import pycom
from config import Config
import wlan_manager
# Disable Pybytes and smart configuration
pycom.pybytes_on_boot(False)
pycom.smart_config_on_boot(False)
# Load config from flash memory
config = Config()
config.load()
# Enable or disable heartbeat
pycom.heartbeat(config.get('heartbeat'))
# Reconfigure FTP and Telnet server
server = network.Server()
server.deinit()
server.init(login=(config.get('login_user'), config.get('login_password')), timeout=600)
# Connect to WLAN
wlan_manager.connect(config)

34
src/lib/config.py Normal file
View File

@ -0,0 +1,34 @@
import ujson
CONFIG_FILENAME = '/flash/config.json'
class Config:
def __init__(self):
# Define default config
self._config = {
'login_user': 'micro',
'login_password': 'python',
'heartbeat': True,
'wlan_config': {},
}
# Load config from JSON file on flash memory
def load(self) -> None:
try:
with open(CONFIG_FILENAME, 'r') as config_file:
self._config.update(ujson.load(config_file))
except Exception:
# Use default config
pass
# Save config as JSON file to flash memory
def save(self) -> None:
with open(CONFIG_FILENAME, 'w') as config_file:
config_file.write(ujson.dumps(self._config))
def get(self, key: str):
return self._config[key]
def set(self, **kwargs) -> None:
self._config.update(kwargs)

40
src/lib/wlan_manager.py Normal file
View File

@ -0,0 +1,40 @@
from network import WLAN
import time
from config import Config
default_wlan_config = {
'ssid': '',
'wpa2_key': '',
'ip_address': '',
'ip_subnet': '',
'ip_gateway': '',
'ip_nameserver': '',
}
def connect(config: Config) -> WLAN:
wlan_config = default_wlan_config.copy()
wlan_config.update(config.get('wlan_config'))
wlan = WLAN(mode=WLAN.STA)
wlan.connect(
ssid=wlan_config['ssid'],
auth=(WLAN.WPA2, wlan_config['wpa2_key']),
timeout=10000,
)
print('[wlan_manager] Connecting to WLAN "{}" '.format(wlan_config['ssid']), end='')
while not wlan.isconnected():
print('.', end='')
time.sleep(1)
print('\n[wlan_manager] Connected!')
wlan.ifconfig(config=(
wlan_config['ip_address'],
wlan_config['ip_subnet'],
wlan_config['ip_gateway'],
wlan_config['ip_nameserver'],
))
print('[wlan_manager] IP config: {}'.format(wlan.ifconfig()))
return wlan

26
src/main.py Normal file
View File

@ -0,0 +1,26 @@
# main.py -- run after boot.py
import _thread
import time
def count(name: str, stop: int = 10):
i = 0
while True:
print("[{}] {}".format(name, i))
i += 1
if i > stop:
break
time.sleep(1)
print("[{}] exit".format(name))
# Start some threads
_thread.start_new_thread(count, ('thread1', 5))
_thread.start_new_thread(count, ('thread2', 3))
# Main thread
count('main', 10)