From 5bda87845ff706cc16a54d4aa8a559ac48b069f8 Mon Sep 17 00:00:00 2001 From: Martin Han Date: Wed, 22 Apr 2015 12:50:02 +0800 Subject: [PATCH 1/3] Update to Ver0.1.2, fix logic bug Detail: When require lib before connect to ap, program will return nil but the table. --- lua_modules/yeelink/yeelink_lib.lua | 43 ++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/lua_modules/yeelink/yeelink_lib.lua b/lua_modules/yeelink/yeelink_lib.lua index 39f2b129..8702cbf6 100644 --- a/lua_modules/yeelink/yeelink_lib.lua +++ b/lua_modules/yeelink/yeelink_lib.lua @@ -1,17 +1,12 @@ -- *************************************************************************** --- Yeelink Updata Libiary +-- Yeelink Updata Libiary Version 0.1.2 -- -- Written by Martin -- but based on a script of zhouxu_o from bbs.nodemcu.com -- -- MIT license, http://opensource.org/licenses/MIT -- *************************************************************************** - -if wifi.sta.getip() == nil then - print("Please Connect WIFI First") - return nil -end --==========================Module Part====================== local moduleName = ... @@ -30,7 +25,23 @@ local debug = true --<<<<<<<<<<<<< Don't forget to "false" it before using local sk=net.createConnection(net.TCP, 0) local datapoint = 0 + + --====DNS the yeelink ip advance(in order to save RAM)===== + +if wifi.sta.getip() == nil then + print("Please Connect WIFI First") + tmr.alarm(1,1000,1,function () + if wifi.sta.getip() ~= nil then + tmr.stop(1) + sk:dns("api.yeelink.net",function(conn,ip) + dns=ip + print("DNS YEELINK OK... IP: "..dns) + end) + end + end) +end + sk:dns("api.yeelink.net",function(conn,ip) dns=ip @@ -50,14 +61,32 @@ function M.init(_device, _sensor, _apikey) sensor = tostring(_sensor) apikey = _apikey if dns == "0.0.0.0" then + tmr.alarm(2,5000,1,function () + if dns == "0.0.0.0" then + print("Waiting for DNS...") + end + end) return false else return dns end +end +--========Check the DNS Status=========== +--if DNS success, return the address(string) +--if DNS fail(or processing), return nil +-- +-- +--======================================== +function M.getDNS() + + if dns == "0.0.0.0" then + return nil + else + return dns + end end - --=====Update to Yeelink Sever(At least 10s per sencods))===== -- datapoint->number -- From a60f28cf9ccc875c193986f712a0f765b58fffc3 Mon Sep 17 00:00:00 2001 From: Martin Han Date: Wed, 22 Apr 2015 13:10:06 +0800 Subject: [PATCH 2/3] Ver 0.1.2 r1 Add init ok callback function --- lua_modules/yeelink/yeelink_lib.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua_modules/yeelink/yeelink_lib.lua b/lua_modules/yeelink/yeelink_lib.lua index 8702cbf6..226e33ce 100644 --- a/lua_modules/yeelink/yeelink_lib.lua +++ b/lua_modules/yeelink/yeelink_lib.lua @@ -1,6 +1,6 @@ -- *************************************************************************** --- Yeelink Updata Libiary Version 0.1.2 +-- Yeelink Updata Libiary Version 0.1.2 r1 -- -- Written by Martin -- but based on a script of zhouxu_o from bbs.nodemcu.com From effb57247caefa89476b64824fa57bf08052aa42 Mon Sep 17 00:00:00 2001 From: Martin Han Date: Wed, 22 Apr 2015 13:16:37 +0800 Subject: [PATCH 3/3] Add Example to yeelink lib --- .../yeelink/Example_for_Yeelink_Lib.lua | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lua_modules/yeelink/Example_for_Yeelink_Lib.lua diff --git a/lua_modules/yeelink/Example_for_Yeelink_Lib.lua b/lua_modules/yeelink/Example_for_Yeelink_Lib.lua new file mode 100644 index 00000000..d5cae98c --- /dev/null +++ b/lua_modules/yeelink/Example_for_Yeelink_Lib.lua @@ -0,0 +1,25 @@ +-- *************************************************************************** +-- Example for Yeelink Lib +-- +-- Written by Martin +-- +-- +-- MIT license, http://opensource.org/licenses/MIT +-- *************************************************************************** + +wifi.setmode(wifi.STATION) --Step1: Connect to Wifi +wifi.sta.config("SSID","Password") + +dht = require("dht_lib") --Step2: "Require" the libs +yeelink = require("yeelink_lib") + +yeelink.init(23333,23333,"You api-key",function() --Step3: Register the callback function + + print("Yeelink Init OK...") + tmr.alarm(1,60000,1,function() --Step4: Have fun~ (Update your data) + + dht.read(4) + yeelink.update(dht.getTemperature()) + + end) +end)