# Photon-raspberry pi communication

**URL:** <https://community.particle.io/t/photon-raspberry-pi-communication/34468>\
**Category:** General\
**Created:** [July 12, 2017, 8:21am UTC](https://community.particle.io/t/photon-raspberry-pi-communication/34468 "2017-07-12T08:21:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anasMansour](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/anasmansour/32/14897_2.png) [@anasMansour](https://community.particle.io/u/anasMansour)\
**Post date:** [July 12, 2017, 8:21am UTC](https://community.particle.io/t/photon-raspberry-pi-communication/34468/1 "2017-07-12T08:21:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [July 12, 2017, 11:34am UTC](https://community.particle.io/t/photon-raspberry-pi-communication/34468/2 "2017-07-12T11:34:53Z")

</div>

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

> **[rickkas7/particle-i2c-tutorial](https://github.com/rickkas7/particle-i2c-tutorial)**
>
> particle-i2c-tutorial - Using the I2C bus with Particle devices

---

<div class="post-metadata">

**Author:** ![anasMansour](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/anasmansour/32/14897_2.png) [@anasMansour](https://community.particle.io/u/anasMansour)\
**Post date:** [July 12, 2017, 11:50am UTC](https://community.particle.io/t/photon-raspberry-pi-communication/34468/3 "2017-07-12T11:50:09Z")

</div>

thank you.  
i will try this at the moment 😃

---

<div class="post-metadata">

**Author:** ![anasMansour](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/anasmansour/32/14897_2.png) [@anasMansour](https://community.particle.io/u/anasMansour)\
**Post date:** [July 12, 2017, 12:37pm UTC](https://community.particle.io/t/photon-raspberry-pi-communication/34468/4 "2017-07-12T12:37:10Z")

</div>

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

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/9/9e1875a9397329f1007819e15446f1e2b7343c28.jpg) ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/c/c413cdbd52ca296a909328a749225bf6454b1e4b.jpg)

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [July 12, 2017, 12:48pm UTC](https://community.particle.io/t/photon-raspberry-pi-communication/34468/5 "2017-07-12T12:48:20Z")

</div>

Does this work?

```auto
sudo i2cdetect -y 1

```

If not, you should follow the link to [configuring I2C on the Pi](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c) as you probably need to install a few tools and enable the I2C bus in /boot/config.txt.

---

<div class="post-metadata">

**Author:** ![anasMansour](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/anasmansour/32/14897_2.png) [@anasMansour](https://community.particle.io/u/anasMansour)\
**Post date:** [July 12, 2017, 12:54pm UTC](https://community.particle.io/t/photon-raspberry-pi-communication/34468/6 "2017-07-12T12:54:16Z")

</div>

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

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/b/b25b8fa1e314e3d8fafbc9f5060929a84bb22737.jpg)

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

---

<div class="post-metadata">

**Author:** ![ZachZ](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/zachz/32/16222_2.png) [@ZachZ](https://community.particle.io/u/ZachZ)\
**Post date:** [December 18, 2017, 9:34pm UTC](https://community.particle.io/t/photon-raspberry-pi-communication/34468/7 "2017-12-18T21:34:53Z")

</div>

@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

```auto
#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.

```auto
#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.

> [@](#):
>
> pi@Leaf\_1:~/I2C $ sudo ./a.out  
> reg0=16777216
> 
> reg0=00  
> reg1=00  
> reg2=00  
> reg3=01  
> reg4=01  
> reg5=01  
> reg6=01  
> reg7=01  
> reg8=01  
> reg9=01  
> reg10=01  
> reg11=01  
> reg12=01  
> reg13=01  
> reg14=01  
> reg15=01

Any help you could offer would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [December 18, 2017, 10:06pm UTC](https://community.particle.io/t/photon-raspberry-pi-communication/34468/8 "2017-12-18T22:06:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ZachZ](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/zachz/32/16222_2.png) [@ZachZ](https://community.particle.io/u/ZachZ)\
**Post date:** [December 19, 2017, 4:40pm UTC](https://community.particle.io/t/photon-raspberry-pi-communication/34468/9 "2017-12-19T16:40:53Z")

</div>

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