I'm lost. I'm trying to send data over i2c from the Moteino to a Pi but if I send anything over serial the i2c data gets messed up. What am I missing?
Here is the code. This works fine. If I uncomment Serial.println(cmd); in the receiveData method all of the data gets messed up though.
#include <Wire.h>
#include <RFM69.h>
#include <SPI.h>
#define BASEID 254
#define NETWORKID 100
#define FREQUENCY RF69_433MHZ
#define ENCRYPTKEY "sampleEncryptKey"
#define SERIAL_BAUD 115200
#define ACK_TIME 30
#define SLAVE_ADDRESS 0x04
RFM69 radio;
byte nodeId;
char b[32];
void setup() {
Serial.begin(SERIAL_BAUD); // start serial for output
radio.initialize(FREQUENCY, BASEID, NETWORKID);
radio.encrypt(ENCRYPTKEY);
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
Serial.println("Ready!");
}
void loop() {
if (radio.receiveDone()){
Serial.print("Radio data received: ");
for (byte i = 0; i < radio.DATALEN; i++){
Serial.print((char)radio.DATA[i]);
b[i] = radio.DATA[i];
}
if (radio.ACKRequested()){
radio.sendACK();
}
Serial.println("");
}
}
void receiveData(int number_of_bytes){
byte cmd = Wire.read();
//Serial.println(cmd);
}
void sendData(){
char *ptr = b;
Serial.println((char *)ptr);
byte written = Wire.write(ptr, 32);
}
When I run the code with the Serial.println commented out I get the correct data, 32 bytes worth.
[70, 76, 65, 83, 72, 95, 77, 69, 77, 95, 73, 68, 58, 50, 54, 53, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
FLASH_MEM_ID:2658
[70, 76, 65, 83, 72, 95, 77, 69, 77, 95, 73, 68, 58, 50, 54, 54, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
FLASH_MEM_ID:2664
[70, 76, 65, 83, 72, 95, 77, 69, 77, 95, 73, 68, 58, 50, 54, 54, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
FLASH_MEM_ID:2664
[70, 76, 65, 83, 72, 95, 77, 69, 77, 95, 73, 68, 58, 50, 54, 54, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
FLASH_MEM_ID:2664
When I put that println back in all of the data is messed up. Any thoughts why that's causing such issues?
[9, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
������������������������������
[9, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
������������������������������
[9, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
������������������������������
[9, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
Python code for reference
import smbus
import time
# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)
# This is the address we setup in the Arduino Program
address = 0x04
while True:
try:
cal = bus.read_i2c_block_data(address, 42, 32)
print cal
print "".join(map(chr, cal))
except IOError as e:
print "ERROR: %s" % e
time.sleep(2)
Are you using I2C pullups?
There's 1.8 kOhm pullups on RPi already so I didn't think adding more to the moteino would help. After researching I think the RPi i2c is just bugged and shouldn't be trusted. I wanted to use a different bus instead of serial so I can still use the serial to login but that might not work well.
Generally 4.7k is the preferred pullup values otherwise the signals can be too noisy or too inconsistent.
Read this excellent article (http://www.thebackshed.com/forum/uploads/BobD/2013-01-26_175209_I2C_-_Effects_of_Varying_Pullup_Resistors.pdf) on the effects of different value pullups on the I2C bus.
So if I can't change the RPi pullups can I just add to it on the moteino side? Is that considered parallel resistance?
Also, something I thought of that I'll have to try. The moteino is plugged into the computer via usb while the pi is plugged in to the same computer via a different usb connection. Should I try using the same power source? I've shared a ground but maybe I need to share the same vcc line too?
Are you sure there are pullups on the Pi?
What are the values and where is this specified officially?
Often I2C devices also use some 0.1uF decouplers near their VCC pins.
I don't think it matters how you power them. Both regulate down to 3.3V and GND is shared if they have the same source (same PC). If you have different sources with different GNDs you must watch out for GND loops and differences in GND potential, a tricky situation. I've seen weirdness happen when different supplies are used where the GNDs differ in potential by anywhere from 0 to a few volts. Sparks can fly if you connect pins from one side to the other.