Try   HackMD

MicroPython 用於儲存小型資料的 KeyValue 模組

tags: ESP32 MicroPython

PaoyungMay 23, 2022

ℳ𝒾𝒸𝓇ℴ𝒫𝓎𝓉𝒽ℴ𝓃 隨手記

記不住的金魚腦

因為原本就喜歡用 Python 寫些小程式,所以 Arduino IDE 還沒有摸熟,就跑去玩 MicroPython了。之後接觸到 IoT 時,發現 MCU 除了要處理週邊訊號,還得搞定網路各種通訊。網路問題幸有諸多高手分享得以找到解答,但接著就遇到本機參數或資料儲存的問題了。ESP32有 4MB 的 Flash Memroy,而 MicroPython 也可以輕鬆的以檔案的方式讀取,但需要儲存的參數或資料一旦多了,一直 open 覺得實在很煩瑣,若能用 KeyValue 的方式存取豈不美哉!因此寫了個簡單的 KeyValue 模組,方便儲存小型的資料,只要 import 進來就可以簡單的操作了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
例如:

import KeyValue as kv #. 存入 wifi 設定值 kv.save('wifi.cfg', {"ssid":"ap_name", "pass":"pazzw0rd"}) #. 讀取 wifiCfg = kv.load('wifi.cfg') #. => wifiConnect(wifiCfg["ssid"], wifiCfg["pass"]) #. 也可以在沒有設定值的情況下,載入預設值並儲存 default_wifiCfg = {"ssid": "ap0", "pass": "pazzw0rd"} wifiCfg = kv.get('wifi.cfg', default_wifiCfg) #. 設定 debug mode debug = kv.get('debug', True) if debug: print('Debug mode enabled.') #. 記錄開機時間 import time kv.save('boot_time', time.time()) #. ... a few moments later time.sleep(5) #. ... a few moments later work = (time.time() - kv.load('boot_time')) print("running {} second.".format(work))

來碗益氣聰明湯

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
檔案下載:KeyValue.py

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
完整用法:

# 模組載入 import KeyValue as kv # 列出清單 kv.list() # 存入 # kv.save(key, value) # python 的資料型態都可存, key name 必需是字串 kv.save('wifi.cfg', {"ssid":"ap_name", "pass":"pazzw0rd"}) kv.save('debug.flg', True) kv.save('num_lst', [1, 2, 3, 4, 5]) # 需注意的是存入 tuple 在載入時會變成 list kv.save('num_lst', (1, 2, 3)) print(kv.load('num_lst')) # 結果為=> [1, 2, 3] # 資料型態可以混合 kv.save('mix', ['andy', 38, {"tags":["bike", "cook"]}]) # 1v18的 MicroPython 可以單獨存 None, 較早版本會出現錯誤訊息 #kv.save('ng', None) # 存為資料結構則沒問題 kv.save('none', {'value': None}) # flash memory 有壽命的考量,為節省擦寫次數, 存入相同資料時不會複寫檔案 # 讀取 # kv.load(key) wifiCfg = kv.load('wifi.cfg') # 在還沒有設定值的情況下,載入預設值並儲存, 若已有值則載入已儲存的值 # kv.get(key, default_value) default_wifiCfg = {"ssid": "ap0", "pass": "pazzw0rd"} wifiCfg = kv.get('wifi.cfg', default_wifiCfg) # 是否顯示訊息 # kv.enableMsg(true_or_false) # 未給值的預設是開啟的 kv.enableMsg() kv.enableMsg(True) # 關閉訊息 kv.enableMsg(False) # 重新命名 # kv.rename(old_key, new_key) kv.rename('old_name', 'new_name') # 刪除單一key的資料 # kv.rm(key) kv.rm('unused') # 清除所有資料 # kv.renew(True) # 必需參數給 True 才會執行 kv.renew(True) # 沒給參數不會進行清除動作 kv.renew()

資料方便存起來,許多事情做起來就順多了!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

關於作者  

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

我是 𝙋𝙖𝙤𝙮𝙪𝙣𝙜,是 MicroPythonEspruino 的愛好者,一直認為:

「Maker 應該把重心放在應用上,而不該被語言工具限制了創意」

所以想藉由較為簡捷且詳細的解說讓更多人可以學習進而自由發揮,而非淪為只能將程式碼複製貼上的複製人,如果你認同這樣的理念,請一起為此目標努力,如果我的文章內容對你有幫助,請轉發或協助需要的人。

若有專案需求,請利用 連絡我,謝謝!