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

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: LINUX

2014-07-21 14:52:38

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个物联网平台. 

点击(此处)折叠或打开

  1. --[[
    DS9490R读取DS18B20的温度并上传到国内2个著名的物联网站
    http://blog.chinaunix.net/uid-27194309-id-4365340.html
    #如下网址可直接查看
    http://www.yeelink.net/devices/4420

    #DS9490R从这里查看
  2. ]]--

  3. print("--> Hello LUA, qiushui_007 test!")

  4. -- load library
  5. local owlua = require "owlua"
  6. package.loadlib("./owlua.so", "luaopen_array")()


  7. -- 底层库测试
  8. device_name = "USB"
  9. portnum = owlua.open(device_name)
  10. if portnum > 0 then
  11.     value = owlua.ds18b20_get(portnum)
  12.     print("data = " .. tostring(value))
  13.     owlua.close(portnum)

  14.     -- 上传的数据参数值
  15.     --value = 32.08

  16.     http = require("socket.http")
  17.     local ltn12 = require "ltn12"

  18.     -- lewei50
  19.     local reqbody = "[{\"Name\":\"T1\", \"Value\":\"" .. tostring(value) .. "\"}]"
  20.     local respbody = {}
  21.     local body, code, headers, status = http.request {
  22.         method = "POST",
  23.         url = "",
  24.         source = ltn12.source.string(reqbody),
  25.         headers = {
  26.           ["userkey"] = "36be8ff22f794f1e8a0bee3336eef237",
  27.           ["Content-Type"] = "application/x-www-form-urlencoded",
  28.           ["content-length"] = string.len(reqbody),
  29.           ["Connection"] = "Close"
  30.         },
  31.         sink = ltn12.sink.table(respbody)
  32.     }

  33.     print("lewei50: " .. "reqbody = " .. reqbody)
  34.     print('body:' .. tostring(body))
  35.     print('code:' .. tostring(code))
  36.     print('status:' .. tostring(status) .. "\n")


  37.     -- yeelink
  38.     local reqbody = "{\"value\":" .. tostring(value) .. "}"
  39.     local respbody = {}
  40.     local body, code, headers, status = http.request {
  41.         method = "POST",
  42.         url = "http://api.yeelink.net/v1.0/device/4420/sensor/9089/datapoints",
  43.         source = ltn12.source.string(reqbody),
  44.         headers = {
  45.           ["U-ApiKey"] = "729d1ba15b26b6a48f4807ef3f2f4df4",
  46.           ["Content-Type"] = "application/x-www-form-urlencoded",
  47.           ["content-length"] = string.len(reqbody),
  48.           ["Connection"] = "Close"
  49.         },
  50.         sink = ltn12.sink.table(respbody)
  51.     }

  52.     print("yeelink: " .. "reqbody = " .. reqbody)
  53.     print('body:' .. tostring(body))
  54.     print('code:' .. tostring(code))
  55.     print('status:' .. tostring(status) .. "\n")

  56. 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


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