def tb_client_start(telemetries, token): logging.basicConfig( filename=token + '_app.log', filemode='w', format='%(asctime)s : %(levelname)s : %(name)s : %(message)s', level=logging.INFO) client = TBDeviceMqttClient(mqtt_host, token) client.max_inflight_messages_set(50) client.connect() # logging.info("connect device: ", token) for telemetry in telemetries: logging.info("send telemetry: {0} for device with token: {1}".format( telemetries.index(telemetry), token)) client.send_telemetry(telemetry) time.sleep(delay) # Sending telemetry and checking the delivery status (QoS = 1 by default) result = client.send_telemetry(telemetry) logging.debug( "results of client.send_telemetry(telemetry): {}".format(result)) # get is a blocking call that awaits delivery status # success = result.get() == TBPublishInfo.TB_ERR_SUCCESS # if (success): # # print("Success") # logging.info("message sent successfully:{0} ".format(success)) # else: # logging.error("MQTT Failure: {0}".format(result)) # Disconnect from ThingsBoard client.disconnect() # logging.info("all thread: ", threading.enumerate()) global all_done all_done = True
def prosesACC(): print('ACC\n') serialACC = serial.Serial('COM19', 9600) clientthingsboard = TBDeviceMqttClient("202.46.7.33", "TEWSSiporaAccelero") clientthingsboard.max_inflight_messages_set(50) clientthingsboard.connect() while True: dataInput = serialACC.readline()[:-2] data = dataInput.decode() print(data) if data: dataacc = parsingACC(data) telemetry_with_ts = { "ts": int(round(time.time() * 1000)), "values": { "x": float(dataacc[0]), "y": float(dataacc[1]), "z": float(dataacc[2]) } } clientthingsboard.send_telemetry(telemetry_with_ts) lock.acquire() xDataQueue.put(dataacc[0]) yDataQueue.put(dataacc[1]) zDataQueue.put(dataacc[2]) timestampDataQueue.put(int(round(time.time() * 1000))) lock.release() writelogdataaccelerometer(dataacc)
def main(): client = TBDeviceMqttClient("127.0.0.1", "A2_TEST_TOKEN") # we set maximum amount of messages sent to send them at the same time. it may stress memory but increases performance client.max_inflight_messages_set(100) client.connect() results = [] result = True for i in range(0, 100): results.append(client.send_telemetry(telemetry_with_ts)) for tmp_result in results: result &= tmp_result.get() == TBPublishInfo.TB_ERR_SUCCESS print("Result " + str(result)) client.stop()
def prosesBPR(): print('BPR\n') global pressurebuffer global serialBPR clientthingsboard = TBDeviceMqttClient("202.46.7.33", "TEWSSiporaBPR") clientthingsboard.max_inflight_messages_set(10) clientthingsboard.connect() while True: dataInput = serialBPR.readline()[:-2] data=dataInput.decode() print(data) if data: databpr = parsingBPR(data) #send to thingsboard telemetry_with_ts = {"ts": int(round(time.time() * 1000)), "values": {"pressure": float(pressuredata), "temperature": float(temperaturedata)}} clientthingsboard.send_telemetry(telemetry_with_ts) lock.acquire() pressureDataQueue.put(databpr[1]) temperatureDataQueue.put(databpr[2]) timestampDataQueue.put(int(round(time.time() * 1000))) lock.release() pressurebuffer = databpr[1] writelogdataBPR(databpr)
from tb_device_mqtt import TBDeviceMqttClient, TBPublishInfo import time logging.basicConfig(level=logging.DEBUG) telemetry_with_ts = { "ts": int(round(time.time() * 1000)), "values": { "temperature": 42.1, "humidity": 70 } } client = TBDeviceMqttClient("127.0.0.1", "A2_TEST_TOKEN") # we set maximum amount of messages sent to send them at the same time. it may stress memory but increases performance client.max_inflight_messages_set(100) client.connect() results = [] result = True for i in range(0, 100): results.append(client.send_telemetry(telemetry_with_ts)) for tmp_result in results: result &= tmp_result.get() == TBPublishInfo.TB_ERR_SUCCESS print("Result " + str(result)) client.disconnect()