si7021-lewei.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. --创建一个定时器
  2. tmr.alarm(0, 60000, 1, function()
  3. SDA_PIN = 6 -- sda pin, GPIO12
  4. SCL_PIN = 5 -- scl pin, GPIO14
  5. si7021 = require("si7021")
  6. si7021.init(SDA_PIN, SCL_PIN)
  7. si7021.read(OSS)
  8. Hum = si7021.getHumidity()
  9. Temp = si7021.getTemperature()
  10. --定义数据变量格式
  11. PostData = "[{\"Name\":\"T\",\"Value\":\"" .. (Temp/100).."."..(Temp%100) .. "\"},{\"Name\":\"H\",\"Value\":\"" .. (Hum/100).."."..(Hum%100) .. "\"}]"
  12. --创建一个TCP连接
  13. socket=net.createConnection(net.TCP, 0)
  14. --域名解析IP地址并赋值
  15. socket:dns("www.lewei50.com", function(conn, ip)
  16. ServerIP = ip
  17. print("Connection IP:" .. ServerIP)
  18. end)
  19. --开始连接服务器
  20. socket:connect(80, ServerIP)
  21. socket:on("connection", function(sck) end)
  22. --HTTP请求头定义
  23. socket:send("POST /api/V1/gateway/UpdateSensors/yourID HTTP/1.1\r\n" ..
  24. "Host: www.lewei50.com\r\n" ..
  25. "Content-Length: " .. string.len(PostData) .. "\r\n" ..
  26. "userkey: yourKEY\r\n\r\n" ..
  27. PostData .. "\r\n")
  28. --HTTP响应内容
  29. socket:on("receive", function(sck, response)
  30. print(response)
  31. end)
  32. -- release module
  33. si7021 = nil
  34. package.loaded["si7021"]=nil
  35. end)