main.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. PROJECT = "camerademo"
  2. VERSION = "1.0.0"
  3. sys = require("sys")
  4. httpplus = require "httpplus"
  5. local camera_id = camera.DVP
  6. local dvp_camera_table = {
  7. id = camera_id,
  8. sensor_width = 1280,
  9. sensor_height = 720
  10. }
  11. -- 摄像头回调
  12. camera.on(camera_id, "scanned", function(id, str)
  13. if type(str) == 'string' then
  14. log.info("扫码结果", str)
  15. elseif str == false then
  16. log.error("摄像头没有数据")
  17. else
  18. log.info("摄像头数据", str)
  19. sys.publish("capture_done")
  20. end
  21. end)
  22. -- testMode等于1时演示摄像头拍照,将图片保存在根目录下,文件名为abc.jpg
  23. -- 通过模块自身启动的一个http服务端访问图片,访问url为http://xxx.xxx.xxx.xxx/abc.jpg,其中xxx.xxx.xxx.xxx为模块ip地址
  24. -- 需要用电脑或其他设备与模块连接同一热点
  25. -- testMode等于2时,演示摄像头拍照,将图片数据存在zbuff中
  26. -- 通过http post将拍照文件上传至upload.air32.cn,数据访问页面是 https://www.air32.cn/upload/data/
  27. local testMode = 2
  28. -- WIFI热点名称
  29. local ssid = "uiot"
  30. -- WIFI热点密码
  31. local password = "czcjhp1985cbm"
  32. local rawbuff, err
  33. sys.taskInit(function()
  34. local result
  35. -- 初始化WIFI
  36. wlan.init()
  37. -- 连接WIFI
  38. wlan.connect(ssid, password)
  39. while true do
  40. -- 等待网络就绪消息,超时时间30秒
  41. result = sys.waitUntil("IP_READY", 30000)
  42. if result then
  43. log.info("网络已就绪", result)
  44. break
  45. end
  46. log.info("等待网络就绪超时")
  47. end
  48. if testMode == 1 then
  49. log.info("演示模式一")
  50. -- 启动HTTP服务
  51. httpsrv.start(80, function()
  52. end)
  53. -- 初始化摄像头
  54. result = camera.init(dvp_camera_table)
  55. while 1 do
  56. -- 初始化摄像头
  57. -- camera.init(dvp_camera_table)
  58. -- 启动摄像头
  59. camera.start(camera_id)
  60. -- 摄像头拍照并将数据保存在根目录下,文件名为abc.jpg
  61. camera.capture(camera_id, "/abc.jpg", 1)
  62. result = sys.waitUntil("capture_done", 30000)
  63. camera.stop(camera_id)
  64. -- 完全关闭摄像头才用这个
  65. -- camera.close(camera_id)
  66. if result then
  67. log.info("拍照成功")
  68. else
  69. log.info("拍照失败")
  70. end
  71. -- 等待30秒
  72. sys.wait(30000)
  73. end
  74. elseif testMode == 2 then
  75. log.info("演示模式二")
  76. rawbuff = zbuff.create(200 * 1024, 0, zbuff.HEAP_PSRAM)
  77. if rawbuff == nil then
  78. log.info("zbuff创建失败", err)
  79. while true do
  80. sys.wait(1000)
  81. end
  82. end
  83. -- 初始化摄像头
  84. camera.init(dvp_camera_table)
  85. while 1 do
  86. -- camera.init(dvp_camera_table)
  87. -- 启动摄像头
  88. camera.start(camera_id)
  89. camera.capture(camera_id, rawbuff, 1)
  90. result, param = sys.waitUntil("capture_done", 30000)
  91. camera.stop(camera_id)
  92. -- 完全关闭摄像头才用这个
  93. -- camera.close(camera_id)
  94. if result then
  95. log.info("拍照成功")
  96. local code, resp = httpplus.request({
  97. url = "http://upload.air32.cn/api/upload/jpg",
  98. method = "POST",
  99. body = rawbuff
  100. })
  101. log.info("http", code, resp)
  102. if code == 200 then
  103. log.info("上传成功")
  104. else
  105. log.info("上传失败")
  106. end
  107. else
  108. log.info("拍照失败")
  109. end
  110. rawbuff:resize(200 * 1024)
  111. -- 等待30秒
  112. sys.wait(30000)
  113. end
  114. else
  115. log.info("不支持的演示模式")
  116. end
  117. end)
  118. -- 用户代码已结束---------------------------------------------
  119. -- 结尾总是这一句
  120. sys.run()
  121. -- sys.run()之后后面不要加任何语句!!!!!