Sunspot Home

more notes for a bad memory - - -

Setting up the Raspberry Pi with a working i2c bus

Objective
link to and control i2c devices

I have a long line to a cluster of i2c devices
One of the LM75 thermometers would not run all the time at the default baud rate of 100.000 Hz

many methods are offered via Google but none worked until I created a file :-

/etc/modprobe.d/i2c_speed.conf

containing

options i2c_bcm2708 baudrate=10000 - in the end I used a low speed - 10000 for my very long line
(but with Jessie - later - this was not needed - just the config.txt add on below)

I also added

i2c-bcm2708
i2c-dev
to
/etc/modules

Not sure if these were needed - check next time - remove them 1 by 1 to see if they are active - getting i2c speed to change was a long ordeal!!
modprobe -r i2c_bcm2708
modprobe i2c_bcm2708 baudrate=10000
also, I have at the the end of boot/config.txt
dtparam=i2c_arm=on
dtparam=i2c0_baudrate=10000
also
/etc/init.d/graham_startup
contains
modprobe i2c_bcm2708
modprobe i2c_dev

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

snd-bcm2835

i2c-bcm2708
i2c-dev

This is for a Mod B Pi

You can see the set speed in dmesg


The blassic code used for testing :-

#!/usr/sbin/blassic
'/home/pi/solar/solar_display.bas

LABEL GoAgain
PRINT "Hello"

'read the LM75 thermometers on the panel and tanks
shell "echo -n `i2c_lm75 72`>/var/www/ramdisk/T72.txt"
PAUSE 50
shell "echo -n `i2c_lm75 74`>/var/www/ramdisk/T74.txt"
PAUSE 50
shell "echo -n `i2c_lm75 75`>/var/www/ramdisk/T75.txt"
PAUSE 50
shell "echo -n `i2c_lm75 76`>/var/www/ramdisk/T76.txt"
PAUSE 50
shell "echo -n `i2c_lm75 77`>/var/www/ramdisk/T77.txt"

' bring the temperature values into blassic as strings
OPEN "/var/www/ramdisk/T72.txt" FOR INPUT AS #1 : INPUT #1,T72$ : CLOSE #1
PAUSE 50
OPEN "/var/www/ramdisk/T74.txt" FOR INPUT AS #1 : INPUT #1,T74$ : CLOSE #1
PAUSE 50
OPEN "/var/www/ramdisk/T75.txt" FOR INPUT AS #1 : INPUT #1,T75$ : CLOSE #1
PAUSE 50
OPEN "/var/www/ramdisk/T76.txt" FOR INPUT AS #1 : INPUT #1,T76$ : CLOSE #1
PAUSE 50
OPEN "/var/www/ramdisk/T77.txt" FOR INPUT AS #1 : INPUT #1,T77$ : CLOSE #1

PRINT "temperatures", T72$, T74$, T75$, T76$, T77$

'build a test web page

OPEN "/var/www/solar.html" FOR OUTPUT AS #1 : PRINT #1, "<html><head><META HTTP-EQUIV='refresh' CONTENT='5; URL=http://192.168.0.84/solar.html'></head><body><h1>Solar Hot Water</h1><p>Temperatures</p>" : CLOSE #1
OPEN "/var/www/solar.html" FOR APPEND AS #1 : PRINT #1, T72$, T74$, T75$, T76$, T77$, "<BR> </body></html>" : CLOSE #1

PRINT "done"
PAUSE 5000
GOTO GoAgain

SYSTEM

 

/*
LM75 I2C thermometer

i2c_lm75.c
-----------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#define I2C_SLAVE 0x0703 /* Change slave address */

int i2c;
char filename[20];
unsigned long address;
int rc;
unsigned char data[2];
int byte1;
int byte2;
int temperature;
double realtemp;
int main(int argc, char ** argv)
{
if (argc != 2) { /* error if we are not getting just 1 input after the program name */
fprintf(stderr, "usage: %s <address> \n",argv[0]);
exit(1);
}

/* address is the first number after the program name */
address = atoi(argv[1]);

i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */
rc=ioctl(i2c,I2C_SLAVE,address); /* set the i2c chip address */

read(i2c,data,2);

byte1 = data[0];
byte2 = data[1];

temperature = byte1*256+byte2;
temperature >>=7;
if (temperature>512)
temperature &= -1;

realtemp = (double)temperature / 2;
printf("%.1f", realtemp);

i2c = close(i2c);
return(0);
}

 


The test web page created -

<html> <head> <META HTTP-EQUIV='refresh' CONTENT='5; URL=http://192.168.0.84/solar.html'>
</head> <body> <h1>Solar Hot Water</h1> <p>Temperatures</p>  20.5	18.0	25.5	20.0	49.5	
<BR> </body> </html> 

Please email me if you want to swap notes

SUNSPOT HOME

more to come . . .