1. 硬件: DS9490R, 温度探头: DS18B20, 刷好openwrt的WR703N一台
2. 软件: 必须安装好libusb_0.1.12, luasocket
opkg install libusb
opkg install luasocket
3. 提供ar71xx内核的lua专用库, 文件名为: owlua.so
用winscp将本文件拷贝到路由器上
chmod +x owlua.so
4. lua代码, 读取温度值后再上传到国内著名的2个物联网平台.
-
--[[
DS9490R读取DS18B20的温度并上传到国内2个著名的物联网站
http://blog.chinaunix.net/uid-27194309-id-4365340.html
#如下网址可直接查看
http://www.yeelink.net/devices/4420
#DS9490R从这里查看
-
]]--
-
print("--> Hello LUA, qiushui_007 test!")
-
-
-- load library
-
local owlua = require "owlua"
-
package.loadlib("./owlua.so", "luaopen_array")()
-
-
-
-- 底层库测试
-
device_name = "USB"
-
portnum = owlua.open(device_name)
-
if portnum > 0 then
-
value = owlua.ds18b20_get(portnum)
-
print("data = " .. tostring(value))
-
owlua.close(portnum)
-
-
-- 上传的数据参数值
-
--value = 32.08
-
-
http = require("socket.http")
-
local ltn12 = require "ltn12"
-
-
-- lewei50
-
local reqbody = "[{\"Name\":\"T1\", \"Value\":\"" .. tostring(value) .. "\"}]"
-
local respbody = {}
-
local body, code, headers, status = http.request {
-
method = "POST",
-
url = "",
-
source = ltn12.source.string(reqbody),
-
headers = {
-
["userkey"] = "36be8ff22f794f1e8a0bee3336eef237",
-
["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")
-
-
-
-- yeelink
-
local reqbody = "{\"value\":" .. tostring(value) .. "}"
-
local respbody = {}
-
local body, code, headers, status = http.request {
-
method = "POST",
-
url = "http://api.yeelink.net/v1.0/device/4420/sensor/9089/datapoints",
-
source = ltn12.source.string(reqbody),
-
headers = {
-
["U-ApiKey"] = "729d1ba15b26b6a48f4807ef3f2f4df4",
-
["Content-Type"] = "application/x-www-form-urlencoded",
-
["content-length"] = string.len(reqbody),
-
["Connection"] = "Close"
-
},
-
sink = ltn12.sink.table(respbody)
-
}
-
-
print("yeelink: " .. "reqbody = " .. reqbody)
-
print('body:' .. tostring(body))
-
print('code:' .. tostring(code))
-
print('status:' .. tostring(status) .. "\n")
-
-
end
5. 运行结果如下
root@OpenWrt:/xutest# lua ds18b20.lua
--> Hello LUA, qiushui_007 test!
data = 32.4375
lewei50: reqbody = [{"Name":"T1", "Value":"32.4375"}]
body:1
code:200
status:HTTP/1.1 200 OK
yeelink: reqbody = {"value":32.4375}
body:1
code:200
status:HTTP/1.1 200 OK
6. 相关的结果在如下的网站上能直观显示出来
http://www.yeelink.net/devices/4420
7. owlua.so和ds18b20.lua的下载
openwrt_owlua_140721_ar71xx.rar
阅读(3556) | 评论(0) | 转发(0) |