I am trying to get the max31855 thermocouple board from Adafruit working. I’ve copied the code from github and added a few cosmetic changes and server.log function but no change to the spi readblob line. But I am getting all 0’s returned from the thermocouple board.
I verified the thermocouple board is working and the thermocouple is connected correctly (via Arduino code and a different PID controller). The board is connected to the electric imp per the tutorial (http://learn.adafruit.com/connecting-the-max31855-thermocouple-amplifier-breakout-to-an-electric-imp/get-the-code)
clock=pin1
chip select=pin2
data=pin9
But when I run the dode I get the results:
2014-01-19 18:54:07 UTC-8: [Status] Device booting; 4.56% program storage used
2014-01-19 18:54:07 UTC-8: [Device] Device started as Thermocouple test
2014-01-19 18:54:07 UTC-8: [Device] 0000 0000 0000 0000
2014-01-19 18:54:07 UTC-8: [Device] 0°C
2014-01-19 18:54:07 UTC-8: [Device] 32°F
Here is my code for the device (no agent code):
`
// Read data from MAX31855 chip on Adafruit breakout boards
// Code developed by Electric Imp Forum members rivers,
// mjkuwp94, Brown, as well as Hugo, peter, and others.
// Modified for use by Joel Wehr.
// pins:
// imp 1 CLK
// imp 2 CS
// imp 5 UNASSIGNED
// imp 7 UNASSIGNED
// imp 8 UNASSIGNED
// imp 9 DO
//Configure Pins
spi <- hardware.spi189;
spi.configure(MSB_FIRST | CLOCK_IDLE_LOW , 1000);
chipSelPin <- hardware.pin2;
chipSelPin.configure(DIGITAL_OUT); //chip select
// define variables
farenheit <- 0;
celcius <- 0;
// print binary data (first 16 bits)
function logBin(temp32) {
local res=((temp32[0]&128).tostring()+(temp32[0]&64).tostring()+(temp32[0]&32).tostring()+(temp32[0]&16).tostring());
res+=(" “+(temp32[0]&8).tostring()+(temp32[0]&4).tostring()+(temp32[0]&2).tostring()+(temp32[0]&1).tostring());
res+=(” “+(temp32[1]&128).tostring()+(temp32[1]&64).tostring()+(temp32[1]&32).tostring()+(temp32[1]&16).tostring());
res+=(” "+(temp32[1]&8).tostring()+(temp32[1]&4).tostring()+(temp32[1]&2).tostring()+(temp32[1]&1).tostring());
server.log(res);
}
//Define functions
function readChip189(){
//Get SPI data
chipSelPin.write(0); //pull CS low to start the transmission of temp data
local temp32 = spi.readblob(4); //SPI read is totally completed here
chipSelPin.write(1); // pull CS high
logBin(temp32);
// Begin converting Binary data for chip 1
local tc = 0;
if ((temp32[1] & 1) ==1){
//Error bit is set
local errorcode = (temp32[3] & 7);// 7 is B00000111
local TCErrCount = 0;
if (errorcode>0){
//One or more of the three error bits is set
//B00000001 open circuit
//B00000010 short to ground
//B00000100 short to VCC
switch (errorcode){
case 1:
server.log("TC open circuit");
break;
case 2:
server.log("TC short to ground");
break;
case 3:
server.log("TC open circuit and short to ground")
break;
case 4:
server.log("TC short to VCC");
break;
default:
//Bad coding error if you get here
break;
}
TCErrCount+=1;
//if there is a fault return this number, or another number of your choice
tc= 67108864;
}
else
{
server.log("error in SPI read");
}
}
else //No Error code raised
{
local highbyte =(temp32[0]<<6); //move 8 bits to the left 6 places
local lowbyte = (temp32[1]>>2); //move to the right two places
tc = highbyte | lowbyte; //now have right-justifed 14 bits but the 14th digit is the sign
//Shifting the bits to make sure negative numbers are handled
//Get the sign indicator into position 31 of the signed 32-bit integer
//Then, scale the number back down, the right-shift operator of squirrel/impOS
tc = ((tc<<18)>>18);
// Convert to Celcius
celcius = (1.0* tc/4.0);
// Convert to Farenheit
farenheit = (((celcius*9)/5)+32);
server.log(celcius + "°C");
server.log(farenheit + "°F");
//agent.send("Xively", farenheit);
imp.wakeup(10, readChip189); //Wakeup every 10 second and read data.
}
}
//Begin executing program
server.log(“Device started as Thermocouple test”);
chipSelPin.write(1); //Set the Chip Select pin to HIGH prior to SPI read
readChip189(); //Read SPI data
`