I try to learn I2C with goal : read my ESP8266
events (I2C master) from my Raspberry 3 model B v2
(I2C slave).
I use the Arduino Wire library
on the ESP and python pigpio lib
on the Raspberry.
There is my SLAVE python code:
import time
import pigpio
# sudo pigpiod
I2C_SLAVE=0x08
def i2c(id, tick):
global pi
print("i2c", id, tick, pi.bsc_i2c(I2C_SLAVE))
pi = pigpio.pi()
if not pi.connected:
print("not connected")
exit()
e = pi.event_callback(pigpio.EVENT_BSC, i2c)
pi.bsc_i2c(I2C_SLAVE)
time.sleep(25)
e.cancel()
pi.bsc_i2c(0)
pi.stop()
In my first test I use the Raspberry SCL and SDA
pins without success. But after read some related questions about pigpio, for the Raspberry I2C i use GPIO18
and GPIO19
(but without any resistors) and it's work.
I receive always only 16 bytes ("Hello from ESP82" with sometimes another line with "66"
) of the datas "Hello from ESP8266"
when it will work well but also some datas garbages. Here is an answer from @joan:
I have changed the buffer size to 512 bytes.
However the hardware RX/TX FIFO is 16 bytes so you can only guarantee messages of 16 bytes or less.
Receiving or transmitting more data will depend on how fast the system can notify you that activity has started and how quickly your software can respond. You must aim to transmit/receive more data before the TX/RX FIFO is emptied of the initial 16 bytes.
I deduce : this is the answer about my received datas with 16 bytes max.
Also from this another @johan answer:
You do need external pull-ups to 3V3 as those GPIO do not have any external pulls. For reference GPIO 2/3 have 1k8 external pulls to 3V3.
My first question is , GPIO18/GPIO19 are only used for RPi i2c SLAVE mode and SCL/SDA (GPIO2/GPIO3) are used for RPi i2c MASTER mode ?
My second question is, how i can read datas while until datas exists into the buffer without losing any datas (Using ESP tx chunck datas of 16 bytes max and always read for this size on RPi) ? How I can/should I clean the buffer after received event (flush) for prevent garbages ? Or maybe it can be a problem related to the missing external pull-ups for GPIO18/GPIO19 in my case ?
NOTE: I have in the same time UART working, maybe it is not a good idea when the Raspeberry act as I2C slave ?
EDIT:
Using pigpio version 72
i can read more than 16 bytes but i have again some garbages datas. The full datas send for the test are Test hello world from esp webserver
(all bytes are sent separately on the master : loop on each chars and write chars at index with Wire lib.)
Raspberry i2c SLAVE output (where each lines are the result of a button click on esp webserver interface):
('<= i2c', 31, 1567020218, (18, 36L, bytearray(b'Test hello world from esp webserver')))
('<= i2c', 31, 1590029087, (18, 4L, bytearray(b'Test')))
('<= i2c', 31, 1593909847, (18, 1L, bytearray(b'Z')))
('<= i2c', 31, 1597031605, (18, 10L, bytearray(b'Test hell7')))
('<= i2c', 31, 1600645303, (18, 36L, bytearray(b'Test hello world from esp webserver')))
('<= i2c', 31, 1604711563, (18, 2L, bytearray(b'Td')))
('<= i2c', 31, 1607309131, (18, 2L, bytearray(b'Tr')))
('<= i2c', 31, 1609641512, (18, 36L, bytearray(b'Test hello world from esp webserver')))
('<= i2c', 31, 1613179085, (18, 1L, bytearray(b'*')))
('<= i2c', 31, 1616748905, (18, 33L, bytearray(b'Test hello world from esp webse9')))
('<= i2c', 31, 1620674854, (18, 3L, bytearray(b'Tey')))
('<= i2c', 31, 1623388548, (18, 11L, bytearray(b'Test hello ')))
('<= i2c', 31, 1626841431, (18, 18L, bytearray(b'Test hello world g')))
('<= i2c', 31, 1630841191, (18, 36L, bytearray(b'Test hello world from esp webserver')))
('<= i2c', 31, 1645572228, (18, 36L, bytearray(b'Test hello world from esp webserver')))
('<= i2c', 31, 1656723506, (18, 5L, bytearray(b'Test\x10')))
('<= i2c', 31, 1659930953, (18, 36L, bytearray(b'Test hello world from esp webserver')))
('<= i2c', 31, 1664479902, (18, 2L, bytearray(b'Tb')))
('<= i2c', 31, 1667340658, (18, 31L, bytearray(b'Test hello world from esp webs')))
Thank you for your help.
-
@tlfong01, Thank you. My master is actually the ESP and on the pigpio it is specified : This function allows the Pi to act as a slave I2C device.Ephemeral– Ephemeral2019年12月24日 08:23:29 +00:00Commented Dec 24, 2019 at 8:23
-
Ah yes, I overlooked that you are using software I2C. So my quick answer does not apply. Sorry about that.tlfong01– tlfong012019年12月24日 13:03:14 +00:00Commented Dec 24, 2019 at 13:03
-
@tlfong01, no problem.Ephemeral– Ephemeral2019年12月24日 15:09:30 +00:00Commented Dec 24, 2019 at 15:09