Problems with i2c

Hello,

I have a problem to send data with i2c from arduino mega to raspberry pi.

My code is :

String str = “{“time”:“2305”,“truc”:“aa”}”;
char buff[100];
str.toCharArray(buff,100);
Serial.println(str);
int nb = Wire.write(buff);

This code work, raspi receive the JSON code and can parse it, but if i send

String str = “{“time”:“2305”,“truc”:“aab”}”; // I have add ‘b’ at the end
The raspi receive wrong data, for exemple it receive this :
<Buffer 7b 22 74 69 6d 65 22 3a 22 32 33 30 35 22 2c 22 74 72 75 63 22 3a 22 61 61 22 7d ff ff ff ff ff ac c7 3c 02 38 17 00 00 b0>
For the first str and receive :
<Buffer 01 00 00 00 05 00 00 00 00 00 00 00 10 11 2d 00 38 e0 a1 00 40 54 a2 00 3c 17 00 00 64 72 f8 be ac f7 a1 00 38 17 00 00 b0>
with the second str (with a ‘b’)

I don’t know where come the problem, raspi or arduino ?

When i send 27 char it’s work, but if it’s 28 or more, it dosn’t work.

Thanks you

serveur2.js (980 Bytes)

client.ino (1.17 KB)

char buff[100];
	str.toCharArray(buff,100);

	Serial.println(str);

	int nb = Wire.write(buff);

It isn't currently mentioned in the documentation, but the internal buffer used for I2C communications is 32 bytes. That means you can transfer a maximum of 32 bytes in one transaction.

100 is more than 32.

Ok, thank you,

So if i want send more than 32, i can make more Wire.write in the function ?

Between beginTransmission and endTransmission you can only send 32 bytes. You could update the library, there are two constants you need to change.

However why send JSON data like that? Why not "pure" data?

If you must send like that make each field a separate transmission. Eg. send "time" first then "truc" in a separate transmission.

By the way please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software

Yes i can use pure data, but i don’t undersant what is cmd ?

And can i use bagin/endTransaction in a slave arduino ?

Did you read this?

http://www.gammon.com.au/i2c

I have read your link, but in a slave arduino, beginTransmission need the adress of the master, but i don't have the adress for my raspy.

I have read your link, but in a slave arduino, beginTransmission need the adress of the master, but i don't have the adress for my raspy.

No, it needs it's own address, the address it should react on. The master doesn't need an address because it's controlling the bus, it's the only one sending out addresses, the client just responds to requests of the master, it never initiates a transfer by it's own.

Ok, thanks you.
So, i must send data from raspi to arduino to say what i want and the arduino reply with data .

Correct.