Current Lua downloadable firmware will be posted here

User avatar
By gerardwr
#2652
cendev wrote:posted a small python script that can upload code via command line for linux like
<snip>
but no experiance on mac


Hi,
I saw your Python script to upload, interesting. I could run this python script on my Mac, probably without major changes.

Your solution is based on a TCP connection between PC and ESP.

Personally I prefer using the serial port for now.

I intend to write a simple BASH script that sends lua commands that you would normally type in the console. Intended steps:
- send "file.open(filename)"
- read textfile with lua script on PC line-by-line, and embed it in a "file.write" statement, and send the line to the ESP
- send "file.close"
- send "dofile filename"

This would have, for me , the advantage that I do not have to type in the lua upload program in the terminal, before I can start uploading. I hate typing …. :-(

Thanks for sharing your solution, will keep it in mind. Will share my BASH solution when it works.
User avatar
By gerardwr
#2689
cendev wrote:First you must create a lua script on esp :D You will call it whenever you want to upload a file :)

Code: Select all    file.open("server.lua","w+")
    s=net.createServer(net.TCP)
    s:listen(8888,function(c) c:on("receive",function(c,pl) print(pl) pcall(loadstring(pl)) end) end)
    file.close()


After you dofile("server.lua") and start server on esp you can call this python file :)


Hi cendev,

After some thought, I now appreciate the charm of your solution using TCP instead of serial.

I have uploaded your lua script to my ESP, and it works fine. To facilitate the upload I have squeezed the script into 1 line that can be easily transmitted to the ESP using Coolterm "line mode".
Code: Select allfile.open("file.lua","w") file.write([[s=net.createServer(net.TCP) ]]) file.write([[s:listen(8888,function(c) c:on("receive",function(c,pl) print(pl) pcall(loadstring(pl)) end) end)]]) file.close()


After starting the lua server with dofile("file.lua") I can type lua commands on Mac using telnet.
Code: Select allMac-mini-van-gerard:Documents gerard$ telnet 192.168.0.27 8888
Trying 192.168.0.27...
Connected to 192.168.0.27.
Escape character is '^]'.
print("hallo gerard")


Result in the terminal emulator is:
Code: Select all> dofile("file.lua")
print("hallo gerard")
hallo gerard


Next step will be testing your python script on my mac.

Thanks for the tip.
User avatar
By gerardwr
#2698
cendev wrote:you are welcome :) glad to hear that :) if only you were using windows or linux i would send the gui supported tcp project =) i'll upload them tomorrow, hope they'll help =)


Hi,

I have your python script to upload a lua file to the ESP running now on my Mac. I had to make a small change to remove the \n formatting character from the line variable (by using line.strip) . The original version resulted in an unwanted line break BEFORE to ]]) ending of the line This is probably due to a different line-ending character in Mac OS X. This is the changed version that runs OK on my Mac.
Code: Select all#!/usr/bin/env python

import socket
import time
import sys

toCreate = str(sys.argv[1])
toUpload = str(sys.argv[2])
ip = str(sys.argv[3])
port = str(sys.argv[4])

TCP_IP = ip
TCP_PORT = int(port)
BUFFER_SIZE = 1024

time.sleep(1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send("file.open(\"%s\",\"w+\")" %(toCreate,))
time.sleep(0.1)
with open(toUpload) as f:
    for line in f:
#        print(line)
        print line.strip('\n')
        s.send("file.writeline([[%s]])" % line.strip('\n'))
        time.sleep(0.1)
s.send("file.close()")
s.close()


Uploading a stupid textile with 4 "hello world" lines in it looks like this on the ESP:
Code: Select all> dofile("file.lua")
> file.open("test1.lua","w+")
file.writeline([[print("hello world")]])
file.writeline([[print("hello world 2")]])
file.writeline([[print("hello world 3")]])
file.writeline([[print("hello world 4")]])
file.close()
c_G.RS.fJ[.fJS.f.
lua: cannot open init.lua
NodeMcu 0.9.2  powered by Lua 5.1.4
> file.list()
  file.lua size:125
  test1.lua size:90
> dofile("test1.lua")
hello world
hello world 2
hello world 3
hello world 4
>

When the upload is finished the ESP reboots, probably due to the Python script closing the connection.

Thanks to your script I have an operational upload mechanism now running on my Mac. Time for some Lua coding now ;)