Photon-raspberry pi communication

Hi everyone…
is there anyone who were able to create a communication between photon and raspberry pi (model 3)?
i found something like that in the official website but it was not helpfull for me because i followed the steps shown there and it created a virtual photon device well how can i connect the sensor to virtual device and make it read a value and send it to raspberry pi ?
originally i want to try and practice with l2C it there a possibility for me to do so or if you may know of a method similar to it i would like you to guide me through it

The last example in this tutorial uses I2C to communicate between a Raspberry Pi and a Photon:

2 Likes

thank you.
i will try this at the moment :smiley:

I’ve tried raspberry pi part and tried to run the example but its dose not work

Does this work?

sudo i2cdetect -y 1

If not, you should follow the link to configuring I2C on the Pi as you probably need to install a few tools and enable the I2C bus in /boot/config.txt.

earlier I’ve used " configuring I2C on the Pi " and i had install toold and enable the I2C bus but my screenshot is :

i use “rpi 3 B” . is there any problem in rpi version

@rickkas7 I am trying to expand the Raspberry pi I2C master to photon slave example in your particle-i2c-tutorial to read more registers than just register 0. I was able to successfully run the example you provided; however, I am unable to successfully read more than just register 0.

Here is my photon code

#include "Particle.h"

#include "I2CSlaveRK.h"

// Set up this Photon as an I2C device device, address 0x10, with 10 uint32 registers
I2CSlave device(Wire, 0x10, 10);

unsigned long lastCounterUpdate = 0;
uint32_t counter = 0;

void setup() {
	Serial.begin(9600);

	device.begin();
	device.setRegister(0,16777216);
	device.setRegister(1,1);
}

void loop() {

/*
	if (millis() - lastCounterUpdate >= 1000) {
		// Once per second increment register 0
		lastCounterUpdate = millis();

		Serial.printlnf("register 0 set to %u", counter);
		device.setRegister(0, counter++);
	}
*/

	uint16_t regAddr;
	while(device.getRegisterSet(regAddr)) {
		// regAddr was updated from the I2C master
		Serial.printlnf("from Pi master updated %u to %u", regAddr, device.getRegister(regAddr));
	}
}

Here is my Raspberry Pi Code. My assumption is that the i2c registers would be sequential in memory, but that doesn't seem to be the case.

#include <errno.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
  int file;
  int addr = 0x10;

  // Code adapted from:
  // http://elinux.org/Interfacing_with_I2C_Devices

  if ((file = open("/dev/i2c-1",O_RDWR)) < 0) {
    printf("Failed to open the bus.");
    exit(1);
  }

  if (ioctl(file,I2C_SLAVE,addr) < 0) {
    printf("Failed to acquire bus access and/or talk to slave.\n");
    exit(1);
  }

  char buf[16];
  buf[0] = buf[1] = 0;

  if (write(file, buf, 2) != 2) {
    printf("Failed to write to the i2c bus.\n");
    exit(1);
  }

  if (read(file, buf, 16) != 16) {
    printf("Failed to read from the i2c bus.\n");
    exit(1);
  }

  printf("reg0=%ld\n\n", *(unsigned long*)buf);

  for(int i=0; i<16; i++){
    printf("reg%i=%02X\n", i, buf[i]);
  }

  return 0;
}

This is the output I get from running the compiled file.

Any help you could offer would be greatly appreciated.

Yes, you need to read each register separately.

The write command sends a uint16_t value containing the register number to read, in little endian byte order. So basically just set buf[0] to be the register number 0-15 you want to read from.

And, of course, do both the write and the read in your loop.

That should do it.

1 Like

Ah yes, the register number in little endian is what tripped me up. Thanks for the help.

1 Like