Post your best Lua script examples here

User avatar
By F. Goncalves
#26101 I have the following code:
INIT.LUA
Code: Select allprint("Setting up WIFI...")
wifi.setmode(wifi.STATION)
wifi.sta.config("WIFI","PASS")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
print("START")
dofile("main.lua")
end
end)


MAIN.LUA
Code: Select all--PINOUTS
pinTemperatureSensor = 1
gpio.mode(pinTemperatureSensor, gpio.INPUT)

pinLightSensor = 0
gpio.mode(pinLightSensor, gpio.INPUT)

pinWaterSensor = 2
gpio.mode(pinWaterSensor,gpio.INT)

--Interrupts
gpio.trig(pinWaterSensor, 'both', sendData)


function sendData()
t=require("ds18b20")
aquatemperature=t.readNumber(pinTemperatureSensor)
print("the temperature is  "..aquatemperature)
t = nil
ds18b20 = nil
package.loaded["ds18b20"]=nil

WaterSensorValue= gpio.read(pinWaterSensor)

light=adc.read(0)
print("Luz: "..light)
-- conection to thingspeak.com
print("Sending data to Thingspeak ")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
---- api.thingspeak.com 184.106.153.149
conn:connect(80,'184.106.153.149')
conn:send("GET /update?key=**********&field1="..aquatemperature.."&field2="..light.."&field3="..WaterSensorValue.." HTTP/1.1\r\n")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")
conn:on("sent",function(conn)
                      print("Closing connection")
                      conn:close()
                  end)
conn:on("disconnection", function(conn)
                      print("Got disconnection...")
  end)
end

-- Timers
tmr.alarm(2, 60000, 1, sendData)



but when i just run MAIN.LUA everything works fine. When i restart and start from INIT.LUA it gives me an rendom error "ü! C§>ä)¤äΪ}†ÖHÌø" and restarts

any idea?
User avatar
By bram
#26296 Are you sure you created the main.lua file? I could only recreate the error by trying to open a file that doesn't exist.