-
--[[
-
LUA的温湿度测试例程, 如下地址可以查看
-
http://www.lewei50.com/u/g/2375
-
]]--
-
-
print("--> Hello LUA, qiushui_007 test!")
-
-- load library
-
local xucommon = require "luaxucommon"
-
local uart = require "luauart"
-
local http = require("socket.http")
-
local ltn12 = require "ltn12"
-
-
str_ip = xucommon.get_local_ip("auto")
-
to_id = "qiushui@foxmail.com"
-
function mail_tx(to_id, subject, content)
-
xucommon.mail_tx(to_id, subject, content)
-
end
-
-
function hex(s) --显示字符串的HEX码
-
local s1 = string.gsub(s, "(.)", function (x) return string.format("%02X ", string.byte(x)) end)
-
return s1
-
end
-
-
---
-
function http_lewei50(deviceid, data)
-
local gateid = "01"
-
local userkey = "36be8ff22f794f1e8a0bee3336eexxxx"
-
-
local reqbody = "[{\"Name\":\"" .. deviceid .. "\", \"Value\":\"" .. tostring(data) .. "\"}]"
-
local respbody = {}
-
local body, code, headers, status = http.request {
-
method = "POST",
-
url = "" .. gateid,
-
source = ltn12.source.string(reqbody),
-
headers = {
-
["userkey"] = userkey,
-
--["Content-Type"] = "application/x-www-form-urlencoded",
-
["content-length"] = string.len(reqbody),
-
["Connection"] = "Close"
-
},
-
sink = ltn12.sink.table(respbody)
-
}
-
-
print("lewei50: " .. "reqbody = " .. reqbody)
-
print('body:' .. tostring(body))
-
print('code:' .. tostring(code))
-
print('status:' .. tostring(status) .. "\n")
-
-
end
-
-
function http_lewei50_2(deviceid, data, deviceid1, data1)
-
local gateid = "01"
-
local userkey = "36be8ff22f794f1e8a0bee3336eexxx"
-
-
--local reqbody = "[{\"Name\":\"" .. deviceid .. "\", \"Value\":\"" .. tostring(data) .. "\"}]"
-
local reqbody = "[{\"Name\":\"" .. deviceid .. "\", \"Value\":\"" .. tostring(data) .. "\"}, {\"Name\":\"" .. deviceid1 .. "\", \"Value\":\"" .. tostring(data1) .. "\"}]"
-
local respbody = {}
-
local body, code, headers, status = http.request {
-
method = "POST",
-
url = "" .. gateid,
-
source = ltn12.source.string(reqbody),
-
headers = {
-
["userkey"] = userkey,
-
--["Content-Type"] = "application/x-www-form-urlencoded",
-
["content-length"] = string.len(reqbody),
-
["Connection"] = "Close"
-
},
-
sink = ltn12.sink.table(respbody)
-
}
-
-
print("lewei50: " .. "reqbody = " .. reqbody)
-
print('body:' .. tostring(body))
-
print('code:' .. tostring(code))
-
print('status:' .. tostring(status) .. "\n")
-
-
end
-
-
to_id = "qiushui_008@foxmail.com"
-
function mail_tx(to_id, subject, content)
-
xucommon.mail_tx(to_id, subject, content)
-
end
-
-
--device_name = "/dev/ttyATH0"
-
device_name = "/dev/ttyUSB0"
-
-
portnum = uart.open(device_name)
-
--print(portnum)
-
-
if portnum >= 0 then
-
uart.buad_set(portnum, 0)
-
uart.flush(portnum)
-
--wifi温湿度, 必须>=200
-
timeout_ms = 200
-
uart.timeout(timeout_ms)
-
-
--wifi温湿度, 读取湿度和温度
-
str1 = string.char(0x01, 0x03, 0x90, 0x01, 0x00, 0x02, 0xb8, 0xcb)
-
len = string.len(str1)
-
print("tx(" .. len .. "): " .. hex(str1))
-
len_ret, str_ret = uart.tx_rx(portnum, str1, len)
-
if len_ret == 9 then
-
print("rx(" .. len_ret .. "): " .. hex(str_ret))
-
--01 03 04 01 DD 00 F9 AB B7
-
--- 判断CRC16
-
--str_tmp = string.char(0x31, 0x32, 0x33, 0x34, 0x35, 0x36)
-
crc16 = xucommon.get_crc16(str_ret)
-
print("crc16 = " .. tostring(crc16) .. ", 0x" .. string.format("%04X", crc16))
-
-
h1_h = string.byte(str_ret, 4)
-
h1_l = string.byte(str_ret, 5)
-
f_h1 = (h1_h * 256 + h1_l) / 10
-
-
t1_h = string.byte(str_ret, 6)
-
t1_l = string.byte(str_ret, 7)
-
f_t1 = (t1_h * 256 + t1_l) / 10
-
print("h1 = " .. tostring(f_h1) .. ", t1 = " .. tostring(f_t1) )
-
-
end
-
-
--最后一定要close
-
uart.close(portnum)
-
end
-
-
-
--- 温度的最小和最大值
-
t_min = 10.0
-
t_max = 35.0
-
-
http_lewei50_2("T1", f_t1, "H1", f_h1)
-
-
if f_t1 < t_min or f_t1 > t_max then
-
local str_time = os.date("%Y") .. "-" .. os.date("%m") .. "-" .. os.date("%d") .. " " .. os.date("%X")
-
local content = str_time .. ", " .. str_ip .. ", " .. tostring(f_t1) .. "\n"
-
subject = "BH4DTV read error"
-
mail_tx(to_id, subject, content)
-
end
-
-
--- 同一网关不能更新太快, 否则可能封IP
-
xucommon.delay_ms(2000)
-
-
--- 读取18B20的温度
-
local owlua = require "owlua"
-
-
device_name = "USB"
-
portnum = owlua.open(device_name)
-
if portnum > 0 then
-
f_t2 = owlua.ds18b20_get_one(portnum)
-
print("T2 = " .. tostring(f_t2))
-
owlua.close(portnum)
-
end
-
-
http_lewei50("T2", f_t2)
-
-
--- 差值大于特定范围则报告
-
f_mark = 2.0
-
--f_t2 = f_t1 - f_mark - 0.1 --only test
-
f_diff = f_t1 - f_t2
-
if f_diff < -f_mark or f_diff > f_mark then
-
local str_time = os.date("%Y") .. "-" .. os.date("%m") .. "-" .. os.date("%d") .. " " .. os.date("%X")
-
local content = str_time .. ", " .. str_ip .. ", BH4TDV: " .. tostring(f_t1) .. ", 18B20: " .. tostring(f_t2)
-
print(content)
-
subject = "BH4DTV diff bigger than mark"
-
mail_tx(to_id, subject, content)
-
end
设备如下图
阅读(2166) | 评论(0) | 转发(1) |