I am looking to control the Electron as Slave, Pi 3 As Master
I have some very basic code to try to have the Electron be the slave and the Pi be the master
Here is the Electron code
#include OneWire.h
#define MyAddress 0x40
byte DataToBeSend[1];
byte ReceivedData;
bool MessageNeedsProcessing =false;
char msg[255] = "";
void setup()
{
/* Initialize I2C Slave & assign call-back function 'onReceive' on 'I2CReceived'*/
Wire.begin(MyAddress);
Wire.onReceive(I2CReceived);
Wire.onRequest(I2CRequest);
}
void loop()
{
/* Increment DataToBeSend every second and make sure it ranges between 0 and 99 */
DataToBeSend[0] = (DataToBeSend[0] >= 99) ? 0 : DataToBeSend[0] + 1;
delay(1000);
if(MessageNeedsProcessing)
{
char myJSON[255] = "";
snprintf(myJSON, 255, "{\"Data\":{\"DeviceID\":%d,\"item\":%d}}","\"I2CTEST\"", msg);
// Trigger the integration
Particle.publish("Data", myJSON, PRIVATE);
MessageNeedsProcessing=false;
}
}
/* This function will automatically be called when RPi2 sends data to this I2C slave */
void I2CReceived(int NumberOfBytes)
{
/* WinIoT have sent data byte; read it */
while(Wire.available())
{
char ReceivedData = Wire.read();
msg = ReceivedData;
MessageNeedsProcessing=true;
}
}
/* This function will automatically be called when RPi2 requests for data from this I2C slave */
void I2CRequest()
{
/*Send data to WinIoT */
Wire.write(DataToBeSend,1);
}
Here is the Master code i am using on the pi to try to locate the Electron
public static async Task<IEnumerable<byte>> FindDevicesAsync()
{
IList<byte> returnValue = new List<byte>();
// ***
// *** Get a selector string that will return all I2C controllers on the system
// ***
string aqs = I2cDevice.GetDeviceSelector();
// ***
// *** Find the I2C bus controller device with our selector string
// ***
var dis = await DeviceInformation.FindAllAsync(aqs).AsTask();
if (dis.Count > 0)
{
const int minimumAddress = 1;
const int maximumAddress = 88;
for (byte address = minimumAddress; address <= maximumAddress; address++)
{
var settings = new I2cConnectionSettings(address);
settings.BusSpeed = I2cBusSpeed.FastMode;
settings.SharingMode = I2cSharingMode.Shared;
// ***
// *** Create an I2cDevice with our selected bus controller and I2C settings
// ***
using (I2cDevice device = await I2cDevice.FromIdAsync(dis[0].Id, settings))
{
if (device != null)
{
try
{
byte[] writeBuffer = new byte[1] { 0 };
device.Write(writeBuffer);
// ***
// *** If no exception is thrown, there is
// *** a device at this address.
// ***
returnValue.Add(address);
}
catch
{
// ***
// *** If the address is invalid, an exception will be thrown.
// ***
}
}
}
}
}
return returnValue;
}
It Does not seem like i am talking to the Electron. It should publish a command when i do…
I am new to the hardware side of things… Not sure if i have it hooked up correct I have the
PI_SDA(gpio02) to Electron_SDA(gpioD0) and PI_SCL(gpio03) to Electron_SCL(gpioD1)
Do i need to have some type of resistor in place to get this to work correct?
Any help would be great.
thanks
Chris