2022年4月7日 星期四

arduino紅外線溫度計MLX90614+溫濕度計DHT11+OLED實作

/*

 * PHYduino紅外線溫度計+溫濕度計

* need 需要元件 DHT11溫濕度計、MLX90614紅外線溫度計、OLED0.96吋

 * design by orangeV,taiwan,tainan 台南大成國中 蔡智恆

 * need libraries 需要程式庫<DHT.h>,<SPI.h>,<Wire.h>,<Adafruit_GFX.h>,<Adafruit_SSD1306.h>,<Adafruit_MLX90614.h>

*/


int

//對應腳位

ds18b20 = 7, 

DHTPin = 15,      //A1溫濕度DHT

DHT2Pin = 16;     //A2溫濕度DHT2


//MLX90614腳位 SCL->A5、SDA->A4

//DHT11/22

#include <DHT.h>

DHT dht(DHTPin, DHT11);

DHT dht2(DHT2Pin, DHT11);


#include <OneWire.h>

#include <DallasTemperature.h>

OneWire oneWire(ds18b20);

DallasTemperature sensors(&oneWire);



//MLX90614

#include <Wire.h>

#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();



//OLED

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define OLED_RESET -1

Adafruit_SSD1306 display(OLED_RESET);

//OLEDsetting

#define LOGO16_GLCD_HEIGHT 32

#define LOGO16_GLCD_WIDTH  16

#if (SSD1306_LCDHEIGHT != 32)

#error("Height incorrect, please fix Adafruit_SSD1306.h!");

#endif


int line_count=0;


//PHYduino圖檔

const unsigned char PHYduino_Logo [] PROGMEM = {

0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,

0x5f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,

0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,

0x67, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe6,

0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa6,

0x66, 0xff, 0xfc, 0xff, 0xfc, 0xe0, 0x38, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x66,

0x66, 0x00, 0x14, 0xa0, 0x14, 0xa0, 0x68, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x66,

0x66, 0xfc, 0x0c, 0xc0, 0x0c, 0xc0, 0x98, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x66,

0x66, 0x0c, 0x0c, 0xc0, 0x00, 0x83, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0xa6,

0x66, 0x0c, 0x0c, 0xc0, 0x00, 0x8c, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0xe6,

0x66, 0x0c, 0x1c, 0xe0, 0x00, 0xd0, 0x18, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0x06,

0x66, 0xf7, 0xf4, 0xa0, 0x1c, 0xa0, 0x39, 0xff, 0xf7, 0x80, 0x7f, 0xc3, 0xf3, 0x06, 0x00, 0xe6,

0x66, 0xac, 0x18, 0xff, 0xf4, 0xff, 0xe9, 0x40, 0x1e, 0x80, 0x51, 0x42, 0xd3, 0x05, 0xff, 0xa6,

0x66, 0xcc, 0x00, 0x00, 0x0c, 0x00, 0x39, 0x80, 0x19, 0x80, 0xf1, 0xf3, 0x33, 0x06, 0x00, 0x66,

0x66, 0x0c, 0x00, 0x00, 0x04, 0x00, 0x19, 0x80, 0x19, 0x83, 0x31, 0x98, 0x33, 0x06, 0x00, 0x66,

0x66, 0x0c, 0x00, 0x00, 0x04, 0x00, 0x19, 0x80, 0x19, 0x8c, 0x31, 0x8e, 0x33, 0x06, 0x00, 0x66,

0x66, 0x0e, 0x00, 0x00, 0x0c, 0x00, 0x39, 0xc0, 0x39, 0xb0, 0x31, 0x83, 0xb3, 0x92, 0x00, 0x66,

0x66, 0x0a, 0x00, 0x00, 0x14, 0x00, 0x29, 0x40, 0x29, 0x40, 0x51, 0x40, 0xd2, 0xad, 0xff, 0xa6,

0x67, 0x0f, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0xf9, 0xff, 0xf9, 0xc0, 0x73, 0xc6, 0x00, 0xe6,

0x65, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x66,

0x67, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf6,

0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e,

0x5f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,

0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

};


void setup() {

  //DHT

  Wire.begin();

  dht.begin();

  dht2.begin();

  

  //ds18b20

  sensors.begin();

  

  Serial.begin(9600);

  mlx.begin();  

  //OLED

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.display();

  display.clearDisplay();

  display.drawBitmap(0, 8, PHYduino_Logo, 128, 64, WHITE);

  display.setTextColor(WHITE);

  display.setCursor(0,0);

  display.setTextSize(1);

  display.print("orangeV TCJHS.lab T&C");

  display.display();

  delay(1500);

  display.clearDisplay();

}




void loop() {

  display.setTextColor(WHITE);

  display.setCursor(0,0);

  display.setTextSize(1);


//此處可方便未來用電腦輸出檔案

  switch(0) {

    case 0:

      // 第一列印出 Excel 標題列

      if(line_count == 0) {

        Serial.println("MLX_temperature,IR_temperature,DHT_temperature,DHT_humidity");

        Serial.println("MLX*C,IR *C,DHT*C,DHT %");

      }  

      else {

        Serial.print(mlx.readAmbientTempC());

        Serial.print(",");

        Serial.print(mlx.readObjectTempC());

        Serial.print(",");

        Serial.print(dht.readTemperature());

        Serial.print(",");

        Serial.println(dht.readHumidity());

      }

      line_count ++;

    break;

  }

  

  DHTxx();

  delay(1000);

}


void DHTxx() {

  sensors.requestTemperatures();

  float DHThum = dht.readHumidity();

  float DHTtemp = dht.readTemperature();

  float cc = sensors.getTempCByIndex(0);

  float cc1 = sensors.getTempCByIndex(1);

  float DHT2hum = dht2.readHumidity();

  float DHT2temp = dht2.readTemperature();

  display.println("orangeV TCJHS.lab T&C"); display.print("IR_T = "); display.setTextSize(2); display.print(mlx.readObjectTempC()); display.setTextSize(1); display.print("\t"); display.setCursor(102, 16); display.println("C"); display.print("DHT_T= "); display.print(DHTtemp,1); //小數點1位 display.setTextSize(1); display.print("\t"); display.print("C "); display.print(DHThum,1); //小數點1位 display.println("%");

  

  display.display();

  display.clearDisplay();

}


--------------程式在上方的分隔線---------------

參考資料

一、筆記(78):利用Python讀取序列埠溫濕度值儲存到csv檔

https://atceiling.blogspot.com/2019/12/arduino78pythoncsv.html


二、紅外線溫度計 使用紅外線溫度感測器[MLX90614 / GY-906]

https://sites.google.com/mail3.hwsh.tc.edu.tw/earth/arduinoadvanced/ir-temperature-sensor-mlx90614