Post your best Lua script examples here

User avatar
By Umberto Cilia
#35162 I have a problems with my new nodemcu dev kit, i am tring to send temperature and humidity from dht11 sensor to a php page on my web server with GET method.

this is the lua code

main.lua




---------------------------

service.php


<?php

if(isset($_GET['value'])){
$value=$_GET['value'];//This is the URI encoded string
var_dump ($value);
var_dump (json_decode($value));
var_dump ("Json last error is ".json_last_error());
}
$json_input_data = json_decode($value);
echo $json_input_data;

$file = 'test.txt';
file_put_contents($file, $json_input_data, FILE_APPEND | LOCK_EX);
?>





at the end i can't see nothing inside my test.txt file.


thanks to everybody can help me.





PIN = 1 -- data pin, GPIO2
DHT= require("dht_lib")
deviceID = 1


while(1==1)

do


DHT.read(PIN);
t = DHT.getTemperature();
h = DHT.getHumidity();
while ((t == nil) or (h==nil)) do
print(".")
DHT.read(PIN)
tmr.delay(1000000)
t = DHT.getTemperature()
h = DHT.getHumidity()

end

-- temperature in degrees Celsius and Farenheit
print("Temperature: "..((t-(t % 10)) / 10).."."..(t % 10).." deg C")
-- humidity
print("Humidity: "..((h - (h % 10)) / 10).."."..(h % 10).."%")



value = '[true, {"temp": '..t..' , "humidity": '..h..' }]'
json_text = cjson.encode(value)


if(t==nil) then
t=0
end


local socket = net.createConnection(net.TCP, 0)
socket:connect(80, "www.umbertocilia.it")
local json = "GET /service.php?type=command&param=udevice&idx="..deviceID..
"&nvalue=0&value="..json_text..
";0 HTTP/1.1\r\nHost: http://www.umbertocilia.it\r\n"
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n"
socket:send(json)

print("data sent")

tmr.delay(10000000)

end

DHT = nil
package.loaded["dht_lib"]=nil



----------------------------------------


init.lua

--init.lua
wifi.setmode(wifi.STATION)
wifi.sta.config("ssid","pass")
wifi.sta.connect()
wifi.sta.setip({ip="192.168.1.52",netmask="255.255.255.0",gateway="192.168.1.1"})
print("ESP8266 mode is: " .. wifi.getmode())
print("The module MAC address is: " .. wifi.ap.getmac())
print("Config done, IP is "..wifi.sta.getip())





dofile ("main.lua")



----------------------


dht_lib.lua



local moduleName = ...
local M = {}
_G[moduleName] = M


local humidity
local temperature


local bitStream = {}



local function read(pin)

local bitlength = 0
humidity = 0
temperature = 0


local gpio_read = gpio.read


for j = 1, 40, 1 do
bitStream[j] = 0
end


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)


while (gpio_read(pin) == 0 ) do end
local c=0
while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end

while (gpio_read(pin) == 0 ) do end
c=0
while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end


for j = 1, 40, 1 do
while (gpio_read(pin) == 1 and bitlength < 10 ) do
bitlength = bitlength + 1
end
bitStream[j] = bitlength
bitlength = 0

while (gpio_read(pin) == 0) do end
end
end


function M.read(pin)
read(pin)

local byte_0 = 0
local byte_1 = 0
local byte_2 = 0
local byte_3 = 0
local byte_4 = 0

for i = 1, 8, 1 do
if (bitStream[i] > 3) then
byte_0 = byte_0 + 2 ^ (8 - i)
end
end

for i = 1, 8, 1 do
if (bitStream[i+8] > 3) then
byte_1 = byte_1 + 2 ^ (8 - i)
end
end

for i = 1, 8, 1 do
if (bitStream[i+16] > 3) then
byte_2 = byte_2 + 2 ^ (8 - i)
end
end

for i = 1, 8, 1 do
if (bitStream[i+24] > 3) then
byte_3 = byte_2 + 2 ^ (8 - i)
end
end

for i = 1, 8, 1 do
if (bitStream[i+32] > 3) then
byte_4 = byte_4 + 2 ^ (8 - i)
end
end


if byte_1==0 and byte_3 == 0 then


if(byte_4 ~= byte_0+byte_2) then
humidity = nil
temperature = nil
else
humidity = byte_0 *10
temperature = byte_2 *10
end

else

humidity = byte_0 * 256 + byte_1
temperature = byte_2 * 256 + byte_3
checksum = byte_4

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

temperature = -(temperature - 0x8000)
end


if (checksumTest - checksum >= 1) or (checksum - checksumTest >= 1) then
humidity = nil
end

end

byte_0 = nil
byte_1 = nil
byte_2 = nil
byte_3 = nil
byte_4 = nil

end

function M.getTemperature()
return temperature
end

function M.getHumidity()
return humidity
end

return M