The way that the DHT temp/humid code is laid out it is extremely hard to follow.
For instance, the tmr.alarm is has 2 end statements and the end of the script has 3.
If this would be laid out in a more linear fashion, using indentation were required, it would be easier for a novice to comprehend.
Trying to stuff as much code in 1 line my be efficient on your part but I think it looses it’s intent.
In addition, trying to debug the code by commenting out section to verify others is difficult for someone that doesn’t completely understand what’s going on.
Suggestion: Rewrite this code so that it is modular and better comments. Think of the audience.
Hello Jack, thanks for bringing that up and I’ll try to make it easier to understand in a future update.
Just to be sure, which section are you having trouble with? Is it with the ReadDHT11() function?
--Read DHT Sensor function ReadDHT11() status, temp, humi, temp_dec, humi_dec = dht.read(pin) if status == dht.OK then print(string.format("DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n", math.floor(temp), temp_dec, math.floor(humi), humi_dec )) fare = (9 * math.floor(temp) / 5) + 32 fare_dec = (9 * temp / 5) % 10 elseif status == dht.ERROR_CHECKSUM then print( "DHT Checksum error." ) elseif status == dht.ERROR_TIMEOUT then print( "DHT timed out." ) end end ReadDHT11()
Thanks!
It’s the code after what you depicted above.
NOTE:
1. after the tmr statement there are 2 end statements on the same line
2. at the very end there are 3 end statements
3. this would be more readable if there was some PROPER indenting…very hard to follow.
— The tmr runs the function every 5 secs.
tmr.alarm(1,5000, 1, function() ReadDHT11() bimb=bimb+1 if bimb==5 then bimb=0 wifi.sta.connect() print(“Reconnect”)end end)
— Create the web server at port 80.
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
— The conn:send is the HTML basic web page
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/bootstrap/3.3.5/css/bootstrap.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=”‘..math.floor(humi)..’.’..humi_dec..’ %”>\
</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=”‘..math.floor(temp)..’.’..temp_dec..’ deg C”>\
<input type=”text” class=”form-control” value=”‘..fare..’.’..fare_dec..’ deg F”>\
</div></div></div></div></div></div></html>’)
conn:on(“sent”,function(conn) conn:close() end)
end)
end)
Thanks for letting me know!
I always have that thought in mind: trying to make the code easier to understand and with comments.
However, I wrote that script in 2015 (it still works perfectly, but at first I didn’t comment the code that much). I’ll definitely make that code easier to follow in the next eBook update.