-2

I am storing some data like temperature,humidity and Intensiy in array which is my arduino output and input for my python2.7 and I am plotting graphs from that data. I want to store the arduino output into text file also but i am unable to do it as i am new to python.

This is my python code

import serial
import numpy as np
import matplotlib.pyplot as plt
from drawnow import *
l=[]
t = []
h = []
arduinoData = serial.Serial('com3',115200)
plt.ion()
count=0
def makeFig():
    ax1 = plt.subplot(211)
    plt.ylim(0,100)
    plt.title('Temperature and Humidity')
    plt.grid(True)
    plt.ylabel('temp in C')
    plt.plot(t, 'ro-', label='Drgrees Celsius')
    plt.legend(loc='upper left')
    plt2=plt.twinx()
    plt.ylim(0,100)
    plt2.plot(h,'b^-',label='Humidity in %')
    plt2.legend(loc='upper right')
    ax2 = plt.subplot(212, sharex=ax1)
    plt.ylim(0,100)
    plt.grid(True)
    plt.ylabel('Intensity in Lux')
    plt.plot(l, 'ro-', label='Lux')
    plt.legend(loc='upper left')
while True:
    while (arduinoData.inWaiting()==0):
        pass
    arduinoString = arduinoData.readline()
    dataArray = arduinoString.split(',')
    lux = float (dataArray[0] )
    humd = float ( dataArray[1])
    temp = float ( dataArray[2])
    t.append(temp)
    h.append(humd)
    l.append(lux)
    drawnow(makeFig)
    plt.pause(.000001)
    count=count+1
    if(count>60):
        t.pop(0)
        h.pop(0)
        l.pop(0)

I want to store t , h , l in text file as an output Help will be appreciated....

2 Answers2

1

Open a file simply using this command :

f = open('file.txt', 'w')

and then you can write to it using :

f.write("Value of t : {}".format(t))

finally close the file with

f.close()
0

By print, do you mean output to a text file? If so, you can add:

np.savetxt('filename.txt', np.r_[t,h,l])

[filename.txt can be whatever name you want the text file to be unless you have one]

np.r would concatenate the matrices. I am not sure if this is what you intend to do but this is an option

WIT
  • 1,043
  • 2
  • 15
  • 32