I am trying to run a Python script on startup.
I followed this guide from Instructibles on setting up a cron job.
It returns the following error in the logs:
File "IoTTest.py", line 58, in <module>
client.connect("Axxxxxxxxxx.iot.eu-west-1.amazonaws.com", xxxx, xx)
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 612, in connect
return self.reconnect()
File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 734, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
Basically, I want to send a message to AWS IoT using Python:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
if rc == 0 :
print("CONNECTED TO AWS IoT")
client.subscribe("$aws/things/XXXXX/shadow/update/accepted")
....
return True
else :
client.disconnect()
return False
def on_message(client, userdata, msg):
....
client = mqtt.Client(client_id="Test")
client.on_connect = on_connect
client.on_message = on_message
client.tls_set(ca_certs='./certs/root-CA.crt', certfile='./certs/xxxx-certificate.pem.crt', keyfile='./certs/xxxx-private.pem.key', tls_version=ssl.PROTOCOL_SSLv23)
client.tls_insecure_set(True)
client.connect("Axxxxxxxxxx.iot.eu-west-1.amazonaws.com", xxxx, xx) ===> log showed error here !
client.loop_forever()
Note: I can run this python script successfully with direct command:
python IoTTest.py
Any suggestion is appreciated.