main.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. PROJECT = "adcdemo"
  2. VERSION = "1.0.0"
  3. SSID = "goat"
  4. WIFIPASS = "Linxumeng1234"
  5. AccessKeyID = "LTAI5tNJonTcotn8XYFqD5eq"
  6. AccessKeySecret = "MtvSCvWf1jnpZljeLILMkRRR7tZKTF"
  7. sys = require("sys")
  8. sysplus = require("sysplus")
  9. libnet = require "libnet"
  10. log.setLevel(1)
  11. log.info("main", PROJECT, VERSION)
  12. pendingData = nil
  13. function ConnectWifi()
  14. -- 联网 对表
  15. wlan.init()
  16. wlan.connect(SSID, WIFIPASS)
  17. log.info("wlan", "wait for IP_READY")
  18. sys.waitUntil("IP_READY", 30000)
  19. if wlan.ready() then
  20. log.info("wlan", "ready !!")
  21. end
  22. end
  23. ping = zbuff.create(42)
  24. pong = zbuff.create(42)
  25. function audioCallback(audio_id, msg,data)
  26. if msg == audio.RECORD_DATA then
  27. if(data==1)then
  28. pendingData = pong:toStr()
  29. else
  30. pendingData = ping:toStr()
  31. end
  32. end
  33. end
  34. -- 联网
  35. audio.setBus(0, audio.BUS_DAC)
  36. audio.vol(0,70);
  37. audio.micVol(0,100);
  38. audio.start(0, audio.PCM,1, 8000, 16) -- 通道0,PCM格式,单声道,采样率16000Hz,16位深度
  39. audio.on(0, audioCallback)
  40. audio.record(0, audio.PCM, 0, 0, nil,1,ping,pong,audio.RECORD_STEREO)
  41. function main()
  42. ConnectWifi()
  43. local netc = socket.create(nil, "main")
  44. socket.config(netc, 8899, true, false)
  45. local result = libnet.connect( "main", 15000, netc, "192.168.32.200", 8899)
  46. if result then
  47. log.info("socket", "服务器连上了")
  48. else
  49. log.info("socket", "服务器没连上了!!!")
  50. end
  51. libnet.tx( "main", 0, netc, "helloworld")
  52. local rolling=0
  53. local buff = zbuff.create(1024)
  54. while true do
  55. sys.wait(10)
  56. local ok, param = socket.rx(netc, buff)
  57. if ok and buff ~= nil and buff:used() > 0 then
  58. while true do
  59. local resu = audio.write(0,buff)
  60. if resu then
  61. break
  62. end
  63. end
  64. libnet.tx( "main", 0, netc, "OK")
  65. end
  66. if buff ~= nil then
  67. buff:del()
  68. end
  69. if pendingData then
  70. local max_size = 1000 -- 每段的最大字节数 udp MTU(一包最多)约1400
  71. local total_length = #pendingData
  72. local offset = 0
  73. while offset < total_length do
  74. local send_length = math.min(max_size, total_length - offset)
  75. local segment = pendingData:sub(offset + 1, offset + send_length)
  76. libnet.tx("main", 0, netc, string.pack("<I", rolling) .. segment)
  77. rolling = rolling +1
  78. offset = offset + send_length
  79. end
  80. pendingData = nil
  81. end
  82. end
  83. end
  84. local function netCB(msg)
  85. --log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  86. end
  87. local txqueue = {}
  88. sysplus.taskInitEx( main, "main", netCB, "main", txqueue, "main")
  89. sys.run()