decode msg from python

i have some msg get form python like this [0, 0, 0, 1, 0]. but showing error when i'm using string.

String str;
void loop() {
str = Serial.readStringUntil('\n');
while (Serial.available()>0)
  {
    str = Serial.readStringUntil('\n');
  }
  if (str == '[0, 0, 0, 1, 0]')
  {
    digitalWrite(ledPin, HIGH);
  }
  else if (str != '[0, 0, 0, 1, 0]')
  {
    digitalWrite(ledPin, LOW);
  }
}

Strings in C++ use " not '

if (str == '[0, 0, 0, 1, 0]')

aren't single quotes used to describe a single character and double quotes for strings?

will not shown error on below code...but can not fuction to on/off LED. problem on the python?

  while (Serial.available()>0)
  {
    str = Serial.readStringUntil('\n');
  }
  if (str.equals("[0, 0, 0, 0, 1]"))
  {
    digitalWrite(ledPin, LOW);
    str = " ";
  }
  else if (str.equals("[1, 0, 0, 0, 0]"))
  {
    digitalWrite(ledPin, HIGH);
    str = " ";
  }

str = Serial.readStringUntil('\n');

You should print out what you have read to see if it is what you think.

What is the python sending code?

i want to read msg...but when i running python program, i can not open arduino serial monitor. actually, i have some data send to python to calculate and want to send back the answer to arduino for do something.

while True:
    ser = serial.Serial('/dev/ttyUSB0',4800)
    r1=ser.readline()
    # x = [400, 150, 150, 130, 140] sample data from arduino it's will keep change
    x = r1.decode('utf8').rstrip().split(", ")
    for i in range(0, len(x)): 
        x[i] = int(x[i])
    k = 2
    clusters, centroids = kmeans1d.cluster(x, k)
    print(clusters)   # [1, 0, 0, 0, 0] sample result sent to arduino
    ser.write(clusters)

I believe the issue is on the python side. When I run this pair of programs, I can blink the led.

Arduino code

String str;
void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(115200);
  Serial.println("starting"); 
}
void loop() {

  while (Serial.available()>0)
  {
    str = Serial.readStringUntil('\n');
    Serial.println(str);//will echo back to python
  }
  if (str.equals("[0, 0, 0, 0, 1]"))
  {
    digitalWrite(ledPin, LOW);
    str = " ";
  }
  else if (str.equals("[1, 0, 0, 0, 0]"))
  {
    digitalWrite(ledPin, HIGH);
    str = " ";
  }
}

Python code

import serial
import time

arduino = serial.Serial('COM4', 115200, timeout=.1)
time.sleep(2);#time for serial port to open
count = 0
prevTime = time.time()
while True:      

        if time.time() - prevTime > 1.0:
                prevTime = time.time()
                if count == 0:
                        count = 1
                        message= "[1, 0, 0, 0, 0]"
                        newMessage = message.encode()
                        arduino.write(newMessage)
            
                elif count == 1:
                        count = 0
                        message= "[0, 0, 0, 0, 1]"
                        newMessage = message.encode()
                        arduino.write(newMessage)
                
                
        #read echo back from arduino verify receipt of message
        data = arduino.readline()[:-2] #the last bit gets rid of the new-line char
        if data:
                print (data.decode('UTF-8'))#eliminate the 'b' bytes indicator

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