Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Before running this script, you must have paso-mqtt installed. Use pip install paho-mqtt
- # This script was tested with python version 3.7
- import paho.mqtt.client as paho
- import json
- import time
- import datetime
- import sys
- import random
- # MQTT device event topic string format
- # iot-2/type/device_type/id/device_id/evt/event_id/fmt/format_string
- # MQTT device event topic string we will be publishing to
- topic = "iot-2/type/temperaturesensor/id/sensor1/evt/temperature/fmt/json"
- # device credentials
- # replace these with credentials to your own instance of IOT platform
- appname =
- iotorg =
- username =
- password =
- # Note that all client ids must be unique
- client_id = 'a:'+iotorg+':'+appname
- # ------------- #
- # MQTT settings #
- # ------------- #
- # create the MQTT client
- client = paho.Client(client_id=client_id, protocol=paho.MQTTv311)
- # client connection
- # MQTT server credentials
- client.username_pw_set(username, password)
- # Connect at the MQTT server address
- try:
- client.connect(iotorg + ".messaging.internetofthings.ibmcloud.com")
- except:
- print('could not connect')
- #send out this message forever, at intervals of 10 seconds
- while True:
- time.sleep(10)
- print("sending event to Watson IOT platform")
- # We wish to publish the following JSON message
- temp = random.randint(0,90)
- event_json = json.dumps({"d":{"deviceid":"sensor1","temperature":temp}})
- print(event_json)
- client.publish(topic,event_json)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement