2026年9月14日月曜日

microlua and esplorer at ubuntu

lion d1 miniでビルトインLEDブリンク成功

 

if my_timer then my_timer:unregister() my_timer = nil endの意味

そのコードは、「もし過去に作成したタイマーが動いていたら、安全に停止してメモリから完全に消去する」という意味の、エラーを防ぐための安全対策(クリーンアップ)コードです。
1行ずつ分解すると、以下のような仕組みになっています。
  • if my_timer then
    my_timer という変数の中に、すでにタイマーオブジェクトが存在している(nil ではない)かどうかを確認しています。
  • my_timer:unregister()
    タイマーのカウントダウン(定期実行ループ)を停止し、登録を解除します。
  • my_timer = nil
    変数の中身を空っぽ(nil)にして、タイマーが使っていたメモリを完全に解放します。
なぜこのコードが必要なのか?
NodeMCU(Lua)で開発しているとき、プログラム(スクリプト)を何度も修正してはボードに再送信(実行)しますよね。
もしこのクリーンアップ処理を入れずに新しいタイマーを何度も作ってしまうと、古いタイマーがバックグラウンドで動き続けたまま、新しいタイマーも同時に動き出してしまいます。 その結果、LEDの点滅がめちゃくちゃに速くなったり、メモリ不足でシステムがクラッシュしたりします。
コードの一番最初(タイマーを新しく作る前)にこの処理を入れておくことで、常に古いタイマーをリセットし、1つのタイマーだけが正しく動く状態を保つことができます。

ーーーーbuiltin led blinkーーーーーーーーーーーーーーーーーーーーーーー 

led = nil

if my_timer then my_timer:unregister() my_timer = nil end

led = 4

gpio.mode(led, gpio.OUTPUT)

my_timer = tmr.create()

my_timer:register(1000, tmr.ALARM_AUTO, function()

    if gpio.read(led) == gpio.HIGH then

        gpio.write(led, gpio.LOW)

    else

        gpio.write(led, gpio.HIGH)

    end

end)

my_timer:start()

ーーーーーーーーーーーーーーーーーーーーーーーー

 https://nodemcu-build.com/ (オーダーメード) and use firware bin

 https://www.limpkin.fr/index.php?post/2016/04/17/A-Small-Collection-of-NodeMCU-Lua-Scripts-for-Data-Collection :: to study myself

1. APTを使用する方法(簡単)
Python環境を別途用意せず、システム管理として手軽にインストールしたい場合
bash
sudo apt update
sudo apt install esptool

esptool --port /dev/ie:ttyUSB0 --baud 460800 write_flash 0x00000 ie:firmware.bin

---------------------------------------------

https://tomosoft.jp/design/?p=6810 :: esplorer-for-lua


--------------------------------------------------

can download the tool from ESPlorer to program ESP8266 and ESP32 chips with Lua on Ubuntu.

Installation Steps
  1. Install Java: ESPlorer is a Java application. Open your terminal and install Java using sudo apt update and sudo apt install default-jre.
  2. Download ESPlorer: Get the latest zip file from the official site.
  3. Extract the file: Unzip the downloaded archive to a folder of your choice.
  4. Run the program: Open a terminal in that folder and run java -jar ESPlorer.jar.

0 件のコメント:

コメントを投稿