[PD] Sensors GPIO Raspberry Pi Pd

Martin Peach martin.peach at sympatico.ca
Fri Apr 19 15:36:30 CEST 2013


Hi Julian,
Yes I've been messing with coding it in c on the pi and sending the data 
to a [netreceive] in a Pd patch on another machine. I'm attaching the 
source code for the pi part and the Pd patch.
The code can be compiled on the pi with
gcc -o hello hello.c
You need to set the IP address of the receiving machine in the code (I 
have 192.168.2.15, it could be 127.0.0.1 for the same machine).
I tried changing the baud rate with
sudo modprobe -r i2c_bcm2708
sudo modprobe i2c)bcn2708 baudrate=90000
but it works fine at the default 100000.
It seems that you only need to write the command once, after that simply 
reading gets you another packet. Using a combined write/read operation 
only works half the time, as I also found on the Arduino. All you need 
to do is write the 0x4C command once, wait a millisecond or so and then 
read it.
Another issue is that I tried this with the 8X1 sensor, not the 4X4 one, 
so the code reads 19 bytes (need to change the expected read size in the 
code). The 4X4 sensor sends 35 bytes which is 3 more than the i2c driver 
maximum, so you may not get the last part of a packet.
I'll try it later with a 4X4 sensor to see what happens.

Martin

