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.
mqtt_pub.py
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)
mqtt_sub.py
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()
from above 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 mqtt_sub.py
script
Connected with result code 0
[3]
[9]
[5]
[3]
[7]
[2]
...
...
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.
[UPDATE]
I add/change the following line in on_message
method
def on_message(client, userdata, msg):
datas = map(int, msg.payload)
df = pd.DataFrame(data=datas, columns=['the_number'])
f = open("test.csv", 'a')
df.to_csv(f)
f.close()
With help from Nulljack, I am able to get the result like this in my CSV file
| the_number
0 | 3
| the_number
0 | 9
| the_number
0 | 5
| the_number
0 | 3
| the_number
0 | 7
My goal is to achieve something like this in the CSV file
| the_number
0 | 3
1 | 9
2 | 5
3 | 3
4 | 7