Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1511210
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: 项目管理

2014-10-27 15:05:45

原装电源(5V1A),  品胜的USB HUB,  接了三个设备(CH340+DHT22,  DS9490+DS18B20,  CP2102+sharp2)


点击(此处)折叠或打开

  1. --[[
  2. (温湿度+sharp2+此处省略N...) LUA测试例程, 如下地址可以查看
  3. http://www.lewei50.com/u/g/2375
  4. ]]--

  5. print("--> Hello LUA, qiushui_007 test!")
  6. -- load library
  7. local xucommon = require "luaxucommon"
  8. local uart = require "luauart"
  9. local http = require("socket.http")
  10. local ltn12 = require "ltn12"

  11. str_ip = xucommon.get_local_ip("auto")

  12. to_id = "qiushui@foxmail.com"
  13. function mail_tx(to_id, subject, content)
  14.     xucommon.mail_tx(to_id, subject, content)
  15. end

  16. function delay_ms(delay1)
  17.     xucommon.delay_ms(delay1)
  18. end


  19. function hex(s)        --显示字符串的HEX码
  20.     local s1 = string.gsub(s, "(.)", function (x) return string.format("%02X ", string.byte(x)) end)
  21.     return s1
  22. end

  23. ---
  24. function http_lewei50_request(reqbody)
  25.     local gateid = "01"
  26.     local userkey = "36be8ff22f794f1e8a0bee3336eexxxx"

  27.   --local reqbody = "[{\"Name\":\"" .. deviceid .. "\", \"Value\":\"" .. tostring(data) .. "\"}]"
  28.   local respbody = {}
  29.   local body, code, headers, status = http.request {
  30.       method = "POST",
  31.       url = "" .. gateid,
  32.       source = ltn12.source.string(reqbody),
  33.       headers = {
  34.         ["userkey"] = userkey,
  35.         --["Content-Type"] = "application/x-www-form-urlencoded",
  36.         ["content-length"] = string.len(reqbody),
  37.         ["Connection"] = "Close"
  38.       },
  39.       sink = ltn12.sink.table(respbody)
  40.   }

  41.   print("lewei50: " .. "reqbody = " .. reqbody)
  42.   print('body:' .. tostring(body))
  43.   print('code:' .. tostring(code))
  44.   print('status:' .. tostring(status) .. "\n")
  45. end

  46. function http_lewei50(deviceid, data)
  47.   local reqbody = "[{\"Name\":\"" .. deviceid .. "\", \"Value\":\"" .. tostring(data) .. "\"}]"
  48.   http_lewei50_request(reqbody)
  49. end

  50. function http_lewei50_2(deviceid, data, deviceid1, data1)
  51.   local reqbody = "[{\"Name\":\"" .. deviceid .. "\", \"Value\":\"" .. tostring(data) .. "\"}, {\"Name\":\"" .. deviceid1 .. "\", \"Value\":\"" .. tostring(data1) .. "\"}]"
  52.   http_lewei50_request(reqbody)
  53. end

  54. --- 读取BH4TDV之温湿度 start -------------------------------------------------------------------------
  55. function bh4tdv_get()
  56.     --device_name = "/dev/ttyATH0"
  57.     local device_name = "/dev/ttyUSB0"

  58.     local portnum = uart.open(device_name)
  59.     --print(portnum)

  60.     -- 先赋特定值
  61.     f_h1 = 0.0
  62.     f_t1 = 0.0

  63.     if portnum >= 0 then
  64.         uart.buad_set(portnum, 0)
  65.         uart.flush(portnum)
  66.         --wifi温湿度, 必须>=200
  67.         timeout_ms = 200
  68.         uart.timeout(timeout_ms)

  69.         --wifi温湿度, 读取湿度和温度
  70.         str1 = string.char(0x01, 0x03, 0x90, 0x01, 0x00, 0x02, 0xb8, 0xcb)
  71.         len = string.len(str1)
  72.         print("tx(" .. len .. "): " .. hex(str1))
  73.         len_ret, str_ret = uart.tx_rx(portnum, str1, len)
  74.         if len_ret == 9 then
  75.             print("rx(" .. len_ret .. "): " .. hex(str_ret))
  76.             --01 03 04 01 DD 00 F9 AB B7
  77.             --- 判断CRC16
  78.             --str_tmp = string.char(0x31, 0x32, 0x33, 0x34, 0x35, 0x36)
  79.             crc16 = xucommon.get_crc16(str_ret)
  80.             print("crc16 = " .. tostring(crc16) .. ", 0x" .. string.format("%04X", crc16))

  81.             h1_h = string.byte(str_ret, 4)
  82.             h1_l = string.byte(str_ret, 5)
  83.             f_h1 = (h1_h * 256 + h1_l) / 10

  84.             t1_h = string.byte(str_ret, 6)
  85.             t1_l = string.byte(str_ret, 7)
  86.             f_t1 = (t1_h * 256 + t1_l) / 10
  87.             print("h1 = " .. tostring(f_h1) .. ", t1 = " .. tostring(f_t1) )

  88.         end

  89.         --最后一定要close
  90.         uart.close(portnum)
  91.     end

  92.     return f_h1, f_t1
  93. end

  94. f_h1, f_t1 = bh4tdv_get()
  95. http_lewei50_2("T1", f_t1, "H1", f_h1)

  96. --- 温度的最小和最大值
  97. t_min = 10.0
  98. t_max = 35.0
  99. if f_t1 < t_min or f_t1 > t_max then
  100.   local str_time = os.date("%Y") .. "-" .. os.date("%m") .. "-" .. os.date("%d") .. " " .. os.date("%X")
  101.     local content = str_time .. ", " .. str_ip .. ", " .. tostring(f_t1) .. "\n"
  102.     subject = "BH4DTV read error"
  103.     mail_tx(to_id, subject, content)
  104. end
  105. --- 读取BH4TDV之温湿度 end -------------------------------------------------------------------------

  106. --- 同一网关不能更新太快, 否则可能封IP
  107. delay_ms(1000)

  108. --- 读取DS18B20的温度 start -------------------------------------------------------------------------
  109. function ds18b20_get()
  110.     local owlua = require "owlua"

  111.     f_t2 = 0.0
  112.     local device_name = "USB"
  113.     local portnum = owlua.open(device_name)
  114.     if portnum > 0 then
  115.         f_t2 = owlua.ds18b20_get_one(portnum)
  116.         print("T2 = " .. tostring(f_t2))
  117.         owlua.close(portnum)
  118.     end

  119.     return f_t2
  120. end

  121. f_t2 = ds18b20_get()
  122. http_lewei50("T2", f_t2)

  123. --- 差值大于特定范围则报告
  124. f_mark = 2.0
  125. --f_t2 = f_t1 - f_mark - 0.1 --only test
  126. f_diff = f_t1 - f_t2
  127. if f_diff < -f_mark or f_diff > f_mark then
  128.     local str_time = os.date("%Y") .. "-" .. os.date("%m") .. "-" .. os.date("%d") .. " " .. os.date("%X")
  129.     local content = str_time .. ", " .. str_ip .. ", BH4TDV: " .. tostring(f_t1) .. ", 18B20: " .. tostring(f_t2)
  130.     print(content)
  131.     subject = "BH4DTV diff bigger than mark"
  132.     mail_tx(to_id, subject, content)
  133. end
  134. --- 读取DS18B20的温度 End -------------------------------------------------------------------------

  135. delay_ms(1000)

  136. --- read sharp2 start ------------------------------------------------------------------------
  137. function sharp2_get()
  138.     local device_name = "/dev/ttyUSB1"

  139.     f_vo, pc_ret = uart.sharp2_get(device_name)
  140.     if f_vo > 0.0 then
  141.         if f_vo <= 3 then
  142.                 k = 200
  143.         end
  144.         f_data = f_vo * k
  145.     else
  146.         f_data = 0.0
  147.     end

  148.     str_data = "PM2.5 = " .. tostring(f_data) .. ", str = " .. pc_ret .. ", vo = " .. tostring(f_vo)
  149.     print(str_data)

  150.     return f_data, str_data
  151. end

  152. f_u1, str_u1 = sharp2_get()

  153. t_min = 0.0
  154. t_max = 560.0
  155. --f_u1 = 0.0    -- only
  156. if f_u1 > t_min and f_u1 < t_max then
  157.     http_lewei50("U1", f_u1)

  158. else
  159.     local str_time = os.date("%Y") .. "-" .. os.date("%m") .. "-" .. os.date("%d") .. " " .. os.date("%X")
  160.     local content = str_time .. ", " .. str_ip .. ", " .. str_u1
  161.     print(content)
  162.     subject = "sharp2 data error"
  163.     mail_tx(to_id, subject, content)

  164. end
  165. --- read sharp2 end ------------------------------------------------------------------------

阅读(1681) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~