AWS Iot Core Integration

Problem to intregrate Balena to AWS IOT Core, any onde can help to solve this generic traceback?

  13.05.20 19:20:11 (-0300)  main  Traceback (most recent call last):
13.05.20 19:20:11 (-0300)  main    File "src/main.py", line 67, in <module>
13.05.20 19:20:11 (-0300)  main      mqtt_client.connect()
13.05.20 19:20:11 (-0300)  main    File "/usr/local/lib/python3.6/site-packages/AWSIoTPythonSDK/MQTTLib.py", line 355, in connect
13.05.20 19:20:11 (-0300)  main      return self._mqttCore.connect(keepAliveIntervalSecond)
13.05.20 19:20:11 (-0300)  main    File "/usr/local/lib/python3.6/site-packages/AWSIoTPythonSDK/core/protocol/mqttCore.py", line 282, in connect
13.05.20 19:20:11 (-0300)  main      self._pahoClient.connect(self._host, self._port, keepAliveInterval)  # Throw exception...
13.05.20 19:20:11 (-0300)  main    File "/usr/local/lib/python3.6/site-packages/AWSIoTPythonSDK/core/protocol/paho/client.py", line 655, in connect
13.05.20 19:20:11 (-0300)  main      return self.reconnect()
13.05.20 19:20:11 (-0300)  main    File "/usr/local/lib/python3.6/site-packages/AWSIoTPythonSDK/core/protocol/paho/client.py", line 807, in reconnect
13.05.20 19:20:11 (-0300)  main      self._sock.setblocking(0)
13.05.20 19:20:11 (-0300)  main  OSError: [Errno 9] Bad file descriptor

I’m using this tutorial https://www.balena.io/blog/use-a-raspberry-pi-to-communicate-with-amazon-aws-iot/#addenvironmentvariables as guide.

Hi ,
can this be due to adaptions you have made or are you still testing the basic setup ? In other words - have you managed to successfully run the test described in the ‘Test It’ section of the document ?
Cheers Thomas

Hi Samonthx,

I’m try tu run basic test.

   ### Functions

def payload_report(self, params, packet):
    print("----- New Payload -----")
    print("Topic: ", packet.topic)
    print("Message: ", packet.payload)
    print("-----------------------")

def set_cred(env_name, file_name):
    #Turn base64 encoded environmental variable into a certificate file
    env = os.getenv(env_name)
    with open(file_name, "wb") as output_file:
        output_file.write(base64.b64decode(env))

### MQTT Setup
certRootPath = '/usr/src/app/'
print("MQTT Thing Starting...")

aws_endpoint = os.getenv("AWS_ENDPOINT")
aws_port = os.getenv("AWS_PORT", 8883)
device_uuid = os.getenv("BALENA_DEVICE_UUID")

# Save credential files
set_cred("AWS_ROOT_CERT","root-CA.crt")
set_cred("AWS_THING_CERT","thing.cert.pem")
set_cred("AWS_PRIVATE_CERT","thing.private.key")

# Unique ID. If another connection using the same key is opened the previous one is auto closed by AWS IOT
mqtt_client = AWSIoTMQTTClient(device_uuid) 

#Used to configure the host name and port number the underneath AWS IoT MQTT Client tries to connect to.
mqtt_client.configureEndpoint("a18vq9nw7exoan-ats.iot.sa-east-1.amazonaws.com", aws_port)

# Used to configure the rootCA, private key and certificate files. configureCredentials(CAFilePath, KeyPath='', CertificatePath='')
mqtt_client.configureCredentials(certRootPath+"root-CA.crt", certRootPath+"thing.private.key", certRootPath+"thing.cert.pem")

# Configure the offline queue for publish requests to be 20 in size and drop the oldest
mqtt_client.configureOfflinePublishQueueing(-1)

# Used to configure the draining speed to clear up the queued requests when the connection is back. (frequencyInHz)
mqtt_client.configureDrainingFrequency(2) 

# Configure connect/disconnect timeout to be 10 seconds
mqtt_client.configureConnectDisconnectTimeout(10)

# Configure MQTT operation timeout to be 5 seconds
mqtt_client.configureMQTTOperationTimeout(5)
 
# Connect to AWS IoT with default keepalive set to 600 seconds
mqtt_client.connect()

# Subscribe to the desired topic and register a callback.
mqtt_client.subscribe("balena/payload_test", 1, payload_report)

i=0
while True:
    i=i+1
    print('Publishing to "balena/payload_write_test" the value: ', i)
    mqtt_client.publish("balena/payload_write_test", i, 0)
    sleep(5)

I have forwarded this to the maintainers to look into.

Thanks Samonthx,

It’s works when i update AWSIoTPythonSDK from 1.0.0 to 1.4.8

Problem solved.

Hey - thanks for letting us know what fixed the problem for you. Oddly several of our Hardware Hackers team tried this example again in the past week, and didn’t run into the same problem. But nevertheless, I have suggested they update that SDK any way.

Thanks, Phil

1 Like