openwrt下DS9490R的lua测试

0. 之前折腾openwrt时的笔记, 原文这里

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值