Post your best Lua script examples here

User avatar
By babsndeep
#58525 Hello,

I am working on building an IoT prototype using ESP8266-12F and a Moisture Sensor.

I am trying to achieve the following:
1) Read Analog Value from a Moisture Sensor using ESP8266-12F ADC Pin.
2) Create a WebServer on the ESP8266-12F which constantly pulls information from the Moisture Sensor
plugged into the ADC Pin & update the value on the WebServer.

I have the following completed and working:
1) I am able to read Analog Value from the Moisture Sensor using the ADC Pin on the ESP8266-12F
2) I am able to display the Analog Value form the Sensor on the WebServer

I would appreciate some help with the following:
1) How do I get the Moisture Sensor Value to update on the WebServer when it changes on the Sensor?

/****************************************** Code ***********************************/

--[[
NOTE: replace the "ssid" & "password" with your network credentials
]]
wifi.setmode(wifi.STATION)
wifi.sta.config("ssid", "pwd")
-- print the obtained IP from the specified network AP
print(wifi.sta.getip())

sensorValue = adc.read(0)
--[[
]]
function computeMoisture(sensorValue)
if sensorValue >= 80 and sensorValue < 84
then
-- 3.1 V (saturated)
contextualizedSensorValue = "Drowning in Water."
-- print(contextualizedSensorValue)

elseif sensorValue >= 84 and sensorValue < 88
then
-- 3.0 V
contextualizedSensorValue = "Hi There, Thanks for Checking-In, No H2O Needed yet."
-- print(contextualizedSensorValue)
elseif sensorValue >= 88 and sensorValue < 92
then
-- 2.5 V
contextualizedSensorValue = "Oh Hey Good Lookin', Stil okay with H2O."
-- print(contextualizedSensorValue)
elseif sensorValue >= 92 and sensorValue < 96
then
-- 2.0 V
contextualizedSensorValue = "Howdy, Please make sure to water me later."
-- print(contextualizedSensorValue)
elseif sensorValue >= 96 and sensorValue < 100
then
-- 1.5 V
contextualizedSensorValue = "Howdy, So some water would be nice...soon"
-- print(contextualizedSensorValue)
elseif sensorValue >= 100 and sensorValue < 104
then
-- 1.0 V
contextualizedSensorValue = "Howdy, please water...like....yesterday"
-- print(contextualizedSensorValue)
elseif sensorValue >= 104 and sensorValue < 108
then
-- 0.5 V (DAF Dry.As.f***)
contextualizedSensorValue = "Dying from Thrist."
-- print(contextualizedSensorValue)
else
contextualizedSensorValue = "Oh Snap, Something went Wrong :("
-- print(contextualizedSensorValue)
end
return contextualizedSensorValue
end
--[[
]]
clientServer = net.createServer(net.TCP)
clientServer:
listen(80, function(clientConnected)

clientConnected:
on("receive", function (serveThis)
serveThis: send ("<h1>Hello,</h1>"..
"<h2>The Contextulaized Data is:</h2>"..
sendThisToUser..
"<h2>Goodbye</h2>")
serveThis: close()
end)
end)

/**************************** End Code ****************************/

Thank You!