Configuração do processador e conexão com a rede.
-- configure ESP as a station
SSID = 'GL-iNet-866'
SSID_PASSWORD = 'goodlife'
station_cfg={}
station_cfg.ssid=SSID
station_cfg.pwd=SSID_PASSWORD
wifi.setmode(wifi.STATION)
wifi.sta.config(station_cfg)
wifi.sta.connect()
Tentando a conexão por1 segundo uma vez e informando sucesso ou falha.
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip() == nil then
print("IP unavailable, Waiting...")
else
tmr.stop(1)
print("ESP8266 mode is: " .. wifi.getmode())
print("The module MAC address is: " .. wifi.ap.getmac())
print("Config done, IP is "..wifi.sta.getip())
end
end)
Setando os leds e a função para encontrar na resposta HTTP os valores preenchidos no formulário pelo usuário.
led1 = 3
led2 = 4
gpio.mode(4, 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
_, _, path = string.find(request, "/(.*) HTTP")
if(path == 'config-email') then
print(request)
_, _, luminosity, receiver, body = string.find(request, "luminosity=([0-9]*)&receiver=(.+)&body=(.*)")
receiver = string.gsub(receiver, "%%40", '@')
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hex_to_char)
end
body = string.gsub(body, "+", " ")
body = unescape(body)
print(luminosity)
print(receiver)
print(body)
buf = "<html>Luminosidade: "..luminosity.."<br>"..
"Dest: "..receiver.."<br>"..
"Corpo: "..body.."<br>"..
"</html>"
client:send(buf)
tmr.alarm(0, 3000, tmr.ALARM_AUTO, function()
valor = adc.read(0)
if (valor > tonumber(luminosity)) then
send_email(receiver, body, luminosity)
end
print(valor)
end)
...
Caso não receba dados no parâmetro, devolve a página Web.
else
buf =
[[<html>
<head>
<meta charset="ISO-8859">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<style>
html {
height: 100%;
width: 100%;
}
body {
background-color: #bcd6ff;
height: 100%;
width: 100%;
}
.main {
background-color: #84b4ff;
width: 800px;
padding: 30px;
margin: auto;
}
#form-save textarea {
height: 200px;
}
</style>
</head>
<body>
<div class="main">
<form action="/config-email" id="form-save" method="POST">
<div class="form-group">
<label for="luminosity">Luminosidade minima:</label>
<input type="number" name="luminosity" class="form-control" id="luminosity">
</div>
<div class="form-group">
<label for="receiver">Destinatário:</label>
<input type="email" name="receiver" class="form-control" id="receiver">
</div>
<div class="form-group">
<label for="body">Corpo do email:</label>
<textarea type="text" name="body" class="form-control" id="body"></textarea>
</div>
<input type="submit">
</form>
</div>
</body>
</html>]]
client:send(buf)
end
end)
conn:on("sent", function(client)
client:close()
collectgarbage()
end)
end)
Função para enviar email.
function send_email(dest, body, luminosity)
count = 0
socket = net.createConnection(net.TCP, 0)
socket:on("receive", function(conn, payload)
print(payload)
count = count + 1
if (count == 1) then
socket:send("AUTH LOGIN\r\n")
elseif (count == 2) then
socket:send("bWFpY29uc2FudG9zMjUwMUBnbWFpbC5jb20=\r\n")
elseif (count == 3) then
socket:send("bWFpY29uMTIz\r\n")
elseif (count == 4) then
socket:send("MAIL FROM:<maiconsantos2501@gmail.com>\r\n")
elseif (count == 5) then
socket:send("RCPT TO:<"..dest..">\r\n")
elseif (count == 6) then
socket:send("DATA\r\n")
elseif (count == 7) then
socket:send("From: \"Maicon\"<maiconsantos2501@gmail.com>\r\n"..
"To: \"Receiver\"<"..dest..">\r\n"..
"Subject: Email de alerta\r\n\r\n"..
body..
"\r\n.\r\n")
elseif (count == 8) then
socket:send("QUIT\r\n")
socketTS = net.createConnection(net.TCP, 0)
socketTS:on("connection", function(conn, payload)
print("Conectou")
socketTS:send("GET /update?api_key=UKZQMM7H1D4CHCQS&field1="..luminosity.." HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end)
socketTS:connect(80, "api.thingspeak.com")
end
end)
socket:on("connection", function(conn, payload)
socket:send("EHLO mail.smtp2go.com\r\n");
print(payload)
end)
socket:connect(2525, "mail.smtp2go.com")
end