fragment.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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.sta.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: 192.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)
  21. s:on("receive",function(s,c) print(c) end)
  22. s:listen(5683)
  23. su=net.createConnection(net.UDP)
  24. su:on("receive",function(su,c) print(c) end)
  25. su:connect(5683,"192.168.18.101")
  26. su:send("hello")
  27. mm=node.list()
  28. for k, v in pairs(mm) do print('file:'..k..' len:'..v) end
  29. for k,v in pairs(d) do print("n:"..k..", s:"..v) end
  30. gpio.mode(0,gpio.INT) gpio.trig(0,"down",function(l) print("level="..l) end)
  31. t0 = 0;
  32. function tr0(l) print(tmr.now() - t0) t0 = tmr.now()
  33. if l==1 then gpio.trig(0,"down") else gpio.trig(0,"up") end end
  34. gpio.mode(0,gpio.INT)
  35. gpio.trig(0,"down",tr0)
  36. su=net.createConnection(net.UDP)
  37. su:on("receive",function(su,c) print(c) end)
  38. su:connect(5001,"114.215.154.114")
  39. su:send([[{"type":"signin","name":"nodemcu","password":"123456"}]])
  40. su:send([[{"type":"signout","name":"nodemcu","password":"123456"}]])
  41. su:send([[{"type":"connect","from":"nodemcu","to":"JYP","password":"123456"}]])
  42. su:send("hello world")
  43. s=net.createServer(net.TCP) s:listen(8008,function(c) c:on("receive",function(s,c) print(c) pcall(loadstring(c)) end) end)
  44. 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
  45. 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)
  46. s=net.createServer(net.TCP)
  47. s:listen(23,function(c)
  48. con_std = c
  49. function s_output(str)
  50. if(con_std~=nil)
  51. then con_std:send(str)
  52. end
  53. end
  54. node.output(s_output, 0)
  55. c:on("receive",function(c,l) node.input(l) end)
  56. c:on("disconnection",function(c)
  57. con_std = nil
  58. node.output(nil)
  59. end)
  60. end)
  61. srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive",function(conn,payload)
  62. print(node.heap()) door="open" if gpio.read(8)==1 then door="open" else door="closed" end
  63. conn:send("<h1> Door Sensor. The door is " .. door ..".</h1>") conn:close() end) end)
  64. srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive",function(conn,payload)
  65. print(node.heap()) print(adc.read(0)) door="open" if gpio.read(0)==1 then door="open" else door="closed" end
  66. conn:send("<h1> Door Sensor. The door is " .. door ..".</h1>") end) conn:on("sent",function(conn) conn:close() end) end)
  67. srv=net.createServer(net.TCP) srv:listen(80,function(conn)
  68. conn:on("receive",function(conn,payload)
  69. print(node.heap())
  70. door="open"
  71. if gpio.read(0)==1 then door="open" else door="closed" end
  72. conn:send("<h1> Door Sensor. The door is " .. door ..".</h1>")
  73. end)
  74. conn:on("sent",function(conn) conn:close() end)
  75. end)
  76. port = 9999
  77. hostip = "192.168.1.99"
  78. sk=net.createConnection(net.TCP, false)
  79. sk:on("receive", function(conn, pl) print(pl) end )
  80. sk:connect(port, hostip)
  81. file.remove("init.lua")
  82. file.open("init.lua","w")
  83. file.writeline([[print("Petes Tester 4")]])
  84. file.writeline([[tmr.alarm(5000, 0, function() dofile("thelot.lua") end )]])
  85. file.close()
  86. file.remove("thelot.lua")
  87. file.open("thelot.lua","w")
  88. file.writeline([[tmr.stop()]])
  89. file.writeline([[connecttoap = function (ssid,pw)]])
  90. file.writeline([[print(wifi.sta.getip())]])
  91. file.writeline([[wifi.setmode(wifi.STATION)]])
  92. file.writeline([[tmr.delay(1000000)]])
  93. file.writeline([[wifi.sta.config(ssid,pw)]])
  94. file.writeline([[tmr.delay(5000000)]])
  95. file.writeline([[print("Connected to ",ssid," as ",wifi.sta.getip())]])
  96. file.writeline([[end]])
  97. file.writeline([[connecttoap("MyHub","0011223344")]])
  98. file.close()
  99. s=net.createServer(net.UDP) s:listen(5683) s:on("receive",function(s,c) print(c) s:send("echo:"..c) end)
  100. s:on("sent",function(s) print("echo donn") end)
  101. sk=net.createConnection(net.UDP, 0) sk:on("receive", function(sck, c) print(c) end ) sk:connect(8080,"192.168.0.88")
  102. sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.88\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
  103. srv=net.createServer(net.TCP, 5) srv:listen(80,function(conn) conn:on("receive",function(conn,payload)
  104. print(node.heap()) print(adc.read(0)) door="open" if gpio.read(0)==1 then door="open" else door="closed" end
  105. conn:send("<h1> Door Sensor. The door is " .. door ..".</h1>") end) end)
  106. srv=net.createServer(net.TCP)
  107. srv:listen(80,function(conn)
  108. conn:on("receive",function(conn,payload)
  109. print(payload) print(node.heap())
  110. conn:send("<h1> Hello, NodeMcu.</h1>")
  111. end)
  112. conn:on("sent",function(conn) conn:close() end)
  113. end)
  114. function startServer()
  115. print("WIFI AP connected. Wicon IP:")
  116. print(wifi.sta.getip())
  117. sv=net.createServer(net.TCP,180)
  118. sv:listen(8080,function(conn)
  119. print("Wifi console connected.")
  120. function s_output(str)
  121. if(conn~=nil) then
  122. conn:send(str)
  123. end
  124. end
  125. node.output(s_output,0)
  126. conn:on("receive",function(conn,pl)
  127. node.input(pl)
  128. if (conn==nil) then
  129. print("conn is nil")
  130. end
  131. print("hello")
  132. mycounter=0 srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive",function(conn,payload)
  133. if string.find(payload,"?myarg=") then mycounter=mycounter+1
  134. m="<br/>Value= " .. string.sub(payload,string.find(payload,"?myarg=")+7,string.find(payload,"HTTP")-2) else m="" end
  135. conn:send("<h1> Hello, this is Pete's web page.</h1>How are you today.<br/> Count=" .. mycounter .. m .. "Heap=".. node.heap())
  136. end) conn:on("sent",function(conn) conn:close() conn = nil end) end)
  137. srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive",function(conn,payload)
  138. conn:send("HTTP/1.1 200 OK\r\n") conn:send("Connection: close\r\n\r\n") conn:send("<h1> Hello, NodeMcu.</h1>")
  139. print(node.heap()) conn:close() end) end)
  140. conn=net.createConnection(net.TCP)
  141. conn:dns("www.nodemcu.com",function(conn,ip) print(ip) print("hell") end)
  142. function connected(conn) conn:on("receive",function(conn,payload)
  143. conn:send("HTTP/1.1 200 OK\r\n") conn:send("Connection: close\r\n\r\n") conn:send("<h1> Hello, NodeMcu.</h1>")
  144. print(node.heap()) conn:close() end) end
  145. srv=net.createServer(net.TCP)
  146. srv:on("connection",function(conn) conn:on("receive",function(conn,payload)
  147. conn:send("HTTP/1.1 200 OK\r\n") conn:send("Connection: close\r\n\r\n") conn:send("<h1> Hello, NodeMcu.</h1>")
  148. print(node.heap()) conn:close() end) end)
  149. srv:listen(80)
  150. -- sieve.lua
  151. -- the sieve of Eratosthenes programmed with coroutines
  152. -- typical usage: lua -e N=500 sieve.lua | column
  153. -- generate all the numbers from 2 to n
  154. function gen (n) return coroutine.wrap(function () for i=2,n do coroutine.yield(i) end end) end
  155. -- filter the numbers generated by `g', removing multiples of `p'
  156. 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
  157. N=N or 500 -- from command line
  158. x = gen(N) -- generate primes up to N
  159. while 1 do
  160. local n = x() -- pick a number until done
  161. if n == nil then break end
  162. print(n) -- must be a prime number
  163. x = filter(n, x) -- now remove its multiples
  164. end
  165. file.remove("mylistener.lua")
  166. file.open("mylistener.lua","w")
  167. file.writeline([[gpio2 = 9]])
  168. file.writeline([[gpio0 = 8]])
  169. file.writeline([[gpio.mode(gpio2,gpio.OUTPUT)]])
  170. file.writeline([[gpio.write(gpio2,gpio.LOW)]])
  171. file.writeline([[gpio.mode(gpio0,gpio.OUTPUT)]])
  172. file.writeline([[gpio.write(gpio0,gpio.LOW)]])
  173. file.writeline([[l1="0\n"]])
  174. file.writeline([[l2="0\n"]])
  175. file.writeline([[l3="0\n"]])
  176. file.writeline([[l4="0\n"]])
  177. file.writeline([[sv=net.createServer(net.TCP, 5) ]])
  178. file.writeline([[sv:listen(4000,function(c)]])
  179. file.writeline([[c:on("disconnection", function(c) print("Bye") end )]])
  180. file.writeline([[c:on("receive", function(sck, pl) ]])
  181. -- file.writeline([[print(pl) ]])
  182. file.writeline([[if (pl=="GO1\n") then c:send(l1) ]])
  183. file.writeline([[elseif pl=="GO2\n" then c:send(l2) ]])
  184. file.writeline([[elseif pl=="GO3\n" then c:send(l3) ]])
  185. file.writeline([[elseif pl=="GO4\n" then c:send(l4) ]])
  186. file.writeline([[elseif pl=="YES1\n" then l1="1\n" c:send("OK\n") gpio.write(gpio2,gpio.HIGH) ]])
  187. file.writeline([[elseif pl=="NO1\n" then l1="0\n" c:send("OK\n") gpio.write(gpio2,gpio.LOW) ]])
  188. file.writeline([[elseif pl=="YES2\n" then l2="1\n" c:send("OK\n") gpio.write(gpio0,gpio.HIGH) ]])
  189. file.writeline([[elseif pl=="NO2\n" then l2="0\n" c:send("OK\n") gpio.write(gpio0,gpio.LOW) ]])
  190. file.writeline([[elseif pl=="YES3\n" then l3="1\n" c:send("OK\n") print(node.heap()) ]])
  191. file.writeline([[elseif pl=="NO3\n" then l3="0\n" c:send("OK\n") print(node.heap()) ]])
  192. file.writeline([[elseif pl=="YES4\n" then l4="1\n" c:send("OK\n") print(node.heap()) ]])
  193. file.writeline([[elseif pl=="NO4\n" then l4="0\n" c:send("OK\n") print(node.heap()) ]])
  194. file.writeline([[else c:send("0\n") print(node.heap()) ]])
  195. file.writeline([[end]])
  196. file.writeline([[end)]])
  197. file.writeline([[end)]])
  198. file.close()
  199. file.remove("myli.lua") file.open("myli.lua","w")
  200. file.writeline([[sv=net.createServer(net.TCP, 5) ]])
  201. file.writeline([[sv:listen(4000,function(c)]])
  202. file.writeline([[c:on("disconnection", function(c) print("Bye") end )]])
  203. --file.writeline([[c:on("sent", function(c) c:close() end )]])
  204. file.writeline([[c:on("receive", function(sck, pl) ]])
  205. file.writeline([[sck:send("0\n") print(node.heap()) ]])
  206. file.writeline([[end)]]) file.writeline([[end)]]) file.close()
  207. sv=net.createServer(net.TCP, 50) sv:listen(4000,function(c) c:on("disconnection",function(c) print("Bye") end)
  208. c:on("receive", function(sck, pl) sck:send("0\n") print(node.heap()) end) end)
  209. sv=net.createServer(net.TCP, 5) sv:listen(4000,function(c) c:on("disconnection",function(c) print("Bye") end)
  210. c:on("receive", function(sck, pl) sck:send("0\n") print(node.heap()) end) c:on("sent", function(sck) sck:close() end) end)
  211. s=net.createServer(net.UDP)
  212. s:on("receive",function(s,c) print(c) end)
  213. s:listen(8888)
  214. print("This is a long long long line to test the memory limit of nodemcu firmware\n")
  215. collectgarbage("setmemlimit",8)
  216. print(collectgarbage("getmemlimit"))
  217. tmr.alarm(1,5000,1,function() print("alarm 1") end)
  218. tmr.stop(1)
  219. tmr.alarm(0,1000,1,function() print("alarm 0") end)
  220. tmr.stop(0)
  221. tmr.alarm(2,2000,1,function() print("alarm 2") end)
  222. tmr.stop(2)
  223. tmr.alarm(6,2000,1,function() print("alarm 6") end)
  224. tmr.stop(6)
  225. for k,v in pairs(_G.package.loaded) do print(k) end
  226. for k,v in pairs(_G) do print(k) end
  227. for k,v in pairs(d) do print("n:"..k..", s:"..v) end
  228. a="pin=9"
  229. t={}
  230. for k, v in string.gmatch(a, "(%w+)=(%w+)") do t[k]=v end
  231. print(t["pin"])
  232. function switch() gpio.mode(4,gpio.OUTPUT) gpio.mode(5,gpio.OUTPUT) tmr.delay(1000000) print("hello world") end
  233. tmr.alarm(0,10000,0,function () uart.setup(0,9600,8,0,1) end) switch()
  234. 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")
  235. sk=net.createConnection(net.TCP, 0) sk:on("receive", function(sck, c) print(c) end )
  236. 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")
  237. sk=net.createConnection(net.TCP, 0) sk:on("receive", function(sck, c) print(c) end ) sk:connect(80,"115.239.210.27")
  238. sk:send("GET / HTTP/1.1\r\nHost: 115.239.210.27\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
  239. sk=net.createConnection(net.TCP, 1) sk:on("receive", function(sck, c) print(c) end )
  240. 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")
  241. wifi.sta.setip({ip="192.168.18.119",netmask="255.255.255.0",gateway="192.168.18.1"})
  242. uart.on("data","\r",function(input) if input=="quit\r" then uart.on("data") else print(input) end end, 0)
  243. uart.on("data","\n",function(input) if input=="quit\n" then uart.on("data") else print(input) end end, 0)
  244. uart.on("data", 5 ,function(input) if input=="quit\r" then uart.on("data") else print(input) end end, 0)
  245. uart.on("data", 0 ,function(input) if input=="q" then uart.on("data") else print(input) end end, 0)
  246. uart.on("data","\r",function(input) if input=="quit" then uart.on("data") else print(input) end end, 1)
  247. for k, v in pairs(file.list()) do print('file:'..k..' len:'..v) end
  248. m=mqtt.Client()
  249. m:connect("192.168.18.101",1883)
  250. m:subscribe("/topic",0,function(m) print("sub done") end)
  251. m:on("message",function(m,t,pl) print(t..":") if pl~=nil then print(pl) end end )
  252. m:publish("/topic","hello",0,0)
  253. uart.setup(0,9600,8,0,1,0)
  254. sv=net.createServer(net.TCP, 60)
  255. global_c = nil
  256. sv:listen(9999, function(c)
  257. if global_c~=nil then
  258. global_c:close()
  259. end
  260. global_c=c
  261. c:on("receive",function(sck,pl) uart.write(0,pl) end)
  262. end)
  263. uart.on("data",4, function(data)
  264. if global_c~=nil then
  265. global_c:send(data)
  266. end
  267. end, 0)
  268. file.open("hello.lua","w+")
  269. file.writeline([[print("hello nodemcu")]])
  270. file.writeline([[print(node.heap())]])
  271. file.close()
  272. node.compile("hello.lua")
  273. dofile("hello.lua")
  274. dofile("hello.lc")
  275. -- use copper addon for firefox
  276. cs=coap.Server()
  277. cs:listen(5683)
  278. myvar=1
  279. cs:var("myvar") -- get coap://192.168.18.103:5683/v1/v/myvar will return the value of myvar: 1
  280. -- function should tack one string, return one string.
  281. function myfun(payload)
  282. print("myfun called")
  283. respond = "hello"
  284. return respond
  285. end
  286. cs:func("myfun") -- post coap://192.168.18.103:5683/v1/f/myfun will call myfun
  287. cc = coap.Client()
  288. cc:get(coap.CON, "coap://192.168.18.100:5683/.well-known/core")
  289. cc:post(coap.NON, "coap://192.168.18.100:5683/", "Hello")
  290. file.open("test1.txt", "a+") for i = 1, 100*1000 do file.write("x") end file.close() print("Done.")
  291. for n,s in pairs(file.list()) do print(n.." size: "..s) end
  292. file.remove("test1.txt")
  293. for n,s in pairs(file.list()) do print(n.." size: "..s) end
  294. file.open("test2.txt", "a+") for i = 1, 1*1000 do file.write("x") end file.close() print("Done.")
  295. function TestDNSLeak()
  296. c=net.createConnection(net.TCP, 0)
  297. c:connect(80, "bad-name.tlddfdf")
  298. tmr.alarm(1, 3000, 0, function() print("hack socket close, MEM: "..node.heap()) c:close() end) -- socket timeout hack
  299. print("MEM: "..node.heap())
  300. end
  301. v="abc%0D%0Adef"
  302. print(string.gsub(v, "%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end))
  303. function ex(x) string.find("abc%0Ddef","bc") return 's' end
  304. string.gsub("abc%0Ddef", "%%(%x%x)", ex)
  305. function ex(x) string.char(35) return 's' end
  306. string.gsub("abc%0Ddef", "%%(%x%x)", ex) print("hello")
  307. function ex(x) string.lower('Ab') return 's' end
  308. string.gsub("abc%0Ddef", "%%(%x%x)", ex) print("hello")
  309. v="abc%0D%0Adef"
  310. pcall(function() print(string.gsub(v, "%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end)) end)
  311. mosca -v | bunyan
  312. m=mqtt.Client()
  313. m:connect("192.168.18.88",1883)
  314. topic={}
  315. topic["/topic1"]=0
  316. topic["/topic2"]=0
  317. m:subscribe(topic,function(m) print("sub done") end)
  318. m:on("message",function(m,t,pl) print(t..":") if pl~=nil then print(pl) end end )
  319. m:publish("/topic1","hello",0,0)
  320. m:publish("/topic3","hello",0,0) m:publish("/topic4","hello",0,0)
  321. m=mqtt.Client()
  322. m:connect("192.168.18.88",1883)
  323. m:subscribe("/topic1",0,function(m) print("sub done") end)
  324. m:subscribe("/topic2",0,function(m) print("sub done") end)
  325. m:on("message",function(m,t,pl) print(t..":") if pl~=nil then print(pl) end end )
  326. m:publish("/topic1","hello",0,0)
  327. m:publish("/topic3","hello",0,0) m:publish("/topic4","hello",0,0)
  328. m:publish("/topic1","hello1",0,0) m:publish("/topic2","hello2",0,0)
  329. m:publish("/topic1","hello",1,0)
  330. m:subscribe("/topic3",0,function(m) print("sub done") end)
  331. m:publish("/topic3","hello3",2,0)
  332. m=mqtt.Client()
  333. m:connect("192.168.18.88",1883, function(con) print("connected hello") end)
  334. m=mqtt.Client()
  335. m:on("connect",function(m) print("connection") end )
  336. m:connect("192.168.18.88",1883)
  337. m:on("offline",function(m) print("disconnection") end )
  338. m=mqtt.Client()
  339. m:on("connect",function(m) print("connection "..node.heap()) end )
  340. m:on("offline", function(conn)
  341. if conn == nil then print("conn is nil") end
  342. print("Reconnect to broker...")
  343. print(node.heap())
  344. conn:connect("192.168.18.88",1883,0,1)
  345. end)
  346. m:connect("192.168.18.88",1883,0,1)
  347. m=mqtt.Client()
  348. m:on("connect",function(m) print("connection "..node.heap()) end )
  349. m:on("offline", function(conn)
  350. if conn == nil then print("conn is nil") end
  351. print("Reconnect to broker...")
  352. print(node.heap())
  353. conn:connect("192.168.18.88",1883)
  354. end)
  355. m:connect("192.168.18.88",1883)
  356. m:close()
  357. m=mqtt.Client()
  358. m:connect("192.168.18.88",1883)
  359. m:on("message",function(m,t,pl) print(t..":") if pl~=nil then print(pl) end end )
  360. m:subscribe("/topic1",0,function(m) print("sub done") end)
  361. m:publish("/topic1","hello3",2,0) m:publish("/topic1","hello2",2,0)
  362. m:publish("/topic1","hello3",0,0) m:publish("/topic1","hello2",2,0)
  363. m:subscribe("/topic2",2,function(m) print("sub done") end)
  364. m:publish("/topic2","hello3",0,0) m:publish("/topic2","hello2",2,0)
  365. m=mqtt.Client()
  366. m:on("connect",function(m)
  367. print("connection "..node.heap())
  368. m:subscribe("/topic1",0,function(m) print("sub done") end)
  369. m:publish("/topic1","hello3",0,0) m:publish("/topic1","hello2",2,0)
  370. end )
  371. m:on("offline", function(conn)
  372. print("disconnect to broker...")
  373. print(node.heap())
  374. end)
  375. m:connect("192.168.18.88",1883,0,1)
  376. -- serout( pin, firstLevel, delay_table, [repeatNum] )
  377. gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
  378. gpio.serout(1,1,{30,30,60,60,30,30}) -- serial one byte, b10110010
  379. gpio.serout(1,1,{30,70},8) -- serial 30% pwm 10k, lasts 8 cycles
  380. gpio.serout(1,1,{3,7},8) -- serial 30% pwm 100k, lasts 8 cycles
  381. gpio.serout(1,1,{0,0},8) -- serial 50% pwm as fast as possible, lasts 8 cycles
  382. gpio.mode(1,gpio.OUTPUT,gpio.PULLUP)
  383. gpio.serout(1,0,{20,10,10,20,10,10,10,100}) -- sim uart one byte 0x5A at about 100kbps
  384. gpio.serout(1,1,{8,18},8) -- serial 30% pwm 38k, lasts 8 cycles
  385. -- Lua: mqtt.Client(clientid, keepalive, user, pass)
  386. -- test with cloudmqtt.com
  387. m_dis={}
  388. function dispatch(m,t,pl)
  389. if pl~=nil and m_dis[t] then
  390. m_dis[t](pl)
  391. end
  392. end
  393. function topic1func(pl)
  394. print("get1: "..pl)
  395. end
  396. function topic2func(pl)
  397. print("get2: "..pl)
  398. end
  399. m_dis["/topic1"]=topic1func
  400. m_dis["/topic2"]=topic2func
  401. m=mqtt.Client("nodemcu1",60,"test","test123")
  402. m:on("connect",function(m)
  403. print("connection "..node.heap())
  404. m:subscribe("/topic1",0,function(m) print("sub done") end)
  405. m:subscribe("/topic2",0,function(m) print("sub done") end)
  406. m:publish("/topic1","hello",0,0) m:publish("/topic2","world",0,0)
  407. end )
  408. m:on("offline", function(conn)
  409. print("disconnect to broker...")
  410. print(node.heap())
  411. end)
  412. m:on("message",dispatch )
  413. m:connect("m11.cloudmqtt.com",11214,0,1)
  414. -- Lua: mqtt:connect( host, port, secure, auto_reconnect, function(client) )
  415. tmr.alarm(0,10000,1,function() local pl = "time: "..tmr.time()
  416. m:publish("/topic1",pl,0,0)
  417. end)