Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sensirion SHT1x temperature sensor library
02-05-2013, 01:37 AM,
#1
Sensirion SHT1x temperature sensor library
Hello,

I'm new to Pinguino but i worked with arduino in the past. But for now i'm trying to get a SHT15 temperature / humidity sensor working on my pinguino. For arduino it is harder to find working programs, but for pinguino i cant find a thing. So i tried to rework the arduino one for my pinguino but i failed so i need a bit help.
I started with the arduino package on their site: http://playground.arduino.cc/code/Sensirion

The "strange" solutions i found where changing const bool to const BOOL, deleting comments and merging the sensirion.h file with the SimpleSensirion program. i also deleted the serial things because the output is for later. I want to build an incubator heater with it.

My code so far:
Code:
#ifndef Sensirion_h
#define Sensirion_h
#define CRC_ENA
#define DATA_PU 1
#define PULSE_LONG  delayMicroseconds(3)
#define PULSE_SHORT delayMicroseconds(1)
#define measTemp(result)  meas(TEMP, result, BLOCK)
#define measHumi(result)  meas(HUMI, result, BLOCK)

// User constants
const uint8_t TEMP     =     0;
const uint8_t HUMI     =     1;
const BOOL   BLOCK    =  true;
const BOOL    NONBLOCK = false;

// Status register bit definitions
const uint8_t HIGH_RES =  0x00;  // 14-bit Temp / 12-bit RH - 4s / meting
const uint8_t LOW_RES  =  0x01;  // 12-bit Temp / 8-bit RH - 1s / meting
const uint8_t NORELOAD =  0x02;  // No reload of calibrarion data
const uint8_t HEAT_ON  =  0x04;  // Built-in heater on
const uint8_t BATT_LOW =  0x40;  // VDD < 2.47V

// Function return code definitions
const uint8_t S_Err_NoACK  = 1;  // ACK expected but not received
const uint8_t S_Err_CRC    = 2;  // CRC failure
const uint8_t S_Err_TO     = 3;  // Timeout
const uint8_t S_Meas_Rdy   = 4;  // Measurement ready

class Sensirion
{
  private:
    uint8_t _pinData;
    uint8_t _pinClock;
    uint16_t *_presult;
    uint8_t _stat_reg;
#ifdef CRC_ENA
    uint8_t _crc;
#endif
    uint8_t getResult(uint16_t *result);
    uint8_t putByte(uint8_t value);
    uint8_t getByte(bool ack);
    void startTransmission(void);
    void resetConnection(void);
#ifdef CRC_ENA
    void calcCRC(uint8_t value, uint8_t *crc);
    uint8_t bitrev(uint8_t value);
#endif

  public:
    Sensirion(uint8_t dataPin, uint8_t clockPin);
    uint8_t measure(float *temp, float *humi, float *dew);  
    uint8_t meas(uint8_t cmd, uint16_t *result, bool block);
    uint8_t measRdy(void);
    uint8_t writeSR(uint8_t value);
    uint8_t readSR(uint8_t *result);
    uint8_t reset(void);
    float calcTemp(uint16_t rawData);
    float calcHumi(uint16_t rawData, float temp);
    float calcDewpoint(float humi, float temp);
};

#endif  // #ifndef Sensirion_h





const uint8_t dataPin  =  2;
const uint8_t clockPin =  3;

float temperature;
float humidity;
float dewpoint;

Sensirion tempSensor = Sensirion(dataPin, clockPin);

void setup()
{

}

void loop()
{
  tempSensor.measure(&temperature, &humidity, &dewpoint);

}
And the errors:
Code:
Board: PIC32 Pinguino 220
Proc: 32MX220F032D
File: C:\x4-easy-rev685mod\projects\Sensirion\examples\SimpleSensirion\SimpleSensirion.pde
C:\x4-easy-rev685mod\source\/user.c:82:11: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'tempSensor'

C:\x4-easy-rev685mod\source\/user.c:91:3: error: 'tempSensor' undeclared (first use in this function)

