Guest User

Untitled

a guest
Apr 29th, 2016
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. rtctime.set(1)
  2. client = nil
  3.  
  4. function wifiClose()
  5.  
  6. client = nil
  7. wifi.sta.eventMonStop()
  8. wifi.sta.disconnect()
  9. print( "Wifi closed, mem: "..node.heap().."\n\r" )
  10.  
  11. tmr.alarm(0, 3000, tmr.ALARM_SINGLE, function()
  12. local sec = 1000000
  13. local interval = 20
  14. local tm = interval * sec - tmr.now()
  15. if tm < sec then tm = interval * sec end
  16. rtctime.dsleep( tm )
  17. end)
  18.  
  19. end
  20.  
  21. function createClient()
  22.  
  23. client = net.createConnection( net.TCP, 0 )
  24.  
  25. client:on( "connection", function( socket, data )
  26. local sec,usec = rtctime.get()
  27. print( "Connected to server\n\r" )
  28. socket:send( sec..","..tmr.time() )
  29. end)
  30.  
  31. client:on( "receive", function( socket, data )
  32. print( "Received from server: "..data.."\n\r" )
  33. socket:close()
  34. wifiClose()
  35. end)
  36.  
  37. client:connect( 10000, "10.1.1.1" )
  38.  
  39. end
  40.  
  41. function wifiClient()
  42.  
  43. print( "-------------------------------------------------\n\r" )
  44. print( "Wifi on\n\r" )
  45.  
  46. local ip = {}
  47. ip.ip = "10.1.1.10"
  48. ip.netmask = "255.255.255.0"
  49. ip.gateway = "10.1.1.1"
  50.  
  51. wifi.sleeptype( wifi.NONE_SLEEP )
  52. wifi.setphymode( wifi.PHYMODE_B )
  53. wifi.setmode( wifi.STATION )
  54. wifi.sta.config( "blah", "blah" )
  55. wifi.sta.setip( ip )
  56.  
  57. wifi.sta.eventMonReg( wifi.STA_GOTIP, function()
  58. print( "GOT IP in "..tmr.time().." sec\n\r" )
  59. tmr.alarm( 0, 1000, tmr.ALARM_SINGLE, function()
  60. createClient()
  61. end)
  62. end)
  63.  
  64. wifi.sta.eventMonStart()
  65. wifi.sta.connect()
  66.  
  67. end
  68.  
  69. wifiClient()
Advertisement
Add Comment
Please, Sign In to add comment