Last active
August 29, 2015 14:24
-
-
Save masato/0b6d93c8d863cc56b036 to your computer and use it in GitHub Desktop.
BME280のMQTT publishサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import paho.mqtt.client as mqtt | |
from time import sleep | |
import json | |
import sys | |
import bme280 | |
from config import conf | |
def sensing(): | |
return bme280.readData() | |
def on_connect(client, userdata, rc): | |
print("Connected with result code {}".format(rc)) | |
def on_publish(client, userdata, mid): | |
print("publish: {}".format(mid)) | |
def main(): | |
client = mqtt.Client(client_id='', | |
clean_session=True, protocol=mqtt.MQTTv311) | |
client.username_pw_set(conf["TRIGGER_UUID"], conf["TRIGGER_TOKEN"]) | |
client.on_connect = on_connect | |
client.on_publish = on_publish | |
client.connect(conf["IDCF_CHANNEL_URL"], 1883, 60) | |
while True: | |
retval = sensing() | |
if retval: | |
message = json.dumps({"devices": | |
[conf["ACTION_UUID"], | |
conf["FREEBOARD_UUID"]], | |
"payload": retval}) | |
print(message) | |
client.publish("message",message) | |
sleep(5) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment