├── ping.lua
├── README.md
├── intest.lua
├── mqping.lua
├── httpd.lua
├── mqtt.lua
├── temperature.lua
├── telnet.lua
├── mqtt2.lua
├── newmqtt.lua
└── init.lua
/ping.lua:
--------------------------------------------------------------------------------
1 | print("I've been launched!")
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # nodemcu-scripts
2 | Simple scripts for ESP8266 Lua, aka: NodeMcu
3 |
--------------------------------------------------------------------------------
/intest.lua:
--------------------------------------------------------------------------------
1 | gpio.mode(4,gpio.INT, gpio.PULLUP)
2 | function pubOnTrig(level)
3 | print("Bang")
4 | end
5 |
6 | gpio.trig(4, "both",pubOnTrig)
--------------------------------------------------------------------------------
/mqping.lua:
--------------------------------------------------------------------------------
1 | dofile('newmqtt.lua')
2 |
3 | count = 0
4 | m:on("command", function(conn, topic, data)
5 | print(topic .. ":" .. data)
6 | m:publish("response","temp",0,0, function(conn) print("sent") end)
7 | end)
--------------------------------------------------------------------------------
/httpd.lua:
--------------------------------------------------------------------------------
1 | -- A simple http server
2 | srv=net.createServer(net.TCP)
3 | srv:listen(2345,function(conn)
4 | conn:on("receive",function(conn,payload)
5 | print(payload)
6 | conn:send("
Hello, Mike's ESP.
")
7 | end)
8 | conn:on("sent",function(conn) conn:close() end)
9 | end)
--------------------------------------------------------------------------------
/mqtt.lua:
--------------------------------------------------------------------------------
1 | mqtt = net.createConnection(net.TCP, 0)
2 |
3 | mqtt:mqtt("myid", 60, "mike", "apasswd")
4 |
5 | mqtt:on("receive", function(conn, topic, data) print(topic .. ":" .. data) end)
6 |
7 | mqtt:on("connection", function(con) print("connected") end)
8 | mqtt:connect(1883,"192.168.10.195")
9 |
10 | mqtt:subscribe("hello/world",0, function(conn) print("subscribe success") end)
--------------------------------------------------------------------------------
/temperature.lua:
--------------------------------------------------------------------------------
1 | dofile('newmqtt.lua')
2 |
3 | tmr.alarm(1,10000,1, function()
4 | temp = (adc.read(0)-500) /( 195/10)
5 | print(temp)
6 | if pcall(readtemp) then
7 | print("Temp sent OK")
8 | else
9 | print("Temp err" )
10 | end
11 | end)
12 |
13 | function readtemp()
14 | m:publish("temperature",temp,0,0, function(conn) print("sent") end)
15 | end
16 |
--------------------------------------------------------------------------------
/telnet.lua:
--------------------------------------------------------------------------------
1 | -- a simple telnet server
2 | s=net.createServer(net.TCP,180)
3 | s:listen(2323,function(c)
4 | function s_output(str)
5 | if(c~=nil)
6 | then c:send(str)
7 | end
8 | end
9 | node.output(s_output, 0)
10 | -- re-direct output to function s_ouput.
11 | c:on("receive",function(c,l)
12 | node.input(l)
13 | --like pcall(loadstring(l)), support multiple separate lines
14 | end)
15 | c:on("disconnection",function(c)
16 | node.output(nil)
17 | --unregist redirect output function, output goes to serial
18 | end)
19 | print("Welcome to NodeMcu world.")
20 | end)
21 |
--------------------------------------------------------------------------------
/mqtt2.lua:
--------------------------------------------------------------------------------
1 | mqtt = net.createConnection(net.TCP, 0)
2 | -- init mqtt client with keepalive timer 30sec
3 | mqtt:mqtt("esp1", 30, "user", "password")
4 |
5 | -- on publish message receive event
6 | mqtt:on("receive", function(conn, topic, data) print(topic .. ":" .. data) end)
7 |
8 |
9 |
10 | -- on connection event (all event inherit from net module)
11 | mqtt:on("connection", function(con)
12 | print("MQTT connected, subscribing")
13 | mqtt:subscribe("hello/world",0, function(conn)
14 | print("subscribe success")
15 | mqtt:on("receive", function(conn, topic, data)
16 | print(topic .. ":" .. data)
17 | end)
18 | end)
19 | end)
20 |
21 |
22 |
23 | mqtt:connect(1883,"192.168.10.195")
24 | -- subscribe topic with qos = 0
25 |
26 | -- publish a message
27 | count = 0
28 | gpio.mode(4,gpio.INT, gpio.PULLUP)
29 | function pubOnTrig(level)
30 | print("Bang")
31 | count = count +1
32 | mqtt:send("hello/world","hello from esp" .. count,0,0, function(conn) print("sent") end)
33 | end
34 |
35 | gpio.trig(4, "down",pubOnTrig)
36 |
37 |
--------------------------------------------------------------------------------
/newmqtt.lua:
--------------------------------------------------------------------------------
1 |
2 | -- init mqtt client with keepalive timer 30sec
3 | m = mqtt.Client("esp2", 30, "user", "password")
4 |
5 | -- init mqtt client with keepalive timer 120sec
6 | --m = mqtt.Client()
7 |
8 | -- setup Last Will and Testament (optional)
9 | -- Broker will publish a message with qos = 0, retain = 0, data = "offline"
10 | -- to topic "/lwt" if client don't send keepalive packet
11 | m:lwt("/lwt", "offline", 0, 0)
12 |
13 | m:on("connect", function(con)
14 | print ("connected")
15 | -- subscribe topic with qos = 0
16 | m:subscribe("commands",0,
17 | function(conn)
18 | print("subscribe success")
19 | m:on("message", function(conn, topic, data)
20 | print(topic .. ":" .. data)
21 | pin = 5
22 | gpio.mode(pin, gpio.OUTPUT)
23 | if( gpio.read(pin) == gpio.HIGH) then
24 | output = gpio.LOW
25 | else
26 | output = gpio.HIGH
27 | end
28 | gpio.write(pin, output)
29 | end)
30 | end)
31 | end)
32 |
33 | m:on("offline", function(con)
34 | print ("offline")
35 | dofile('temperature.lua')
36 | end)
37 |
38 | -- on publish message receive event
39 | -- m:on("message", function(conn, topic, data)
40 | -- print(topic .. ":" )
41 | -- if data ~= nil then
42 | -- print(data)
43 | -- end
44 | -- end)
45 |
46 | -- for secure: m:connect("192.168.11.118", 1880, 1)
47 | m:connect("192.168.10.195", 1883, 0)
48 |
49 |
50 |
51 | -- publish a message with data = hello, QoS = 0, retain = 0
52 | --m:publish("/topic","hello",0,0, function(conn) print("sent") end)
53 |
54 | --m:close();
55 | -- you can call m:connect again
56 |
--------------------------------------------------------------------------------
/init.lua:
--------------------------------------------------------------------------------
1 | print("Starting ESP with MQTT")
2 |
3 | -- Constants
4 | SSID = "MeteorLondon"
5 | APPWD = "electr1c"
6 | CMDFILE = "ping.lua" -- File that is executed after connection
7 |
8 | -- Some control variables
9 | wifiTrys = 0 -- Counter of trys to connect to wifi
10 | NUMWIFITRYS = 200 -- Maximum number of WIFI Testings while waiting for connection
11 |
12 | -- Change the code of this function that it calls your code.
13 | function launch()
14 | print("Connected to WIFI!")
15 | print("IP Address: " .. wifi.sta.getip())
16 | -- Call our command file every minute.
17 | dofile('newmqtt.lua')
18 | --tmr.alarm(0, 60000, 1, function() dofile(CMDFILE) end )
19 | end
20 |
21 | function checkWIFI()
22 | if ( wifiTrys > NUMWIFITRYS ) then
23 | print("Sorry. Not able to connect")
24 | else
25 | ipAddr = wifi.sta.getip()
26 | if ( ( ipAddr ~= nil ) and ( ipAddr ~= "0.0.0.0" ) )then
27 | -- lauch() -- Cannot call directly the function from here the timer... NodeMcu crashes...
28 | tmr.alarm( 1 , 500 , 0 , launch )
29 | else
30 | -- Reset alarm again
31 | tmr.alarm( 0 , 2500 , 0 , checkWIFI)
32 | print("Checking WIFI..." .. wifiTrys)
33 | wifiTrys = wifiTrys + 1
34 | end
35 | end
36 | end
37 |
38 | print("-- Starting up! ")
39 | wifi.sta.disconnect()
40 | -- Lets see if we are already connected by getting the IP
41 | ipAddr = wifi.sta.getip()
42 | if ( ( ipAddr == nil ) or ( ipAddr == "0.0.0.0" ) ) then
43 | -- We aren't connected, so let's connect
44 | print("Configuring WIFI....")
45 | wifi.setmode( wifi.STATION )
46 | wifi.sta.config( SSID , APPWD)
47 | print("Waiting for connection")
48 | tmr.alarm( 0 , 2500 , 0 , checkWIFI ) -- Call checkWIFI 2.5S in the future.
49 | else
50 | -- We are connected, so just run the launch code.
51 | launch()
52 |
53 | end
54 | -- Drop through here to let NodeMcu run
55 |
56 |
57 |
--------------------------------------------------------------------------------