Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
a3b72906
Commit
a3b72906
authored
Apr 14, 2015
by
Martin Han
Browse files
Yeelink Modules
parent
81d9f889
Changes
1
Hide whitespace changes
Inline
Side-by-side
lua_modules/yeelink/yeelink_lib.lua
0 → 100644
View file @
a3b72906
-- ***************************************************************************
-- Yeelink Updata Libiary
--
-- 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
=
...
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
--================================
local
sk
=
net
.
createConnection
(
net
.
TCP
,
0
)
local
datapoint
=
0
--====DNS the yeelink ip advance(in order to save RAM)=====
sk
:
dns
(
"api.yeelink.net"
,
function
(
conn
,
ip
)
dns
=
ip
print
(
"DNS YEELINK OK... IP: "
..
dns
)
end
)
--========Set the init function===========
--device->number
--sensor->number
-- apikey must be -> string <-
-- e.g. xxx.init(00000,00000,"123j12b3jkb12k4b23bv54i2b5b3o4")
--========================================
function
M
.
init
(
_device
,
_sensor
,
_apikey
)
device
=
tostring
(
_device
)
sensor
=
tostring
(
_sensor
)
apikey
=
_apikey
if
dns
==
"0.0.0.0"
then
return
false
else
return
dns
end
end
--=====Update to Yeelink Sever(At least 10s per sencods))=====
-- datapoint->number
--
--e.g. xxx.update(233.333)
--============================================================
function
M
.
update
(
_datapoint
)
datapoint
=
tostring
(
_datapoint
)
sk
:
on
(
"connection"
,
function
(
conn
)
print
(
"connect OK..."
)
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
)
sk
:
on
(
"receive"
,
function
(
sck
,
content
)
if
debug
then
print
(
"
\r\n
"
..
content
..
"
\r\n
"
)
else
print
(
"Date Receive"
)
end
end
)
sk
:
connect
(
80
,
dns
)
end
--================end==========================
return
M
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment