We figured out how to get an Arduino and RoboRio to send and receive over I2C.
Everything works great while the computer is connected to the RoboRio via USB.
When we connect via WIFI or Network Cable to the RoboRio we get nothing received on either device.
Robot Code
package frc.robot;
import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.TimedRobot;
public class Robot extends TimedRobot {
public static I2C arduino;
@Override
public void robotInit() {
arduino = new I2C(I2C.Port.kOnboard, 8);
}
@Override
public void testInit() {
}
@Override
public void testPeriodic() {
byte[] sendData = "Gimme my data".getBytes();
byte[] receiveData = new byte[12];
arduino.transaction(sendData, sendData.length, receiveData, receiveData.length);
System.out.println("Received: " + new String(receiveData, 0, receiveData.length));
}
}
Arduino Code
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Wire.onRequest(requestEvent);
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent()
{
Serial.print("Received: ");
while (Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
Serial.println("");
}
void requestEvent()
{
Wire.write("From Arduino");
}
Why would this work over USB and not WIFI or Network Cable?