On 2013-04-19 07:01, Julian Brooks wrote:
> Hi Martin,
>
> Did you manage to make any progress with the sensor on the Pi?
> I also wanted to ask whether the output we're receiving from i2cdump
> makes any sense to you as it doesn't to us currently?  Tried searching
> around for possible info on the 'XX' & 'ff' but drawing a blank here.
>
> Cheers,
>
> Julian
>
>
> On 13 April 2013 01:11, Julian Brooks <jbeezez at gmail.com
> <mailto:jbeezez at gmail.com>> wrote:
>
>     Hey all,
>
>     Some success finally:
>
>     Hurrah!!
>
>     The scl breakout pin on the pi proto plate wasn't working properly.
>
>     When unscrewed halfway it works, when fully screwed in it doesn't.
>
>     So - now got this:
>
>     i2cdetect -y 0
>
>     0 1 2 3 4 5 6 7 8 9 a b c d e f
>
>     00: -- -- -- -- -- -- -- 0a -- -- -- -- --
>
>     10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>
>     20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>
>     30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>
>     40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>
>     50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>
>     60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
>
>     70: -- -- -- -- -- -- -- --
>
>     and
>
>     i2cdump -y 0 0xa
>     No size specified (using byte-data access)
>
>     Gives a whole host of stuff I don't yet understand but I don't care
>     currently as something is actually happening.
>
>     Will figure out a way of saving the console info (any hints
>     anyone?)  as it gets badly mangled when cutting and pasting  but
>     basically something like this:
>
>     i2cdump -y 0 0xa
>     No size specified (using byte-data access)
>           0  1  2  3  4  5  6  7  8  9  a  b  c  d  e
>     f                      0123456789abcdef
>     00: ff XX XX XX XX XX XX ff XX XX XX XX ff ff ff XX    .XXXXXX.XXXX...X
>     10: XX ff XX XX XX XX XX ff XX ff XX ff XX XX ff XX    X.XXXXX.X.X.XX.X
>     20: ff XX XX ff XX XX ff XX XX XX XX ff XX XX XX ff    .XX.XX.XXXX.XXX.
>     30: XX ff XX ff XX XX XX XX ff ff ff XX XX XX XX XX    X.X.XXXX...XXXXX
>     40: XX XX XX ff XX ff XX XX XX 64 XX XX d5 XX XX ff    XXX.X.XXXdXX?XX.
>     50: XX ff XX XX XX XX XX XX XX ff XX XX ff XX XX XX    X.XXXXXXX.XX.XXX
>     60: ff XX XX XX ff XX XX XX XX XX XX XX XX ff XX XX    .XXX.XXXXXXXX.XX
>     70: XX XX XX XX XX XX XX ff XX XX XX XX XX XX XX XX    XXXXXXX.XXXXXXXX
>     80: XX ff XX XX ff ff XX XX XX ff XX XX XX XX XX XX    X.XX..XXX.XXXXXX
>     90: XX XX ff XX XX ff XX ff XX ff ff XX XX ff ff XX    XX.XX.X.X..XX..X
>     a0: XX ff XX XX ff XX XX XX XX XX XX XX XX XX ff XX    X.XX.XXXXXXXXX.X
>     b0: XX XX ff XX XX XX ff XX XX ff XX XX XX XX XX XX    XX.XXX.XX.XXXXXX
>     c0: XX XX XX XX ff XX XX ff ff XX XX ff ff XX XX XX    XXXX.XX..XX..XXX
>     d0: XX XX XX XX XX ff XX ff XX XX XX XX XX ff XX ff    XXXXX.X.XXXXX.X.
>     e0: XX XX XX ff XX ff XX XX XX XX XX XX XX XX ff XX    XXX.X.XXXXXXXX.X
>     f0: ff XX ff ff XX XX XX XX XX XX XX XX XX XX ff XX    .X..XXXXXXXXXX.X
>
>
>     Progress at least.
>
>     Cheers,
>
>     Julian
>
>
>
>
>
>
>
>     On 12 April 2013 11:27, Julian Brooks <jbeezez at gmail.com
>     <mailto:jbeezez at gmail.com>> wrote:
>
>         Message resent for thread archives with smaller picture size.
>
>         Julian
>
>         ---------- Forwarded message ----------
>         From: *Julian Brooks* <jbeezez at gmail.com <mailto:jbeezez at gmail.com>>
>         Date: 11 April 2013 19:24
>         Subject: Re: [PD] Sensors GPIO Raspberry Pi Pd
>         To: Martin Peach <martin.peach at sympatico.ca
>         <mailto:martin.peach at sympatico.ca>>
>         Cc: PD List <pd-list at iem.at <mailto:pd-list at iem.at>>
>
>
>         Hey Martin / list,
>
>         Finally got all the stuff and ...
>
>         It’s not working!
>
>         We spent the day soldering cables and connecting stuff up as per
>         the Omron ‘App Note 01’ spec sheet.
>
>         Started off super-conservative using the  I2C level converter
>         (case 3 page 4) http://www.adafruit.com/products/757#Blog/Flickr
>
>         We tried resistors on both sides (being super paranoid!) and
>         then we took  the low (Pi) side ones off.
>
>         We then moved on to case 2 page 3 of this same document…
>
>         At each stage we checked with “I2Cdetect –Y 1” and nothing was
>         visible.
>
>         The grid shows no attached devices every time we run it.
>
>         We re-booted at every stage following the various online
>         tutorials/methods of setting up I2C GPIO on the Pi (checked &
>         double checked).
>
>
>         As you can see we’re using a pi protoplate:
>
>         https://www.adafruit.com/products/801
>
>         In the photo I’ve attached the cables are coded as follows:
>
>         Orange           GND
>
>         Yellow            5v
>
>         Blue                SCL
>
>         Green             SDA
>
>         The white is also 5v for the pull up resistors.
>
>         The resistor values are 4.7k btw.
>
>         We have tested the cable that terminates at the sensor and all
>         that is OK.
>
>         I put a multimeter on the GND and SDA solder points on the
>         sensor itself and got 3.7v…
>
>         I put a multimeter on the GND and SCL solder points on the
>         sensor itself and got 0.0v…
>
>         Don’t know if this means anything or could be useful to know!
>
>         Stuck and frustrated now but hey, 3 weeks ago I knew absolutely
>         bugger all about any of this and now I do (sort of).
>
>         I'm thinking we could do with the most basic i2c sensor we can
>         find as we have nothing to compare.
>
>         Tonight I'm going to d/l a fresh raspbian and start from scratch
>         to check that end.
>
>         Feel like if we can't get past the 'i2c-tools' tests we're
>         screwed - never mind getting it in and out of Pd.
>
>         Any thoughts/pointers/options from anyone will be really
>         appreciated?
>
>
>         Cheers,
>
>
>         Julian
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Pd-list at iem.at mailing list
> UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
>

