''' Creating the Sensor Data ''' sensor = SensorData(topic,0,30) sensor.curValue = uniform(float(sensor.getMinValue()), float(sensor.getMaxValue())); sensor.addValue(sensor.curValue); sensor.diffValue = sensor.curValue - sensor.avgVal; sensor.timestamp = datetime.now(); logging.info('SensorData to be sent:') print("Sensor data before converting to Json: " + str(sensor)); ''' Converting the Sensor Data to the JSON format ''' data = DataUtil() jsonData = data.SensorDataToJson(sensor) logging.info('SensorData converted into Json:') print("SensorData in Json Format before publishing" + "\n" + str(jsonData) + "\n") pub_client = MqttClientConnector(); ''' This Function is used to publish the JSON data to the MQTT broker through the MQTT ClientConnector class @param topic: Topic of the message @param jsonData: It is the JSON Payload @param host: It is the address of the MQTT broker ''' pub_client.publish(topic,jsonData,host)
config = ConfigUtil('../../../config/ConnectedDevicesConfig.props') config.loadConfig() data = DataUtil() host = config.getProperty(ConfigConst.ConfigConst.COAP_DEVICE_SECTION, ConfigConst.ConfigConst.HOST_KEY) port = int(config.getProperty(ConfigConst.ConfigConst.COAP_DEVICE_SECTION, ConfigConst.ConfigConst.PORT_KEY)) path = 'temperature' sensor = SensorData("Temperature",0.0,30.0) sensor.addValue(4.0) print(str(sensor)) coapClient = CoapClientConnector(host, port, path) coapClient.ping() json_data = data.SensorDataToJson(sensor) print("json"+json_data) #coapClient.get() #Get will respond with NOT_FOUND since data object on server is not initialized coapClient.post((json_data)) #Post JSON to server coapClient.get() sensor.addValue(5.00) coapClient.put(data.SensorDataToJson(sensor)) #Update resource on the server coapClient.get() coapClient.delete() #Delete resource coapClient.get() coapClient.stop()