steve041264
Posts: 13
Joined: Wed Jan 03, 2018 8:34 am

Send 64 bit from one Raspberry PI 3 B to another

Fri Mar 16, 2018 9:50 am

Good evening,
I'm trying send 64 bit from one raspberry to another, using gpio. My problem is synchronize to raspberry, so I can receive correct data.
TX program is:

Code: Select all

import RPi.GPIO as GP
import numpy as np
import bitstring as bs

GP.setmode(GP.BCM)
GP.setup(17,GP.OUT)

SIGIFS=np.array([0.0000,0.79308,0.05000,3.4908,0.10000,7.6360,.......900,25.976,86.950,25.933,87.000,25.889,87.050,25.848,87.100,25.813,87.150]);
SIGIF=SIGIFS[1::2]#take only odds values from SIGIFS
lung_SIGIF=len(SIGIF)#length of SIGIF
lung_SIGIFS=len(SIGIFS)#length of SIGIFS
length_stop=bs.Bits(int=9999,length=64);#convert 9999 to binary with length 64 bit to stop TX
length_start=bs.Bits(int=1000, length=64);#convert 1000 to binary with length 64 bit to stop RX
Y=np.zeros(64);#I create an array of 64 positions to insert the bits that make up the val value in binary of the i-th value of SIGIF
arrive_SIGIF=np.zeros(lung_SIGIF);#I create an array of the length necessary to contain the starting array
start=np.zeros(64);#I create an array of the length necessary to contain the start int 1000


#send array to start rx
for i in range(0,64):
    GP.output(17,length_start[i])

for i in range(0,lung_SIGIF):
    val=SIGIF[i];#take 'i' value of SIGIF
    binary=bs.Bits(float=val,length=64);#convert val to bin  
    for x in range(0,64):
        GP.output(17,int(binary.bin[x]));#write bit on port number 17

 
#send array to stop tx
for i in range(0,64):
    GP.output(17,length_stop[i])

GP.cleanup();

and the RX code is:

Code: Select all

import RPi.GPIO as GP
import numpy as np
import bitstring as bs

GP.setmode(GP.BCM)
GP.setup(27,GP.IN)

SIGIFS=np.array([0.0000,0.79308,0.05000,3.4908,0.10000,7.6360,.......900,25.976,86.950,25.933,87.000,25.889,87.050,25.848,87.100,25.813,87.150]);
SIGIF=SIGIFS[1::2]#take only odds values from SIGIFS
lung_SIGIF=len(SIGIF)#length of SIGIF
lung_SIGIFS=len(SIGIFS)#length of SIGIFS
length_stop=bs.Bits(int=9999,length=64);#convert 9999 to binary with length 64 bit to stop TX
length_start=bs.Bits(int=1000, length=64);#convert 1000 to binary with length 64 bit to stop RX
Y=np.zeros(64);#I create an array of 64 positions to insert the bits that make up the val value in binary of the i-th value of SIGIF
arrive_SIGIF=np.zeros(lung_SIGIF);#I create an array of the length necessary to contain the starting array
start=np.zeros(64);#I create an array of the length necessary to contain the start int 1000

#read array to start rx
while 1:
    for x in range(0,64):
        start[x]=GP.input(22);
    str1 = ''.join(str(e) for e in start.astype(int))#I transform the array created in string str1
    aa=bs.Bits(bin=str1);#I recreate the starting binary number
    g=aa.int
  
    if str1==g:
        break;    

while 1:
    for i in range(0,lung_SIGIF):
        for x in range(0,64):
            k=GP.input(22);#I read the received bit
            Y[x]=int(k);#I insert the received bit inside the position 
        str2 = ''.join(str(e) for e in Y.astype(int))#I transform the array created in string str1
        aa=bs.Bits(bin=str2);#I recreate the starting binary number
        g2=aa.float;#I convert the binary number received in float
        arrive[i]=g2;#insert the value val sent in the same position of the starting array SIGIF
#stop to receive
        if int(g)==9999:
            break;
            
I have to recreate the same SIGIF array in the arrival Raspberry. My problem is that the second value of the array is not in the arrival array either as a position or as a value. How can I synchronize the two raspberry?

Thank you

Steve

harms
Posts: 27
Joined: Tue Feb 13, 2018 7:38 pm

Re: Send 64 bit from one Raspberry PI 3 B to another

Sun Mar 18, 2018 8:14 am

Are you stuck to use a GPIO connection? If not, by far the easiest and most simple approach would be serial communication via Bluetooth - the serial transmission already takes care of synchronisation at the bit and byte level. At the application level (8-byte messages), a combination of Unix Select and buffered or unbuffered I/O will do the rest (Select also can serve to handle event sequencing).

Documentation on Bluetooth is shockingly bad, but I just - and finally - got communication between 2 Pi's working - in fact came to this forum branch looking for a place where to document how that works for me.

