2015-04-14 14:42:16 +02:00
|
|
|
|
|
|
|
-- ***************************************************************************
|
2015-04-22 07:10:06 +02:00
|
|
|
-- Yeelink Updata Libiary Version 0.1.2 r1
|
2015-04-14 14:42:16 +02:00
|
|
|
--
|
|
|
|
-- Written by Martin
|
|
|
|
-- but based on a script of zhouxu_o from bbs.nodemcu.com
|
|
|
|
--
|
|
|
|
-- MIT license, http://opensource.org/licenses/MIT
|
|
|
|
-- ***************************************************************************
|
|
|
|
--==========================Module Part======================
|
|
|
|
|
|
|
|
local moduleName = ...
|
|
|
|
local M = {}
|
|
|
|
_G[moduleName] = M
|
|
|
|
--=========================Local Args=======================
|
|
|
|
local dns = "0.0.0.0"
|
|
|
|
|
|
|
|
local device = ""
|
|
|
|
local sensor = ""
|
|
|
|
local apikey = ""
|
|
|
|
|
|
|
|
--================================
|
|
|
|
local debug = true --<<<<<<<<<<<<< Don't forget to "false" it before using
|
|
|
|
--================================
|
2019-12-31 00:53:54 +01:00
|
|
|
local sk = net.createConnection(net.TCP, 0)
|
2015-04-14 14:42:16 +02:00
|
|
|
|
|
|
|
local datapoint = 0
|
2015-04-22 06:50:02 +02:00
|
|
|
|
|
|
|
|
2015-04-14 14:42:16 +02:00
|
|
|
--====DNS the yeelink ip advance(in order to save RAM)=====
|
2015-04-22 06:50:02 +02:00
|
|
|
|
|
|
|
if wifi.sta.getip() == nil then
|
|
|
|
print("Please Connect WIFI First")
|
2019-12-31 00:53:54 +01:00
|
|
|
local wifiTimer = tmr.create()
|
|
|
|
wifiTimer:alarm(1000, tmr.ALARM_AUTO,function ()
|
2015-04-22 06:50:02 +02:00
|
|
|
if wifi.sta.getip() ~= nil then
|
2019-12-31 00:53:54 +01:00
|
|
|
wifiTimer:stop()
|
|
|
|
sk:dns("api.yeelink.net",function(_,ip)
|
2015-04-22 06:50:02 +02:00
|
|
|
dns=ip
|
|
|
|
print("DNS YEELINK OK... IP: "..dns)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
sk:dns("api.yeelink.net",function(conn, ip) -- luacheck: no unused
|
|
|
|
dns = ip
|
|
|
|
print("DNS YEELINK OK... IP: "..dns)
|
2015-04-14 14:42:16 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
--========Set the init function===========
|
|
|
|
--device->number
|
|
|
|
--sensor->number
|
|
|
|
-- apikey must be -> string <-
|
|
|
|
-- e.g. xxx.init(00000,00000,"123j12b3jkb12k4b23bv54i2b5b3o4")
|
|
|
|
--========================================
|
2022-07-10 17:01:21 +02:00
|
|
|
function M.init(device_, sensor_, apikey_)
|
|
|
|
device = tostring(device_)
|
|
|
|
sensor = tostring(sensor_)
|
|
|
|
apikey = apikey_
|
2015-04-14 14:42:16 +02:00
|
|
|
if dns == "0.0.0.0" then
|
2019-12-31 00:53:54 +01:00
|
|
|
tmr.create():alarm(5000,tmr.ALARM_AUTO,function ()
|
|
|
|
if dns == "0.0.0.0" then
|
|
|
|
print("Waiting for DNS...")
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
return false
|
2015-04-14 14:42:16 +02:00
|
|
|
else
|
|
|
|
return dns
|
|
|
|
end
|
|
|
|
end
|
2015-04-22 06:50:02 +02:00
|
|
|
--========Check the DNS Status===========
|
|
|
|
--if DNS success, return the address(string)
|
|
|
|
--if DNS fail(or processing), return nil
|
2019-02-17 19:26:29 +01:00
|
|
|
--
|
|
|
|
--
|
2015-04-22 06:50:02 +02:00
|
|
|
--========================================
|
|
|
|
function M.getDNS()
|
|
|
|
|
|
|
|
if dns == "0.0.0.0" then
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return dns
|
|
|
|
end
|
2015-04-14 14:42:16 +02:00
|
|
|
|
2015-04-22 06:50:02 +02:00
|
|
|
end
|
2015-04-14 14:42:16 +02:00
|
|
|
|
|
|
|
--=====Update to Yeelink Sever(At least 10s per sencods))=====
|
|
|
|
-- datapoint->number
|
|
|
|
--
|
|
|
|
--e.g. xxx.update(233.333)
|
|
|
|
--============================================================
|
2022-07-10 17:01:21 +02:00
|
|
|
function M.update(datapoint_)
|
2015-04-14 14:42:16 +02:00
|
|
|
|
2022-07-10 17:01:21 +02:00
|
|
|
datapoint = tostring(datapoint_)
|
2015-04-14 14:42:16 +02:00
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
sk:on("connection", function()
|
2019-02-17 19:26:29 +01:00
|
|
|
|
|
|
|
print("connect OK...")
|
2015-04-14 14:42:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
local a=[[{"value":]]
|
|
|
|
local b=[[}]]
|
|
|
|
|
|
|
|
local st=a..datapoint..b
|
|
|
|
|
|
|
|
sk:send("POST /v1.0/device/"..device.."/sensor/"..sensor.."/datapoints HTTP/1.1\r\n"
|
|
|
|
.."Host: www.yeelink.net\r\n"
|
|
|
|
.."Content-Length: "..string.len(st).."\r\n"--the length of json is important
|
|
|
|
.."Content-Type: application/x-www-form-urlencoded\r\n"
|
|
|
|
.."U-ApiKey:"..apikey.."\r\n"
|
|
|
|
.."Cache-Control: no-cache\r\n\r\n"
|
|
|
|
..st.."\r\n" )
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
sk:on("receive", function(conn, content) -- luacheck: no unused
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2015-04-14 14:42:16 +02:00
|
|
|
if debug then
|
2019-02-17 19:26:29 +01:00
|
|
|
print("\r\n"..content.."\r\n")
|
2015-04-14 14:42:16 +02:00
|
|
|
else
|
|
|
|
print("Date Receive")
|
|
|
|
end
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
sk:connect(80,dns)
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
--================end==========================
|
|
|
|
return M
|