Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SHTXX/DHTXX
05-07-2013, 09:32 PM,
#1
SHTXX/DHTXX
Hello,

here my library for SHTXX/DHTXX Humidity/Temperature Sensors


DHT Example

/* -----------------------------------------------------------------------
Pinguino example to read DHT22 1wire humidity/temperature sensor
IDE Pinguino > b9.5

-----------------------------------------------------------------------
DHT22 wiring
-----------------------------------------------------------------------
pin 1: VDD (+5V)
pin 2: DQ (Data in/out) must be connected to the PIC
pin 3: NC
pin 4: GND
NB : 1-wire bus (DQ line) must have 10K pull-up
----------------------------------------------------------------------*/

#define DHTPIN 18 // DQ line 1

KSDHT22 dht;

u8 bErr; //para pasar el codigo de error de vuelta de las funciones
u8 DHTDAT[5]; //Array with
u8 DHTCHECKSUM=0;
u32 mil;

void setup()
{
pinMode(USERLED, OUTPUT);
pinMode(DHTPIN,INPUT);
digitalWrite(USERLED,LOW);
delay(3000);
}


void loop()
{
bErr=KS_DHT.read(DHTPIN,&dht);
switch (bErr)
{
case 0: //no error
CDC.printf("Humidity: %02.2f ",dht.hum);
if (dht.sign) CDC.print("-");
CDC.printf("Temperature: %02.2f \r\n",dht.temp);
break;
case 1:
CDC.print("start condition error 1.\r\n");
break;
case 2:
CDC.print("start condition error 2.\r\n");
break;
case 3:
CDC.print("no response signal.\r\n");
break;
case 4:
CDC.print("preparation of data fails.\r\n");
break;
case 5:
CDC.print("no data received.\r\n");
break;
case 6:
CDC.print("data read error.\r\n");
break;
case 7:
CDC.print("checksum error.\r\n");
break;
}


digitalWrite(USERLED,HIGH);
delay(2);
digitalWrite(USERLED,LOW);
delay(5000);
}


--> DHT Library - copy to “\p32\include\pinguino\libraries\ks_dht.c”


#ifndef __KS_DHT_C
#define __KS_DHT_C

#include <digitalw.c>
/* -----------------------------------------------------------------------
Pinguino library; DHT22 1wire humidity/temperature sensor
Code base: » Forum Post by mirasu
http://forum.pinguino.cc/showthread.php?tid=3911
--------------------------------------------------------------------------*/
#define wt 5000 //Timeout

typedef struct
{
u8 sign; // sign (1=negative)
double temp; // temperature
double hum; // humidity
u8 dht_error //global error
} KS_DHT_Data;

/// PROTOTYPES
u8 KS_DHTRead(u8,KS_DHT_Data *);

/* -----------------------------------------------------------------------
---------- KS_DHTRead()
-----------------------------------------------------------------------
* Description: reads the dht22 device via 1-wire bus
* Arguments:
dhpin = pin number where one wire bus is connected.
dh = data record
return errorcode or 0
--------------------------------------------------------------------*/
u8 KS_DHTRead(u8 dhpin,KS_DHT_Data * dh)
{
u16 timeout;
double h,t;
u8 DHTDAT[5]={0};
u8 DHTCHECKSUM;
u8 i,j;
u8 dhtbyte=0;

h=0;
t=0;
if (digitalread(dhpin)==LOW) {return 1;} // Bus not free
pinmode(dhpin,OUTPUT);
digitalwrite(dhpin,LOW); // MCU start signal (>=500us)
Delayms(1);
//Request Data
pinmode(dhpin,INPUT);
timeout = wt;
while(digitalread(dhpin)) {timeout--;if (timeout==0) {return 2;}} // Wait for DHT’s response (20-40us)
timeout = wt;
while(!digitalread(dhpin)) {timeout--;if (timeout==0) {return 3;}} // Response signal (80us)
timeout = wt;
while(digitalread(dhpin)) {timeout--;if (timeout==0) {return 4;}} // Preparation for sending data (80us)
//Read Data
for(i=0;i<5;i++)
{
for(j=1;j<=8;j++)
{
timeout = wt;
while(digitalread(dhpin)==LOW) {timeout--;if (timeout==0) {return 5;}} // Start to transmit 1-Bit (50 us)
Delayus(30);
dhtbyte <<= 1;
if (digitalread(dhpin)) // Hi > 30us (70 us) -> Bit=1
{
dhtbyte |= 1;
timeout = wt;
while(digitalread(dhpin)) {timeout--;if (timeout==0) {return 6;}}
} // Hi < 30us (26-28 us) -> Bit=0
}
DHTDAT[i] = dhtbyte;
}

DHTCHECKSUM = DHTDAT[0]+DHTDAT[1]+DHTDAT[2]+DHTDAT[3];
if (DHTCHECKSUM != DHTDAT[4]) // Checksum
{
return 7;
}
dh->sign = 0;

if (DHTDAT[2] & 0b11111000) // test if sign is set, i.e. negative
{
dh->sign = 1;
DHTDAT[2] = (DHTDAT[2] ^ 0xFFFF) + 1; // 2's complement conversion
}

h=(DHTDAT[0]<<8)+DHTDAT[1];
t=(DHTDAT[2]<<8)+DHTDAT[3];
dh->hum=h/10;
dh->temp=t/10;
return 0;
}

#endif


--> PDL File - Copy to \p32\pdl\dht.pdl32:

KSDHT22 KS_DHT_Data#include <ks_dht.c>
KS_DHT.read KS_DHTRead#include <ks_dht.c>
Reply
15-08-2013, 10:33 PM,
#2
RE: SHTXX/DHTXX
hello.
Might it be ported to forgive p8 (2550.4550 ..) or explain that differences have

I also wanted to say that the code is not mine http://tallerarduino.com/2012/12/24/sens...n-arduino/
thanks
Reply
29-09-2015, 11:10 PM, (This post was last modified: 29-09-2015, 11:52 PM by machocastellano.)
#3
RE: SHTXX/DHTXX
Hello:


I'm trying your library but I'm having some troubles.
IDE: Pinguino IDE 11.0-beta.3
OS: Ubuntu 15.04 32bits

Compiling stdout:

Code:
# ------------------------------------------------------------------
# cleaning all build files
# ------------------------------------------------------------------
rm -f -v /home/txema/.pinguino/source/main32.o
rm -f -v /home/txema/.pinguino/source/main32.elf
rm -f -v /home/txema/.pinguino/source/main32.hex
rm -f -v /home/txema/.pinguino/source/obj/non-free/processor.o
«/home/txema/.pinguino/source/obj/non-free/processor.o» borrado
# ------------------------------------------------------------------
# adding proc. definitions
# ------------------------------------------------------------------
cp /home/txema/.pinguino/source/obj/non-free/32MX440F256H.o /home/txema/.pinguino/source/obj/non-free/processor.o
# ------------------------------------------------------------------
# compiling and linking
# ------------------------------------------------------------------
#/usr/bin/p32-gcc -march=m4k -EL -msoft-float -nostdlib -static -O3 -minterlink-mips16 -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,-L/home/txema/.pinguino/source/obj/non-free -Wl,--defsym,_min_heap_size=1024 -Wl,-Map=/home/txema/.pinguino/source/output.map -D __PIC32MX__ -D __32MX440F256H__ -D PIC32_PINGUINO_OTG -T/usr/share/pinguino-11/p32/lkr/PIC32_PINGUINO_OTG/procdefs.ld -T/usr/share/pinguino-11/p32/lkr/PIC32_PINGUINO_OTG/elf32pic32mx.x -L/home/txema/.pinguino/source/obj/non-free/usb -lm -lgcc -lc -fdollars-in-identifiers -I/usr/share/pinguino-11/p32/include/non-free -I/usr/share/pinguino-11/p32/include/pinguino/core -I/usr/share/pinguino-11/p32/include/pinguino/libraries -I/usr/share/pinguino-11/p32/lkr/PIC32_PINGUINO_OTG -I/home/txema/Descargas/DHTXX -I/home/txema/.pinguino/source/obj/non-free -o /home/txema/.pinguino/source/main32.elf /home/txema/.pinguino/source/main32.c
/usr/bin/p32-gcc -march=m4k -EL -msoft-float -nostdlib -static -O3 -minterlink-mips16 -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,-L/home/txema/.pinguino/source/obj/non-free -Wl,--defsym,_min_heap_size=1024 -Wl,-Map=/home/txema/.pinguino/source/output.map -D __PIC32MX__ -D __32MX440F256H__ -D PIC32_PINGUINO_OTG -T/usr/share/pinguino-11/p32/lkr/PIC32_PINGUINO_OTG/procdefs.ld -T/usr/share/pinguino-11/p32/lkr/PIC32_PINGUINO_OTG/elf32pic32mx.x -L/home/txema/.pinguino/source/obj/non-free/usb -lm -lgcc -lc -fdollars-in-identifiers -I/usr/share/pinguino-11/p32/include/non-free -I/usr/share/pinguino-11/p32/include/pinguino/core -I/usr/share/pinguino-11/p32/include/pinguino/libraries -I/usr/share/pinguino-11/p32/lkr/PIC32_PINGUINO_OTG -I/home/txema/Descargas/DHTXX -I/home/txema/.pinguino/source/obj/non-free -mips16 -c -g -Wa,-a,-ad /home/txema/.pinguino/source/main32.c > /home/txema/.pinguino/source/main32.lst
In file included from /home/txema/.pinguino/source/define.h:5:0,
                 from /home/txema/.pinguino/source/main32.c:30:
