OpenWrt Forum Archive

Topic: Bad Gateway when starting lua script

The content of this topic has been archived on 14 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I am novice in openwrt and lua but I have strange problem.
Have this file located in w ww/cgi-bin/signal.lua
-- it sends sends signal to serial port with arduino attached

#!/usr/bin/env lua

port= "/dev/ttyUSB0"
serialout= io.open(port,"w")  --open serial port and prepare to write data
str= "1"
serialout:write(str)

when i run it from console i have no problem, but when I try to reach it with browser I got:

Bad Gateway
The process did not produce any response

anyway, it still sends the signal to my arduino

What could be the problem

Well, your script does not seem to be producing any output for the browser, does it?

Now this is the original code..

#!/usr/bin/lua

-- LuaSerial interface
-- for Wireless Router Home Automation
-- by SM.Ching ht tp: //ediy.com.my
--
-- Put this file to /w ww /cgi-bin directory of router
-- use code below to allow 0755 permission for luaSerial file
-- chmod 0755 /w ww /cgi-bin/luaSerial
--
-- protocol (Receiving from serial port):
-- @aaccpp 
-- where aa is the Arduino address ranged from 0 to 255 (0 means all Arduino)
-- where cc in the command (TG, ON, OF, RS)
-- where pp is the parameters or channel ranged from 0 to 8 (0 means all channes)
-- to toggle output for channel 1: @00TG1
-- to get status from all output: @00RS0
--
-- protocal (Sending to serial port):
-- #aapp
-- where aa is the Arduino address ranged from 0 to 255 (0 means all Arduino)
-- where pp is either 1 or 0 (ON or OFF for a channel), or ranged from 0(00000000) to 255(11111111) for all channels

port= "/dev/ttyUSB0"
serialout= io.open(port,"w")  --open serial port and prepare to write data
serialin= io.open(port,"r")   --open serial port and prepare to read data

function readSerial()
    while true do
        --serialData= nil
        serialData= serialin:read();serialin:flush() -- read data from serial port

        if string.len(serialData)>0 then
            serData = serialData
        end

        if  string.len(serialData) == 0 then
            return serData
        end

    end
end

function toggleOutput()
    serialout:write(queryStr)
    queryResult= readSerial()                        -- read data from serial port
    queryResult= string.sub(queryResult,5)    -- from character 5 until the end, eg. 1 (is on)
       if queryResult== "1" then
          cmd="of"
    else
        cmd="on"
    end
    str= address..cmd..parameter                    -- eg. @00of2 (including \r)
end

----------------------------------------------------------------

str= os.getenv("QUERY_STRING").."\r"      -- get message from URL and terminate with carriage return(\r), eg. @00on2
str= string.upper(str)
address= string.sub(str,1,3)                -- get first 3 characters, eg. @00
cmd= string.sub(str,4,5)                      -- from character 4 until character 5, eg. on
parameter= string.sub(str,6)           -- from character 6 until the end, eg. 2 (including \r)
queryStr= address.."rs"..parameter        -- eg. @00rs2

if cmd=="TG" then
  toggleOutput()
end

serialout:write(str)                 -- write string(str) to serial port
readSerial()                            -- read data from serial port, use to clear serial buffer
serialout:write(queryStr)            -- write string(queryStr) to serial port
print(readSerial())                    -- read data from serial port and display it

The discussion might have continued from here.