PS: I have quite some experience with synchronising micro-processors - and developed a strong preference to SPI which is ideal for this kind of task (but dont know how software support is implemented on Raspberry). But if you want to got this "hardware way" (and assuming that you will also will spend time in getting familiar with the support) the effort will certainly be a multiple of that to be spent on making a bluetooth connection work
Last edited by harms on Mon Jun 11, 2018 11:23 am, edited 1 time in total.

steve041264
Posts: 13
Joined: Wed Jan 03, 2018 8:34 am

Re: Send 64 bit from one Raspberry PI 3 B to another

Sun Jun 10, 2018 7:09 pm

Yes, I'm stuck because I need transfer an array from the first Raspberry to a second Raspberry.
Unfortunately I cannot use SPI, because the Raspberry cannot settings like slave. However I want transmitt data by one wire lonely, over GND.

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Send 64 bit from one Raspberry PI 3 B to another

Sun Jun 10, 2018 8:33 pm

Connect tx GPIO14 to rx GPIO15, rx GPIO15 to tx GPIO14 (note the cross-over) and GND to GND.
Disable the serial console on both Raspberries.

Use plain old serial with /dev/ttyAMA0 on each end. It's simple, it'll easily run at 115200,8,N,1 or faster.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

steve041264
Posts: 13
Joined: Wed Jan 03, 2018 8:34 am

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 9:35 am

But GPIO14 and GPIO15 is UART, I would like use GPIO23 on RP1 and RP2, and GND only. I' ve already used UART port, but for my problem I have use two wire only:can you help me?
If this is no possible, why?

Thank you very much

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 9:40 am

Stop trying to re-invent a wheel. The best ways to connect two Raspberries together are ethernet, WiFi or serial. Anything else is going to cause you a bucket load of pain.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

GuruMeditation
Posts: 31
Joined: Thu May 31, 2018 3:33 pm
Location: UK

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 9:45 am

IMHO, using a single wire without a second one to provide a clock is a bit tricky. E.g. many 1-wire interfaces use short and long pulses to send 0 or 1. So one way would be to do that in software.

With 2 wires you could use a software SPI implementation.

steve041264
Posts: 13
Joined: Wed Jan 03, 2018 8:34 am

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 9:57 am

But using SPI, RP is not setup as slave.
I saw using a 433 transceiver connecting it right on a GPIO port: how did they do it?

Thank you very much

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 10:05 am

One wire needs two cables - signal and GND.
Serial needs three cables - RX/TX, TX/RX and GND.
WiFi needs none.
Ethernet needs four cables - TX pair & RX pair.

1-Wire, SPI (five cables - MISO, MOSI, CLK, CS & GND) & I2C (three cables - SCL, SDA & GND) are all non-starters because of the master/slave model. The Raspberry is always the master.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

steve041264
Posts: 13
Joined: Wed Jan 03, 2018 8:34 am

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 10:48 am

thank you all for your answers, and sorry if I insist because I want to understand better. on the web I saw that a 433MHZ transmitter and receiver is used, which is connected neither on the UART, on SPI, etc., but only on a normal pin, such as 23 for example. How do they send and above all to receive?

GuruMeditation
Posts: 31
Joined: Thu May 31, 2018 3:33 pm
Location: UK

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 10:52 am

The RaspberryPI's hardware SPI does not support slave mode.

If you look for a software SPI library (or 1-wire, etc), you may be able to find one that does.

433MHz RF module are usually controlled through SPI. If there are connected to plain GPIOs it implies that a software SPI interface is used.


steve041264
Posts: 13
Joined: Wed Jan 03, 2018 8:34 am

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 10:56 am

GuruMeditation wrote:
Mon Jun 11, 2018 10:52 am
The RaspberryPI's hardware SPI does not support slave mode.

If you look for a software SPI library (or 1-wire, etc), you may be able to find one that does.

433MHz RF module are usually controlled through SPI. If there are connected to plain GPIOs it implies that a software SPI interface is used.
without clock?

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 11:00 am

433MHz uses three wires (3V3, data & GND).
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

GuruMeditation
Posts: 31
Joined: Thu May 31, 2018 3:33 pm
Location: UK

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 11:12 am

RF modules usually use the standard SPI interface. But we're digressing.

The point is that if hardware interfaces are used up then you can use GPIOs and implement an interface in software, including the clock if needed (e.g. for SPI). There are libraries.

I'm also surprised that you only have 1 GPIO available.

steve041264
Posts: 13
Joined: Wed Jan 03, 2018 8:34 am

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 11:15 am

DougieLawson wrote:
Mon Jun 11, 2018 11:00 am
433MHz uses three wires (3V3, data & GND).
yes, but the data are connected to a single pin, over the mass and to the power supply. Maybe I explained myself badly: I have to send an array of integers from one RP1 to another. Having seen that 433MHz, use a port that is not SPI, I2C or UART, why should not it work without connecting a pin and GND?
I still apologize

