-3

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()
vbhatt
  • 48
  • 4
  • You've not explained what is not working with the code you've posted. – hardillb Sep 20 '19 at 19:33
  • You also need to fix the indentation of that code. – hardillb Sep 20 '19 at 19:34
  • @hardillb sorry about the indentation.. will try to correct... – vbhatt Sep 21 '19 at 04:48
  • @hardillb I want to know if its possible to switch the file name like below by breaking the loop, above code does not work... 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() – vbhatt Sep 21 '19 at 04:49
  • Edit the question to add detail – hardillb Sep 21 '19 at 07:03

1 Answers1

1

None of the code after the fist call to client.loop_start() will ever be run because that call blocks forever.

If you want to change the file name you will have to do the file size test in the on_message callback.

def on_message(client, userdata, message):
  global filename
  y = json.loads(message.payload)
  v = (len(y['sec_data']))
  p = int(v)

  if int(os.path.getsize(file_name)) > 47216840 :
     timestr = time.strftime("%Y%m%d%H%M%S")
     file_name = '/vol/vol_HDB/data/gyro_256'+"_"+timestr+".csv"

  if p >= 200:
    d = (y["sec_data"][10]["GyroX"])
    with open(file_name,'a+') as f:
      f.write(d + "\n")
hardillb
  • 54,545
  • 11
  • 67
  • 105