micropython / micropython/micropython-lib

cannot connect to aws iot using umqtt.simple

Open
#915 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.9k
Forks
1.1k
Avg merge
7d 6h
Merged PRs (30d)
3

Description

My code

import time
import machine
import network
import ujson
from simple1 import MQTTClient

SSID = b'Exxxye'
PASS = b'8xxx766'
CLIENT_ID = b'ixxxxxxxxxxxx6eb63'
AWS_ENDPOINT = b'xxxxx-1.amazonaws.com'

PUB_TOPIC = b'temperature'
SUB_TOPIC = b'temperature'
with open('/pri.der', 'rb') as f:
    DEV_KEY = f.read()

with open('/cert.der', 'rb') as f:
    DEV_CRT = f.read()

light = machine.Pin("LED", machine.Pin.OUT)
light.off()


def wifi_connect():
    print('Connecting to wifi...')
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(SSID, PASS)
    while wlan.isconnected() == False:
        light.on()
        print('Waiting for connection...')
        time.sleep(0.5)
        light.off()
        time.sleep(0.5)
    print('Connection details: %s' % str(wlan.ifconfig()))

def mqtt_subscribe_callback(topic, msg):
    print("Received topic: %s message: %s" % (topic, msg))
    if topic == SUB_TOPIC:
        mesg = ujson.loads(msg)
        if 'state' in mesg.keys():
            if mesg['state'] == 'on' or mesg['state'] == 'ON' or mesg['state'] == 'On':
                light.on()
                print('Light is ON')
            else:
                light.off()
                print('Light is OFF')

def get_rpi_temperature():
    sensor = machine.ADC(4)
    voltage = sensor.read_u16() * (3.3 / 65535)
    temperature = 27 - (voltage - 0.706) / 0.001721
    return temperature


wifi_connect()


mqtt = MQTTClient(
    client_id=CLIENT_ID,
    server=AWS_ENDPOINT,
    port=8883,
    keepalive=120,
    ssl=True,
    ssl_params={'key':DEV_KEY, 'cert':DEV_CRT, 'server_side':False})

mqtt.connect()

mqtt.set_callback(mqtt_subscribe_callback)


mqtt.subscribe(SUB_TOPIC)

while True:
message = b'{"temperature":%s, "temperature_unit":"Degrees Celsius"}' % get_rpi_temperature()
    print('Publishing topic %s message %s' % (PUB_TOPIC, message))

    mqtt.publish(topic=PUB_TOPIC, msg=message, qos=1)

    # Check subscriptions for message
    mqtt.check_msg()
    time.sleep(5)

My error code:

Connecting to wifi...
Connection details: ('192.168.x.xx', '255.255.255.0', '192.168.xx.xx', '192.168.xx.xx')
dbug-msg: 0x9 b'04:4d:51:54:54:04:02:00:78'
dbug-resp: b''
Traceback (most recent call last):
File "", line 81, in
File "simple1.py", line 92, in connect
IndexError: bytes index out of range

please someone help me on this..i tried everything

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with simple1.py at line 92 in connect and the debug output showing an empty response after the MQTT bytes are sent. Reproduce the connection attempt with the provided AWS IoT settings, then trace the response handling to identify the failure. Done means the cause is confirmed and a successful connection is demonstrated or the required setup is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
cloud, embedded-iot, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.