Initialize a MQTT client, connect to the MQTT broker on a specific topic, for each message received on that topic execute a function call to check if the length of an attribute in message is greater than 200, if yes then find the Gyro meter reading: GyroX & save to file ensuring that the file size is kept less than say 400MB if not then open a new file and start writing to it.
Referring post : https://stackoverflow.com/a/54306598/12097046
I want to write to multiple files i.e. post incoming json messages to different files based on size instead of just one file. how to do it ? any help is appreciated
file_name='/tmp/gyro_256'+"_"+timestr+".csv"
def on_message(client, userdata, message):
y = json.loads(message.payload)
v = (len(y['sec_data']))
p = int(v)
if p >= 200:
d = (y["sec_data"][10]["GyroX"])
with open(file_name,'a+') as f:
f.write(d + "\n")
client = mqttClient.Client("123") #create new instance
client.username_pw_set(user, password=password) #set username and
client.on_connect= on_connect #attach function to
client.on_message= on_message #attach function to
client.connect(broker_address,port,100) #connect
client.subscribe("tes1") #subscribe
client.loop_start() #then keep listening forever
if int(os.path.getsize(file_name)) > 47216840 :
client.loop_stop()
timestr = time.strftime("%Y%m%d%H%M%S")
file_name = '/vol/vol_HDB/data/gyro_256'+"_"+timestr+".csv"
client.loop_start()