How do you use pyserial to control arduino function

soooooooo I am trying to get python to send a string/password to arduino when it hear a certain string that is from the mic that I devolped voice recog. it would turn on LED strip. But it doesn't work?

 if "turn on lights" in sound:
        ser = serial.Serial('COM3', 9600) # Establish the connection on a specific port
        ser.write("lights".encode())
        speak("Ok, turning lights to coding mode")

and on the arduino side

#include "FastLED.h"
#define NUM_LEDS 300        // How many leds in your strip?
#define LED_PIN 7
CRGB leds[NUM_LEDS];  // Define the array of leds
void setup() {
  Serial.begin(9600);  
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  for(int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB(0,0,0);
  }
 }

void loop() {
  if (Serial.read() == "lights"){
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB(10,10,10);
    }
    FastLED.show();
    delay(100);
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB(10, 0,10);
    }
    FastLED.show();
    delay(100);
  
    }
  }

Arduino is not python. Serial.read() returns a single char. You cannot compare strings with '=='

Also note that every time you open the serial port on your PC, you reset the arduino so you will need to give it a little time to boot up before it will be able to accept any command.

look at the Serial Input Basics - updated tutorial

@yousef_elhariry

Can you please refrain from using light gray text on a slightly darker gray backgray; it is difficult to read. Colouring of parts of text can add value (as a highlight) but in this case it doesn't.

Thanks

PS Karma++ for using code tags in your first post.

There is a good tutorial here on python/arduino communication

https://forum.arduino.cc/index.php?topic=598209.0

If you follow that tutorial and are using Windows it requires rtscts=False in the serial port setup.

Change

ser.write("lights".encode())

to

ser.write_line("lights")

and then use this code on the Arduino side to read each line.
the == "lights" now works with this code.

// readStringUntil_nonBlocking.ino
// Reads chars into a String until newline '\n'
// https://www.forward.com.au/pfod/ArduinoProgramming/SoftwareSolutions/index.html
// Pros: Simple. Non-Blocking
// Cons: Does not return until the until_c char is found. i.e. no timeout or length limit

String input;

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println();
  Serial.println(F("readStringUntil_nonBlocking.ino"));
  input.reserve(80); // expected line size
}

// read Serial until until_c char found, returns true when found else false
// non-blocking, until_c is returned as last char in String, updates input String with chars read
bool readStringUntil(String& input, char until_c) {
  while (Serial.available()) {
    char c = Serial.read();
    input += c;
    if (c == until_c) {
      return true;
    }
  }
  return false;
}

void loop() {
  if (readStringUntil(input, '\n')) { // read until find newline
    input.trim(); // remove newline and any leading space
    // echo back?? Serial.print(F(" got a line of input '")); Serial.print(input); Serial.println("'");
    if (input == "lights"){
       // got lights do something here
    }
    input = ""; // clear after processing for next line
  }
}

drmpf:
Change

ser.write("lights".encode())

to

ser.write_line("lights")

and then use this code on the Arduino side to read each line.
the == "lights" now works with this code.

// readStringUntil_nonBlocking.ino

// Reads chars into a String until newline '\n'
// www.forward.com.au//[mainDir]
// Pros: Simple. Non-Blocking
// Cons: Does not return until the until_c char is found. i.e. no timeout or length limit

String input;

void setup() {
 Serial.begin(9600);
 for (int i = 10; i > 0; i--) {
   Serial.print(' '); Serial.print(i);
   delay(500);
 }
 Serial.println();
 Serial.println(F("readStringUntil_nonBlocking.ino"));
 input.reserve(80); // expected line size
}

// read Serial until until_c char found, returns true when found else false
// non-blocking, until_c is returned as last char in String, updates input String with chars read
bool readStringUntil(String& input, char until_c) {
 while (Serial.available()) {
   char c = Serial.read();
   input += c;
   if (c == until_c) {
     return true;
   }
 }
 return false;
}

void loop() {
 if (readStringUntil(input, '\n')) { // read until find newline
   input.trim(); // remove newline and any leading space
   // echo back?? Serial.print(F(" got a line of input '")); Serial.print(input); Serial.println("'");
   if (input == "lights"){
      // got lights do something here
   }
   input = ""; // clear after processing for next line
 }
}

the ser.write_line() doesn't work???? it gives the following error code: ser.write_line("lights")
AttributeError: 'Serial' object has no attribute 'write_line'

It is actually under class serial.threaded.LineReader(Packetizer)
so not the direct replacement I thought it was
go back to

eol = "\n";
cmd = "lights" + eol;
ser.write_line(cmd.encode())

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