yeelink_lib.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. -- ***************************************************************************
  2. -- Yeelink Updata Libiary Version 0.1.2 r1
  3. --
  4. -- Written by Martin
  5. -- but based on a script of zhouxu_o from bbs.nodemcu.com
  6. --
  7. -- MIT license, http://opensource.org/licenses/MIT
  8. -- ***************************************************************************
  9. --==========================Module Part======================
  10. local moduleName = ...
  11. local M = {}
  12. _G[moduleName] = M
  13. --=========================Local Args=======================
  14. local dns = "0.0.0.0"
  15. local device = ""
  16. local sensor = ""
  17. local apikey = ""
  18. --================================
  19. local debug = true --<<<<<<<<<<<<< Don't forget to "false" it before using
  20. --================================
  21. local sk = net.createConnection(net.TCP, 0)
  22. local datapoint = 0
  23. --====DNS the yeelink ip advance(in order to save RAM)=====
  24. if wifi.sta.getip() == nil then
  25. print("Please Connect WIFI First")
  26. local wifiTimer = tmr.create()
  27. wifiTimer:alarm(1000, tmr.ALARM_AUTO,function ()
  28. if wifi.sta.getip() ~= nil then
  29. wifiTimer:stop()
  30. sk:dns("api.yeelink.net",function(_,ip)
  31. dns=ip
  32. print("DNS YEELINK OK... IP: "..dns)
  33. end)
  34. end
  35. end)
  36. end
  37. sk:dns("api.yeelink.net",function(conn, ip) -- luacheck: no unused
  38. dns = ip
  39. print("DNS YEELINK OK... IP: "..dns)
  40. end)
  41. --========Set the init function===========
  42. --device->number
  43. --sensor->number
  44. -- apikey must be -> string <-
  45. -- e.g. xxx.init(00000,00000,"123j12b3jkb12k4b23bv54i2b5b3o4")
  46. --========================================
  47. function M.init(_device, _sensor, _apikey)
  48. device = tostring(_device)
  49. sensor = tostring(_sensor)
  50. apikey = _apikey
  51. if dns == "0.0.0.0" then
  52. tmr.create():alarm(5000,tmr.ALARM_AUTO,function ()
  53. if dns == "0.0.0.0" then
  54. print("Waiting for DNS...")
  55. end
  56. end)
  57. return false
  58. else
  59. return dns
  60. end
  61. end
  62. --========Check the DNS Status===========
  63. --if DNS success, return the address(string)
  64. --if DNS fail(or processing), return nil
  65. --
  66. --
  67. --========================================
  68. function M.getDNS()
  69. if dns == "0.0.0.0" then
  70. return nil
  71. else
  72. return dns
  73. end
  74. end
  75. --=====Update to Yeelink Sever(At least 10s per sencods))=====
  76. -- datapoint->number
  77. --
  78. --e.g. xxx.update(233.333)
  79. --============================================================
  80. function M.update(_datapoint)
  81. datapoint = tostring(_datapoint)
  82. sk:on("connection", function()
  83. print("connect OK...")
  84. local a=[[{"value":]]
  85. local b=[[}]]
  86. local st=a..datapoint..b
  87. sk:send("POST /v1.0/device/"..device.."/sensor/"..sensor.."/datapoints HTTP/1.1\r\n"
  88. .."Host: www.yeelink.net\r\n"
  89. .."Content-Length: "..string.len(st).."\r\n"--the length of json is important
  90. .."Content-Type: application/x-www-form-urlencoded\r\n"
  91. .."U-ApiKey:"..apikey.."\r\n"
  92. .."Cache-Control: no-cache\r\n\r\n"
  93. ..st.."\r\n" )
  94. end)
  95. sk:on("receive", function(conn, content) -- luacheck: no unused
  96. if debug then
  97. print("\r\n"..content.."\r\n")
  98. else
  99. print("Date Receive")
  100. end
  101. end)
  102. sk:connect(80,dns)
  103. end
  104. --================end==========================
  105. return M