Seira07
Posts: 7
Joined: Sat Jun 16, 2018 3:44 am

Communication of Raspberry Pi 3 Model B and Arduino via Xbee S2C

Mon Jun 18, 2018 2:45 am

I don't know what is wrong here.
I have my explorer in xbee connected in the Rasp Pi.
This the code I wrote for the Raspberry Pi:

Code: Select all

#import Serial
import serial, time

#define function for setup
def setup_serial():

    #opening the serial connection
    sc = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
    
    #character handshake with python
    while(sc.read() != 'a'): pass
    sc.flushInput()
    sc.write('a')
    
    #Return the serial connection
    return sc

#define function for sensor
def get_val():
    
    #Send request to the arduino
    sc.write('T')
    
    #Read response
    val = sc.readline().rstrip()
    
    #return val
    return val

#Define switch for the indicator
def switch_led(on):
    if on:
        sc.write('1')
    
    else:
        sc.write('0')
        
#create connection

sc = setup_serial()

#Getting the value of sensor
sens = get_val()
print ('The value is %s')% sens

#Turn the led on
print ('Turning LED on')
switch_led(True)
time.sleep(2)

#Turn the LED off
print ('Turning LED off')
switch_led(False)

I have my Xbee shield in arduino and I have my sensors in Arduino that is connected to the A0 and ground. This is my code for Arduino:

Code: Select all

#include <Wire.h>
void setup()
{

  //Initialize serial communications at 9600 bps:
  Serial.begin(9600);
  
  //Acknowledgement of Routine
  Serial.println('a');
  char a = 'b';
  while (a != 'a')
  {
    //A specific character from the PC
    a = Serial.read();
  }


}

void loop()
{
  if (Serial.available() > 0) //check if there is data sent
  {
    int mode = Serial.read(); //check if there is request for data
    switch(mode)
    {
      case 'T':
      {
        //Sensor
        float PztS = getPiezo();
        Serial.println(PztS); // Send value over Serial
        break;
      }

      case '1': //Led on
        //When there is leak
        digitalWrite(2,HIGH);
        break;
      case '0': //Led off
        //When it is normal
        digitalWrite(2,LOW);
        break;
 
    }
  }
}

float getPiezo()
{
  Wire.requestFrom((0x91>> 1), 2); //Request 2 bytes of data from the PZT
  byte byteH = Wire.read(); //read the higher byte
  byte byteL = Wire.read(); //read the lower byte
  int Sens = (byteH << 4); //Bitshift the MSB left
  Sens |= (byteL >> 4); //Bitshift the LSB right and logic OR with MSB
  return (Sens*0.25);
}
When I run the Python code:


it says,
"Traceback (most recent call last):
File "/home/pi/xbee.py", line 40, in <module>
sc = setup_serial()
File "/home/pi/xbee.py", line 11, in setup_serial
while(sc.read() != 'a'): pass
File "/home/pi/.local/lib/python3.5/site-packages/serial/serialposix.py", line 483, in read
ready, _, _ = select.select([self.fd, self.pipe_abort_read_r], [], [], timeout.time_left())
KeyboardInterrupt: Execution interrupted"


I don't know why the Rasp Pi didn't receive an Input. Please help.

Idahowalker
Posts: 372
Joined: Wed Jan 03, 2018 5:43 pm

Re: Communication of Raspberry Pi 3 Model B and Arduino via Xbee S2C

Mon Jun 18, 2018 12:10 pm

Perhaps it would be a good idea to break this issue down into smaller parts.

Have you done a loop back test with the RPi to see if your PI serial port is set up correctly and that you are using the correct parameters?

Code: Select all

import serial


sPortToUse = "/dev/serial0"
#sPortToUse = "/dev/ttyAMA0"


sTest = "Bad Boys Race Our Young Girls Behind Victory Garden Walls Get Started Now".encode('utf-8')
#sTest = b"Bad Boys Race Our Young Girls Behind Victory Garden Walls Get Started Now"

sReceive = b""

iBytesSent = 0

serialPort = serial.Serial(sPortToUse, 9600, timeout = 2)
serialPort.flushOutput()
serialPort.flushInput()
if serialPort.open:
    print("Opened port", sPortToUse)
    #iBytesSent = serialPort.write(sTest)
    #print ("Sent", iBytesSent, "bytes")
    #sReceive = serialPort.read(iBytesSent)
    sReceive = serialPort.readline()
    #if sReceive == sTest:
        #print ("Received", len(sReceive), "bytes on, serial port", sPortToUse)
        #print (sReceive.decode('utf-8'))print
    print (sReceive.decode('utf-8')) 
    #else:
        #print ("Receive error", sReceive, "over", sPortToUse)
else:
    print("Port", sPortToUse, "failed to open")
