#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
unsigned int readDHT22(int pin, float* Hum, float* Tem);
int readByte(int pin);
int checkSum(int pin);
const unsigned char PROGMEM weather[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x04,
0x10, 0x00, 0x27, 0xA0, 0x00, 0x1C, 0x0E, 0x00, 0x08, 0x30, 0x80, 0x50, 0x40, 0x40, 0x10, 0x80,
0x20, 0x10, 0x80, 0x78, 0x31, 0x80, 0x84, 0x0C, 0x40, 0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01,
0x10, 0x00, 0x01, 0x10, 0x00, 0x00, 0x10, 0x00, 0x02, 0x08, 0x00, 0x04, 0x03, 0xFF, 0xF0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const unsigned char PROGMEM clock[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x00, 0x03, 0xFF, 0x80, 0x07,
0x81, 0xC0, 0x0E, 0x00, 0xE0, 0x0C, 0x10, 0x60, 0x1C, 0x18, 0x30, 0x18, 0x18, 0x30, 0x18, 0x18,
0x30, 0x18, 0x18, 0x10, 0x18, 0x1C, 0x30, 0x18, 0x0E, 0x30, 0x18, 0x06, 0x30, 0x0C, 0x00, 0x60,
0x0E, 0x00, 0x60, 0x07, 0x01, 0xC0, 0x03, 0xFF, 0x80, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
void setup() {
display.begin(SSD1306_SWITCHCAPVCC,0x3c);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.clearDisplay();
display.setTextSize(2);
display.setCursor(40, 10);
display.print("12:35pm");
display.drawRoundRect(0,0,128,64,7,1);
display.drawLine(0,32,128,32,1);
display.drawBitmap(2,32,weather,24,24,1);
display.drawBitmap(2,3,clock,24,24,1);
Serial.begin(9600);
// put your setup code here, to run once:
float h,t;
readDHT22(2,&h,&t);
Serial.println(h);
Serial.println(t);
}
void loop() {
float h,t;
readDHT22(2,&h,&t);
//display.fillRect(35,5,90,22,0);//<< clear zone time
display.fillRect(35,36,90,22,0);//<< clear zone weather
display.setTextSize(2);
display.setCursor(35, 40);
display.print((int)h);
display.print("%");
display.setCursor(80, 40);
display.print((int)t);
display.print("C");
display.display();
delay(2000);
// put your main code here, to run repeatedly:
}
unsigned int readDHT22(int pin, float* Hum, float* Tem){
unsigned int status = 0;
pinMode(pin,OUTPUT);
digitalWrite(pin,LOW);
delay(2);
digitalWrite(pin,HIGH);
delayMicroseconds(30);
pinMode(pin,INPUT);
while(digitalRead(pin));
while(!digitalRead(pin));
while(digitalRead(pin));
int i = 32;
long int HT = 0;
do{
while(!digitalRead(pin));
delayMicroseconds(30);
if(digitalRead(pin)){
HT = ((HT<<1) | 1);
}else{
HT = (HT<<1);
}
while(digitalRead(pin));
i--;
}while(i != 0);
unsigned char cSum = 0;
i = 8;
do{
while(!digitalRead(pin));
delayMicroseconds(30);
if(digitalRead(pin)){
cSum = ((cSum<<1) | 1);
}else{
cSum = (cSum<<1);
}
while(digitalRead(pin));
i--;
}while(i != 0);
int H = ((0xFFFF0000 & HT)>>16);
int T = ((0xFFFF & HT));
unsigned char HH = ((0xFF00 & H)>>8);
unsigned char HL = (0xFF & H);
unsigned char TH = ((0xFF00 & T)>>8);
unsigned char TL = (0xFF & T);
unsigned char checkSum = (HH+HL+TH+TL);
if(checkSum && cSum){
*Hum = H;
*Hum /= 10;
*Tem = T;
*Tem /= 10;
}else{
status = 1;
}
return status;
}