error while linking C:\x4-easy-rev685mod\projects\Sensirion\examples\SimpleSensirion\SimpleSensirion.o
I'm not sure that what i did is correct. If i look to the user.c file, all #define lines are gone.
(Btw, using pinguino X.4 on a windows 8 X64, pinguino drivers correctly installed, including the whole list as on the wiki for windows 7)
Help is welcome, thanks
SkulltrailDualQuadXeon5420;MacProDualG5;AsusE350;AtomInaVCR;PrecisionM90;EEE1000h;NixdorfComputerfrom1970;Xbox;X360;PS1;PS2;CDI;ATARI2600;LasVegas;C128;ZXSpectrum;Dreamcast;Saturn;Mastersystem;Megadrive;nes;snes;N64;wii;Videopack;Colecovision,GB/P/C/A;DS;
Reply
02-05-2013, 08:21 AM, (This post was last modified: 02-05-2013, 08:48 AM by pingotg.)
#2
RE: Sensirion SHT1x temperature sensor library
Convert it to C and it should be OK.

A full re-write might be easier & quicker.

The code on sparkfun might help http://wiring.org.co/learning/basics/hum...sht15.html

Be sure to check you don't drive the I/O too fast (see datasheets).

John
Reply
02-05-2013, 12:18 PM,
#3
RE: Sensirion SHT1x temperature sensor library
I'm not that good with c and c++ and their differences. As for now it would be great if someone can help me doing it. I do not understand much about the library things yet so still a lot to learn. I do prefer remaking the library so it is usable for everyone

And yes i know about the overdrive of the I/O. In 14/12 bit mode it can only do 1 measurement each 4 seconds and in 12/8 bit it can do 1 measurement in 1s. This to keep the accuracy right and to prevent the sensor to heat up by itself. The delay isn't yet in the code because the code isn't working yet
SkulltrailDualQuadXeon5420;MacProDualG5;AsusE350;AtomInaVCR;PrecisionM90;EEE1000h;NixdorfComputerfrom1970;Xbox;X360;PS1;PS2;CDI;ATARI2600;LasVegas;C128;ZXSpectrum;Dreamcast;Saturn;Mastersystem;Megadrive;nes;snes;N64;wii;Videopack;Colecovision,GB/P/C/A;DS;
Reply
02-05-2013, 12:31 PM, (This post was last modified: 02-05-2013, 12:31 PM by pingotg.)
#4
RE: Sensirion SHT1x temperature sensor library
Throw away the C++. Write your own C or use the sparkfun or other C.

If it's too hard, change sensor to one that works with Pinguino without needing code you can't write.

John
Reply
02-05-2013, 12:54 PM,
#5
RE: Sensirion SHT1x temperature sensor library
i'm trying.
It has to be that sensor because it was the most accurate i found (+-0.3°) with a linear resolution of 0.01°. It is to rebuild an incubator. The temperature has to be as perfect as possible to have the best results. If it isn't, the chickens or whatever are unhealthy when they are born such as crooked legs etc.
I prefer to do one measurement/s at 12/8 bit (reso is 0.04°) and to control the lights with a Solit state relais. The electronics are fine, it is only the sensor
SkulltrailDualQuadXeon5420;MacProDualG5;AsusE350;AtomInaVCR;PrecisionM90;EEE1000h;NixdorfComputerfrom1970;Xbox;X360;PS1;PS2;CDI;ATARI2600;LasVegas;C128;ZXSpectrum;Dreamcast;Saturn;Mastersystem;Megadrive;nes;snes;N64;wii;Videopack;Colecovision,GB/P/C/A;DS;
Reply
02-05-2013, 01:43 PM,
#6
RE: Sensirion SHT1x temperature sensor library
Code:
/*-----------------------------------------------------
Author:  --<>
Date: Thu May  2 21:09:52 2013
Description:
-----------------------------------------------------*/
  
int temperatureCommand  = 0B00000011;  // command used to read temperature
int humidityCommand = 0B00000101;  // command used to read humidity

int clockPin = 8;  // pin used for clock
int dataPin  = 9;  // pin used for data
int ack;  // track acknowledgment for errors
int val;
float temperature;
float humidity;