/usr/share/pinguino-11/p32/include/pinguino/libraries/__cdc.c:126:65: warning: backslash and newline separated by space
 #if !defined(__32MX220F032D__) && !defined(__32MX220F032B__) && \
 ^
In file included from /home/txema/.pinguino/source/main32.c:46:0:
/home/txema/.pinguino/source/user.c:17:1: error: unknown type name 'KSDHT22'
 KSDHT22 dht;
 ^
/home/txema/.pinguino/source/user.c: In function 'loop':
/home/txema/.pinguino/source/user.c:34:26: warning: passing argument 2 of 'KS_DHTRead' from incompatible pointer type
   bErr=KS_DHTRead(DHTPIN,&dht);
                          ^
In file included from /home/txema/.pinguino/source/define.h:8:0,
                 from /home/txema/.pinguino/source/main32.c:30:
/usr/share/pinguino-11/p32/include/pinguino/libraries/ks_dht.c:32:4: note: expected 'struct KS_DHT_Data *' but argument is of type 'int *'
 u8 KS_DHTRead(u8 dhpin,KS_DHT_Data * dh)
    ^
In file included from /home/txema/.pinguino/source/main32.c:46:0:
/home/txema/.pinguino/source/user.c:38:40: error: request for member 'hum' in something not a structure or union
       CDCprintf("Humidity: %02.2f ",dht.hum);
                                        ^
/home/txema/.pinguino/source/user.c:39:14: error: request for member 'sign' in something not a structure or union
       if (dht.sign) CDCprint("-");
              ^
/home/txema/.pinguino/source/user.c:40:47: error: request for member 'temp' in something not a structure or union
       CDCprintf("Temperature: %02.2f \r\n",dht.temp);
                                               ^
/home/txema/.pinguino/source/Makefile32.linux:136: recipe for target 'compile' failed
make: *** [compile] Error 1


My problem starts on line:


In file included from /home/txema/.pinguino/source/main32.c:46:0:
/home/txema/.pinguino/source/user.c:17:1: error: unknown type name 'KSDHT22'
KSDHT22 dht;


but I check (double checking) that files "ks_dht.c" and "dht.pdl32" are correctly pasted and they have permission:


Code:
txema@unleashed:/usr/share/pinguino-11/p32/include/pinguino/libraries$ ls -l ks_dht.c

-rwxrwxrwx 1 txema txema 2455 sep 29 21:46 ks_dht.c

txema@unleashed:/usr/share/pinguino-11/p32/pdl$ ls -l dht.pdl32

-rwxrwxrwx 1 txema txema 81 sep 29 21:45 dht.pdl32

And define.h contains:

Code:
#define CDCPRINT

#define DHTPIN    8      
#define DIGITALWRITE
#define PINMODE
#include <__cdc.c>
#include <delay.c>
#include <digitalw.c>
#include <ks_dht.c>
#include <typedef.h>

I tried changing "KSDHT22" for "KS_DHT_Data" and it works up to certain point. Now it shouts something about "CDCprintf":

Code:
/home/txema/.pinguino/source/main32.o: In function `loop':
(.text.loop+0x36): undefined reference to `CDCprintf'
/home/txema/.pinguino/source/main32.o: In function `loop':
(.text.loop+0x46): undefined reference to `CDCprintf'
collect2: error: ld returned 1 exit status



I've tried examples with "CDC.printf()" function and it worked correctly. Any ideas why it keeps happening?

UPDATED 29/09/2015 23:37
:
I solved it, just including:

#include <printf.c>
#define CDCPRINTF
#define CDCPRINT

but output is wrong:
  • Humidity: 0.00 Temperature: 0.00
  • Humidity: -107374184.00 Temperature: 0.00
Electrical connectioin diagram is: https://www.adafruit.com/datasheets/Digi...AM2302.pdf

Thanks a lot,

machocastellano.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)