Bluetooth Humidity Temperature

Bluetooth & Humidity & Temperature sensor ( +Water sensor + Snow)

I have internal data collection item, MrSmart. It have collected data more than 16 years. It also sends data to Internet to my home page.
I has collected also outside temperature more than two years using Bluetooth communication and SHT160-30 temperature sensor.
I have collected outside rain information more than one year.
No I have also a snow sensor.


A graphical interface before I did my add on.

It has worked well.
It will give digital immediate data and analog charts for one month at time.


This is immediate digital data.

I need more data, so I decided to get outside humidity also.


This is temperature and humidity sensor module for microprocessor DHT22.

I looked sensors and I found very popular temp and humidity sensor DHT22( Banggood).
It has three wires, but most of DHT22 modules has four wires.
I also ordered Bluetooth (Banggood) serial module HC06.
It has only four wires VCC ( 3.3.. 5.0V), GND, TXD and RXD.


This is Bluetooth module for serial data HC06

I tested Bluetooth module with PC Bluetooth module and PC serial test program (Probyte/MrSmart)


This is Bluetooth module for serial data to PC.

It worked well.

Then I tested DHT22 module, but it has very bad English/Chinese datasheet(
All the programs which I found were Arduino type, but I use always PIC-cpus.

I can't find working PIC program, so I started to build it from zero.
Well it don't work at first, but then I got all the timings to work.

Still it has problems in getting the data. The data sheet doesn't give exact help.

I finally got everything right to serial data to PC.
I used PIC16F886 at this test stage to PC serial data.

I fetch my old Bluetooth device and installed DHT22 to it.
I take may old temperature device (SMT160-30) off and installed DHT22 instead.

I compile new PIC 18F2431 for that device.


This is a schematic diagram for my sensor, communications and cpu.

I put 230/5V power supply on and tested it in my workroom and went to look my data collector computer, which is in living room.

It is HP550, very modest device with WIN7 operating system.
Now I have updated it to WIN10, because it stopped to get Bluetooth data in.
I tried about 10 different Bluetooth drivers, but nothing help.
Then I update WIN7 to WIN10
Now it worked!
The outside device uses a PIC-cpu, PIC18F2620.

I put my CCS/PCH 5.008 compile code to here, if somebody will try it later.
This has also a snow sensor code included.

    The temperature, humidity, rain  and snow height meter.
    The serial signal  goes by blutooth to inside PC  and there it makes a digital and graphical display. 
    Date  12.3.2019 added a snowmeter 
    Snow meter consists of Sharp distance sensor 2Y0A21. It has Ir transmitter and receiver.Signal comes out 3.1V..0V.
    4.8.2018 added a water meter by an interrupt timer1
    In water meter there is 555 timer and rain sensor 
    It gives pulses, dry 14000 pulses/s, wet 20 pulses/s to Timer0.
    Timer1 measures these pulses by interrupt. 

    Name: Pekka Ritamaki   
    Compiler: CCS/PCW 5.008
    DHT22 in pin B0.
    This DHT22 has internal 10k pullup resistor.
    It has three pins from left GND,VCC,Data,
    VCC = 3.3-5V
    DHT22 operation 
    First give pulse 18 ms pulse down, then wait 26 ms
    Check that DHT22 responses with zero pulse  80 us
    Then DHT22 should give  80us high pulse
    Then read 5*8 bytes data so that DHT2 gives 50us low pulse, 
    then it gives actual data after 26us low or high data read 
    Humidity high value is value[0]*256 + humidity low value[1],  hum/10
    Temp  high value is value[2]*256 + low temp value is value[3], temp/10
    Then multiply value[0] with 256 and add low value[1], you get humidity value/10  
    Then multiply value[2] without highest bit with 256 and add value[3],  you get temperature value/10
    Take value[2] highest bit and if it is 1 then it the temperature is negative temperature
    This gives temperatue in Celsius degrees. 
    Convert to Farenheit T(°F) = T(°C) × 9/5 + 32 

#include <18F2620.h>
#device *= 16
#use delay (internal = 8MHz)
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7,errors)

