I am trying to receive data using mosquitto and save it as csv file using python pandas. The data is continuos until I stop the script.
import paho.mqtt.client as mqtt import random import schedule import time mqttc = mqtt.Client("python_pub") mqttc.connect("localhost", 1883) def job(): mqttc.publish("hello/world", random.randint(1, 10)) schedule.every(1).seconds.do(job) while True: schedule.run_pending() time.sleep(1) mqttc.loop(2)
import paho.mqtt.client as mqtt import pandas as pd def on_connect(client, userdata, rc): print("Connected with result code "+str(rc)) client.subscribe("hello/world") def on_message(client, userdata, msg): datas = map(int, msg.payload) for num in datas: df = pd.DataFrame(data=datas, columns=['the_number']) df.to_csv("testing.csv") client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect("localhost", 1883, 60) client.loop_forever()
mqtt_sub.py script, I get
testing.csv that looks like this
| the _number 0 | 2
2 is the last digit that I receive before I stop the
Connected with result code 0       ... ... KeyboardInterrupt
I was hoping to get
testing.csv like this
| the_number 0 | 3 1 | 9 2 | 5 ... ... 5 | 2
To achieve that I try to change the following
df = pd.DataFrame(data=datas, columns=['the_number']) to
df = pd.DataFrame(data=num, columns=['the_number']) and the following error occured
pandas.core.common.PandasError: DataFrame constructor not properly called!
Do anyone have any idea how to solve the error? I also feel that I did not use the for loop properly in here.
Thank you for your suggestion and help.