// commands for reading/sending data to a SHTx sensor

// send a command to the SHTx sensor
void sendCommandSHT(int command, int dataPin, int clockPin)
{
  int ack;

  // transmission start
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, LOW);

  // shift out the command (the 3 MSB are address and must be 000, the last 5 bits are the command)
  // XXXXXXX
  //shiftOut(dataPin, clockPin, MSBFIRST, command);

  // verify we get the right ACK
  digitalWrite(clockPin, HIGH);
  pinMode(dataPin, INPUT);
  ack = digitalRead(dataPin);
  if (ack != LOW)
    Serial.println("ACK error 0");
  digitalWrite(clockPin, LOW);
  ack = digitalRead(dataPin);
  if (ack != HIGH)
    Serial.println("ACK error 1");
}

// wait for the SHTx answer
void waitForResultSHT(int dataPin)
{
  int ack;
  int i;

  pinMode(dataPin, INPUT);
  for (i = 0; i < 100; ++i)
  {
    delay(20);
    ack = digitalRead(dataPin);
    if (ack == LOW)
      break;
  }
  if (ack == HIGH)
    Serial.println("ACK error 2");
}

// get data from the SHTx sensor
int getData16SHT(int dataPin, int clockPin)
{
  int val;

  // get the MSB (most significant bits)
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  // XXXXXXX
  //val = shiftIn(dataPin, clockPin, MSBFIRST, 8, 3);
  val *= 256; // this is equivalent to val << 8;

  // send the required ACK
  pinMode(dataPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);

  // get the LSB (less significant bits)
  pinMode(dataPin, INPUT);
  // XXXXXXX  
  //val |= shiftIn(dataPin, clockPin, MSBFIRST, 8, 3);
  return val;
}

// skip CRC data from the SHTx sensor
void skipCrcSHT(int dataPin, int clockPin)
{
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);
}


void setup()
{
  Serial.begin(9600); // open serial at 9600 bps
}

void loop()
{
  // read the temperature and convert it to centigrades
  sendCommandSHT(temperatureCommand, dataPin, clockPin);
  waitForResultSHT(dataPin);
  val = getData16SHT(dataPin, clockPin);
  skipCrcSHT(dataPin, clockPin);
  temperature = (float)val * 0.01 - 40;
  Serial.printf("temperature: %f\r\n", temperature);
  // read the humidity
  sendCommandSHT(humidityCommand, dataPin, clockPin);
  waitForResultSHT(dataPin);
  val = getData16SHT(dataPin, clockPin);
  skipCrcSHT(dataPin, clockPin);
  humidity = -4.0 + 0.0405 * val + -0.0000028 * val * val;
  Serial.printf(" humidity: %f\r\n", humidity);
  delay(200); // wait for 200 milliseconds for next reading
}

I slightly reworked this code (to which pingotg linked for you) so that it compiles cleanly, but see the XXXXXs.

Looks like you'll need to write your own shiftIn() and shiftOut() functions.
Reply
02-05-2013, 07:05 PM, (This post was last modified: 02-05-2013, 08:46 PM by fjutrackx.)
#7
RE: Sensirion SHT1x temperature sensor library
Thanks for everything. But i don't know what i'm doing wrong. Even with your code i still have errors and cannot compile it. Tried both on windows XP and 8.
And the second problem.. i just don't understand the code that good. Libraries are not my thing i guess. I worked, tested and searched for 3 days and i still have nothing more. So i think i'll give up and get my arduino board back. That code is working for me so far.

EDIT: But don't be to enthousiast.. I'm trying to understand the code. Maybe.. ever.. i'll succeed! XD
SkulltrailDualQuadXeon5420;MacProDualG5;AsusE350;AtomInaVCR;PrecisionM90;EEE1000h;NixdorfComputerfrom1970;Xbox;X360;PS1;PS2;CDI;ATARI2600;LasVegas;C128;ZXSpectrum;Dreamcast;Saturn;Mastersystem;Megadrive;nes;snes;N64;wii;Videopack;Colecovision,GB/P/C/A;DS;
Reply
02-05-2013, 11:42 PM, (This post was last modified: 02-05-2013, 11:44 PM by fjutrackx.)
#8
RE: Sensirion SHT1x temperature sensor library
What do you think of:
Code:
/*-----------------------------------------------------
Author:  --<>
Date: Thu May  2 21:09:52 2013
Description:
-----------------------------------------------------*/
    
