As the title says... Chat on...

User avatar
By xykon
#3641 Hi all,

first I need to say that I am totally excited about Lua on the ESP8266. This is really great!

Like probably most of the people here, I hacked up a few small but useful scripts that I keep using quite often. So I thought maybe I can save someone a bit of time by sharing them here.
Comments and additions welcome.

Code: Select all-- file functions

-- list files
function ls()
  l=file.list()
  for k,v in pairs(l) do
    print("\t"..k.."\t"..v)
  end
end

-- show contents of file
function cat(fname)
  file.open(fname,"r")
  l=file.readline()
  while l~=nil do
    print(string.sub(l,1,-2))
    l=file.readline()
  end
end

-- delete all files
function wipe()
   l=file.list()
   for k in pairs(l) do
     file.remove(k)
   end
end


Code: Select all-- misc helper functions

-- list members of object
function dir(o)
  for k,v in pairs(o) do
    print(k)
  end
end

-- string to hex
function str2hex(s)
  r=""
  for i=1,string.len(s) do
    r = r .. string.format("%02x",string.byte(s,i))
  end
  return r
end


A ruby script to upload files to the ESP8266. Redirect to appropriate serial port.

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


I hope somebody finds this useful.
Keep up the good work!

Xykon
User avatar
By alonewolfx2
#3642 Look likes good. I think you can post on the example script section. One post for functions on scripts one post for your ruby scripts. Nice work. Thank you for sharing.
User avatar
By xykon
#3679
alonewolfx2 wrote:Look likes good. I think you can post on the example script section. One post for functions on scripts one post for your ruby scripts. Nice work. Thank you for sharing.


D'oh, it was late yesterday. I didn't realize there's an example script section. :?
I will repost it there. Thanks for the hint... :)