i have this script on raspbian
and sometimes it fails and stops
could you help me to understand what's the problem?
here is the log of the script:
and sometimes it fails and stops
could you help me to understand what's the problem?
here is the log of the script:
Error:
Traceback (most recent call last):
File "Documents/PY/cancelloaperto.py", line 46, in <module>
client.loop_forever()
File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 1378, in loop_forever
rc = self.loop(timeout, max_packets)
File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 897, in loop
rc = self.loop_read(max_packets)
File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 1177, in loop_read
rc = self._packet_read()
File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 1766, in _packet_read
rc = self._packet_handle()
File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 2239, in _packet_handle
return self._handle_publish()
File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 2414, in _handle_publish
self._handle_on_message(message)
File "/usr/local/lib/python3.4/dist-packages/paho/mqtt/client.py", line 2574, in _handle_on_message
self.on_message(self, self._userdata, message)
File "Documents/PY/cancelloaperto.py", line 37, in on_message
inviamail("Cancello Aperto", "[email protected]", "Appena aperto il cancello\nalle ore " + tempo)
File "Documents/PY/cancelloaperto.py", line 17, in inviamail
server = smtplib.SMTP('smtp.gmail.com:587')
File "/usr/lib/python3.4/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.4/smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.4/smtplib.py", line 292, in _get_socket
self.source_address)
File "/usr/lib/python3.4/socket.py", line 491, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 530, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
here's my code (i changed email details)#!/usr/bin/env python3 import os import paho.mqtt.publish as publish import paho.mqtt.client as mqtt import time import smtplib import requests from datetime import datetime def inviamail(sogg, dest, testo): fromaddr = '[email protected]' toaddrs = dest msg = testo sub = sogg username = '[email protected]' password = 'xxxxxx' server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() server.login(username,password) message = 'Subject: {}\n\n{}'.format(sub, msg) server.sendmail(fromaddr, toaddrs, message) server.quit() return while True: def on_connect(client, userdata, flags, rc): #print("Connected with result code "+str(rc)) client.subscribe("mytopic") def on_message(client, userdata, msg): messaggio = str(msg.payload) if "APERTO" in messaggio: t = datetime.now().time() tempo = str(t)[0:8] print(tempo+" - ACCESSO") inviamail("Cancello Aperto", "[email protected]", "Appena aperto il cancello\$ client.disconnect() client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect("127.0.0.1",1883,60) client.loop_forever()