"Minichat" using SPI between 2 Raspberry Pi

274 views
Skip to first unread message

Konrad B

unread,
Jul 25, 2015, 4:41:51 PM7/25/15
to Pi4J
Hi
My project include connecting two Raspberry Pi by SPI and exchange some data in form of messages in some minichat application. 
Everything should be done in JAVA, therefore I would like to now if I can use Pi4J in this case? And perhaps somebody has got some example code 
that may be useful in my case. I would be grateful for every help.

Thanks in advance.  

Konrad B

unread,
Jul 29, 2015, 4:47:54 AM7/29/15
to Pi4J, konrad...@gmail.com
Hi again,
I am still trying to create some kind of "minichat" thru console window between two Raspberry Pi, using SPI.
I have activated the SPI on both Rasp's tested with spidev_test on each.
Now I am on stage of writing Java code for it with help of wiringPi libraries but I am totally confused if it's right direction

import com.pi4j.wiringpi.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ChatSPI 
{
   
    public static void main(String[] args) throws Exception 
   {
        int fd = Spi.wiringPiSPISetup(0, 10000000);
        if (fd <= -1) 
        {
            System.out.println(" ==>> SPI SETUP FAILED");
            return;
        }
   
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        String msgout=br.readLine();
        byte[] data = msgout.getBytes(); 
        write(data);
   }
   
   public static void write(byte [] data) throws Exception
   {    
        byte packet[] = data;  
        System.out.println("-----------------------------------------------");
        System.out.println("[TX] " + packet);
        Spi.wiringPiSPIDataRW(0, packet);
        String str = new String(packet, "UTF-8");
        System.out.println("[RX] " + str);
        System.out.println("-----------------------------------------------");
    }
}
If I connect MOSI MISO on only one Pi it''s printing our console message, but when I connect Pi's together and run the program on them I do not receive any message on second side. (line [RX] empty).
I assumed that there is lack of read method or/and perhaps I didn't set up connection correctly (no pins definition).
But if I understood it correctly method "wiringPiSPIDataRW" is responsible for write/read and wiringPi take over the definig pins stage.
I'll be grateful if somone could give me some hints how to overcome this problem.

Thanks in advance.

Robert Savage

unread,
Jul 29, 2015, 10:12:05 AM7/29/15
to Pi4J, konrad...@gmail.com, konrad...@gmail.com
If you are just looking for a terminal connection between 2 raspberry pi boards, you could simply using a serial connection between the two where the TX and RX pins are crossed. (In the RS232 world, this is called a null modem cable)  
You could use the minicom application to establish the serial connection on each Pi and then any data typed on one Pi would be received by the other.  You could also programmatically send and receive the serial data via Java + Pi4J.  

If you are looking for simple, I think the serial connection is the answer. 

Konrad B

unread,
Jul 30, 2015, 4:28:59 AM7/30/15
to Pi4J, rob...@savagehomeautomation.com
The problem is that the project require to establish connection by SPI and then allow for exchanging the messages in console( in JAVA))

pedros

unread,
Jul 30, 2015, 5:01:09 AM7/30/15
to Pi4J, rob...@savagehomeautomation.com, konrad...@gmail.com
Probably I have to follow this specifications : 

Before being able to use it, there is a couple of thing to understand about SPI:
  • As there is no signal to indicate if you want to read or write, what will be done depend only of what master will write.
  • This means, master will always initiate the dialog with the slave by sending data
  • Depending on these data, slave will read or write after it has decoded the master message.
  • As a consequence, an access to a slave is made with a WriteAndRead function
  • As what is done depend on the slave protocol, at SPI level, we do not exactly know when to write data and when to write data
  • The best way is to do both in parallel !
But the question is how to solve it only  with :
  • int wiringPiSPISetup (int channel, intSPEED) ;
  • int wiringPiSPIDataRW (int channel, unsigned char *data, int len);

pedros

unread,
Jul 31, 2015, 6:05:28 AM7/31/15
to Pi4J, rob...@savagehomeautomation.com, konrad...@gmail.com
I appears that this task is impossible on raspberry Pi, so my task changed, instead of using 2xPi'2 I have to check the SPI only on one Pi (MOSI connected to MISO from one Pi) and create in separate console windows(minichat app) commutation channel to send and receive messages between to windows in JAVA. 
It's clear for me that I still may use wiringPi librarys so : wiringPiSPISetup() to initialize the SPI and wiringPiSPIDataRW() to send message, but how to use wiringPiSPIDataRW() to read the message??
And additionally I wonder how I connect those two consoles windows together??
Finally is it possible at all??
Reply all
Reply to author
Forward
0 new messages