From b5f36e54456cf33af0929183f4df906739823079 Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 10 Feb 2021 23:05:54 +0100 Subject: [PATCH] Serial flashing while controller is running is possible --- mqtt.lua | 18 +++++++++--------- tools/initialDietFlash.sh | 19 +++++++++++++++++-- tools/luatool.py | 37 +++++++++++++++++++++++++------------ tools/stopController.lua | 2 ++ 4 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 tools/stopController.lua diff --git a/mqtt.lua b/mqtt.lua index 9b611dc..4137e33 100644 --- a/mqtt.lua +++ b/mqtt.lua @@ -6,10 +6,10 @@ local t=nil function handleSingleCommand(client, topic, data) if (data == "ON") then - briPer=100 + briPercent=100 m:publish(mqttPrefix .. "/clock", "ON", 0, 0) elseif (data == "OFF") then - briPer=0 + briPercent=0 m:publish(mqttPrefix .. "/clock", "OFF", 0, 0) elseif ((data:sub(1,1) == "#" and data:len() == 7) or (string.match(data, "%d+,%d+,%d+"))) then local red=0 @@ -22,7 +22,7 @@ function handleSingleCommand(client, topic, data) else red, green, blue = string.match(data, "(%d+),(%d+),(%d+)") end - colorBg=string.char(green * briPer / 100, red * briPer / 100, blue * briPer / 100) + colorBg=string.char(green * briPercent / 100, red * briPercent / 100, blue * briPercent / 100) print("Updated BG: " .. tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue) ) m:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) if (displayTime~= nil) then @@ -30,7 +30,7 @@ function handleSingleCommand(client, topic, data) end else if (tonumber(data) >= 0 and tonumber(data) <= 100) then - briPer=tonumber(data) + briPercent=tonumber(data) m:publish(mqttPrefix .. "/clock", tostring(data), 0, 0) else print "Unknown MQTT command" @@ -55,7 +55,7 @@ function parseBgColor(data, row) end if ((red ~= nil) and (green ~= nil) and (blue ~= nil) ) then m:publish(mqttPrefix .. "/"..row, tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) - return string.char(green * briPer / 100, red * briPer / 100, blue * briPer / 100) + return string.char(green * briPercent / 100, red * briPercent / 100, blue * briPercent / 100) else return nil end @@ -116,7 +116,7 @@ function registerMqtt() end end) - m:connect(mqttServer, 1883, 0, function(client) + m:connect(mqttServer, 1883, false, function(client) print("MQTT is connected") mqttConnected = true -- subscribe topic with qos = 0 @@ -158,15 +158,15 @@ function startMqttClient() if (t ~= nil) then temp=readTemp() end - if (oldBrightness ~= briPer) then - m:publish(mqttPrefix .. "/brightness", tostring(briPer), 0, 0) + if (oldBrightness ~= briPercent) then + m:publish(mqttPrefix .. "/brightness", tostring(briPercent), 0, 0) elseif (temp ~= nil and temp ~= oldTemp) then oldTemp = temp m:publish(mqttPrefix .. "/temp", tostring(temp/100).."."..tostring(temp%100), 0, 0) else m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0) end - oldBrightness = briPer + oldBrightness = briPercent end end) mqtttimer:start() diff --git a/tools/initialDietFlash.sh b/tools/initialDietFlash.sh index 56f8a57..f258f67 100755 --- a/tools/initialDietFlash.sh +++ b/tools/initialDietFlash.sh @@ -8,6 +8,8 @@ DIET=bin/luasrcdiet DEVICE=$1 BAUD=115200 +LUASCRIPT_STOP=${TOOLDIR}/stopController.lua + # check environment if [ ! -f $LUATOOL ]; then echo "$LUATOOL not found" @@ -59,6 +61,17 @@ if [ $# -eq 1 ]; then echo "STOOOOP" exit 1 fi +else + echo "Reboot ESP and stop init timer" + if [ ! -f $LUASCRIPT_STOP ]; then + echo "Cannot find $LUASCRIPT_STOP" + exit 1 + fi + python3 $LUATOOL -p $DEVICE -f $LUASCRIPT_STOP -b $BAUD --volatile --delay 2 + if [ $? -ne 0 ]; then + echo "Could not reboot" + exit 1 + fi fi echo "Start Flasing ..." @@ -78,7 +91,9 @@ for f in $FILES; do fi done -echo "Reboot the ESP" -echo "node.restart()" >> $DEVICE +if [ $# -eq 1 ]; then + echo "Reboot the ESP" + echo "node.restart()" >> $DEVICE +fi exit 0 diff --git a/tools/luatool.py b/tools/luatool.py index 5bfb296..4f87995 100755 --- a/tools/luatool.py +++ b/tools/luatool.py @@ -54,6 +54,9 @@ class AbstractTransport: def writer(self, data): self.writeln("file.writeline([==[" + data + "]==])\r") + def execute(self, data): + self.writeln(data + "\r") + def performcheck(self, expected): line = '' char = '' @@ -190,6 +193,7 @@ if __name__ == '__main__': parser.add_argument('-e', '--echo', action='store_true', help='Echo output of MCU until script is terminated.') parser.add_argument('--delay', default=0.3, help='Delay in seconds between each write.', type=float) parser.add_argument('--delete', default=None, help='Delete a lua/lc file from device.') + parser.add_argument('--volatile', action='store_true', help='Volatile (nothing stored on ESP)') parser.add_argument('--ip', default=None, help='Connect to a telnet server on the device (--ip IP[:port])') args = parser.parse_args() @@ -201,7 +205,7 @@ if __name__ == '__main__': char = transport.read(1) if char == '' or char == chr(62): break - sys.stdout.write(char) + sys.stdout.write(str.encode(char)) sys.exit(0) if args.id: @@ -212,7 +216,7 @@ if __name__ == '__main__': if char == '' or char == chr(62): break if char.isdigit(): - id += char + id += char.decode("utf-8") print("\n"+id) sys.exit(0) @@ -222,10 +226,10 @@ if __name__ == '__main__': fn = "" while True: char = transport.read(1) - if char == '' or ord(char) == 62: + if char == '' or len(char) == 0 or ord(char) == 62: break if char not in ['\r', '\n']: - fn += char.decode("utf-8") + fn += str(char) else: if fn: file_list.append(fn.strip()) @@ -270,7 +274,7 @@ if __name__ == '__main__': sys.stderr.write("Upload starting\r\n") # remove existing file on device - if args.append==False: + if args.append==False and not args.volatile: if args.verbose: sys.stderr.write("Stage 1. Deleting old file from flash memory") transport.writeln("file.open(\"" + args.dest + "\", \"w\")\r") @@ -284,23 +288,31 @@ if __name__ == '__main__': # read source file line by line and write to device if args.verbose: sys.stderr.write("\r\nStage 2. Creating file in flash memory and write first line") - if args.append: - transport.writeln("file.open(\"" + args.dest + "\", \"a+\")\r") + if not args.volatile: + if args.append: + transport.writeln("file.open(\"" + args.dest + "\", \"a+\")\r") + else: + transport.writeln("file.open(\"" + args.dest + "\", \"w+\")\r") else: - transport.writeln("file.open(\"" + args.dest + "\", \"w+\")\r") + if args.verbose: + sys.stderr.write("\r\nStage 2. Directly execute the script...") line = f.readline() if args.verbose: sys.stderr.write("\r\nStage 3. Start writing data to flash memory...") while line != '': - transport.writer(line.strip()) + if args.volatile: + transport.execute(line.strip()) + else: + transport.writer(line.strip()) line = f.readline() # close both files f.close() if args.verbose: sys.stderr.write("\r\nStage 4. Flush data and closing file") - transport.writeln("file.flush()\r") - transport.writeln("file.close()\r") + if not args.volatile: + transport.writeln("file.flush()\r") + transport.writeln("file.close()\r") # compile? if args.compile: @@ -319,7 +331,8 @@ if __name__ == '__main__': if args.verbose: sys.stderr.write("\r\nEchoing MCU output, press Ctrl-C to exit") while True: - sys.stdout.write(transport.read(1)) + data = transport.read(1) + sys.stdout.write( data.decode("ascii") ) # close serial port transport.close() diff --git a/tools/stopController.lua b/tools/stopController.lua new file mode 100644 index 0000000..be33f8e --- /dev/null +++ b/tools/stopController.lua @@ -0,0 +1,2 @@ +node.restart() +if (initTimer ~= nil) then initTimer:unregister() end