int temperatureCommand  = 0B00000011;  // command used to read temperature
int humidityCommand = 0B00000101;  // command used to read humidity
int bitorder;
int clockPin = 8;  // pin used for clock
int dataPin  = 9;  // pin used for data
int ack;  // track acknowledgment for errors
int val;
float temperature;
float humidity;

// commands for reading/sending data to a SHTx sensor
void shiftOut(int dataPin, int clockPin, int bitOrder, int commando)
{
  int bits = 8;
  int wait = 0;
  int i;
  for (i = 0; i < bits; i++)  {
    if (bitOrder == 1)
      digitalWrite(dataPin, !!(commando & (1 << i)));
    else    
      digitalWrite(dataPin, !!(commando & (1 << ((bits - 1 - i)))));
    digitalWrite(clockPin, HIGH);
    delayMicroseconds(wait);
    digitalWrite(clockPin, LOW);            
  }
}

  void shiftIn(int dataPin, int clockPin, int bitOrder, int bits)
    {
      int i;
      for (i = 0; i < bits; i++)  {
        if (bitOrder == 1)
          digitalRead(dataPin);
        else    
          digitalRead(dataPin);
        digitalWrite(clockPin, HIGH);
        digitalWrite(clockPin, LOW);            
      }
  return val;
}    

  
  
    
// send a command to the SHTx sensor
void sendCommandSHT(int command, int dataPin, int clockPin)
{
   int ack;

   // transmission start
   pinMode(dataPin, OUTPUT);
   pinMode(clockPin, OUTPUT);
   digitalWrite(dataPin, HIGH);
   digitalWrite(clockPin, HIGH);
   digitalWrite(dataPin, LOW);
   digitalWrite(clockPin, LOW);
   digitalWrite(clockPin, HIGH);
   digitalWrite(dataPin, HIGH);
   digitalWrite(clockPin, LOW);

   // shift out the command (the 3 MSB are address and must be 000, the last 5 bits are the command)
   // XXXXXXX
   shiftOut(dataPin, clockPin, 2, command);

   // verify we get the right ACK
   digitalWrite(clockPin, HIGH);
   pinMode(dataPin, INPUT);
   ack = digitalRead(dataPin);
   if (ack != LOW)
     Serial.println("ACK error 0");
   digitalWrite(clockPin, LOW);
   ack = digitalRead(dataPin);
   if (ack != HIGH)
     Serial.println("ACK error 1");
}

// wait for the SHTx answer
void waitForResultSHT(int dataPin)
{
   int ack;
   int i;

   pinMode(dataPin, INPUT);
   for (i = 0; i < 100; ++i)
   {
     delay(20);
     ack = digitalRead(dataPin);
     if (ack == LOW)
       break;
   }
   if (ack == HIGH)
     Serial.println("ACK error 2");
}

// get data from the SHTx sensor
int getData16SHT(int dataPin, int clockPin)
{
   int val;

   // get the MSB (most significant bits)
   pinMode(dataPin, INPUT);
   pinMode(clockPin, OUTPUT);
   // XXXXXXX
   //val = shiftIn(dataPin, clockPin, MSBFIRST, 8, 3);
   val *= 256; // this is equivalent to val << 8;

   // send the required ACK
   pinMode(dataPin, OUTPUT);
   digitalWrite(dataPin, HIGH);
   digitalWrite(dataPin, LOW);
   digitalWrite(clockPin, HIGH);
   digitalWrite(clockPin, LOW);

   // get the LSB (less significant bits)
   pinMode(dataPin, INPUT);
   // XXXXXXX  
   //val |= shiftIn(dataPin, clockPin, MSBFIRST, 8, 3);
   return val;
}