#FUSES WDT                      // Watch Dog Timer
#FUSES INTRC_IO                 // Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                // Code not protected from reading
#FUSES IESO                     // Internal External Switch Over mode enabled
#FUSES BROWNOUT                 // Reset when brownout detected
#FUSES PUT                      // Power Up Timer
#FUSES NOCPD                    // No EE protection
#FUSES NODEBUG                  // No Debug mode for ICD
#FUSES NOLVP                    // No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    // Program memory not write protected
#FUSES NOMCLR                   // programming pin 1 is normal IO-pin later
#define DHT22_pin PIN_B0
#define LED PIN_B5 
int16 counter=32;
int16 timer1_reset=0;
// timer1 interrup program 
void  TIMER1_isr(void) 
if(++timer1_reset> 32) {
counter= get_timer0();
void measure_water_pulses( ) 
unsigned int16 i;
counter =0; 
for( i=0; i<1000; i++) 
 if( get_timer0() )
 unsigned char value[5];   // read bytes from DHT22 
 float temp,hygro;         // real values
 // read first 30us low pulse, then actual data 

unsigned char get_byte()
   unsigned char s = 0;
   unsigned char value = 0;

   for(s = 0; s < 8; s ++)
      value <<= 1;
      while(!input(DHT22_pin)); // wait till DHT22 goes to low state 
          value |= 1;
   return value;   // return 8 bits byte 

// give a start pulse low 18ms,
// then wait 26 ms and chech DHT22 state
// then read 5 byte digits to common value[]'s 

unsigned char get_data()
   short chk = 0;
   unsigned char s = 0;
   unsigned char check_sum = 0;
   output_high(DHT22_pin);   // prosessor high
   delay_ms(18);             // wait 18 ms
   output_low(DHT22_pin);    // then low 
   delay_ms(18);             // wait 18 ms
   output_high(DHT22_pin);   // prosessor high
   delay_us(26);             // delay 26 ms
   chk = input(DHT22_pin);   // raed DHT22 state
   if(chk)                    // if DHT22 goes low it is error state 
      return 1;
   delay_us(80);              // small delay 80us

   chk = input(DHT22_pin);    // read DHT22 again 
   if(!chk)                   // DHT22 keeps high state, it is a error, no device
      return 2;
   delay_us(80);               // small delay 80us

   for(s = 0; s <=4; s ++)     // read 5 digits to value[]'s   
       value[s] = get_byte();  

   output_high(DHT22_pin);     // put the prosessor to high state to wait the next measurement 

   for(s = 0; s < 4; s ++)
       check_sum += value[s];  // count  check_sum

   if(check_sum != value[4])
      return 3;                // not right, error 
      return 0;                 // no errors

// change  decimal chacters to real numbers,note 0x80 in value[2] gives negative temperature
void change( void)
   temp = 256*(float) (value[2] & 127); // hi byte gives 25 deg* bytes to low bytes 
   temp += (float) value[3];            // add value[3]/10 
   temp /= 10; 

 if (value[2] & 0x80){    // this is negative 
   temp =-temp ; 

    hygro = 256*(float) (value[0] &127);   // hi byte gives 25 deg* bytes
    hygro += (float) value[1];             // add value[1]/10 
    hygro /=  10; 

// Main starts the program 
void main(void)
 unsigned char state = 0;
 unsigned int16  lumi;  
 // first setup 
   setup_adc_ports(A0_A3_A1_A3  | VSS_VDD); 
   setup_adc(ADC_CLOCK_INTERNAL  );
   setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_256); //9,0 s overflow
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);      //32,7 ms overflow
   setup_timer_2(T2_DIV_BY_16,0,1);             //8,0 us overflow, 8,0 us interrupt
   printf("timer0=%ld", counter);
  //  enable_interrupts(INT_TIMER1); //this was taken out, because it stopped working after 1-2 minutes. 

   printf("\rDHT22 read");

    lumi= read_adc();  // Read A0 ADC() 
    state = get_data();// give a start pulse and then read 5 bytes 

      switch(state) // check how data is going
             case 1: 
               printf("\rDHT22 no!"); // no pulses down
            case 2:
               printf("\rDHT22 Sensor not Found!");
            case 3:
               printf("\rChecksum Error!");
               change ();
               printf("\rtemp=%f hygro=%f", temp, hygro); 

       printf(" water=%ld lumi=%ld\r", counter,lumi);
       delay_ms(500);     // minimum time  betwen measurements is 500ms


A part of graphical interface

Click the picture two times to get it bigger

Oh yes, how is it now?

The top red curve is moisture.
In the day it will drop when the temperature rises 30-20%, at night the humidity is up to 82%

The blue light curve rises up to 60 in the morning (= * 100 lux) and at night it drops to a few luxes.

The next blue curve is the temperature of the cold pipe in the air pump.
It spins over a warm air pipe, because these days there is no need for warmth.

The read warm tub already if up to 7C,
Normally in winter it is always above + 35C.

