0

I am trying to create an "echo server" with Arduino UNO, so that user could send some message to it through Raspberry Pi and receive the same response. On Pi I am using Python and PySerial library to communicate with Arduino over Usb. Unfortunately, I cannot make it work properly, and I cannot find a decent example to guide myself. It would be nice, so that you could explain what am I doing wrong... Rpi Python code:

import serial as ser
import io
dataIn = ser.Serial("/dev/ttyACM1", 9600)
ready = False
while True:
  if(dataIn.inWaiting() > 0):
    data = dataIn.readline()
    if(data == "R"):
      print '200 >> Arduino Ready!'
      ready = True

      while ready:
        userIn = str(raw_input('Enter Command >> '))
        ser.write(b+userIn + "\n")
    else:
      print '500 << Arduino not ready'

And Arduino code:

String content = "";
char c;
boolean sendM = false;

void setup(){

  Serial.begin(9600);
  Serial.println("R");
}

void loop(){
  while(Serial.available() > 0){
    c = Serial.read();
    content.concat(c);
    delay(10); 
    sendM=true;   
  }
  if(content != ""){
     Serial.println(content); 
     sendM = false;
     content = “”;
  }
}

Also, when I try to communicate with Arduino by using Putty I receive response from it, however, the Python code crashes without receiving any response

  • How do you know it crashes? Would you care to share that information? – joan Jul 18 '18 at 20:09
  • Yeah, sure! So when the I run the Python code, it returns message that the Arduino is unavailable although it returns ‘R’ (for ready). – alex_z Jul 18 '18 at 21:42
  • That does not sound like a crash. – joan Jul 18 '18 at 21:45
  • I have edited the code, since I was getting redundant messages from Arduino when I connect with Putty. The real problem is the control part on Rpi ... – alex_z Jul 18 '18 at 21:46
  • That isn’t a crash but the message exchange is done incorrectly.. – alex_z Jul 18 '18 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.