5

This question is related to this one after you helped me to fix my mistake I have connected to Eclipse broker, it worked just fine, connection and publishing, then I switched to AWS IoT broker with this code

#!/user/bin/python3
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time
import ssl
import _thread

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

# Define Variables
MQTT_PORT = 8883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "ldr"
MQTT_MSG = "there is a product"
#MQTT_HOST = "iot.eclipse.org"

MQTT_HOST = "xxxxxxx"
THING_NAME = "LDRsensor"
CLIENT_ID ="LDRsensor"
CA_ROOT_CERT_FILE = "xxxxxxx"
THING_CERT_FILE = "xxxxxxxxxxxx"
THING_PRIVATE_KEY = "xxxxxxxxxxx"


# Define on_publish event function
def on_publish(client, userdata, mid):
    print ("Message Published...")

# Initiate MQTT Client
mqttc = mqtt.Client()

# Register publish callback function
mqttc.on_publish = on_publish


# Configure TLS Set
mqttc.tls_set(CA_ROOT_CERT_FILE, certfile=THING_CERT_FILE, keyfile=THING_PRIVATE_KEY, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)


# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print ("Connected Successfully")
#mqttc.loop_forever()


def publishMessage(Variable):

    while (1):
        input_value = GPIO.input(4)
        if input_value == 1:
            mqttc.publish(MQTT_TOPIC,MQTT_MSG,qos=1)
            #print ("message published")
            time.sleep(1)
_thread.start_new_thread(publishMessage,("publishMessage",))
mqttc.loop_forever()

But what I get is that, I can connect successfully but I can not publish the mesages. Is it because of forever loop or there is some thing else.I tried debug method, on line _thread.start_new_thread(publishMessage,("publishMessage",)) i got [Errno 32] Broken pipe what is that mean and how i can fix it?

should i install AWSIoTPythonSDK?

4
  • Well, I suspect that THING_NAME name is the MQTT client ID that should be defined for sure. Jan 21, 2018 at 16:06
  • i already try it but i am still facing the same issue @BenceKaulics Jan 21, 2018 at 16:10
  • I started a meta discussion to see what others think
    – Mawg
    Jan 22, 2018 at 7:51
  • Hey please share that ARN which you changed.
    – Suresh
    Nov 8, 2018 at 7:52

1 Answer 1

5

I finally figured out what my mistake is. It was in the ARN Resources of policy I wrote a wrong topic in the end of policy resources line. I wrote ldr instead of LDRsensor.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.