From 32dc2934158824d206612a698272de1b5eaa346f Mon Sep 17 00:00:00 2001 From: Christian Zufferey Date: Sun, 20 Oct 2019 12:32:30 +0200 Subject: [PATCH] =?UTF-8?q?Commenc=C3=A9=20=C3=A0=20tester=20le=20petit=20?= =?UTF-8?q?WEB=20IDE=20de=20Matthieu=20Borgognon=20;-)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WebIDE/Matthieu Borgognon/a_script1.lua | 17 +++++ IDE_remote/WebIDE/Matthieu Borgognon/boot.lua | 15 +++++ .../WebIDE/Matthieu Borgognon/boot2.lua | 22 +++++++ .../Matthieu Borgognon/flash_led_xfois.lua | 39 ++++++++++++ .../WebIDE/Matthieu Borgognon/initz.lua | 63 +++++++++++++++++++ .../WebIDE/Matthieu Borgognon/web_ide.lua | 34 +++++----- .../Matthieu Borgognon/wifi_ap_stop.lua | 10 +++ .../Matthieu Borgognon/wifi_cli_conf.lua | 11 ++++ .../Matthieu Borgognon/wifi_cli_start.lua | 20 ++++++ .../WebIDE/Matthieu Borgognon/wifi_info.lua | 33 ++++++++++ 10 files changed, 250 insertions(+), 14 deletions(-) create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/a_script1.lua create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/boot.lua create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/boot2.lua create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/flash_led_xfois.lua create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/initz.lua create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/wifi_ap_stop.lua create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/wifi_cli_conf.lua create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/wifi_cli_start.lua create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/wifi_info.lua diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/a_script1.lua b/IDE_remote/WebIDE/Matthieu Borgognon/a_script1.lua new file mode 100644 index 0000000..5b61bd6 --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/a_script1.lua @@ -0,0 +1,17 @@ + + +f= "web_ide.lc" if file.exists(f)==false then + print("no exist") +end + + + +--[[ +f= "web_ide.lua" if file.exists(f) then dofile(f) end + + +node.compile("hello.lua") + + +f= "web_ide.lc" if file.exists(f) then dofile(f) end +]] diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/boot.lua b/IDE_remote/WebIDE/Matthieu Borgognon/boot.lua new file mode 100644 index 0000000..cfabff5 --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/boot.lua @@ -0,0 +1,15 @@ +-- Scripts à charger après le boot pour démarrer le core system + +print("\n boot.lua zf191020.1150 \n") + +-- charge ses propres secrets +f= "secrets_energy.lua" if file.exists(f) then dofile(f) end + +--f= "led_rgb.lua" if file.exists(f) then dofile(f) end +--f= "wifi_ap_start.lua" if file.exists(f) then dofile(f) end +f= "wifi_ap_stop.lua" if file.exists(f) then dofile(f) end +f= "wifi_cli_conf.lua" if file.exists(f) then dofile(f) end +f= "wifi_cli_start.lua" if file.exists(f) then dofile(f) end +--f= "telnet_srv2.lua" if file.exists(f) then dofile(f) end +--f= "web_ide2.lua" if file.exists(f) then dofile(f) end +--f= "web_srv2.lua" if file.exists(f) then dofile(f) end diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/boot2.lua b/IDE_remote/WebIDE/Matthieu Borgognon/boot2.lua new file mode 100644 index 0000000..e26895a --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/boot2.lua @@ -0,0 +1,22 @@ +-- Scripts à charger après le boot pour démarrer son appli + +print("\n boot2.lua zf191020.1151 \n") + +second_chance=nil +function heartbeat() + f= "flash_led_xfois.lua" if file.exists(f) then dofile(f) end + flash_led_xfois() + boottimer1=tmr.create() + boottimer1:alarm(1*1000, tmr.ALARM_AUTO, function() + xfois =2 + blink_LED () + end) +end + +--f= "0_get_data.lua" if file.exists(f) then dofile(f) end +--f= "0_send_data.lua" if file.exists(f) then dofile(f) end +--f= "0_cron.lua" if file.exists(f) then dofile(f) end + +f=nil +--heartbeat=nil +heartbeat() diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/flash_led_xfois.lua b/IDE_remote/WebIDE/Matthieu Borgognon/flash_led_xfois.lua new file mode 100644 index 0000000..090ec4c --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/flash_led_xfois.lua @@ -0,0 +1,39 @@ +-- programme pour faire clignoter x fois une LED avec un rapport on/off + +function flash_led_xfois() + print("\n flash_led_xfois.lua zf190601.1618 \n") + + --zLED=0 --NodeMCU + zLED=4 --EPS-M3 + zTm_On_LED = 50 --> en ms + zTm_Off_LED = 100 --> en ms + nbfois = 0 + gpio.write(zLED, gpio.HIGH) + gpio.mode(zLED, gpio.OUTPUT) + ztmr_Flash_LED = tmr.create() + + function blink_LED () + if nbfois >= xfois then + -- print(nbfois) + nbfois = 0 + else + if gpio.read(zLED)==gpio.HIGH then + gpio.write(zLED, gpio.LOW) +-- tmr.alarm(ztmr_Flash_LED, zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED) + ztmr_Flash_LED:alarm(zTm_Off_LED, tmr.ALARM_SINGLE, blink_LED) + else + gpio.write(zLED, gpio.HIGH) + nbfois = nbfois+1 +-- tmr.alarm(ztmr_Flash_LED, zTm_On_LED, tmr.ALARM_SINGLE, blink_LED) + ztmr_Flash_LED:alarm(zTm_On_LED, tmr.ALARM_SINGLE, blink_LED) + end + end + end + +-- xfois =2 +-- blink_LED () +end + + + + diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/initz.lua b/IDE_remote/WebIDE/Matthieu Borgognon/initz.lua new file mode 100644 index 0000000..c5aa649 --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/initz.lua @@ -0,0 +1,63 @@ +--Script de bootstrap, test au moment du boot qui a été la cause de boot. +-- Si la cause est un power on ou une connexion depuis l'IDE, alors +-- le script repair.lua pendant xx secondes avant de continuer +--Source: https://nodemcu.readthedocs.io/en/master/en/modules/node/#nodebootreason + +print("\n init.lua zf190917.0001 \n") + +zswitch=3 --switch flash +gpio.mode(zswitch, gpio.INT, gpio.PULLUP) + +function hvbouton() + gpio.trig(zswitch, "none") + initalarme:unregister() initalarme2:unregister() + f= "boot.lua" if file.exists(f) then dofile(f) end + f= "boot2.lua" if file.exists(f) then dofile(f) end +end + +gpio.trig(zswitch, "both", hvbouton) + +function second_chance() + print("seconde chance...") + f= "repair.lua" if file.exists(f) then dofile(f) end + initalarme=tmr.create() + initalarme:alarm(4*1000, tmr.ALARM_SINGLE, function() + f= "boot.lua" if file.exists(f) then dofile(f) end + end) + initalarme2=tmr.create() + initalarme2:alarm(30*1000, tmr.ALARM_SINGLE, function() + gpio.trig(zswitch) + hvbouton=nil + zswitch=nil + reset_reason=nil + f= "boot2.lua" if file.exists(f) then dofile(f) end + end) +end + +_, reset_reason = node.bootreason() +print("reset_reason:",reset_reason) +if reset_reason == 0 then + print("power on") + second_chance() +elseif reset_reason == 4 then + print("node.restart") + gpio.trig(zswitch) + hvbouton=nil + second_chance=nil + zswitch=nil + reset_reason=nil + f= "boot.lua" if file.exists(f) then dofile(f) end + f= "boot2.lua" if file.exists(f) then dofile(f) end +elseif reset_reason == 5 then + print("dsleep wake up") + f= "boot.lua" if file.exists(f) then dofile(f) end + f= "boot2.lua" if file.exists(f) then dofile(f) end +elseif reset_reason == 6 then + print("external reset") + second_chance() +else + print("autre raison") + second_chance() +end + + diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/web_ide.lua b/IDE_remote/WebIDE/Matthieu Borgognon/web_ide.lua index 164e3b1..f1c5922 100644 --- a/IDE_remote/WebIDE/Matthieu Borgognon/web_ide.lua +++ b/IDE_remote/WebIDE/Matthieu Borgognon/web_ide.lua @@ -1,3 +1,9 @@ +-- Petit WEB_IDE assez génial +-- Source: https://github.com/matbgn/NodeMCU/tree/master/lib/web-ide + +print("\n web_ide.lua zf191020.1211 \n") + + local mPort = 88 local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Cloud Editor @@ -121,10 +127,12 @@ local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Clo node.output(s_output, 0) -- re-direct output to function s_output. local st, result = pcall(dofile, url) - + -- delay the output capture by 1000 milliseconds to give some time to the user routine in pcall() - tmr.alarm(0, 1000, tmr.ALARM_SINGLE, function() - node.output(nil) +-- tmr.alarm(0, 1000, tmr.ALARM_SINGLE, function() + local ztmr_web_ide1 = tmr.create() + ztmr_web_ide1:alarm(1000, tmr.ALARM_SINGLE, function() + ztmr_web_ide1=nil node.output(nil) if result then local outp = tostring(result):sub(1,1300) -- to fit in one send() packet result = nil @@ -217,14 +225,12 @@ local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Clo end) end -local t = tmr.create() -t:alarm(500, tmr.ALARM_AUTO, function() - if (wifi.sta.status() == wifi.STA_GOTIP) then - t:unregister() - t=nil - print("\n--- Web server started ---") - print("NodeMCU Web IDE running at http://"..wifi.sta.getip()..":"..mPort.."/") - editor() - - end -end) \ No newline at end of file +local ztmr_web_ide2 = tmr.create() +ztmr_web_ide2:alarm(500, tmr.ALARM_AUTO, function() +if (wifi.sta.status() == wifi.STA_GOTIP) then + ztmr_web_ide2:unregister() ztmr_web_ide2=nil + print("\n--- Web server started ---") + print("NodeMCU Web IDE running at http://"..wifi.sta.getip()..":"..mPort.."/") + editor() +end +end) diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/wifi_ap_stop.lua b/IDE_remote/WebIDE/Matthieu Borgognon/wifi_ap_stop.lua new file mode 100644 index 0000000..2738627 --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/wifi_ap_stop.lua @@ -0,0 +1,10 @@ +-- Démarre le WIFI en mode AP +print("\n wifi_ap_stop.lua zf180824.2000 \n") + +local zmodewifi=wifi.getmode() +if zmodewifi == wifi.SOFTAP then + wifi.setmode(wifi.NULLMODE) +elseif zmodewifi == wifi.STATIONAP then + wifi.setmode(wifi.STATION) +end +print("WIFI AP arrêté") diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/wifi_cli_conf.lua b/IDE_remote/WebIDE/Matthieu Borgognon/wifi_cli_conf.lua new file mode 100644 index 0000000..559916b --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/wifi_cli_conf.lua @@ -0,0 +1,11 @@ +-- Petit script pour configurer le client WIFI du NodeMCU + +function wifi_cli_conf() + print("\n wifi_cli_conf.lua zf190726.1912 \n") + + -- les secrets sont maintenant initialisés par boot.lua ! + wifi.sta.config{ssid=cli_ssid, pwd=cli_pwd, save=true} +end + +wifi_cli_conf() +wifi_cli_conf=nil diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/wifi_cli_start.lua b/IDE_remote/WebIDE/Matthieu Borgognon/wifi_cli_start.lua new file mode 100644 index 0000000..045957e --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/wifi_cli_start.lua @@ -0,0 +1,20 @@ +-- Petit script pour connecter le NodeMCU sur un AP Wifi avec l'accompte sauvé en EEPROM + +function wifi_cli_start() + print("\n wifi_cli_start.lua zf190310.1519 \n") + + local zmodewifi=wifi.getmode() + if zmodewifi == wifi.NULLMODE then + print("WIFI mode CLI only") + wifi.setmode(wifi.STATION) + elseif zmodewifi == wifi.SOFTAP then + print("WIFI mode AP+CLI") + wifi.setmode(wifi.STATIONAP) + end + wifi.sta.autoconnect(1) + wifi.sta.connect() + --f= "wifi_get_ip.lua" if file.exists(f) then dofile(f) end +end + +wifi_cli_start() +wifi_cli_start=nil diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/wifi_info.lua b/IDE_remote/WebIDE/Matthieu Borgognon/wifi_info.lua new file mode 100644 index 0000000..1bc5c41 --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/wifi_info.lua @@ -0,0 +1,33 @@ +-- Petit script pour afficher les infos actuel du WIFI +print("\n wifi_info.lua zf190727.1220 \n") + +local zmodewifi=wifi.getmode() + +--wifi.NULLMODE, wifi.STATION, wifi.SOFTAP, wifi.STATIONAP + +if zmodewifi == wifi.NULLMODE then + print("WIFI OFF") +elseif zmodewifi == wifi.STATION then + print("WIFI mode CLI") + print("Connected IP:\n",wifi.sta.getip()) + local sta_config=wifi.sta.getconfig(true) + print("Current client config:") + print("\tssid:", sta_config.ssid) + print("\tpassword:", sta_config.pwd) + print("\tbssid:", sta_config.bssid) +elseif zmodewifi == wifi.SOFTAP then + print("WIFI mode AP") + print("AP MAC:\n",wifi.ap.getmac()) + print("AP IP:\n",wifi.ap.getip()) + print("AP Connect:\n",wifi.ap.getconfig()) +elseif zmodewifi == wifi.STATIONAP then + print("WIFI mode CLI+AP") + print("Connected IP:\n",wifi.sta.getip()) + local sta_config=wifi.sta.getconfig(true) + print("Current client config:") + print("\tssid:", sta_config.ssid) + print("\tpassword:", sta_config.pwd) + print("\tbssid:", sta_config.bssid) + print("AP MAC: ", wifi.ap.getmac()) + print("AP IP: ", wifi.ap.getip()) +end