Advertisement
mikecmills2

GroveStreams Electric Imp Device

Jun 17th, 2014
2,861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Electric Imp Device Code for GroveStreams Example
  2. // A full "how to" guide for this agent can be found at https://www.grovestreams.com/developers/getting_started_elec_imp_temp.html
  3. //
  4.  
  5. // License:
  6. //  Copyright 2014 GroveStreams LLC.
  7. //  Licensed under the Apache License, Version 2.0 (the "License");
  8. //  you may not use this file except in compliance with the License.
  9. //  You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. //  Unless required by applicable law or agreed to in writing, software
  12. //  distributed under the License is distributed on an "AS IS" BASIS,
  13. //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. //  See the License for the specific language governing permissions and
  15. //  limitations under the License.
  16.  
  17. // Read data from MAX31855 chip on Adafruit breakout boards
  18. // Code developed by Electric Imp Forum members rivers,
  19. // mjkuwp94, Brown, as well as Hugo, peter, and others.
  20. // Modified for use by Mike Mills.
  21. //      pins:
  22. //      imp 1 CLK
  23. //      imp 2 CS
  24. //      imp 5 UNASSIGNED
  25. //      imp 7 UNASSIGNED
  26. //      imp 8 UNASSIGNED
  27. //      imp 9 DO
  28.  
  29. //Configure Pins
  30. hardware.spi189.configure(MSB_FIRST | CLOCK_IDLE_LOW , 1000);
  31. hardware.pin2.configure(DIGITAL_OUT); //chip select
  32.  
  33. // define variables
  34. temp32 <- 0;
  35. farenheit <- 0;
  36. celcius <- 0;
  37.  
  38. //Define functions
  39. function readChip189(){
  40.         //Get SPI data
  41.     hardware.pin2.write(0); //pull CS low to start the transmission of temp data  
  42.       //0[31..24],1[23..16],2[15..8],3[7..0]
  43.         temp32=hardware.spi189.readblob(4);//SPI read is totally completed here
  44.     hardware.pin2.write(1); // pull CS high
  45.         // Begin converting Binary data for chip 1
  46.     local tc = 0;
  47.     if ((temp32[1] & 1) ==1){
  48.         //Error bit is set
  49.         local errorcode = (temp32[3] & 7);// 7 is B00000111
  50.         local TCErrCount = 0;
  51.         if (errorcode>0){
  52.             //One or more of the three error bits is set
  53.             //B00000001 open circuit
  54.             //B00000010 short to ground
  55.             //B00000100 short to VCC
  56.             switch (errorcode){            
  57.             case 1:
  58.                 server.log("TC open circuit");
  59.                 break;
  60.             case 2:        
  61.                 server.log("TC short to ground");
  62.                 break;          
  63.             case 3:          
  64.                 server.log("TC open circuit and short to ground")
  65.                 break;
  66.             case 4:        
  67.                 server.log("TC short to VCC");
  68.                 break;
  69.             default:          
  70.                 //Bad coding error if you get here
  71.                 break;
  72.             }
  73.             TCErrCount+=1;
  74.             //if there is a fault return this number, or another number of your choice
  75.              tc= 67108864;
  76.         }
  77.         else
  78.         {
  79.              server.log("error in SPI read");
  80.         }      
  81.     }
  82.     else //No Error code raised
  83.     {
  84.         local highbyte =(temp32[0]<<6); //move 8 bits to the left 6 places
  85.         local lowbyte = (temp32[1]>>2); //move to the right two places 
  86.         tc = highbyte | lowbyte; //now have right-justifed 14 bits but the 14th digit is the sign    
  87.         //Shifting the bits to make sure negative numbers are handled
  88.         //Get the sign indicator into position 31 of the signed 32-bit integer
  89.         //Then, scale the number back down, the right-shift operator of squirrel/impOS
  90.         tc = ((tc<<18)>>18);
  91.         // Convert to Celcius
  92.             celcius = (1.0* tc/4.0);
  93.         // Convert to Farenheit
  94.         farenheit = (((celcius*9)/5)+32);
  95.         server.log(celcius + "°C");
  96.         server.log(farenheit + "°F");
  97.        
  98.         //Gather and pass the data to the Electric Imp Agent running in the cloud
  99.         local data = { id = hardware.getimpeeid(), mac = imp.getmacaddress(), temp = farenheit, voltage = hardware.voltage()}
  100.         agent.send("GroveStreams", data);
  101.        
  102.         imp.wakeup(20, readChip189); //Wakeup every 20 second and read data. Change this value to change your sample frequency.
  103.     }
  104. }
  105.  
  106.  
  107. //Begin executing program
  108. hardware.pin2.write(1); //Set the Chip Select pin to HIGH prior to SPI read
  109. readChip189();          //Read SPI data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement