yeelink_lib.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. tmr.alarm(1,1000,1,function ()
  27. if wifi.sta.getip() ~= nil then
  28. tmr.stop(1)
  29. sk:dns("api.yeelink.net",function(conn,ip)
  30. dns=ip
  31. print("DNS YEELINK OK... IP: "..dns)
  32. end)
  33. end
  34. end)
  35. end
  36. sk:dns("api.yeelink.net",function(conn,ip)
  37. dns=ip
  38. print("DNS YEELINK OK... IP: "..dns)
  39. end)
  40. --========Set the init function===========
  41. --device->number
  42. --sensor->number
  43. -- apikey must be -> string <-
  44. -- e.g. xxx.init(00000,00000,"123j12b3jkb12k4b23bv54i2b5b3o4")
  45. --========================================
  46. function M.init(_device, _sensor, _apikey)
  47. device = tostring(_device)
  48. sensor = tostring(_sensor)
  49. apikey = _apikey
  50. if dns == "0.0.0.0" then
  51. tmr.alarm(2,5000,1,function ()
  52. if dns == "0.0.0.0" then
  53. print("Waiting for DNS...")
  54. end
  55. end)
  56. return false
  57. else
  58. return dns
  59. end
  60. end
  61. --========Check the DNS Status===========
  62. --if DNS success, return the address(string)
  63. --if DNS fail(or processing), return nil
  64. --
  65. --
  66. --========================================
  67. function M.getDNS()
  68. if dns == "0.0.0.0" then
  69. return nil
  70. else
  71. return dns
  72. end
  73. end
  74. --=====Update to Yeelink Sever(At least 10s per sencods))=====
  75. -- datapoint->number
  76. --
  77. --e.g. xxx.update(233.333)
  78. --============================================================
  79. function M.update(_datapoint)
  80. datapoint = tostring(_datapoint)
  81. sk:on("connection", function(conn)
  82. print("connect OK...")
  83. local a=[[{"value":]]
  84. local b=[[}]]
  85. local st=a..datapoint..b
  86. sk:send("POST /v1.0/device/"..device.."/sensor/"..sensor.."/datapoints HTTP/1.1\r\n"
  87. .."Host: www.yeelink.net\r\n"
  88. .."Content-Length: "..string.len(st).."\r\n"--the length of json is important
  89. .."Content-Type: application/x-www-form-urlencoded\r\n"
  90. .."U-ApiKey:"..apikey.."\r\n"
  91. .."Cache-Control: no-cache\r\n\r\n"
  92. ..st.."\r\n" )
  93. end)
  94. sk:on("receive", function(sck, content)
  95. if debug then
  96. print("\r\n"..content.."\r\n")
  97. else
  98. print("Date Receive")
  99. end
  100. end)
  101. sk:connect(80,dns)
  102. end
  103. --================end==========================
  104. return M