
msriddle68
USA
Asked
— Edited

I am trying to connect my IoTiny to an Arduino Pro Mini via I2C. I am able to successfully write for the IoTiny to the Arduino but when I try to request a response from the Arduino the data see corrupt.
I've tried very basic ez-script:
print(i2cRead( 0, 0xA,1 ))
And Arduino code:
String outtoezb;
outtoezb = "R";
char tempout[outtoezb.length() + 1];
outtoezb.toCharArray(tempout,outtoezb.length() + 1);
Wire.write(tempout);
Serial.print("Response Sent: ");
Serial.println(outtoezb);
When executed I get the follow on the IoTiny:
Start
>
Done (00:00:00.0230307)
And I see that the request get through to the arduion:
My i2c address: 10
Response Sent: R
Any thoughts?
Well interesting.... I replaced my code with yours and I get a "H" as the first letter with 10 special characters (see attached) and the wire hangs. I have to disconnect and reconnect IoTiny to resume. If I only request one byte then the "H" is returned, the wire is closed and all is good. Not sure why requesting multiple bytes is freezing things up confused I'll continue to review the links and look into getting an analyzer.
*** UPDATE ***
Okay much closer! We both had logic issues with our code but here is what I have so far....
Now I get the full msg returned BUT the wire still remains open until I disconnect/reconnect the IoTiny.
@msriddle68,
I have a working code from a previous integration, I'll post in 5 minutes.
I tested the code with an Iotiny and arduino mini.
Note: Please use the code and do not change anything, and let me know the results.
Arduino code:
EZ-Scripts
Script 1: Write to the slave:
Script 2: Read from the slave:
Steps:
expected results:
Ah! Receive and request interrupt events with an event type variable. Now that's how it should be done! I didn't see any of that in the documentation for arduino when I googled. Super awesome it exists and you know about it - because I was wondering how it would magically exist with only one interrupt and no event type.
Thanks for helping! We'll have to turn this into a tutorial
@DJ,
Correct, let's wait to see if it works for the OP. I would like to share more details.
Like you said before I2C is not a serial protocol and is very sensitive.
I2C protocol is inherently half-duplex, basically the master controls the clock and sends the first byte (7 addr bit) with 1 bit (read/write). So the slave obey promptly to the master and performs two different and isolated operations: receiving and sending data.
Why do I see people use delay() in the arduino loop when there's no code? I see it everywhere but I don't know why it's there - it creates extra processing, and the stack nests. So when the interrupts raise, the stack has to be backed up and stopping the demay() anyway. Which slows down the interrupt from starting.
@DJ,
There is no need/excuse for a delay an empty loop function is enough for this particular example.
The original code before the cut, is a man-in-middle implementation to debug a micro-controller firmware. So i used a micro-controller with my code to simulate the same behavior and every 2 seconds sends a serial debug message. So the delay is left overs
BUT
I was lazy... the correct way is to measure time deltas and execute when the condition is true.
Awesome guys! This sample works and releases the wire correctly. I can break it down to figure out my implementation. I really appreciate all the support!

