fragment.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. pwm.setup(0,500,50) pwm.setup(1,500,50) pwm.setup(2,500,50)
  2. pwm.start(0) pwm.start(1) pwm.start(2)
  3. function led(r,g,b) pwm.setduty(0,g) pwm.setduty(1,b) pwm.setduty(2,r) end
  4. wifi.station.autoconnect(1)
  5. a=0
  6. tmr.alarm( 1000,1,function() if a==0 then a=1 led(50,50,50) else a=0 led(0,0,0) end end)
  7. sv:on("receive", function(s,c) s:send("<h1> Hello, world.</h1>") print(c) end )
  8. sk=net.createConnection(net.TCP, 0)
  9. sk:on("receive", function(sck, c) print(c) end )
  10. sk:connect(80,"115.239.210.27")
  11. sk:send("GET / HTTP/1.1\r\nHost: 115.239.210.27\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
  12. sk:connect(80,"192.168.0.66")
  13. sk:send("GET / HTTP/1.1\r\nHost: 1192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
  14. i2c.setup(0,1,0,i2c.SLOW)
  15. function read_bmp(addr) i2c.start(0) i2c.address(0,119,i2c.RECEIVER) c=i2c.read(0,1) i2c.stop(0) print(string.byte(c)) end
  16. function read_bmp(addr) i2c.start(0) i2c.address(0,119,i2c.TRANSMITTER) i2c.write(0,addr) i2c.stop(0) i2c.start(0) i2c.address(0,119,i2c.RECEIVER) c=i2c.read(0,2) i2c.stop(0) return c end
  17. s=net.createServer(net.TCP) s:listen(80,function(c) end)
  18. ss=net.createServer(net.TCP) ss:listen(80,function(c) end)
  19. s=net.createServer(net.TCP) s:listen(80,function(c) c:on("receive",function(s,c) print(c) end) end)
  20. s=net.createServer(net.UDP) s:listen(5683,function(c) c:on("receive",function(s,c) print(c) end) end)
  21. mm=node.list()
  22. for k, v in pairs(mm) do print('file:'..k..' len:'..v) end
  23. for k,v in pairs(d) do print("n:"..k..", s:"..v) end
  24. su=net.createConnection(net.UDP)
  25. su:on("receive",function(su,c) print(c) end)
  26. su:connect(5683,"192.168.0.66")
  27. su:send("/v1/id")
  28. gpio.mode(0,gpio.INT) gpio.trig(0,"down",function(l) print("level="..l) end)
  29. t0 = 0;
  30. function tr0(l) print(tmr.now() - t0) t0 = tmr.now()
  31. if l==1 then gpio.trig(0,"down") else gpio.trig(0,"up") end end
  32. gpio.mode(0,gpio.INT)
  33. gpio.trig(0,"down",tr0)
  34. su=net.createConnection(net.UDP)
  35. su:on("receive",function(su,c) print(c) end)
  36. su:connect(5001,"114.215.154.114")
  37. su:send([[{"type":"signin","name":"nodemcu","password":"123456"}]])
  38. su:send([[{"type":"signout","name":"nodemcu","password":"123456"}]])
  39. su:send([[{"type":"connect","from":"nodemcu","to":"JYP","password":"123456"}]])
  40. su:send("hello world")
  41. s=net.createServer(net.TCP) s:listen(8008,function(c) c:on("receive",function(s,c) print(c) pcall(loadstring(c)) end) end)
  42. s=net.createServer(net.UDP) s:listen(8888,function(c) c:on("receive",function(s,c) print(c) pcall(loadstring(c)) end) end)
  43. s=net.createServer(net.TCP) s:listen(8008,function(c) con_std = c function s_output(str) if(con_std~=nil) then con_std:send(str) end end
  44. node.output(s_output, 0) c:on("receive",function(c,l) node.input(l) end) c:on("disconnection",function(c) con_std = nil node.output(nil) end) end)
  45. s=net.createServer(net.TCP)
  46. s:listen(23,function(c)
  47. con_std = c
  48. function s_output(str)
  49. if(con_std~=nil)
  50. then con_std:send(str)
  51. end
  52. end
  53. node.output(s_output, 0)
  54. c:on("receive",function(c,l) node.input(l) end)
  55. c:on("disconnection",function(c)
  56. con_std = nil
  57. node.output(nil)
  58. end)
  59. end)
  60. srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive",function(conn,payload)
  61. print(node.heap()) door="open" if gpio.read(8)==1 then door="open" else door="closed" end
  62. conn:send("<h1> Door Sensor. The door is " .. door ..".</h1>") conn:close() end) end)
  63. srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive",function(conn,payload)
  64. print(node.heap()) print(adc.read(0)) door="open" if gpio.read(0)==1 then door="open" else door="closed" end
  65. conn:send("<h1> Door Sensor. The door is " .. door ..".</h1>") end) conn:on("sent",function(conn) conn:close() end) end)
  66. srv=net.createServer(net.TCP) srv:listen(80,function(conn)
  67. conn:on("receive",function(conn,payload)
  68. print(node.heap())
  69. door="open"
  70. if gpio.read(0)==1 then door="open" else door="closed" end
  71. conn:send("<h1> Door Sensor. The door is " .. door ..".</h1>")
  72. end)
  73. conn:on("sent",function(conn) conn:close() end)
  74. end)
  75. port = 9999
  76. hostip = "192.168.1.99"
  77. sk=net.createConnection(net.TCP, false)
  78. sk:on("receive", function(conn, pl) print(pl) end )
  79. sk:connect(port, hostip)
  80. file.remove("init.lua")
  81. file.open("init.lua","w")
  82. file.writeline([[print("Petes Tester 4")]])
  83. file.writeline([[tmr.alarm(5000, 0, function() dofile("thelot.lua") end )]])
  84. file.close()
  85. file.remove("thelot.lua")
  86. file.open("thelot.lua","w")
  87. file.writeline([[tmr.stop()]])
  88. file.writeline([[connecttoap = function (ssid,pw)]])
  89. file.writeline([[print(wifi.sta.getip())]])
  90. file.writeline([[wifi.setmode(wifi.STATION)]])
  91. file.writeline([[tmr.delay(1000000)]])
  92. file.writeline([[wifi.sta.config(ssid,pw)]])
  93. file.writeline([[tmr.delay(5000000)]])
  94. file.writeline([[print("Connected to ",ssid," as ",wifi.sta.getip())]])
  95. file.writeline([[end]])
  96. file.writeline([[connecttoap("MyHub","0011223344")]])
  97. file.close()
  98. s=net.createServer(net.UDP) s:listen(5683) s:on("receive",function(s,c) print(c) s:send("echo:"..c) end)
  99. s:on("sent",function(s) print("echo donn") end)
  100. sk=net.createConnection(net.UDP, 0) sk:on("receive", function(sck, c) print(c) end ) sk:connect(8080,"192.168.0.88")
  101. sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.88\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
  102. srv=net.createServer(net.TCP, 5) srv:listen(80,function(conn) conn:on("receive",function(conn,payload)
  103. print(node.heap()) print(adc.read(0)) door="open" if gpio.read(0)==1 then door="open" else door="closed" end
  104. conn:send("<h1> Door Sensor. The door is " .. door ..".</h1>") end) end)
  105. srv=net.createServer(net.TCP)
  106. srv:listen(80,function(conn)
  107. conn:on("receive",function(conn,payload)
  108. print(payload) print(node.heap())
  109. conn:send("<h1> Hello, NodeMcu.</h1>")
  110. end)
  111. conn:on("sent",function(conn) conn:close() end)
  112. end)
  113. function startServer()
  114. print("WIFI AP connected. Wicon IP:")
  115. print(wifi.sta.getip())
  116. sv=net.createServer(net.TCP,180)
  117. sv:listen(8080,function(conn)
  118. print("Wifi console connected.")
  119. function s_output(str)
  120. if(conn~=nil) then
  121. conn:send(str)
  122. end
  123. end
  124. node.output(s_output,0)
  125. conn:on("receive",function(conn,pl)
  126. node.input(pl)
  127. if (conn==nil) then
  128. print("conn is nil")
  129. end
  130. print("hello")
  131. mycounter=0 srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive",function(conn,payload)
  132. if string.find(payload,"?myarg=") then mycounter=mycounter+1
  133. m="<br/>Value= " .. string.sub(payload,string.find(payload,"?myarg=")+7,string.find(payload,"HTTP")-2) else m="" end
  134. conn:send("<h1> Hello, this is Pete's web page.</h1>How are you today.<br/> Count=" .. mycounter .. m .. "Heap=".. node.heap())
  135. end) conn:on("sent",function(conn) conn:close() conn = nil end) end)
  136. srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive",function(conn,payload)
  137. conn:send("HTTP/1.1 200 OK\r\n") conn:send("Connection: close\r\n\r\n") conn:send("<h1> Hello, NodeMcu.</h1>")
  138. print(node.heap()) conn:close() end) end)
  139. conn=net.createConnection(net.TCP)
  140. conn:dns("www.nodemcu.com",function(conn,ip) print(ip) print("hell") end)
  141. function connected(conn) conn:on("receive",function(conn,payload)
  142. conn:send("HTTP/1.1 200 OK\r\n") conn:send("Connection: close\r\n\r\n") conn:send("<h1> Hello, NodeMcu.</h1>")
  143. print(node.heap()) conn:close() end) end
  144. srv=net.createServer(net.TCP)
  145. srv:on("connection",function(conn) conn:on("receive",function(conn,payload)
  146. conn:send("HTTP/1.1 200 OK\r\n") conn:send("Connection: close\r\n\r\n") conn:send("<h1> Hello, NodeMcu.</h1>")
  147. print(node.heap()) conn:close() end) end)
  148. srv:listen(80)
  149. -- sieve.lua
  150. -- the sieve of Eratosthenes programmed with coroutines
  151. -- typical usage: lua -e N=500 sieve.lua | column
  152. -- generate all the numbers from 2 to n
  153. function gen (n) return coroutine.wrap(function () for i=2,n do coroutine.yield(i) end end) end
  154. -- filter the numbers generated by `g', removing multiples of `p'
  155. function filter (p, g) return coroutine.wrap(function () for n in g do if n%p ~= 0 then coroutine.yield(n) end end end) end
  156. N=N or 500 -- from command line
  157. x = gen(N) -- generate primes up to N
  158. while 1 do
  159. local n = x() -- pick a number until done
  160. if n == nil then break end
  161. print(n) -- must be a prime number
  162. x = filter(n, x) -- now remove its multiples
  163. end
  164. file.remove("mylistener.lua")
  165. file.open("mylistener.lua","w")
  166. file.writeline([[gpio2 = 9]])
  167. file.writeline([[gpio0 = 8]])
  168. file.writeline([[gpio.mode(gpio2,gpio.OUTPUT)]])
  169. file.writeline([[gpio.write(gpio2,gpio.LOW)]])
  170. file.writeline([[gpio.mode(gpio0,gpio.OUTPUT)]])
  171. file.writeline([[gpio.write(gpio0,gpio.LOW)]])
  172. file.writeline([[l1="0\n"]])
  173. file.writeline([[l2="0\n"]])
  174. file.writeline([[l3="0\n"]])
  175. file.writeline([[l4="0\n"]])
  176. file.writeline([[sv=net.createServer(net.TCP, 5) ]])
  177. file.writeline([[sv:listen(4000,function(c)]])
  178. file.writeline([[c:on("disconnection", function(c) print("Bye") end )]])
  179. file.writeline([[c:on("receive", function(sck, pl) ]])
  180. -- file.writeline([[print(pl) ]])
  181. file.writeline([[if (pl=="GO1\n") then c:send(l1) ]])
  182. file.writeline([[elseif pl=="GO2\n" then c:send(l2) ]])
  183. file.writeline([[elseif pl=="GO3\n" then c:send(l3) ]])
  184. file.writeline([[elseif pl=="GO4\n" then c:send(l4) ]])
  185. file.writeline([[elseif pl=="YES1\n" then l1="1\n" c:send("OK\n") gpio.write(gpio2,gpio.HIGH) ]])
  186. file.writeline([[elseif pl=="NO1\n" then l1="0\n" c:send("OK\n") gpio.write(gpio2,gpio.LOW) ]])
  187. file.writeline([[elseif pl=="YES2\n" then l2="1\n" c:send("OK\n") gpio.write(gpio0,gpio.HIGH) ]])
  188. file.writeline([[elseif pl=="NO2\n" then l2="0\n" c:send("OK\n") gpio.write(gpio0,gpio.LOW) ]])
  189. file.writeline([[elseif pl=="YES3\n" then l3="1\n" c:send("OK\n") print(node.heap()) ]])
  190. file.writeline([[elseif pl=="NO3\n" then l3="0\n" c:send("OK\n") print(node.heap()) ]])
  191. file.writeline([[elseif pl=="YES4\n" then l4="1\n" c:send("OK\n") print(node.heap()) ]])
  192. file.writeline([[elseif pl=="NO4\n" then l4="0\n" c:send("OK\n") print(node.heap()) ]])
  193. file.writeline([[else c:send("0\n") print(node.heap()) ]])
  194. file.writeline([[end]])
  195. file.writeline([[end)]])
  196. file.writeline([[end)]])
  197. file.close()
  198. file.remove("myli.lua") file.open("myli.lua","w")
  199. file.writeline([[sv=net.createServer(net.TCP, 5) ]])
  200. file.writeline([[sv:listen(4000,function(c)]])
  201. file.writeline([[c:on("disconnection", function(c) print("Bye") end )]])
  202. --file.writeline([[c:on("sent", function(c) c:close() end )]])
  203. file.writeline([[c:on("receive", function(sck, pl) ]])
  204. file.writeline([[sck:send("0\n") print(node.heap()) ]])
  205. file.writeline([[end)]]) file.writeline([[end)]]) file.close()
  206. sv=net.createServer(net.TCP, 50) sv:listen(4000,function(c) c:on("disconnection",function(c) print("Bye") end)
  207. c:on("receive", function(sck, pl) sck:send("0\n") print(node.heap()) end) end)
  208. sv=net.createServer(net.TCP, 5) sv:listen(4000,function(c) c:on("disconnection",function(c) print("Bye") end)
  209. c:on("receive", function(sck, pl) sck:send("0\n") print(node.heap()) end) c:on("sent", function(sck) sck:close() end) end)
  210. s=net.createServer(net.UDP)
  211. s:on("receive",function(s,c) print(c) end)
  212. s:listen(8888)
  213. print("This is a long long long line to test the memory limit of nodemcu firmware\n")
  214. collectgarbage("setmemlimit",8)
  215. print(collectgarbage("getmemlimit"))
  216. tmr.alarm(1,5000,1,function() print("alarm 1") end)
  217. tmr.stop(1)
  218. tmr.alarm(0,1000,1,function() print("alarm 0") end)
  219. tmr.stop(0)
  220. tmr.alarm(2,2000,1,function() print("alarm 2") end)
  221. tmr.stop(2)
  222. tmr.alarm(6,2000,1,function() print("alarm 6") end)
  223. tmr.stop(6)
  224. for k,v in pairs(_G.package.loaded) do print(k) end
  225. for k,v in pairs(_G) do print(k) end
  226. for k,v in pairs(d) do print("n:"..k..", s:"..v) end
  227. a="pin=9"
  228. t={}
  229. for k, v in string.gmatch(a, "(%w+)=(%w+)") do t[k]=v end
  230. print(t["pin"])
  231. function switch() gpio.mode(4,gpio.OUTPUT) gpio.mode(5,gpio.OUTPUT) tmr.delay(1000000) print("hello world") end
  232. tmr.alarm(0,10000,0,function () uart.setup(0,9600,8,0,1) end) switch()
  233. sk=net.createConnection(net.TCP, 0) sk:on("receive", function(sck, c) print(c) end ) sk:connect(80,"www.nodemcu.com") sk:send("GET / HTTP/1.1\r\nHost: www.nodemcu.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
  234. sk=net.createConnection(net.TCP, 0) sk:on("receive", function(sck, c) print(c) end )
  235. sk:on("connection", function(sck) sck:send("GET / HTTP/1.1\r\nHost: www.nodemcu.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end ) sk:connect(80,"www.nodemcu.com")
  236. sk=net.createConnection(net.TCP, 0) sk:on("receive", function(sck, c) print(c) end ) sk:connect(80,"115.239.210.27")
  237. sk:send("GET / HTTP/1.1\r\nHost: 115.239.210.27\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
  238. sk=net.createConnection(net.TCP, 1) sk:on("receive", function(sck, c) print(c) end )
  239. sk:on("connection", function(sck) sck:send("GET / HTTPS/1.1\r\nHost: www.google.com.hk\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end ) sk:connect(443,"173.194.72.199")
  240. wifi.sta.setip({ip="192.168.18.119",netmask="255.255.255.0",gateway="192.168.18.1"})
  241. uart.on("data","\r",function(input) if input=="quit\r" then uart.on("data") else print(input) end end, 0)
  242. uart.on("data","\n",function(input) if input=="quit\n" then uart.on("data") else print(input) end end, 0)
  243. uart.on("data", 5 ,function(input) if input=="quit\r" then uart.on("data") else print(input) end end, 0)
  244. uart.on("data", 0 ,function(input) if input=="q" then uart.on("data") else print(input) end end, 0)
  245. uart.on("data","\r",function(input) if input=="quit" then uart.on("data") else print(input) end end, 1)
  246. for k, v in pairs(file.list()) do print('file:'..k..' len:'..v) end
  247. m=mqtt.Client()
  248. m:connect("192.168.18.101",1883)
  249. m:subscribe("/topic",0,function(m) print("sub done") end)
  250. m:on("message",function(m,t,pl) print(t..":") if pl~=nil then print(pl) end end )
  251. m:publish("/topic","hello",0,0)
  252. uart.setup(0,9600,8,0,1,0)
  253. sv=net.createServer(net.TCP, 60)
  254. global_c = nil
  255. sv:listen(9999, function(c)
  256. if global_c~=nil then
  257. global_c:close()
  258. end
  259. global_c=c
  260. c:on("receive",function(sck,pl) uart.write(0,pl) end)
  261. end)
  262. uart.on("data",4, function(data)
  263. if global_c~=nil then
  264. global_c:send(data)
  265. end
  266. end, 0)