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

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: 项目管理

2014-10-25 15:21:19


点击(此处)折叠或打开

  1. --[[
  2. 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 hex(s)        --显示字符串的HEX码
  17.     local s1 = string.gsub(s, "(.)", function (x) return string.format("%02X ", string.byte(x)) end)
  18.     return s1
  19. end

  20. ---
  21. function http_lewei50(deviceid, data)
  22.     local gateid = "01"
  23.     local userkey = "36be8ff22f794f1e8a0bee3336eexxxx"

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

  38.   print("lewei50: " .. "reqbody = " .. reqbody)
  39.   print('body:' .. tostring(body))
  40.   print('code:' .. tostring(code))
  41.   print('status:' .. tostring(status) .. "\n")

  42. end

  43. function http_lewei50_2(deviceid, data, deviceid1, data1)
  44.     local gateid = "01"
  45.     local userkey = "36be8ff22f794f1e8a0bee3336eexxx"

  46.   --local reqbody = "[{\"Name\":\"" .. deviceid .. "\", \"Value\":\"" .. tostring(data) .. "\"}]"
  47.   local reqbody = "[{\"Name\":\"" .. deviceid .. "\", \"Value\":\"" .. tostring(data) .. "\"}, {\"Name\":\"" .. deviceid1 .. "\", \"Value\":\"" .. tostring(data1) .. "\"}]"
  48.   local respbody = {}
  49.   local body, code, headers, status = http.request {
  50.       method = "POST",
  51.       url = "" .. gateid,
  52.       source = ltn12.source.string(reqbody),
  53.       headers = {
  54.         ["userkey"] = userkey,
  55.         --["Content-Type"] = "application/x-www-form-urlencoded",
  56.         ["content-length"] = string.len(reqbody),
  57.         ["Connection"] = "Close"
  58.       },
  59.       sink = ltn12.sink.table(respbody)
  60.   }

  61.   print("lewei50: " .. "reqbody = " .. reqbody)
  62.   print('body:' .. tostring(body))
  63.   print('code:' .. tostring(code))
  64.   print('status:' .. tostring(status) .. "\n")

  65. end

  66. to_id = "qiushui_008@foxmail.com"
  67. function mail_tx(to_id, subject, content)
  68.     xucommon.mail_tx(to_id, subject, content)
  69. end

  70. --device_name = "/dev/ttyATH0"
  71. device_name = "/dev/ttyUSB0"

  72. portnum = uart.open(device_name)
  73. --print(portnum)

  74. if portnum >= 0 then
  75.     uart.buad_set(portnum, 0)
  76.     uart.flush(portnum)
  77.     --wifi温湿度, 必须>=200
  78.     timeout_ms = 200
  79.     uart.timeout(timeout_ms)

  80.     --wifi温湿度, 读取湿度和温度
  81.     str1 = string.char(0x01, 0x03, 0x90, 0x01, 0x00, 0x02, 0xb8, 0xcb)
  82.     len = string.len(str1)
  83.     print("tx(" .. len .. "): " .. hex(str1))
  84.     len_ret, str_ret = uart.tx_rx(portnum, str1, len)
  85.     if len_ret == 9 then
  86.         print("rx(" .. len_ret .. "): " .. hex(str_ret))
  87.         --01 03 04 01 DD 00 F9 AB B7
  88.         --- 判断CRC16
  89.         --str_tmp = string.char(0x31, 0x32, 0x33, 0x34, 0x35, 0x36)
  90.         crc16 = xucommon.get_crc16(str_ret)
  91.         print("crc16 = " .. tostring(crc16) .. ", 0x" .. string.format("%04X", crc16))

  92.         h1_h = string.byte(str_ret, 4)
  93.         h1_l = string.byte(str_ret, 5)
  94.         f_h1 = (h1_h * 256 + h1_l) / 10

  95.         t1_h = string.byte(str_ret, 6)
  96.         t1_l = string.byte(str_ret, 7)
  97.         f_t1 = (t1_h * 256 + t1_l) / 10
  98.         print("h1 = " .. tostring(f_h1) .. ", t1 = " .. tostring(f_t1) )

  99.     end

  100.     --最后一定要close
  101.     uart.close(portnum)
  102. end


  103. --- 温度的最小和最大值
  104. t_min = 10.0
  105. t_max = 35.0

  106. http_lewei50_2("T1", f_t1, "H1", f_h1)

  107. if f_t1 < t_min or f_t1 > t_max then
  108.   local str_time = os.date("%Y") .. "-" .. os.date("%m") .. "-" .. os.date("%d") .. " " .. os.date("%X")
  109.     local content = str_time .. ", " .. str_ip .. ", " .. tostring(f_t1) .. "\n"
  110.     subject = "BH4DTV read error"
  111.     mail_tx(to_id, subject, content)
  112. end

  113. --- 同一网关不能更新太快, 否则可能封IP
  114. xucommon.delay_ms(2000)

  115. --- 读取18B20的温度
  116. local owlua = require "owlua"

  117. device_name = "USB"
  118. portnum = owlua.open(device_name)
  119. if portnum > 0 then
  120.     f_t2 = owlua.ds18b20_get_one(portnum)
  121.     print("T2 = " .. tostring(f_t2))
  122.     owlua.close(portnum)
  123. end

  124. http_lewei50("T2", f_t2)

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


设备如下图

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