Post your best Lua script examples here

User avatar
By xykon
#3681 I posted this in the "Random LUA Chat" section yesterday, but this is obviously a better place.

A ruby script to upload files to the ESP8266. Redirect to appropriate serial port.
Example:
Code: Select all./pushfile.rb -r init_v1.8.lua init.lua


Code: Select all#!/usr/bin/env ruby

# usage:
# pushfile.rb [-r] filename [filename_on_esp8266]
# -r restarts the module after upload

if ARGV[0] == "-r" then
  restart=true
  ARGV.shift
end

infilename=ARGV[0]
outfilename = infilename
if ARGV.size > 1
  outfilename = ARGV[1]
end

file=File.open(infilename)
puts "file.open(\"#{outfilename}\",\"w\")"
sleep 0.2
file.readlines.each do |line|
  command="file.writeline(\"#{line.gsub("\\","\\\\\\").gsub("\"","\\\"").rstrip}\")"
  puts command
  sleep 0.005*command.size + 0.2    # wait for serial port and esp8266 processing the command
end
puts "file.close()"
sleep 0.1

puts "node.restart()" if restart


Push all files in a folder to the ESP8266. (Useful after a reflash.)
Code: Select all#!/bin/bash

PORT=/dev/ttyUSB0

# uncomment this if you want to delete all files on the esp8266 first
#echo "for k in pairs(file.list()) do file.remove(k) end" >$PORT
#sleep 1

# push all files
for f in files/*.lua ; do
  ./pushfile.rb "$f" "$(basename "$f")" >$PORT
  sleep 1
done

# restart node
echo "node.restart()" >$PORT