bh1750_Example2.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. -- ***************************************************************************
  2. -- BH1750 Example Program for ESP8266 with nodeMCU
  3. -- BH1750 compatible tested 2015-1-30
  4. --
  5. -- Written by xiaohu
  6. --
  7. -- MIT license, http://opensource.org/licenses/MIT
  8. -- ***************************************************************************
  9. --Updata to Lelian
  10. --Ps 需要改动的地方LW_GATEWAY(乐联的设备标示),USERKEY(乐联userkey)
  11. --Ps You nees to rewrite the LW_GATEWAY(Lelian's Device ID),USERKEY(Lelian's userkey)
  12. tmr.alarm(0, 60000, 1, function()
  13. SDA_PIN = 6 -- sda pin, GPIO12
  14. SCL_PIN = 5 -- scl pin, GPIO14
  15. BH1750 = require("BH1750")
  16. BH1750.init(SDA_PIN, SCL_PIN)
  17. BH1750.read(OSS)
  18. l = BH1750.getlux()
  19. --定义数据变量格式 Define the veriables formate
  20. PostData = "[{\"Name\":\"T\",\"Value\":\"" ..(l / 100).."."..(l % 100).."\"}]"
  21. --创建一个TCP连接 Create a TCP Connection
  22. socket=net.createConnection(net.TCP, 0)
  23. --域名解析IP地址并赋值 DNS...it
  24. socket:dns("www.lewei50.com", function(conn, ip)
  25. ServerIP = ip
  26. print("Connection IP:" .. ServerIP)
  27. end)
  28. --开始连接服务器 Connect the sever
  29. socket:connect(80, ServerIP)
  30. socket:on("connection", function(sck) end)
  31. --HTTP请求头定义 HTTP Head
  32. socket:send("POST /api/V1/gateway/UpdateSensors/LW_GATEWAY HTTP/1.1\r\n" ..
  33. "Host: www.lewei50.com\r\n" ..
  34. "Content-Length: " .. string.len(PostData) .. "\r\n" ..
  35. "userkey: USERKEY\r\n\r\n" ..
  36. PostData .. "\r\n")
  37. --HTTP响应内容 Print the HTTP response
  38. socket:on("receive", function(sck, response)
  39. print(response)
  40. end)
  41. end)