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/", 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/", 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/", 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/", 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/", 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 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)

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:

### 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

# 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("", 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

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

# Configure connect/disconnect timeout to be 10 seconds

# Configure MQTT operation timeout to be 5 seconds
# Connect to AWS IoT with default keepalive set to 600 seconds

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

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

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