steve041264
Posts: 13
Joined: Wed Jan 03, 2018 8:34 am

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 11:17 am

I tried but I had trouble synchronizing

GuruMeditation
Posts: 31
Joined: Thu May 31, 2018 3:33 pm
Location: UK

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 11:19 am

steve041264 wrote:
Mon Jun 11, 2018 11:15 am
Having seen that 433MHz, use a port that is not SPI, I2C or UART, why should not it work without connecting a pin and GND?
If that's really the case then it must be a flavour of 1-wire interface, as mentioned.

steve041264
Posts: 13
Joined: Wed Jan 03, 2018 8:34 am

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 11:30 am

GuruMeditation wrote:
Mon Jun 11, 2018 11:19 am
steve041264 wrote:
Mon Jun 11, 2018 11:15 am
Having seen that 433MHz, use a port that is not SPI, I2C or UART, why should not it work without connecting a pin and GND?
If that's really the case then it must be a flavour of 1-wire interface, as mentioned.
that is, can I let the two raspberry talk and do not need a master and a slave?

TElmed
Posts: 3
Joined: Fri Jun 08, 2018 10:21 am

Re: Send 64 bit from one Raspberry PI 3 B to another

Mon Jun 11, 2018 11:30 am

If I may add my thoughts to it:
Using Ethernet, WiFi, RS232 or Bluetooth is definitely much easier, faster, and safer to use (safer regarding to bugs because used so much).

However, if you want to use 2 wires (gnd + gpio), first thing coming to my mind:
You could make a simple communication by using the time as "clock".
The more bits, the more precision both clocks require, and definitely has to be tested with each device combination separately.
Let's use the clock as cycles unit, it could be one second, 0.1second - etc., it's a constant both know.
max_pack_size is a constant both know.

Master:
Start: Make your Pin low for max_pack_size +1 cycles and then high for one cycle.
Data: Make your Pin low/high regarding the data, bit for bit per cycle

Slave:
Start: Check for a low condition which remains for max_pack_size +1 cycles, when high comes in - start your time, let pass 1.5 cycles
Data: Now take the value in each cycles (absolutely seen, it is each cycles + .5 cycle)

For more simplicity the datasize could be fix,
for more performance you could send the data size in the first byte, this data won't have to be a fixed size (but it must remain <= max_pack_size
for safety you could add a simple crc.
If you need to send a lot of data, you could make fragments of the data and send them.

In Reality:
This kind of solution shouldn't be too hard to make, however I guess you won't be able to send much nor fast data.

P.s: Otherwise you could align to the 1-wire (wikipedia.org/wiki/1-Wire) way, however I don't know whether it is possible to make something like this with a scripting language/non-RTOS.

User avatar
Arjan
Posts: 255
Joined: Sat Sep 08, 2012 1:59 pm

Re: Send 64 bit from one Raspberry PI 3 B to another

Tue Jun 12, 2018 6:01 pm

GuruMeditation wrote:
Mon Jun 11, 2018 10:52 am
The RaspberryPI's hardware SPI does not support slave mode.
Do you have a formal written/link/reference for the statement above?
http://www.raspberrypi-dmx.org/
Open Source DMX/RDM/MIDI/OSC/Art-Net/sACN solutions

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Send 64 bit from one Raspberry PI 3 B to another

Tue Jun 12, 2018 8:10 pm

Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
Arjan
Posts: 255
Joined: Sat Sep 08, 2012 1:59 pm

Re: Send 64 bit from one Raspberry PI 3 B to another

Tue Jun 12, 2018 8:23 pm

That page describes the SPI Master interface. The SoC has also a I2C/SPI Slave interface at address (BCM2835_PERI_BASE + 0x214000).
I've got the SPI Slave partially working. Hence I would like to know if it can work at all.
http://www.raspberrypi-dmx.org/
Open Source DMX/RDM/MIDI/OSC/Art-Net/sACN solutions

User avatar
DougieLawson
Posts: 32995
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Send 64 bit from one Raspberry PI 3 B to another

Tue Jun 12, 2018 8:25 pm

Not with Linux, because there's no kernel driver for it.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
Arjan
Posts: 255
Joined: Sat Sep 08, 2012 1:59 pm

Re: Send 64 bit from one Raspberry PI 3 B to another

Tue Jun 12, 2018 8:40 pm

DougieLawson wrote:
Tue Jun 12, 2018 8:25 pm
Not with Linux, because there's no kernel driver for it.
Still the question, does the SoC support the SPI Slave functionality ? The I2C Slave is working nicely.
http://www.raspberrypi-dmx.org/
Open Source DMX/RDM/MIDI/OSC/Art-Net/sACN solutions

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: No registered users and 19 guests