1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TTGO ESP-32 OLED 18650で二酸化炭素観測システムの構築

Posted at

MH-Z19とTTGO ESP-32をつなげてみました。
TTGO-CO2-1.jpg
TTGO-CO2-2.jpg
https://www.arduinolibraries.info/libraries/mh-z-co2-sensors

もとは、こちら
https://qiita.com/kitazaki/items/1b3859a1d29e60af6489
野良ハック 北崎さんのものを頂いています。

ちなみに、動かなくて悩んでいたところ、ESP-32のシリアルはHardwareSerial を使うこと
serial1と入れると動かない

以下のようなコメントが出ます。
Arduino.ino.cpp.o:(.bss.Serial1+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

Serial-errr.jpg

これを解決するのに3か月かかり、

Serial→Ser1など違うものにします。
これはSerialはすでに定義されているということです。
ちなみに、いろいろなライブラリを使おうとしましたが、トラブルに見舞われたので、北崎さんのコードを引用した方が良い気がします。
ちなみに、ArduinoのライブラリでESP32AnalogSoftCalを使ってみましたが、出力されず、使えませんでした・・

次にOLEDにCo2の濃度を表示させますがdisplay.drawStringはStringTextのフォーマットにする必要があります。
変数にして置換しようとしましたが、以下にします。
display.drawString(X,Y, String(StringTextへ変換するもの));


//==============================
// https://qiita.com/kitazaki/items/1b3859a1d29e60af6489
//==============================
# include <WiFi.h>
# include "SSD1306.h"
HardwareSerial Ser1(0);
uint8_t ledPin = 2; // Onboard LED reference
SSD1306 display(0x3c, 5, 4); // instance for the OLED. Addr, SDA, SCLSSD1306 display(0x3c, 5, 4); // instance for the OLED. Addr, SDA, SCL
byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
unsigned char res[9];
uint8_t idx = 0;
bool flag = false;
unsigned long co2 = 0;

void setup() {
  Ser1.begin(115200);
  delay(10*1000);

    pinMode(ledPin, OUTPUT);
    Serial.begin(115200);
    display.init(); // initialise the OLED
    display.flipScreenVertically(); // does what is says
    display.setFont(ArialMT_Plain_24); // does what is says
    // Set the origin of text to top left
    display.setTextAlignment(TEXT_ALIGN_LEFT);
}

void loop() {
    digitalWrite(ledPin, LOW);
    delay(1000);
    digitalWrite(ledPin, HIGH);
    delay(1000);
  Ser1.write(cmd,9);
  while(Ser1.available()>0)
  {
    Ser1.readBytes(res, 9);
    flag=true;
  }
  if(flag)
  {
    flag=false;
    co2 = 0;
    unsigned int resHigh = (unsigned int) res[2];
    unsigned int resLow = (unsigned int) res[3];
    co2 = (256*resHigh)+resLow;
  }
  Ser1.print("co2: ");
  Ser1.print(co2);
  Ser1.println(" ppm");


  if(co2 > 1000){
  Ser1.println("co2:Over ");
  }else{
  Ser1.println("co2:Under ");
  } 
   display.clear(); // clear the display
            display.drawString(0,0, "co2:");
            display.drawString(0,25, String(co2));
            display.drawString(80,25, "ppm");
    display.display(); // display whatever is in the buffer

  delay(5*1000);
}

ちなみに、この後いろいろとトラブルが発生することに。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?