샘플 앱에서 Quickstart로 publish
Watson IoT Platform 사용하기 에서 IoT 앱을 준비했습니다.
샘플 앱에서 Quickstart로 publish
python pub.py
에서 실행 pub.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import paho.mqtt.client as mqtt
import random
import sys
MQTT_BROKER="quickstart.messaging.internetofthings.ibmcloud.com"
MQTT_PORT=1883
MQTT_TIMEOUT=60
MQTT_PUB_TOPIC="iot-2/evt/status/fmt/json"
latTuple = (36.065269, 36.045284, 36.058608, 36.178417, 36.305790, 36.293616, 36.238253, 36.172874, 35.976411, 35.878552, 35.987524)
longTuple = (-112.123288, -112.263364, -112.428159, -112.511929, -112.399319, -112.216672, -112.051877, -111.850003, -111.703061, -111.856870, -112.128781)
def on_connect(client, userdata, flags, rc):
print("on connect")
def on_disconnect(client, userdata, rc):
print("on disconnect")
client.unsubscribe(MQTT_SUB_TOPIC)
def on_message(client, userdata, msg):
print("on message: " + msg.topic + " " + str(msg.payload))
def mqttConnect():
print("mqttConnect")
try:
global client
client = mqtt.Client("d:quickstart:myThing:001122334455")
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.connect(MQTT_BROKER,MQTT_PORT,MQTT_TIMEOUT)
print("connected")
except KeyboardInterrupt, kex:
sys.exit()
except Exception, ex:
print("mqttConnect error")
def mqttPublish():
while client.loop() == 0:
for lat in latTuple:
index = latTuple.index(lat)
long =longTuple[index]
temp = random.randrange(250,450)/10.0
humid = random.randrange(500,1000)/10.0
print "#" + str(index) + ", temp = " + str(temp) + ", humid = " + str(humid) + ", lat = " + str(lat) + ", long = " + str(long)
msg = " {\"d\": {\"temperature\": " + str(temp) +",\"humidity\": " + str(humid) +", \"latitude\": " + str(lat) +", \"longitude\": " + str(long) + "} }";
client.publish(MQTT_PUB_TOPIC, msg, 0, True)
try:
print raw_input("[Enter] to send another request")
except:
sys.exit()
def mqttPublishAuto():
while client.loop() == 0:
for lat in latTuple:
try:
index = latTuple.index(lat)
long =longTuple[index]
temp = random.randrange(250,450)/10.0
humid = random.randrange(500,1000)/10.0
print "#" + str(index) + ", temp = " + str(temp) + ", humid = " + str(humid) + ", lat = " + str(lat) + ", long = " + str(long)
msg = " {\"d\": {\"Temperature\": " + str(temp) +",\"Humidity\": " + str(humid) +", \"latitude\": " + str(lat) +", \"longitude\": " + str(long) + "} }";
client.publish(MQTT_PUB_TOPIC, msg, 0, True)
time.sleep(2)
except:
sys.exit()
if __name__ == "__main__":
print("--- started ---")
mqttConnect()
mqttPublishAuto()
print("--- program ended ---")
Device ID
에 상기의 001122334455
를 지정해 수신합니다. Reference
이 문제에 관하여(샘플 앱에서 Quickstart로 publish), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/egplnt/items/490d9eaf18c9fc95643d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)