步驟1:所需的組件
以下組件列表用于構建天氣和空氣質量監視器。您也可以使用功能相同的組件。
ESP8266 D1 Mini
鋰電池USB充電模塊
3.3V輸出DC-to-DC模塊
5V輸出DC-to-DC模塊
小型開關x 2
HDC1080濕度傳感器
BMP180溫度和氣壓傳感器
CCS811 TVOC傳感器
SenseAir S8 CO2傳感器
PM2。 5/PM10紅外傳感器
電路板,自動換線工具和電線
步驟2:構建電源電路
1。將鋰電池,電池充電器,3.3V和5V DC-DC轉換器連接在一起。您可能想在鋰電池與3.3V和5V DC-DC電源轉換器之間添加一個ON/OFF開關。
2。測試鋰電池是否可以充電。
3。確認可以獲得穩定的3.3V和5V電源。
步驟3:為各種用途分配ESP8266 D1迷你引腳
首先,創建您的設計。為各種目的分配ESP8266 D1迷你引腳。 D1 Mini的針腳數量有限,我們需要仔細進行分配。那些可以保存大頭針的應該被保存。例如,某些傳感器只會通過TTL發送數據,而不會接收數據,因此我們不需要將接收引腳連接到D1 Mini。
以下是我的引腳分配:
/* PIN Assignment
A0 - Not Used
D0 - TFT CS
D1 - I2C CLK
D2 - I2C SDA
D3 - TFT C/D
D4 - PM2.5 CS
D5 - TFT SCK
D6 - S8 SenseAir TX+RX tied
D7 - TFT SDI(MOSI)
D8 - Not Used
TX - Debug Console
RX - PM2.5 Tx
*/
步驟4:通過SPI總線將D1 Mini與240x320 TFT相連
連接D1帶有SPI總線的240x320 TFT微型。使用以下引腳:
D0 - TFT CS
D3 - TFT C/D
D5 - TFT SCK
D7 - TFT SDI(MOSI)
請注意,MISO引腳未接線。原因是我們使用的TFT庫不會從TFT獲取任何數據。因此,我們可以節省1針用于其他用途。 UTFT ESP8266庫用于在SPI模式下驅動支持ILI9341的TFT。如果您的TFT使用其他芯片組,則可能需要使用其他TFT庫。關鍵是您需要選擇一個支持SPI總線的TFT。否則,D1 Mini將沒有足夠的引腳與之連接。
要與TFT一起成功運行,關鍵是要以正確的格式正確聲明UTFT對象。這是我們使用的方法:
// TFT Display
#include
#include
// UTFT::UTFT(byte model, int RS=SDI(MOSI), int WR=SCK, int CS=CS, int RST=NOTINUSE, int SER=DC)
UTFT myGLCD ( ILI9341_S5P, D7, D5, D0, NOTINUSE, D3); //ILI9341 in SPI
步驟5:將濕度傳感器連接到I2C總線
ESP8266 D1 Mini支持IIC總線。引腳D1用于時鐘(SCL或CLK),引腳D2用于數據(SDA)。 IIC總線可以同時支持位于不同地址的多個設備。您可以將傳感器連接到3.3V電源以及D1 Mini的SCL和SDA酒吧。
HDC1080用于感測濕度。我們使用 ClosedCube庫。
temperature[idx] = hdc1080.readTemperature();
if ( temperature[idx] 》 80.0) //Abnormal Reading, reset the chip
hdc1080.reset();
Serial.print(“HDC_Temp = ”); Serial.print(temperature[idx]); Serial.print(“ ”);
//-------------------------------
humidity[idx] = hdc1080.readHumidity();
Serial.print(“HDC_Humidity = ”); Serial.print(humidity[idx]); Serial.print(“ ”);
步驟6:將溫度和氣壓傳感器連接至IIC總線
ESP8266 D1 Mini支持IIC總線。引腳D1用于時鐘(SCL或SCK),引腳D2用于數據(SDA)。 IIC總線可以同時支持位于不同地址的多個設備。您可以將傳感器與3.3V電源以及D1 Mini的SCL和SDA酒吧相連。
BMP180用于感測溫度和氣壓。我們使用 Sparkfun庫。
if ((status = bmp180.startTemperature()) != 0) {
delay(status);
if (bmp180.getTemperature(T) != 0) {
temperature[idx] = T; // Over-ride the inaccurate temperature from HDC1080
if ((status = bmp180.startPressure(3)) != 0) {
delay(status);
if (bmp180.getPressure(P, T) != 0)
pressure[idx] = P;
}
}
}
Serial.print(“bmp180Temp = ”); Serial.print(temperature[idx]); Serial.print(“ ”);
Serial.print(“PRS = ”); Serial.print(pressure[idx]); Serial.print(“ ”);
步驟7:將TVOC傳感器連接到IIC總線
ESP8266 D1 Mini支持IIC總線。引腳D1用于時鐘(SCL或SCK),引腳D2用于數據(SDA)。 IIC總線可以同時支持位于不同地址的多個設備。您可以將傳感器與3.3V電源以及D1 Mini的SCL和SDA酒吧相連。
我們使用的TVOC傳感器是CCS811, Adafruit庫可以讀取數據。
但是,我們需要注意CCS811的自校準過程,以便快速獲取數據。添加了一個額外的例程以將BASELINE數據記錄在ESP8266的EEPROM中。 CCS811會自動連續調整BASELINE。我們定期將BASELINE記錄到EEPROM。在下一次系統啟動時,我們在CCS811預熱后讀入最后記錄的BASELINE。詳細邏輯可從CCS811數據表中獲得。
示例代碼如下:
setup(){
// Sensor Initilization
hdc1080.begin(0x40);
hdc1080.reset();
bmp180.begin();
ccs811.begin();
S8Serial.begin(9600);
S8_begin(&S8Serial);
//==== CCS811 BASELINE MANAGEMENT ====
EEPROM.begin(512);
copyCurrentTime(&curr_epoch, &curr_remain_millis); // Get the current time in Epoch
if (startup_epoch == 0) // initialize startup_epoch = curr_epoch;
if (last_eeprom_write_epoch == 0) // initialize
last_eeprom_write_epoch = curr_epoch;
// Read all the BASELINE records and pick the best one to use
score = 0; best_score = 0; lowest_score = 4294967295; //2^32-1
for (int j = 0; j 《 BASELINEREC_MAX; j++) { // Check all records in the EEPROM
// Read the BASELINE record
int baselinerec_addr_read = sizeof(uint16_t) + sizeof(struct baselinerec) * j;
for (int i = 0; i 《 sizeof(struct baselinerec); i++)
* ((uint8_t*)&_baselinerec_read + i) = (uint8_t)EEPROM.read(baselinerec_addr_read + i);
// Calculate the score of each record
if (_baselinerec_read.signature != 0xABAB) // record not initialized
score = 0;
else if ( (curr_epoch - _baselinerec_read.epoch) 》 28 * 24 * 3600) // record too old
score = 0;
else if ( _baselinerec_read.uptime 《 4 * 3600) // record was obtained over a too short period
score = 0;
else // score formula can be customized
score = _baselinerec_read.uptime + ((28 * 24 * 3600 - (curr_epoch - _baselinerec_read.epoch)) / 2);
Serial.print(“j=”); Serial.print(j);
Serial.print(“ signature=”); Serial.print(_baselinerec_read.signature);
Serial.print(“ epoch=”); Serial.print(_baselinerec_read.epoch);
Serial.print(“ uptime=”); Serial.print(_baselinerec_read.uptime);
Serial.print(“ avg_tvoc=”); Serial.print(_baselinerec_read.avg_tvoc);
Serial.print(“ score=”); Serial.println(score);
delay(100); // prevent D1 Mini software reset due to too much I/O
// Select the one with the highest score for read
if (score 》 best_score) {
best_score = score;
baseline_idx_read = j;
}
// Select the one with the highest score for write
if (score 《 lowest_score) {
lowest_score = score;
baseline_idx_write = j;
}
}
Serial.print(“baseline_idx_read=”); Serial.print(baseline_idx_read);
show_text(“baseline_idx_read=%.0f”, (float)baseline_idx_read, GEN_COLOR, CHART_LEFT, CHART_TOP + 96, 0);
Serial.print(“ baseline_idx_write=”); Serial.println(baseline_idx_write);
show_text(“baseline_idx_write=%.0f”, (float)baseline_idx_write, GEN_COLOR, CHART_LEFT, CHART_TOP + 112, 0);
// Read in the best record
if (best_score != 0) {
int baselinerec_addr_read = sizeof(uint16_t) + sizeof(struct baselinerec) * baseline_idx_read;
for (int i = 0; i 《 sizeof(struct baselinerec); i++)
* ((uint8_t*)&_baselinerec_read + i) = (uint8_t)EEPROM.read(baselinerec_addr_read + i);
}
// update the eeprom baseline index to record where is the current record
if (eeprom_baseline_idx_updated == false) {
EEPROM.write(0, baseline_idx_write 》》 8);
EEPROM.write(1, (baseline_idx_write 《《 8) 》》 8);
eeprom_baseline_idx_updated = true;
}
EEPROM.commit();
}
//The code below should be put in the loop() part of the sketch to write the lastly recorded BASELINE to CCS811
//and record the tunned BASELINE periodically.
//==== CCS811 BASELINE MANAGEMENT ====
// Get curr_epoch & uptiime for the operation below
copyCurrentTime(&curr_epoch, &curr_remain_millis);
uptime = curr_epoch - startup_epoch;
// write the best BASELINE to CCS811 after uptime 》 X minutes
if (ccs811_baseline_updated == false && best_score != 0 && uptime 》= 600) {
ccs811.writeBaseline(_baselinerec_read.baseline);
ccs811_baseline_updated = true;
}
// write to EEPROM periodically - Y minutes
uint8_t baseline[2];
ccs811.readBaseline(baseline);
if ( curr_epoch - last_eeprom_write_epoch 》= 900) {
_baselinerec_write.epoch = curr_epoch;
_baselinerec_write.baseline[0] = baseline[0];
_baselinerec_write.baseline[1] = baseline[1];
_baselinerec_write.uptime = uptime;
int baselinerec_addr_write = sizeof(uint16_t) + sizeof(struct baselinerec) * baseline_idx_write;
for (int i = 0; i 《 sizeof(struct baselinerec); i++)
EEPROM.write(baselinerec_addr_write + i, *((uint8_t*)&_baselinerec_write + i));
EEPROM.commit();
last_eeprom_write_epoch = curr_epoch;
}
步驟8:連接PM2.5/PM 10 TTL RX接口的傳感器
我們使用的傳感器來自中國制造商(六度空氣)。它通過TTL接口提供數據。每秒生成PM2.5和PM10讀數。無需將命令發送到傳感器即可觸發輸出。因此,只需將傳感器上的TX引腳和D1 Mini上的RX引腳連接在一起。
請注意,D1 Mini依靠其TX和RX引腳來更新草圖。將RX引腳與傳感器連接會影響草圖下載。因此,在草圖下載過程中實現了一個斷開RX連接的開關。不使用傳感器時,小風扇會消耗功率。傳感器處有一個啟用(CS)引腳,用于打開/關閉風扇和數據輸出。使能(CS)引腳連接到D1 Mini的D4。
以下是從傳感器獲取數據的代碼:
//--------------------------
// PM2.5
//Serial.println(“PM Begin ”);
digitalWrite(PM25_CS, HIGH);
delay(9000); //64 byte buffer only; 7 byte per sample; 64/7=9 max if 1 sec per sample.
while (Serial.available() 》 0) {
do {
incomingByte = Serial.read();
//Serial.print(incomingByte, HEX);
} while (incomingByte != 0xAA);
if (incomingByte == 0xAA) {
Serial.readBytes(buf, 6);
if (buf[5] == 0xFF && (buf[0] + buf[1] + buf[2] + buf[3]) == buf[4]) { //0xFF = term char; checksum
PM2_5Value = ((buf[0] 《《 8) + buf[1]) / 10.0;
PM10Value = ((buf[2] 《《 8) + buf[3]) / 10.0;
}
}
}
//Serial.println(“PM End”);
digitalWrite(PM25_CS, LOW);
pm25[idx] = (float)PM2_5Value;
pm10[idx] = (float)PM10Value;
Serial.print(“PM2.5 = ”); Serial.print(pm25[idx]); Serial.print(“ ”);
Serial.print(“PM10 = ”); Serial.println(pm10[idx]);
步驟9:將CO2傳感器連接到軟件序列
SensorAir S8通過TTL接口輸出CO2濃度。由于D1 Mini的RX引腳用于PM2.5/PM 10傳感器,因此我們需要使用CO2傳感器的軟件串行。
D1 Mini沒有太多的引腳。我們幾乎用完了別針。幸運的是,我們可以將CO2傳感器的TX和RX引腳連接在一起,并通過單工模式與之通信。 AirSense S8的數據表中沒有對此進行記錄,但是可以使用!因此,與CO2傳感器通信僅在D1 Mini上消耗了一個引腳。
到目前為止,我們還沒有看到來自Internet的用于SensorAir S8的Arduino庫。因此,為此目的創建了軟件串行功能。首先,我們需要為ABC周期初始化SensorAir S8。然后,我們可以自動從傳感器讀取CO2值。
初始化代碼:
void S8_begin(SoftwareSerialx *ss) {
//byte ch, cmd[] = {0xFE, 0x6, 0x0, 0x1F, 0x0, 0xB4, 0xAC, 0x74}; // Set ABC Period to be 180 hours
//byte ch, cmd[] = {0xFE, 0x6, 0x0, 0x1F, 0x0, 0x30, 0xAC, 0x17}; // Set ABC Period to be 48 hours
byte ch, cmd[] = {0xFE, 0x6, 0x0, 0x1F, 0x0, 0x18, 0xAC, 0x09}; // Set ABC Period to be 24 hours
ss-》enableTx(true);
for (int i = 0; i 《 8; i++)
ss-》write(cmd[i]);
ss-》enableTx(false);
delay(250);
if (ss-》available()) {
Serial.print(“SenseAir Response:”);
while (ss-》available()) {
ch = (byte) ss-》read();
Serial.print(ch 《 0x01 ? “ 0” : “ ”);
Serial.print(ch, HEX);
}
Serial.println();
}
}
讀取代碼:
int S8_getCO2(SoftwareSerialx *ss, uint16_t*S8_CO2, uint16_t*S8_meterstatus) {
int i;
//byte ch, cmd[] = {0xFE, 0x4, 0x0, 0x03, 0x0, 0x1, 0xD5, 0xC5}; //Get CO2 Only
byte ch, cmd[] = {0xFE, 0x4, 0x0, 0x0, 0x0, 0x4, 0xE5, 0xC6}; //Get MeterStatus and CO2
byte result[20];
ss-》enableTx(true);
for (int i = 0; i 《 8; i++)
ss-》write(cmd[i]);
ss-》enableTx(false);
delay(250);
while (!ss-》available()) // wait until data is available
delay(100);
while ( ss-》available()) {
if ( (ch = ss-》read()) == 0xFE) // wait until the header is available
i = 0;
result[i] = ch;
i++;
}
*S8_CO2 = result[9] * 256 + result[10];
*S8_meterstatus = result[3] * 256 + result[4];
return *S8_meterstatus;
}
步驟10:將草圖下載到D1 Mini
將草圖下載到D1 Mini并完成。下載草圖時,請記住要斷開PM2.5/PM10傳感器的連接。
步驟11:給電池充電并打開
它將首先連接到WiFi,然后發送NTP數據包并獲得Internet時間。然后,掃描其EEPROM以獲取CCS811的最佳BASELINE設置。
步驟12:讓它自己運行。..
讓它自己運行一段時間。您將看到測量的圖表和實時指標!!!
責任編輯:wv
-
監視器
+關注
關注
1文章
780瀏覽量
33267 -
ESP8266
+關注
關注
50文章
962瀏覽量
45450
發布評論請先 登錄
相關推薦
?懸浮塵埃粒子計數器:多領域應用,守護空氣質量?
空氣質量傳感器有哪些優勢
博世推出BME690室內空氣質量傳感器
空氣質量傳感器的功能特點
守護空氣質量的科技利器:PM2.5傳感器全面解析
![守護<b class='flag-5'>空氣質量</b>的科技利器:PM2.5<b class='flag-5'>傳感器</b>全面解析](https://file1.elecfans.com/web2/M00/FF/9F/wKgaomajYuqAW43AAAA4Szu-SlE887.png)
ESP8266 Huzzah不響應AT命令的原因?
城市空氣質量監測解決方案
如何使用空氣質量傳感器檢測新房室內空氣質量
![如何使用<b class='flag-5'>空氣質量</b><b class='flag-5'>傳感器</b>檢測新房室內<b class='flag-5'>空氣質量</b>](https://file1.elecfans.com/web2/M00/EA/1C/wKgZomZW0bmAai1pAAH15pSAHWo277.png)
一款基于 LoRaWAN 標準協議、遠距離通信的空氣質量傳感器
![一款基于 LoRaWAN 標準協議、遠距離通信的<b class='flag-5'>空氣質量</b><b class='flag-5'>傳感器</b>](https://file1.elecfans.com//web2/M00/E4/EA/wKgZomZBbbGAHKRiAACNekdDq1k762.jpg)
空氣質量傳感器搭上智能手機?三星新專利曝光
城市空氣質量監測系統功能特點
用于室內空氣質量應用的數字氣體傳感器模塊RRH464410 RRRH46410數據表
![用于室內<b class='flag-5'>空氣質量</b>應用的數字氣體<b class='flag-5'>傳感器</b>模塊RRH464410 RRRH46410數據表](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
RRH46410:用于室內空氣質量應用的數字氣體傳感器模塊
![RRH46410:用于室內<b class='flag-5'>空氣質量</b>應用的數字氣體<b class='flag-5'>傳感器</b>模塊](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
評論