def _init_mqtt_client( # *, SyntaxError on python3.5 host: str, port: int, username: typing.Optional[str], password: typing.Optional[str], disable_tls: bool, ) -> paho.mqtt.client.Client: # https://pypi.org/project/paho-mqtt/ client = paho.mqtt.client.Client() client.on_connect = _mqtt_on_connect if not disable_tls: client.tls_set( ca_certs=None) # enable tls trusting default system certs _LOGGER.info( "connecting to MQTT broker %s:%d (TLS %s)", host, port, "disabled" if disable_tls else "enabled", ) if username: client.username_pw_set(username=username, password=password) elif password: raise ValueError("Missing MQTT username") client.connect(host=host, port=port) return client
def main(): client = paho.mqtt.client.Client(client_id='[clientid]', clean_session=False) client.username_pw_set('[username]', '[password]') client.on_connect = on_connect client.on_message = on_message client.tls_set('/home/credd/ca-certificates.crt', tls_version=ssl.PROTOCOL_TLSv1_2) client.tls_insecure_set(True) client.connect('reddtech.us', 8883, 60) #client.loop_forever() client.loop_forever()
def main(): client = paho.mqtt.client.Client(client_id='[clientid]', clean_session=False) #client.username_pw_set('[username]', '[password]') client.on_connect = on_connect client.on_message = on_message # debian stretch client.tls_set('/etc/ssl/certs/DST_Root_CA_X3.pem', tls_version=ssl.PROTOCOL_TLSv1_2) # CentOS 7 # client.tls_set('/etc/ssl/certs/ca-bundle.trust.crt', tls_version=ssl.PROTOCOL_TLSv1_2) client.connect(host=host, port=8883) client.loop_forever()
def main(): #print('Attaching Debugger') # Allow other computers to attach to ptvsd at this IP address and port, using the secret #ptvsd.enable_attach("my_secret", address = ('0.0.0.0', 3000)) # Pause the program until a remote debugger is attached #ptvsd.wait_for_attach() print('Starting MQTT client') client = paho.mqtt.client.Client(client_id='platformio', clean_session=False) #with open('/run/secrets/mqtt_username') as username, open('/run/secrets/mqtt_password') as password: # client.username_pw_set(username.read().strip(), password.read().strip()) client.username_pw_set(os.environ['MQTT_USERNAME'],os.environ['MQTT_PASSWORD'] ) print('On_connect') client.on_connect = on_connect client.on_message = on_message print('TLS + Connect') with open('ca', 'w+') as ca: ca.write(os.environ['CA']) client.tls_set('ca', tls_version=ssl.PROTOCOL_TLSv1_2) client.connect(host='mqtt', port=8883) print('Finished setup, starting loop.') sys.stdout.flush() client.loop_forever()
"temp": temp }) # mqtt Publish client.publish(MQTT_TOPIC_PUB, json_msg) time.sleep(0.01) # Main Procedure if __name__ == '__main__': # Mqtt Client Initialize client = paho.mqtt.client.Client() client.on_connect = mqtt_connect client.on_message = mqtt_message client.tls_set(MQTT_ROOTCA, certfile=MQTT_CERT, keyfile=MQTT_PRIKEY, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) # Connect To Mqtt Broker(aws) client.connect(AWSIoT_ENDPOINT, port=MQTT_PORT, keepalive=60) # Start Mqtt Subscribe client.loop_start() # Start Publish Loop loop = asyncio.get_event_loop() loop.run_until_complete(pub_loop())
endpoint = "******.amazonaws.com" #AWSのエンドポイント port = 8883 #AWSのポート SUB_TOPIC = "AWS_IoT/#" rootCA = "root-CA.crt" #ルート証明書 cert = "aws-iot.cert.pem" #デバイス証明書 key = "aws-iot.private.key" #keyを設定 def on_connect(client, userdata, flags, respons_code): print("Connected") client.subscribe(SUB_TOPIC) #サブスクライブする def on_message(client, userdata, msg): print(msg) if __name__ == '__main__': client = paho.mqtt.client.Client() client.on_connect = on_connect client.on_message = on_message client.tls_set(ca_certs=rootCA, certfile=cert, keyfile=key, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) client.connect(endpoint, port=port, keepalive=60) print("StartScript") client.loop_forever()