srijeda, 20. veljače 2013.

php imagick pdf cropbox image

To create image out of pdf from pdf cropbox area use this snippet:

<?php
        $im = new Imagick();
        $im->setOption('pdf:use-cropbox', 'true');
        $im->setResolution(400, 400);
        $im->readImage($PdfFile."[0]");
        $width=$im->getImageWidth()+0;
        $height=$im->getImageHeight()+0;
       
        $im->setImageFormat( "jpeg" );                   
        $im->writeImage($destFile);
        $im->clear();                           
        $im->destroy();
        return true;

?>

utorak, 19. veljače 2013.

Using DHT22 sensor with Pic18f4550



Quick example of how to use DHT22 sensor with pic18f4550.

Dht22 is a cheap but relatively accurate digital temperature and humidity sensor. Sensor works on 3-5V and it gives you digital signal on one data pin. You can pull out new data every 2 seconds.

You can find it on ebay for about 4-5$.






So, lets get started, to get this sensor working pin1 has to be connected to 3-5V, pin2 is data pin, pin3 is empty and pin4 has to be connected to GND.

It is also recommended to connect data pin to VCC with 10K resistor to act as a medium-strength pull up on the data line

according to datasheet first thing you have to do i to send a signal to sensor. You do that by setting a microcontroller pin to output and sending a low-high signal.

/**
* DHT22 sensor start
*/
unsigned short StartSignal(){
  TRISC0_bit = 0;  // set data port to output
  RC0_bit = 0;       // data output LOW
  Delay_ms(25);    // Low for at least 18ms (it can be even shorter)
  RC0_bit = 1;       // set output HIGH
  Delay_us(30);     // High for 20-40 us
  TRISC0_bit = 1;  // Data port is input  

                           //sensor should now ser input to LOW for next 80us
  Delay_us(2);    
  return 1;
}


Sensor will now set data pin to low for next 80us and then high for next 80us. After this sequence sensor will start sending data.

/**
*  DHT22 senzor - check response from dht22
*/
unsigned short CheckResponse(){

  if (!RC0_bit){
     while (!RC0_bit){}  //waiting till sensor starts sending ones
     if (RC0_bit){
       while (RC0_bit){} //waiting till 80us high period is over
       delay_us(20);      //just in case delay.
       return 1;
     }
  }
  else if (RC0_bit){      // if data pin is still high, then something is wrong
         Lcd_Out(1,1,"error data=1    ");
         Delay_ms(1000);
         return 0;
  }
}


Microcontroller can now read 40bit data.

DATA = 
+ 8 bit integral RH data
+ 8 bit decimal RH data
+8 bit integral T data
+8 bit decimal T data
+8 bit checksum

If the data transmission is right, check-sum should be the last 8 bit of "8 bit integral RH data+8 bit decimal RH
data+8 bit integral T data+8 bit decimal T data.


So you need to do four readings for every byte.

if sensor sends 70us high state for 1 and 26-28us high state for 0.
after each bit there is a 50us low state.

/**
* DHT22 senzor - read data
*/
unsigned short ReadByte()
{
  unsigned short num = 0, t;
 
TRISC0_bit = 1; //postavljam input
  for (i=0; i<8; i++){
   while(!
RC0_bit);
   Delay_us(40);
   if(
RC0_bit) num |= 1<<(7-i);  // If time > 40us, Data is 1
   while(
RC0_bit);
  }
  return num;
}
 

 
/**
* DHT22 main
*/
void getTempHum(){
     unsigned short s_signal, check;
     Delay_ms(2000);
     s_signal=StartSignal();
     if (s_signal){
        check = CheckResponse();
     }
     if (!check) {
       Lcd_Out(2,1,"Sensor ERR         ");
       Delay_ms(900);
     }
     else{
        RH_Byte1 = ReadByte();
        RH_Byte2 = ReadByte();
        T_Byte1 = ReadByte();
        T_Byte2 = ReadByte();
        CheckSum = ReadByte();



        //preparing output

        ToText(output3, 1, ToInt(T_Byte1,T_Byte2) );
        ToText(output4, 1, ToInt(RH_Byte1,RH_Byte2));

       Lcd_Out(1, 1,
output3);
       Lcd_Out(2, 1,
output4);
       Delay_ms(2);
     }
}




  

- matija kancijan