Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import network, socket, machine, time, module
- from umqtt.robust import MQTTClient
- def do_connect():
- import network
- sta_if = network.WLAN(network.STA_IF)
- if not sta_if.isconnected():
- print('connecting to network...')
- sta_if.active(True)
- sta_if.connect('GL-iNet-866', 'goodlife')
- while not sta_if.isconnected():
- pass
- print('network config:', sta_if.ifconfig())
- def leitor(limite, email):
- lim = int(limite)
- while True:
- valor = adc.read()
- print("Limite:")
- print(lim)
- print("Valor Atual:")
- print(valor)
- print("")
- if(valor > lim):
- print("*****Enviando Alerta*****")
- things_speak(valor)
- envia_email(valor, email)
- time.sleep_ms(5000)
- def envia_email(valor, email):
- msg = ("\r\n O leitor de luminosidade está em: " + str(valor))
- endmsg = "\r\n.\r\n"
- mailserver = ("smtp.gmail.com", 465)
- clientSocket = socket(AF_INET, SOCK_STREAM)
- clientSocket.connect(mailserver)
- recv = clientSocket.recv(1024)
- recv = recv.decode()
- print("mensagem após a requisição de conexão:" + recv)
- if recv[:3] != '220':
- print('resposta 220 não recebida do servidor.')
- heloCommand = 'EHLO gmail.com\r\n'
- clientSocket.send(heloCommand.encode())
- recv1 = clientSocket.recv(1024)
- recv1 = recv1.decode()
- print("mensagem após o comando EHLO:" + recv1)
- if recv1[:3] != '250':
- print('resposta 250 não recebida do servidor.')
- username = "pedroluiseccel@gmail.com"
- password = "Farofa123"
- base64_str = ("\x00"+username+"\x00"+password).encode()
- base64_str = base64.b64encode(base64_str)
- authMsg = "AUTH LOGIN ".encode()+base64_str+"\r\n".encode()
- clientSocket.send(authMsg)
- recv_auth = clientSocket.recv(1024)
- print(recv_auth.decode())
- mailFrom = "MAIL FROM:<concentrado@furbi.com>\r\n"
- clientSocket.send(mailFrom.encode())
- recv2 = clientSocket.recv(1024)
- recv2 = recv2.decode()
- print("após comando MAIL FROM: "+recv2)
- rcptTo = "RCPT TO:<" + email + ">\r\n"
- clientSocket.send(rcptTo.encode())
- recv3 = clientSocket.recv(1024)
- recv3 = recv3.decode()
- print("após comando RCPT TO: "+recv3)
- data = "DATA\r\n"
- clientSocket.send(data.encode())
- recv4 = clientSocket.recv(1024)
- recv4 = recv4.decode()
- print("após comando DATA: "+recv4)
- subject = "Subject: Alerta de Luminosidade\r\n\r\n"
- clientSocket.send(subject.encode())
- date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
- date = date + "\r\n\r\n"
- clientSocket.send(date.encode())
- clientSocket.send(msg.encode())
- clientSocket.send(endmsg.encode())
- recv_msg = clientSocket.recv(1024)
- print("resposta após enviar a mensagem:" + recv_msg.decode())
- quit = "QUIT\r\n"
- clientSocket.send(quit.encode())
- recv5 = clientSocket.recv(1024)
- print(recv5.decode())
- clientSocket.close()
- def things_speak(num):
- myMqttClient = bytes("client_" + str(num), 'utf-8')
- thingspeakUrl = b"mqtt.thingspeak.com"
- thingspeakUserId = b"pedroyup"
- thingspeakMqttApiKey = b"OOEM5ZBDVWIIX2HY"
- client = MQTTClient(client_id=myMqttClient,
- server=thingspeakUrl,
- user=thingspeakUserId,
- password=thingspeakMqttApiKey,
- ssl=False)
- client.connect()
- thingspeakChannelId = b"552426"
- thingspeakChannelWriteApiKey = b"P3VQHJPV5BC920TD"
- credentials = bytes("channels/{:s}/publish/{:s}".format(thingspeakChannelId, thingspeakChannelWriteApiKey), 'utf-8')
- payload = bytes("field1={:.1f}\n".format(num), 'utf-8')
- print(payload)
- client.publish(credentials, payload)
- client.disconnect()
- adc = machine.ADC(0)
- do_connect()
- addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
- s = socket.socket()
- s.bind(addr)
- s.listen(1)
- print('listening on', addr)
- while True:
- cl, addr = s.accept()
- cl_file = cl.makefile('rwb', 0)
- while True:
- line = cl_file.readline()
- print(line)
- print(str(line))
- print(type(str(line)))
- if(b'?valor=' in line):
- linha = str(line)
- t = linha.split('=')
- limite = t[1].split('&')[0]
- print("valor limite encontrado = " + limite)
- if(b'&email=' in line):
- linha = str(line)
- t = linha.split('=')
- email = t[2].split('H')[0]
- email = email.split('%40')
- email = email[0] + "@" + email[1]
- print("email encontrado = " + email)
- leitor(limite, email)
- if not line or line == b'\r\n':
- break
- cl.send(html)
- cl.close()
Add Comment
Please, Sign In to add comment