-------------- next part --------------
#N canvas 0 0 450 611 10;
#X obj 34 79 route d6t8l;
#X obj 34 111 unpack 1 2 3 4 5 6 7 8 9;
#X obj 34 39 netreceive 33333 1;
#X floatatom 34 197 5 0 0 0 - - -;
#X floatatom 74 197 5 0 0 0 - - -;
#X floatatom 114 197 5 0 0 0 - - -;
#X floatatom 154 197 5 0 0 0 - - -;
#X floatatom 194 197 5 0 0 0 - - -;
#X floatatom 234 197 5 0 0 0 - - -;
#X floatatom 274 197 5 0 0 0 - - -;
#X floatatom 314 197 5 0 0 0 - - -;
#X floatatom 354 197 5 0 0 0 - - -;
#X text 59 251 Martin Peach 2013_03_18;
#X obj 57 305 vsl 15 128 0 500 0 0 empty empty empty 0 -9 0 10 -260097
-1 -1 7188 1;
#X obj 77 305 vsl 15 128 0 500 0 0 empty empty empty 0 -9 0 10 -4034
-1 -1 6401 1;
#X obj 97 305 vsl 15 128 0 500 0 0 empty empty empty 0 -9 0 10 -4034
-1 -1 6350 1;
#X obj 117 305 vsl 15 128 0 500 0 0 empty empty empty 0 -9 0 10 -4034
-1 -1 6401 1;
#X obj 137 305 vsl 15 128 0 500 0 0 empty empty empty 0 -9 0 10 -4034
-1 -1 6452 1;
#X obj 157 305 vsl 15 128 0 500 0 0 empty empty empty 0 -9 0 10 -4034
-1 -1 6502 1;
#X obj 177 305 vsl 15 128 0 500 0 0 empty empty empty 0 -9 0 10 -4034
-1 -1 6502 1;
#X obj 197 305 vsl 15 128 0 500 0 0 empty empty empty 0 -9 0 10 -4034
-1 -1 6553 1;
#X obj 217 305 vsl 15 128 0 500 0 0 empty empty empty 0 -9 0 10 -4034
-1 -1 6553 1;
#X connect 0 0 1 0;
#X connect 1 0 3 0;
#X connect 1 1 4 0;
#X connect 1 2 5 0;
#X connect 1 3 6 0;
#X connect 1 4 7 0;
#X connect 1 5 8 0;
#X connect 1 6 9 0;
#X connect 1 7 10 0;
#X connect 1 8 11 0;
#X connect 2 0 0 0;
#X connect 3 0 13 0;
#X connect 4 0 14 0;
#X connect 5 0 15 0;
#X connect 6 0 16 0;
#X connect 7 0 17 0;
#X connect 8 0 18 0;
#X connect 9 0 19 0;
#X connect 10 0 20 0;
#X connect 11 0 21 0;
-------------- next part --------------
// based on elinux.org/interfacing_with_I2C_Devices
// Martin Peach 20130416
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <unistd.h>
//#include <sys/ioctl.h>
#include <sys/types.h>
//#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORT 33333
#define IP "192.168.2.15"
// this is Omron's crc code from the DST-44L/DST-8L Thermal Sensor Whitepaper
unsigned char calc_crc (unsigned char data);
int D6T_checkPEC (unsigned char *buf, int pPEC);

unsigned char calc_crc (unsigned char data)
{
  int           index;
  unsigned char temp;

  for (index = 0; index < 8; index++)
  {
    temp = data;
    data <<= 1;
    if (temp & 0x80) data ^= 0x07;
  }
  return data;
}

