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?
THING_NAME
name is the MQTT client ID that should be defined for sure.