nilyad65
Posts: 1
Joined: Fri May 03, 2019 9:24 am

trying to plot as well as manipulate sensor data in matrix (where each column is individual sensor datas)

Fri Aug 16, 2019 5:01 pm

I am working on a project, where I need to read real-time data of 8 sensors attached to Arduino using Raspberry Pi through a serial port(USB).

By using "ser.readline()" I put all the data inside a list. My point is.... I have to copy every 8 elements of the list in the matrix (100*8) in each iteration (100 iterations required to generate one matrix), and when it fills up all the data in matrix, it should be stored in some file (.csv or .txt) and again a new matrix of same (100*8) should be made and note that I have to manipulate data (taking the mean, standard deviation and etc) and update the graph also the graph should be like a single figure showing line-plot for each sensor.
Each matrix should be appended to the previous file. I have to collect this data for 1 hour.
About my code, I send (serial.write()) the pin numbers from raspberry pi to Arduino, it reads and give the value of the respective pin to the Raspberry Pi, RPi reads the data and store it in the list and but got stuck in the mapping of list elements in each row of matrix :roll: ....attaching my both codes. Let me know as early as possible. Please let me know if you don't understand my problem.

##############################################################################

Code: Select all


#python code
import serial
import time
import numpy as np
import matplotlib.pyplot as plt
from drawnow import*

ser = serial.Serial('COM15', 115200)

ser.close()
ser.open()
inputPin = [0, 1, 2, 3, 4, 5, 6, 7]
length = len(inputPin)
mata = np.zeros((100,8))
i = 0
# inputPin = 0
val1 = []
plt.ion()       #tells the matplot you want interactive mode to be on



ser.flushInput()


while True:
    try:
        while i < length:
            # read a single line from the serial interface
            ser.write(str(inputPin[i]).encode())
            lineBytes = ser.readline()                      # data received in " b'0:4.28\r\n' "
            # convert Bytes returned by the ser.readline() function to String
            line = lineBytes.decode('utf-8')
            """split the string that should have the following form: INPUTPIN:VALUE
            into an list"""
            data = line.split(":")
            #print ((data))
            # if the array has two elements, the line was correctly formated as INPUTPIN:VALUE
            if (len(data) == 2):
                pinNum = data[0]
                IP = float(pinNum)

                value = data[1]
                valueInt = float(value)


                val1.append(valueInt)
                #print (val1)


                for row in range(100):
                    if (len(val1) == 8):
                        mata[row,0]= np.mean(val1[::2])
                        mata[row,1]= val1[1]
                        mata[row,2]=val1[3]
                        mata[row,3]=val1[5]
                        mata[row,4]=val1[7]
                        mata[row,5]=val1[9]
                        mata[row,6]=val1[11]
                        mata[row,7]=val1[13]
                        print (mata)
                        val1 = []
                        #if (len(val1) == 14):
                        #time.sleep(21)

            print("Input pin {} has value {}".format(pinNum, value))
            i +=1
            if i >13:
                i = 0
    except KeyboardInterrupt:
        break  # stop the while loop
        
        

################################# Arduino code ###################################################

Code: Select all


# Arduino code
int analogInputPin;
float voltage;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(115200);
}

void loop() {
  // if any data on the serial port are available
  // read it and try to update the analogInputPin
  // based on the number that was read on the serial  
  if (Serial.available() > 0) {
    analogInputPin = Serial.parseInt();
  }

  // read the analog value:
  int analogInputPinValue = analogRead(analogInputPin);
  voltage=(5./1023.)*analogInputPinValue;

  // print the results to the serial port:
  // the output should have the following form: INPUTPIN:VALUE
  // followed by a newline character
  Serial.print(analogInputPin);
  Serial.print(":");
  Serial.print(voltage);
  Serial.println("");

  // wait 50 milliseconds before the next loop
  delay(100);
}


Return to “Python”