NodeMCU Быстрый старт ESP8266
Добавлено: Пн ноя 23, 2015 9:44 pm
Чтобы кто-то не тратил время на изучение, если кто не знает как но хочет..
Увидел такой девайс как NodeMCU
понравился тем что с ним можно сразу начать работать, подключить провод USB. так же есть много выводов, и большой обьём памяти 4мб.
заказывал тут http://www.ebay.com/itm/311413475392?_t ... EBIDX%3AIT
за эти деньги то что из неё можно вытянуть меня устраивает вполне.
Краткий список литературы:
https://github.com/nodemcu/nodemcu-devkit
http://www.nodemcu.com/docs/wifi-ap-mod ... i-ap-setip
https://github.com/nodemcu/nodemcu-firm ... u#gpioread
к NodeMCU подходят прошивки от ESP8266
а можно сделать так:
1. Программой nodemcu-flasher-master https://yadi.sk/d/oLEKhzZvkgBeL
надо залить прошивку, находится по пути ...\\nodemcu-flasher-master\Resources\Binaries\nodemcu_integer_0.9.5_20150318.bin
2. Программой ESPlorer подключиться к NodeMCU https://yadi.sk/d/e9P84IWYkgDuS.
3. Далее в NodeMCU можно заливать скрипты.
в поле Scripts создаётся файл с именем init.lua и копируется в NodeMCU кнопкой (Sent to SEP)
именно он будет выполняться при загрузке.
в данный момент у меня есть такие рабочие скрипты:
1. подключиться к NodeMCU wi-fi:node по ip 192.168.4.1 и включать выключать пины (будет загораться синий светодиод).
2. тоже самое но с авторизацией, не вижу смысла так как подключение в wi-fi уже защищено.
3. Считыватель температуры и влажности с DHT:
и дополнительно надо закачать файл с именем dht.lua
если есть у кого скрипты, можно выкладывать тут.
Увидел такой девайс как NodeMCU
понравился тем что с ним можно сразу начать работать, подключить провод USB. так же есть много выводов, и большой обьём памяти 4мб.
заказывал тут http://www.ebay.com/itm/311413475392?_t ... EBIDX%3AIT
за эти деньги то что из неё можно вытянуть меня устраивает вполне.
Краткий список литературы:
https://github.com/nodemcu/nodemcu-devkit
http://www.nodemcu.com/docs/wifi-ap-mod ... i-ap-setip
https://github.com/nodemcu/nodemcu-firm ... u#gpioread
к NodeMCU подходят прошивки от ESP8266
а можно сделать так:
1. Программой nodemcu-flasher-master https://yadi.sk/d/oLEKhzZvkgBeL
надо залить прошивку, находится по пути ...\\nodemcu-flasher-master\Resources\Binaries\nodemcu_integer_0.9.5_20150318.bin
2. Программой ESPlorer подключиться к NodeMCU https://yadi.sk/d/e9P84IWYkgDuS.
3. Далее в NodeMCU можно заливать скрипты.
в поле Scripts создаётся файл с именем init.lua и копируется в NodeMCU кнопкой (Sent to SEP)
именно он будет выполняться при загрузке.
в данный момент у меня есть такие рабочие скрипты:
1. подключиться к NodeMCU wi-fi:node по ip 192.168.4.1 и включать выключать пины (будет загораться синий светодиод).
СпойлерПоказать
wifi.setmode(wifi.SOFTAP)
--wifi.sta.config("node","node") --подключиться к своей сети
cfg={}
cfg.ssid="node"
cfg.pwd="node"
wifi.ap.config(cfg)
devName = "smart-home"
print(wifi.sta.getip())
--ip config
cfg =
{
ip="192.168.4.1",
netmask="255.255.255.0",
gateway="192.168.4.1"
}
wifi.ap.setip(cfg)
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
buf = buf.."<h1> ESP8266 Web Server</h1>";
buf = buf.."<p>GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a> <a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a> <a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
local _on,_off = "",""
if(_GET.pin == "ON1")then
gpio.write(led1, gpio.HIGH);
elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.LOW);
elseif(_GET.pin == "ON2")then
gpio.write(led2, gpio.HIGH);
elseif(_GET.pin == "OFF2")then
gpio.write(led2, gpio.LOW);
end
client:send(buf);
client:close();
collectgarbage();
end)
end)
--wifi.sta.config("node","node") --подключиться к своей сети
cfg={}
cfg.ssid="node"
cfg.pwd="node"
wifi.ap.config(cfg)
devName = "smart-home"
print(wifi.sta.getip())
--ip config
cfg =
{
ip="192.168.4.1",
netmask="255.255.255.0",
gateway="192.168.4.1"
}
wifi.ap.setip(cfg)
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
buf = buf.."<h1> ESP8266 Web Server</h1>";
buf = buf.."<p>GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a> <a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a> <a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
local _on,_off = "",""
if(_GET.pin == "ON1")then
gpio.write(led1, gpio.HIGH);
elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.LOW);
elseif(_GET.pin == "ON2")then
gpio.write(led2, gpio.HIGH);
elseif(_GET.pin == "OFF2")then
gpio.write(led2, gpio.LOW);
end
client:send(buf);
client:close();
collectgarbage();
end)
end)
СпойлерПоказать
-- Rui Santos
-- Complete project details at http://randomnerdtutorials.com
wifi.setmode(wifi.SOFTAP)
--wifi.sta.config("node","node") --подключиться к своей сети
cfg={}
cfg.ssid="node"
cfg.pwd="node"
wifi.ap.config(cfg)
devName = "smart-home"
print(wifi.sta.getip())
--ip config
cfg =
{
ip="192.168.4.1",
netmask="255.255.255.0",
gateway="192.168.4.1"
}
wifi.ap.setip(cfg)
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _, _, auth = string.find(request, "%cAuthorization: Basic ([%w=\+\/]+)");--Authorization:
if (auth == nil or auth ~= "dXNlcjpwYXNz")then --user:pass
client:send("HTTP/1.0 401 Authorization Required\r\nWWW-Authenticate: Basic realm=\"ESP8266 Web Server\"\r\n\r\n<h1>Unauthorized Access</h1>");
client:close();
return;
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
buf = buf.."<head>";
buf = buf.."<meta charset=\"utf-8\">";
buf = buf.."<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">";
buf = buf.."<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
buf = buf.."<script src=\"https://code.jquery.com/jquery-2.1.3.min.js\"></script>";
buf = buf.."<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstr ... ap.min.css\">";
buf = buf.."</head><div class=\"container\">";
buf = buf.."<h1>Web Server</h1>";
buf = buf.."<h2>GPIO 0</h2>";
buf = buf.."<div class=\"row\">";
buf = buf.."<div class=\"col-md-2\"><a href=\"?pin=ON1\" class=\"btn btn-block btn-lg btn-success\" role=\"button\">ON</a></div>";
buf = buf.."<div class=\"col-md-2\"><a href=\"?pin=OFF1\" class=\"btn btn-block btn-lg btn-danger\" role=\"button\">OFF</a></div>";
buf = buf.."</div>";
buf = buf.."<h2>GPIO 2</h2>";
buf = buf.."<div class=\"row\">";
buf = buf.."<div class=\"col-md-2\"><a href=\"?pin=ON2\" class=\"btn btn-block btn-lg btn-primary\" role=\"button\">ON</a></div>";
buf = buf.."<div class=\"col-md-2\"><a href=\"?pin=OFF2\" class=\"btn btn-block btn-lg btn-warning\" role=\"button\">OFF</a></div>";
buf = buf.."</div></div>";
local _on,_off = "",""
if(_GET.pin == "ON1")then
gpio.write(led1, gpio.HIGH);
elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.LOW);
elseif(_GET.pin == "ON2")then
gpio.write(led2, gpio.HIGH);
elseif(_GET.pin == "OFF2")then
gpio.write(led2, gpio.LOW);
end
client:send(buf);
client:close();
collectgarbage();
end)
end)
-- Complete project details at http://randomnerdtutorials.com
wifi.setmode(wifi.SOFTAP)
--wifi.sta.config("node","node") --подключиться к своей сети
cfg={}
cfg.ssid="node"
cfg.pwd="node"
wifi.ap.config(cfg)
devName = "smart-home"
print(wifi.sta.getip())
--ip config
cfg =
{
ip="192.168.4.1",
netmask="255.255.255.0",
gateway="192.168.4.1"
}
wifi.ap.setip(cfg)
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _, _, auth = string.find(request, "%cAuthorization: Basic ([%w=\+\/]+)");--Authorization:
if (auth == nil or auth ~= "dXNlcjpwYXNz")then --user:pass
client:send("HTTP/1.0 401 Authorization Required\r\nWWW-Authenticate: Basic realm=\"ESP8266 Web Server\"\r\n\r\n<h1>Unauthorized Access</h1>");
client:close();
return;
end
local _GET = {}
if (vars ~= nil)then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
end
end
buf = buf.."<head>";
buf = buf.."<meta charset=\"utf-8\">";
buf = buf.."<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">";
buf = buf.."<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
buf = buf.."<script src=\"https://code.jquery.com/jquery-2.1.3.min.js\"></script>";
buf = buf.."<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstr ... ap.min.css\">";
buf = buf.."</head><div class=\"container\">";
buf = buf.."<h1>Web Server</h1>";
buf = buf.."<h2>GPIO 0</h2>";
buf = buf.."<div class=\"row\">";
buf = buf.."<div class=\"col-md-2\"><a href=\"?pin=ON1\" class=\"btn btn-block btn-lg btn-success\" role=\"button\">ON</a></div>";
buf = buf.."<div class=\"col-md-2\"><a href=\"?pin=OFF1\" class=\"btn btn-block btn-lg btn-danger\" role=\"button\">OFF</a></div>";
buf = buf.."</div>";
buf = buf.."<h2>GPIO 2</h2>";
buf = buf.."<div class=\"row\">";
buf = buf.."<div class=\"col-md-2\"><a href=\"?pin=ON2\" class=\"btn btn-block btn-lg btn-primary\" role=\"button\">ON</a></div>";
buf = buf.."<div class=\"col-md-2\"><a href=\"?pin=OFF2\" class=\"btn btn-block btn-lg btn-warning\" role=\"button\">OFF</a></div>";
buf = buf.."</div></div>";
local _on,_off = "",""
if(_GET.pin == "ON1")then
gpio.write(led1, gpio.HIGH);
elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.LOW);
elseif(_GET.pin == "ON2")then
gpio.write(led2, gpio.HIGH);
elseif(_GET.pin == "OFF2")then
gpio.write(led2, gpio.LOW);
end
client:send(buf);
client:close();
collectgarbage();
end)
end)
СпойлерПоказать
wifi.setmode(wifi.SOFTAP)
--wifi.sta.config("node","node")
cfg={}
cfg.ssid="node"
cfg.pwd="node"
wifi.ap.config(cfg)
devName = "smart-home"
print(wifi.sta.getip())
--ip config
cfg =
{
ip="192.168.4.1",
netmask="255.255.255.0",
gateway="192.168.4.1"
}
wifi.ap.setip(cfg)
tmr.delay(5000)
sensorType="dht11" -- set sensor type dht11 or dht22
pin = 4 -- data pin, GPIO2
humi=0
temp=0
fare=0
bimb=1
--load DHT module for read sensor
function ReadDHT11()
dht=require("dht")
dht.read(pin)
chck=1
h=dht.getHumidity()
t=dht.getTemperature()
if h==nil then h=0 chck=0 end
if sensorType=="dht11"then
humi=h/256
temp=t/256
else
humi=h/10
temp=t/10
end
fare=(temp*9/5+32)
print("Humidity: "..humi.."%")
print("Temperature: "..temp.." deg C")
print("Temperature: "..fare.." deg F")
-- release module
dht=nil
package.loaded["dht"]=nil
end
ReadDHT11()
tmr.alarm(1,5000, 1, function() ReadDHT11() bimb=bimb+1 if bimb==5 then bimb=0 wifi.sta.connect() print("Reconnect")end end)
srv=net.createServer(net.TCP) srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
--print(payload) -- for debugging only
--generates HTML web site
conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\
<!DOCTYPE HTML>\
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"></head>\
<meta http-equiv="X-UA-Compatible" content="IE=edge">\
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstr ... ap.min.css">\
<meta http-equiv="refresh" content="6">\
</head><div class="container">\
<h1>Sensor Data</h1></br><div class="row">\
<div class="col-md-4"><div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title">Humidity</h3>\
</div><div class="panel-body">\
<div class="form-group form-group-lg"><input type="text" class="form-control" value="'..humi..' %">\
</div></div></div></div>\
<div class="col-md-4"><div class="panel panel-info"><div class="panel-heading"><h3 class="panel-title">Temperature</h3>\
</div><div class="panel-body">\
<div class="form-group form-group-lg"><input type="text" class="form-control" value="'..temp..' deg C">\
<input type="text" class="form-control" value="'..fare..' deg F">\
</div></div></div></div></div></div></html>')
conn:on("sent",function(conn) conn:close() end)
end)
end)
--wifi.sta.config("node","node")
cfg={}
cfg.ssid="node"
cfg.pwd="node"
wifi.ap.config(cfg)
devName = "smart-home"
print(wifi.sta.getip())
--ip config
cfg =
{
ip="192.168.4.1",
netmask="255.255.255.0",
gateway="192.168.4.1"
}
wifi.ap.setip(cfg)
tmr.delay(5000)
sensorType="dht11" -- set sensor type dht11 or dht22
pin = 4 -- data pin, GPIO2
humi=0
temp=0
fare=0
bimb=1
--load DHT module for read sensor
function ReadDHT11()
dht=require("dht")
dht.read(pin)
chck=1
h=dht.getHumidity()
t=dht.getTemperature()
if h==nil then h=0 chck=0 end
if sensorType=="dht11"then
humi=h/256
temp=t/256
else
humi=h/10
temp=t/10
end
fare=(temp*9/5+32)
print("Humidity: "..humi.."%")
print("Temperature: "..temp.." deg C")
print("Temperature: "..fare.." deg F")
-- release module
dht=nil
package.loaded["dht"]=nil
end
ReadDHT11()
tmr.alarm(1,5000, 1, function() ReadDHT11() bimb=bimb+1 if bimb==5 then bimb=0 wifi.sta.connect() print("Reconnect")end end)
srv=net.createServer(net.TCP) srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
--print(payload) -- for debugging only
--generates HTML web site
conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\
<!DOCTYPE HTML>\
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"></head>\
<meta http-equiv="X-UA-Compatible" content="IE=edge">\
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstr ... ap.min.css">\
<meta http-equiv="refresh" content="6">\
</head><div class="container">\
<h1>Sensor Data</h1></br><div class="row">\
<div class="col-md-4"><div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title">Humidity</h3>\
</div><div class="panel-body">\
<div class="form-group form-group-lg"><input type="text" class="form-control" value="'..humi..' %">\
</div></div></div></div>\
<div class="col-md-4"><div class="panel panel-info"><div class="panel-heading"><h3 class="panel-title">Temperature</h3>\
</div><div class="panel-body">\
<div class="form-group form-group-lg"><input type="text" class="form-control" value="'..temp..' deg C">\
<input type="text" class="form-control" value="'..fare..' deg F">\
</div></div></div></div></div></div></html>')
conn:on("sent",function(conn) conn:close() end)
end)
end)
СпойлерПоказать
-- ***************************************************************************
-- for communication of DHT sensor only
-- DHT module for ESP8266 with nodeMCU floating point
-- Written by Javier Yanez
-- but based on a script of Pigs Fly from ESP8266.com forum
-- MIT license, http://opensource.org/licenses/MIT
-- ***************************************************************************
local moduleName = ...
local M = {}
_G[moduleName] = M
local humidity
local temperature
function M.read(pin)
local checksum
local checksumTest
humidity = 0
temperature = 0
checksum = 0
-- Use Markus Gritsch trick to speed up read/write on GPIO
local gpio_read = gpio.read
local bitStream = {}
for j = 1, 40, 1 do
bitStream[j] = 0
end
local bitlength = 0
-- Step 1: send out start signal to DHT22
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.HIGH)
tmr.delay(100)
gpio.write(pin, gpio.LOW)
tmr.delay(20000)
gpio.write(pin, gpio.HIGH)
gpio.mode(pin, gpio.INPUT)
-- Step 2: DHT22 send response signal
-- bus will always let up eventually, don't bother with timeout
while (gpio_read(pin) == 0 ) do end
local c=0
while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end
-- bus will always let up eventually, don't bother with timeout
while (gpio_read(pin) == 0 ) do end
c=0
while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end
-- Step 3: DHT22 send data
for j = 1, 40, 1 do
while (gpio_read(pin) == 1 and bitlength < 10 ) do
bitlength = bitlength + 1
end
bitStream[j] = bitlength
bitlength = 0
-- bus will always let up eventually, don't bother with timeout
while (gpio_read(pin) == 0) do end
end
hum=" "
tem=" "
chk=" "
--DHT data acquired, process.
for i = 1, 16, 1 do
hum=hum.." "..tostring(bitStream)
tem=tem.." "..tostring(bitStream[i+16])
if (bitStream > 3) then
humidity = humidity + 2 ^ (16 - i)
end
if (bitStream[i + 16] > 3) then
temperature = temperature + 2 ^ (16 - i)
end
end
for i = 1, 8, 1 do
chk=chk.." "..tostring(bitStream[i+32])
if (bitStream[i + 32] > 3) then
checksum = checksum + 2 ^ (8 - i)
end
end
checksumTest = (bit.band(humidity, 0xFF) + bit.rshift(humidity, 8) + bit.band(temperature, 0xFF) + bit.rshift(temperature, 8))
checksumTest = bit.band(checksumTest, 0xFF)
if temperature > 0x8000 then
-- convert to negative format
temperature = -(temperature - 0x8000)
end
-- conditions compatible con float point and integer
if (checksumTest - checksum >= 1) or (checksum - checksumTest >= 1) then
humidity = nil
end
--print("checksum ", checksum)
--print("checksumTest ", checksumTest)
--print("humidity - timing of bits ",hum)
--print("temperat - timing of bits ",tem)
--print("checksum - timing of bits ",chk)
end
function M.getTemperature()
return temperature
end
function M.getHumidity()
return humidity
end
return M
-- for communication of DHT sensor only
-- DHT module for ESP8266 with nodeMCU floating point
-- Written by Javier Yanez
-- but based on a script of Pigs Fly from ESP8266.com forum
-- MIT license, http://opensource.org/licenses/MIT
-- ***************************************************************************
local moduleName = ...
local M = {}
_G[moduleName] = M
local humidity
local temperature
function M.read(pin)
local checksum
local checksumTest
humidity = 0
temperature = 0
checksum = 0
-- Use Markus Gritsch trick to speed up read/write on GPIO
local gpio_read = gpio.read
local bitStream = {}
for j = 1, 40, 1 do
bitStream[j] = 0
end
local bitlength = 0
-- Step 1: send out start signal to DHT22
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.HIGH)
tmr.delay(100)
gpio.write(pin, gpio.LOW)
tmr.delay(20000)
gpio.write(pin, gpio.HIGH)
gpio.mode(pin, gpio.INPUT)
-- Step 2: DHT22 send response signal
-- bus will always let up eventually, don't bother with timeout
while (gpio_read(pin) == 0 ) do end
local c=0
while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end
-- bus will always let up eventually, don't bother with timeout
while (gpio_read(pin) == 0 ) do end
c=0
while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end
-- Step 3: DHT22 send data
for j = 1, 40, 1 do
while (gpio_read(pin) == 1 and bitlength < 10 ) do
bitlength = bitlength + 1
end
bitStream[j] = bitlength
bitlength = 0
-- bus will always let up eventually, don't bother with timeout
while (gpio_read(pin) == 0) do end
end
hum=" "
tem=" "
chk=" "
--DHT data acquired, process.
for i = 1, 16, 1 do
hum=hum.." "..tostring(bitStream)
tem=tem.." "..tostring(bitStream[i+16])
if (bitStream > 3) then
humidity = humidity + 2 ^ (16 - i)
end
if (bitStream[i + 16] > 3) then
temperature = temperature + 2 ^ (16 - i)
end
end
for i = 1, 8, 1 do
chk=chk.." "..tostring(bitStream[i+32])
if (bitStream[i + 32] > 3) then
checksum = checksum + 2 ^ (8 - i)
end
end
checksumTest = (bit.band(humidity, 0xFF) + bit.rshift(humidity, 8) + bit.band(temperature, 0xFF) + bit.rshift(temperature, 8))
checksumTest = bit.band(checksumTest, 0xFF)
if temperature > 0x8000 then
-- convert to negative format
temperature = -(temperature - 0x8000)
end
-- conditions compatible con float point and integer
if (checksumTest - checksum >= 1) or (checksum - checksumTest >= 1) then
humidity = nil
end
--print("checksum ", checksum)
--print("checksumTest ", checksumTest)
--print("humidity - timing of bits ",hum)
--print("temperat - timing of bits ",tem)
--print("checksum - timing of bits ",chk)
end
function M.getTemperature()
return temperature
end
function M.getHumidity()
return humidity
end
return M
если есть у кого скрипты, можно выкладывать тут.