int D6T_checkPEC (unsigned char *buf, int pPEC)
{
  unsigned char crc = 0;
  int           i;

  //crc = calc_crc( 0x14 );
  //crc = calc_crc( 0x4C ^ crc );
  crc = calc_crc( 0x15 ^ crc );
  for ( i = 0; i < pPEC; i++)
  {
    crc = calc_crc(buf[i] ^ crc);
    //    if (crc == buf[pPEC]) printf("MATCH at %d\n", i);
  }
  printf("D6T_checkPEC says 0x%02X\n", crc);
  return (crc == buf[pPEC]);
}

int main (int argc, char **argv)
{
  int 				file;
  char 				filename[32];
  int                           addr = 0x0A;
  unsigned char                 inbuf[63];
  unsigned char                 outbuf;
  char                          netbuf[256];
  const char                    *buffer;
  int                           i, j, s;
  int                           initialized = 0;
  struct sockaddr_in            sock;

  // set up a socket to send UDP datagrams through
  if (( s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP )) < 0)
  {
    printf ("Unable to create socket (%d)\n", errno);
    buffer = strerror(errno);
    printf(">> %s <<\n", buffer);
    exit ( EXIT_FAILURE );
  }
  memset (( char *) &sock, 0 , sizeof(sock) );
  sock.sin_family = AF_INET;
  sock.sin_port = htons ( PORT );
  if ( inet_aton( IP, &sock.sin_addr ) == 0 )
  {
    printf ( "Unable to make address from %s\n", IP );
    exit ( EXIT_FAILURE );
  }
  // The Omron sensor streams data after being initialized
  if ( argc > 0 ) initialized = atoi(argv[1]);
  printf("%s: initialized:%d\n", argv[0], initialized);
  // Open the I2C connection
  sprintf(filename, "/dev/i2c-1");
  if ((file = open(filename, O_RDWR)) < 0)
  {
    printf ("Failed to open the bus. (%d)\n", errno);
    exit (EXIT_FAILURE);
  }

  if (ioctl(file, I2C_SLAVE, addr) < 0)
  {
    printf ("Failed to acquire bus access and/or talk to slave. (%d)\n", errno);
    exit (EXIT_FAILURE);
  }
  // start reading packets
  do
  {
    do
    {
      // clear the inbuf first
      for ( i = 0; i < 19; ++i) inbuf[i] = 0;

      outbuf = 0x4C; // the command
      // we only need to write the command once.
      // After that the sensor will return data
      // any time it is read.
      if (! initialized)
      {
        if (write( file, &outbuf, 1 ) != 1)
        {
          printf("Write failed (%d)\n", errno);
          buffer = strerror(errno);
          printf(">> %s <<\n", buffer);
          exit (EXIT_FAILURE);
        }
        initialized = 1;
      }
      if (read (file, inbuf, 19) != 19)
      {
        printf("Read failed (%d)\n", errno);
        buffer = strerror(errno);
        printf(">> %s <<\n", buffer);
      }
    } while (!D6T_checkPEC (inbuf, 18));

    for (i = 0; i < 18; i += 2)
    {
      printf("%d ", inbuf[i]+(inbuf[i+1]<<8));
    }
    printf("<0x%02X>\n", inbuf[18]);
    j = sprintf (netbuf, "d6t8l");
    for (i = 0; i < 18; i += 2)
    {
      j += sprintf(&netbuf[j], " %d", inbuf[i] + (inbuf[i+1]<<8));
    }
    j += sprintf(&netbuf[j], "\n"); // CR NOT semicolon
    if (sendto ( s, netbuf, j, 0, (const struct sockaddr *)&sock, sizeof(sock)) < 0 )
    {
      printf ("sendto error (%d)\n", errno);
      buffer = strerror (errno);
      printf (">> %s <<\n", buffer);
    }
    printf("%s\n", netbuf);
    usleep (400000);
  } while (1);
  close(file);
 return EXIT_SUCCESS;
}
 


More information about the Pd-list mailing list