The green curve shows the inside temperature.
It's around 22-23C all the time.
This temperature is the most important, which is why I have done all systems!

The black curve shows the outside temperature.
It spins in the beginning of May around 10-35C, of course there is cold weather at night.

What about those empty areas between the days?

Well, I'll stick the data logger right away and no information will come.

When I sleep, I can not get information about my use,
Here is picture of my computer


On left upper part is picture of PC Bluetooth program.
It shows outside temperature, outside humidity, time (117s) to next saving to data file (c:\hotbox\201806.log) .
These data is combined to MrSmart data, but with different time and channel.

In next row is last saving date and time, how many data is saved.
Then there is Internet marking, which works every time the program is send data to Internet.

In the middle of the picture there is graphics picture which show all the channel together.
On lower left there is digital data from MrSmart.
They show heat pump cold and heat data.
Light in lux and inside temperature.


Rain sensor


In August 2018 I got idea to expand my data logger with rain sensor.
I have made my rain sensor about ten years since, but I had have no time to test it.
Now I tested it.

It do not work, because it has green surface on the sensor.

I took sand paper and I hone it.

Then it started to work. At dry it was oscillating about 14kHz and when I added water on the sensor it was oscillating about 12Hz.

I added the sensor with 7 m three wire cable with three pin connector to my Bluetooth device.
It has PIC18F2620 SO28 processor + DHT22 temperature and humidity sensor + Bluetooth device.
I first connect the 555 oscillator to pin 14, which was timer0 input TCK0.

I made first a program with PCM(CCS), which has a 1 sec timer1 interrupt which reads the timer0 counter.

It was not working and I change my oscillator pin to pin 11 ( TCK1) which is timer1 input and I change 1 sec timer to timer0.
Now it was working and I added my new code to old program.

I added water sensor to output print to Bluetooth.


Then I changed my PC program, so that when 14kHz came I write to my data collection file first time, then channel 6 and then value 0 or 10.
At dry it was 0 and wet it was 10.
Then I added so that it will give data from 0,1,2,3 ..10


It write "\rTemp=22.5 Humidity=72.8 Water=14000"
I put a new picture of my Bluetooth device here.

Snow distance sensor

Next winter (2019) I had an idea to measure snow deep with the same device.

What I need?

Hmm, I look the Internet and I choose the Sharp distance sensor GP2Y0A2


It has three wires, +5V, GND and analog output ( 3.1V when the response is nearer than 10cm. and about 0.3V when the distance is more than 80cm.
The distances in between 80 and 10cm are about linear.

I did external 3 wire connection to outside sensor.
The outside sensor has plexi glass cover and 5 meter cable.
I have not a suitable, small connector devices at hand.
I added a big audio connector to the device outboard.

I need to modify my code.
It was quite easy to do.

But when I tried it it worked only few minutes!
When I touched pin1 of PIC-processor it started, but only one minute.
The LED in pin B0 stopped to work and other things also.

I try everything, but nothing helped.

I changed the processor to PIC18F2620, but it doesn't help.

What to do?

I removed the interrupt code and made a direct rain code calculation.
It helped!
My code runs next year!

Here is my circuit board for temp. humidity, rain and snow sensor


Here is the block diagram of Sharp sensor


Here is my rain sensor


Here is the both distance sensors ( at right) and Bluetooth + processor at left.


Here is my snow height sensor at garden.
Snow is almost gone.


Here is PC/Bluetooth sensor display.
It reads outside sensors from serial ( Bluetooth) connection using USB/Serial converter and convert them to right dimensions and show them at display.
After certain time, they are taken mean value, put them to PC database like.
The time display I have 120 s.

12:00:02, 0, 29.1 // Heat pump ( warm pipe) has 29.1C
12:00:05, 1, 24.6 // Heat pumpu (cold pipe) has 24,6
12:00:06, 2, 22.4 // Inside house temperature 22.4C
12:00:07, 3, 75.5 // Outside light 75.5 lux
12:00:07, 4, -5.5 // Outside house temperature -5.5C using Bluetooth
12:00:08, 5, 89.6 // Outside air humidity 89.6%
12:00:08, 6, 0.0 // Water rain sensor is dry- (10 is fully wet)
12:00:11, 7, 0.1 // snow sensor has 0.1cm snow

i.e clock, channel number and value.

From these are made PC graphics program.

Database is moved to my Internet page, where everybody can use my graphics program and follow how my house is going, if it interest anybody.


Pekka OH3GDO

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License