diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85a5332 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +# IDE / editor files +.idea/ +.vscode/ +.*.swp + +# General +/_tmp + +# Python +__pycache__ +*.py[cod] +/venv diff --git a/src/boot.py b/src/boot.py new file mode 100644 index 0000000..17bcc18 --- /dev/null +++ b/src/boot.py @@ -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) diff --git a/src/lib/config.py b/src/lib/config.py new file mode 100644 index 0000000..7c85c3b --- /dev/null +++ b/src/lib/config.py @@ -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) diff --git a/src/lib/wlan_manager.py b/src/lib/wlan_manager.py new file mode 100644 index 0000000..befdb87 --- /dev/null +++ b/src/lib/wlan_manager.py @@ -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 diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..f09de43 --- /dev/null +++ b/src/main.py @@ -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)