serialPort.close()
Connect the Tx and Rx pins of your Pi together and run the above Python Module. Notice there are two serial port options, tryon first and if it does not work try the other option. If neither option works then you will need to go hunting down the issue with your Pi. There are lottsa info on setting up the Pi serial port. I just tried stuff till the serial port eventually started working. Yup, it is not a clear cut path as to what works and what does not. Do note your port out the Pi GPIO should be the option I have already coded as the default for the Python Module.
Without knowing why you are deleting my postings, I will not know how...

Seira07
Posts: 7
Joined: Sat Jun 16, 2018 3:44 am

Re: Communication of Raspberry Pi 3 Model B and Arduino via Xbee S2C

Mon Jun 18, 2018 1:33 pm

Idahowalker wrote:
Mon Jun 18, 2018 12:10 pm
Perhaps it would be a good idea to break this issue down into smaller parts.

Have you done a loop back test with the RPi to see if your PI serial port is set up correctly and that you are using the correct parameters?

Code: Select all

import serial


sPortToUse = "/dev/serial0"
#sPortToUse = "/dev/ttyAMA0"


sTest = "Bad Boys Race Our Young Girls Behind Victory Garden Walls Get Started Now".encode('utf-8')
#sTest = b"Bad Boys Race Our Young Girls Behind Victory Garden Walls Get Started Now"

sReceive = b""

iBytesSent = 0

serialPort = serial.Serial(sPortToUse, 9600, timeout = 2)
serialPort.flushOutput()
serialPort.flushInput()
if serialPort.open:
    print("Opened port", sPortToUse)
    #iBytesSent = serialPort.write(sTest)
    #print ("Sent", iBytesSent, "bytes")
    #sReceive = serialPort.read(iBytesSent)
    sReceive = serialPort.readline()
    #if sReceive == sTest:
        #print ("Received", len(sReceive), "bytes on, serial port", sPortToUse)
        #print (sReceive.decode('utf-8'))print
    print (sReceive.decode('utf-8')) 
    #else:
        #print ("Receive error", sReceive, "over", sPortToUse)
else:
    print("Port", sPortToUse, "failed to open")
serialPort.close()
Connect the Tx and Rx pins of your Pi together and run the above Python Module. Notice there are two serial port options, tryon first and if it does not work try the other option. If neither option works then you will need to go hunting down the issue with your Pi. There are lottsa info on setting up the Pi serial port. I just tried stuff till the serial port eventually started working. Yup, it is not a clear cut path as to what works and what does not. Do note your port out the Pi GPIO should be the option I have already coded as the default for the Python Module.

Yes, I've tried the loop back test and it is working. I also tried to run the minicom -D /dev/ttyUSB0 and when I enter +++, it answer OK. Do I need to enable something in the Raspberry Pi Configuration? Like GPIO, SPI, I2C, etc.?

Idahowalker
Posts: 372
Joined: Wed Jan 03, 2018 5:43 pm

Re: Communication of Raspberry Pi 3 Model B and Arduino via Xbee S2C

Mon Jun 18, 2018 2:24 pm

Seira07 wrote:
Mon Jun 18, 2018 1:33 pm

Yes, I've tried the loop back test and it is working. I also tried to run the minicom -D /dev/ttyUSB0 and when I enter +++, it answer OK. Do I need to enable something in the Raspberry Pi Configuration? Like GPIO, SPI, I2C, etc.?
If the loop back is working then the issue is not with the RPi serial.

Does the device (xbee) use SPI or an I2C, not looking that up as that should be info you provide?

If it does, use SPI or I2C, then have you tried to detect the device using command line? Have you went to the xbee support for setup info?

If the RPi loop back test works, and serial is your issue, then, taking out SPI or I2C, the RPi is not at fault.

Have you tried the Arduino help forum, as again, the RPI passes a loop back, meaning the RPi serial port can both read and write data to and from the serial port. I take it the xbee is providing some sort of level shifting? Have you did a loop back with the
explorer in xbee connected in the Rasp Pi
to prove that device?

Break the issue into smaller functional pieces and prove each piece.
Without knowing why you are deleting my postings, I will not know how...

Seira07
Posts: 7
Joined: Sat Jun 16, 2018 3:44 am

Re: Communication of Raspberry Pi 3 Model B and Arduino via Xbee S2C

Tue Jun 19, 2018 1:33 pm

Break the issue into smaller functional pieces and prove each piece.
Okay, I will do that. Thanks. Btw, how can I know if it is SPI or I2C? I didn't connect my xbee in any pins because it has a shield and explorer.

Idahowalker
Posts: 372
Joined: Wed Jan 03, 2018 5:43 pm

Re: Communication of Raspberry Pi 3 Model B and Arduino via Xbee S2C

Fri Jun 22, 2018 12:05 am

Seira07 wrote:
Tue Jun 19, 2018 1:33 pm
Break the issue into smaller functional pieces and prove each piece.
Okay, I will do that. Thanks. Btw, how can I know if it is SPI or I2C? I didn't connect my xbee in any pins because it has a shield and explorer.
Reading?

https://www.digi.com/resources/document ... 002002.pdf

Give a look at page 16 for starters.
Without knowing why you are deleting my postings, I will not know how...

Return to “Python”