integer error while implementing sensors into my python app

Hello,
I am pretty new to this. I have created an app in android studio and some calc in python. Just started using arduino but hit a little road block and im hoping its something simple im messing up.

I am attempting to use data from my arduino (Bluno V2.0) and implement it into a calculator type program in place of user input. I am working on utilizing 2 different sensors (both Myoware sensors) for this. I have attached the serial read outs I get both in ardunio and in python.

From my understanding the data through the serial port is read as a string. My error occurs when I attempt to split that string into A0 and A1 and convert it into an integer to be able to use in my project.

Here is my Arduino Code:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0,1:
int sensorValue0 = analogRead(A0);
int sensorValue1 = analogRead(A1);

// print out the value you read:
Serial.print(sensorValue0);
Serial.print("/");
Serial.println(sensorValue1);

delay(1000); // delay in between reads for stability
}

Here is my python code:
import serial
import numpy

arduinoData = serial.Serial('com8', 9600)

while True:
while (arduinoData.inWaiting() ==0):
pass
arduinoString = arduinoData.readline()

print (arduinoString)

dataArray = arduinoString.split("/","r")
sensor1 = int(dataArray[0])
sensor2 = int(dataArray[1])

print(sensor1)

Thanks in advance for any assistance you may offer.

I can’t figure out what your images are trying to show.

In Python you may need to strip trailing characters from the string before you try to split it.

You should really ask for Python advice on a Forum that deals with Python.

This Simple Python - Arduino demo may help. The Python code should work on Windows if you edit it to use the Windows style of COM ports. I believe you also have to change the line where it says rtscts = True to rtscts = False

…R

Robin2's more complete methods may be best in the long run, but here is a simple modification of your python sketch which converts the bytes from Arduino using .decode() into a String which can be split and converted to integers.

import serial

arduinoData = serial.Serial('com7', 115200)

while True:
    while (arduinoData.inWaiting() ==0):
        pass
    #arduinoBytes = arduinoData.readline()
    arduinoBytes = arduinoData.readline()[:-2] #the last bit gets rid of the new-line chars if wanted
    print(arduinoBytes)
    arduinoString = arduinoBytes.decode('UTF-8')#could use 'ASCII' or default()
    print(arduinoString)
    
    dataArray = arduinoString.split('/')
    sensor1 = int(dataArray[0])
    sensor2 = int(dataArray[1])
    print(sensor1)
    print(sensor2)
    print()

Prints

b'123/456'
123/456
123
456

Thank you both.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.