// skip CRC data from the SHTx sensor
void skipCrcSHT(int dataPin, int clockPin)
{
   pinMode(dataPin, OUTPUT);
   pinMode(clockPin, OUTPUT);
   digitalWrite(dataPin, HIGH);
   digitalWrite(clockPin, HIGH);
   digitalWrite(clockPin, LOW);
}


void setup()
{
   Serial.begin(9600); // open serial at 9600 bps
}

void loop()
{
   // read the temperature and convert it to centigrades
   sendCommandSHT(temperatureCommand, dataPin, clockPin);
   waitForResultSHT(dataPin);
   val = getData16SHT(dataPin, clockPin);
   skipCrcSHT(dataPin, clockPin);
   temperature = (float)val * 0.01 - 40;
   Serial.printf("temperature: %f\r\n", temperature);
   // read the humidity
   sendCommandSHT(humidityCommand, dataPin, clockPin);
   waitForResultSHT(dataPin);
   val = getData16SHT(dataPin, clockPin);
   skipCrcSHT(dataPin, clockPin);
   humidity = -4.0 + 0.0405 * val + -0.0000028 * val * val;
   Serial.printf(" humidity: %f\r\n", humidity);
   delay(800); // wait for 800 milliseconds for next reading
}
I'm not quiet sure about what i made. It are some reworked parts from other programs.
The only message i still get is "error while linking C:\x4easyrev685mod\projects\broedkast\broedkast4.o"
But since i also had it with your version, i think it is a bug in pinguino IDE. If i select the pinguino micro board, it compiles fine.
SkulltrailDualQuadXeon5420;MacProDualG5;AsusE350;AtomInaVCR;PrecisionM90;EEE1000h;NixdorfComputerfrom1970;Xbox;X360;PS1;PS2;CDI;ATARI2600;LasVegas;C128;ZXSpectrum;Dreamcast;Saturn;Mastersystem;Megadrive;nes;snes;N64;wii;Videopack;Colecovision,GB/P/C/A;DS;
Reply
03-05-2013, 01:09 AM,
#9
RE: Sensirion SHT1x temperature sensor library
1. You should probably download the latest version of the IDE using svn (the tortoise svn client is available for windows).

2. I really don't think that these two lines are equivalent:

Code:
//val = shiftIn(dataPin, clockPin, MSBFIRST, 8, 3);
   val *= 256; // this is equivalent to val << 8;

Nor are these two lines:

Code:
//val |= shiftIn(dataPin, clockPin, MSBFIRST, 8, 3);
   return val;

Neither does anything that the original shiftIn function does. You have a shiftIn() function now, so why aren't you using it?

About your shiftIn function:

o it's declared as void which means that it doesn't return any value, but you're returning a value (val);
o the value you're returning does not seem to be affected by the function at all.

I didn't try compiling this code as it has some issues as noted above which you need to fix first Smile

Perhaps you would profit from reading The C Book (online version) which might help you avoid some syntactical errors and help you understand a bit about what you are doing.
Reply
03-05-2013, 02:53 AM,
#10
RE: Sensirion SHT1x temperature sensor library
Yes i already found out that my void wasn't correct and yes i should read that book. (Thanks for the link Smile)
Now i updated the IDE with SVN as you said. The memory of the 220 still is too small, and my program is with the new version 30k instead of 15.
Anyways, why i don't use the shiftIn() and out.. because i cant find how i use them, which files i have to include etc.
This whole thing is a lot more complicated then i usually do. My patience with it is gone :/. If i just paid 3 euro extra, it would have spared me 3 days of wasting time. And if it doesn't work before Sunday, thats my plan to do. Sorry that i'm so stressed about it.
SkulltrailDualQuadXeon5420;MacProDualG5;AsusE350;AtomInaVCR;PrecisionM90;EEE1000h;NixdorfComputerfrom1970;Xbox;X360;PS1;PS2;CDI;ATARI2600;LasVegas;C128;ZXSpectrum;Dreamcast;Saturn;Mastersystem;Megadrive;nes;snes;N64;wii;Videopack;Colecovision,GB/P/C/A;DS;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)