class ActuatorEmulator(Thread): ''' This class is used to send signal to actuator every 60 seconds and subscribe to actuator signal from cloud ''' def __init__(self): ''' Constructor ''' Thread.__init__(self) self.temp_topic = "/v1.6/devices/smart_home/temp-act/lv" self.led = False self.message = None self.brightness = 0 self.actuator = ActuatorData() self.set = Settings() self.sensor = SensorData() self.temp_mqtt = MqttClientConnector(self.temp_topic) self.temp_mqtt.subscribe(self.temp_topic) ''' This thread is used to collect values from cloud using mqtt Based on given conditions, will send signal to actuator through sensehat Thread runs once every 60 seconds ''' def run(self): while True: print("Temperature Actuator Value: " + str(self.temp_mqtt.message())) if self.temp_mqtt.message( ) == 30 or self.sensor.temp_avg < self.sensor.temp_set: self.led = self.actuator.setHeater() elif self.sensor.temp_avg > self.sensor.temp_set or self.temp_mqtt.message( ) == 16: self.led = self.actuator.setCooler() else: None if self.sensor.humid_avg < self.sensor.humid_set: self.led = self.actuator.setHumid() else: self.led = self.actuator.resetHumid() self.brightness = self.actuator.setlightBrightness(0) if self.set.status == 1: self.actuator.setMessage(self.message) sleep(60)
Construct objects ''' connector = MqttClientConnector(topic) connector.daemon = True print("Starting system performance application daemon thread...") ### Create an object for sensor data ## call the function ## PAss JSon data into the function connector.subscribe(host) time.sleep(3) print("Ready for receiving data") msg =connector.message() logging.debug('JSon Data Received:') print("Json Data Received:\n "+str(msg)+"\n") ''' Run thread ''' # # brokers_out={"broker2":"test.mosquitto.org"} # print(brokers_out) # print("brokers_out is a ",type(brokers_out)) # print("broker 1 address = ",brokers_out["broker2"]) # data_out=json.dumps(brokers_out)# encode object to JSON # print("\nConverting to JSON\n") # print ("data -type ",type(data_out))
Created on Mar 5, 2019 Simple Python script for MQTT Subscriber Client @author: Shyama Sastha Krishnamoorthy Srinivasan ''' import logging from labs.common import ConfigConst from labs.common.ConfigUtil import ConfigUtil from labs.common.DataUtil import DataUtil from labs.module06.MqttClientConnector import MqttClientConnector #Setting values for Topic and address for MQTT broker topic = "Temperature Sensor" config = ConfigUtil('../../../config/ConnectedDevicesConfig.props') host = config.getProperty(ConfigConst.MQTT_GATEWAY_SECTION, ConfigConst.HOST_KEY) subscribe = MqttClientConnector(topic) subscribe.subscribe(host) # Connecting to MQTT Broker msg = subscribe.message() # Subscribing to required topic logging.debug('\nJSon Received: ') print("\nReceived Json data: \n" + str(msg)) data = DataUtil() sensorData = data.jsonTosensor(msg) # Converting Jsondata to Sensordata logging.debug('\nJson in "SensorData format": ') print("\nMessage received in 'SensorData format': \n" + str(sensorData) + "\n") jsondata = data.sensorTojson(sensorData) # Converting Jsondata to Sensordata logging.debug('\nBack to Json: ') print("\nMessage converted back to Json: \n" + str(jsondata) + "\n")
from labs.common.DataUtil import DataUtil from labs.common.ConfigUtil import ConfigUtil from labs.module06.MqttClientConnector import MqttClientConnector ''' Setting the values for Topic and address for the MQTT broker ''' topic = "Temperature Sensor" config = ConfigUtil('../../../config/ConnectedDevicesConfig.props') host = config.getProperty(ConfigConst.ConfigConst.MQTT_CLOUD_SECTION, ConfigConst.ConfigConst.HOST_KEY) ''' Connecting to the MQTT Broker and Subscribing to a specified topic ''' subscribe = MqttClientConnector(topic) subscribe.subscribe(host) msg = subscribe.message() logging.debug('JSON Data Received: ') print("Json Data Received: " + "\n" + str(msg) + "\n") ''' Converting the JSON data into Sensor Data ''' data = DataUtil() sensor = data.jsonToSensorData(msg) logging.debug('Json data converted into SensorData') print("Received SensorData format message " + str(sensor)) ''' Converting the Sensor data to JSON data again ''' json = data.SensorDataToJson(sensor) logging.debug('SensorData converted into Json Data: ') print('Again the Sensor Data is converted into Json Data : \n' + str(json))
@author: GANESHRAM KANAKASABAI ''' from labs.common import ConfigConst from labs.common.ConfigUtil import ConfigUtil from labs.module06.MqttClientConnector import MqttClientConnector from labs.common.DataUtil import DataUtil import logging ''' Setting values for Topic and address for MQTT broker ''' topic = "Temperature Sensor" config = ConfigUtil('../../../config/ConnectedDevicesConfig.props') host = config.getProperty(ConfigConst.ConfigConst.MQTT_CLOUD_SECTION, ConfigConst.ConfigConst.HOST_KEY) subscribe = MqttClientConnector(topic) subscribe.subscribe(host) # Connecting to MQTT Broker msg = subscribe.message() # Subscribing to specefied Topic logging.debug('JSon Data Received:') print("Json Data Received:\n " + str(msg) + "\n") data = DataUtil() sensor = data.toSensorfromJson(msg) # Converting Json data to Sensor Data logging.debug('Json data converted into SensorData') print("Received message in SensorData format" + str(sensor)) json = data.toJsonfromSensor(sensor) logging.debug('SensorData converted into Json Data: ') print('SensorData converted to Json Data again: \n' + str(json))