Raspberry Pi or other platform can connect to the mqtt client,publisher and subscriber can access to bidirectional communication by switching their identities.
Example:you can get temperature of the enviroment collected by Arduino using Raspberry Pi when Raspberry Pi and Arduino communicate with each other.
The actions' file must be /home/pi/.wukong/action.json
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import time
import json
import os
from robot import config, logging
from robot.sdk.AbstractPlugin import AbstractPlugin
logger = logging.getLogger(name)
class Plugin(AbstractPlugin):
SLUG = "mqttPub"
def search_word(self, text):
home_dir = os.path.expandvars('$HOME')
location = home_dir + '/.wukong/action.json'
if os.path.exists(location):
f = open(location).read()
try:
fjson = json.loads(f)
for key in fjson.keys():
value = fjson[key]
if isinstance(value,list): # 向上兼容
for word in value:
if word in text:
return key,word
if isinstance(value,dict):
for word in value.keys():
if word in text:
return key,value[word]
except Exception as e:
logger.error(e)
self.say("抱歉出了问题", cache=True)
return
else:
return
def handle(self, text, parsed):
profile = config.get()
#get config
if ( self.SLUG not in profile ) or ( 'host' not in profile[self.SLUG] ) or ( 'topic_s' not in profile[self.SLUG] ) or ( 'username' not in profile[self.SLUG] ) or ( 'password' not in profile[self.SLUG] ):
self.say("主人,配置有误", cache=True)
return
host = profile[self.SLUG]['host']
port = 1883
if ( 'port' in profile[self.SLUG] ):
port = int(profile[self.SLUG]['port'])
topic_s = profile[self.SLUG]['topic_s']
username = profile[self.SLUG]['username']
password = profile[self.SLUG]['password']
# text = text.split(",")[0] #百度语音识别返回的数据中有个中文,
topic_p,payload = self.search_word(text)
try:
self.say("已经接收到指令", cache=True)
mqtt_contro(host,port,username,password,topic_s,topic_p,payload,self.con)
except Exception as e:
logger.error(e)
self.say("抱歉出了问题", cache=True)
return
def isValid(self, text, parsed):
if self.search_word(text) == None:
return False
else:
return True
添加了 `# -*- coding: utf-8
author: chenzhuo
Raspberry Pi or other platform can connect to the mqtt client,publisher and subscriber can access to bidirectional communication by switching their identities.
Example:you can get temperature of the enviroment collected by Arduino using Raspberry Pi when Raspberry Pi and Arduino communicate with each other.
The actions' file must be /home/pi/.wukong/action.json
Fix: Hcreak 2019.10
Fix: imliubo 2020.04 NodeMCU code reference: https://wukong.hahack.com/#/contrib?id=controlmqtt
import paho.mqtt.client as mqtt import paho.mqtt.publish as publish import time import json import os from robot import config, logging from robot.sdk.AbstractPlugin import AbstractPlugin
logger = logging.getLogger(name)
class Plugin(AbstractPlugin):
class mqtt_contro(object):
`