2020年12月10日木曜日

CODE05 ~Ambientとの接続(問題未解決、でも普通に動くよ)~

 忙しい人のための最低限の分割したバージョンの記事Part4

~Ambientとの接続、そして困難(でも普通に動くよ)~

 まず今回のコードの特徴としては、下記参考元ページのコードと同様に、setup内で処理を完結させ、ディープスリープに移行して規定時間後に復帰して再度処理を行うという点があります。
 また、setup内に前回のloop内のコードをコピペしてあるということもあり、どこが起動関連でどこがデータ処理関連、Ambientへの送信関連なのかがわかるように改行してあります(あと元loopのコードの順番をデータ処理部とLCD表示部に分割した)。
 他には、下記参考元コードでディープスリープの処理はesp_deep_sleepで行われていましたが、M5StickC非公式日本語リファレンスさんを参考にウェイクアップタイマーの記述とディープスリープ開始の記述に分割しておきました。
 3000ミリ秒、つまり3秒間取得したデータが表示されている(と思ってたけどなんかディープスリープ中も表示されているっぽい??)ので、その場でどんな環境状態なのかを見ることができる時間を設けてあります。また、同じく3秒間WiFi接続成功後に接続したローカルIPアドレスを表示してあります。
接続確認表示
取得時データの表示(前回のと何ら変わってないけど)



code:2 MultiSensingSketchONLYAmbient
//Include Libraries ライブラリをインクルード
#include <M5StickC.h>
#include "SHT3X.h"
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include <Adafruit_BMP280.h>
#include "MHZ19_uart.h"
#include "Ambient.h"

//Define Valiable 変数を定義
SHT3X sht3x;
Adafruit_BMP280 bme;
MHZ19_uart mhz19;

#define uS_TO_S_FACTOR 1000000 //マイクロ秒を秒に変換する定義?
#define TIME_TO_SLEEP 30 //30秒でESP32をスリープにする定義?

WiFiClient client;
Ambient ambient;

//Ssid and password of connecting WiFi 接続するWiFiのSSIDとパスワード
const char* ssid = "YOUR SSID";
const char* password = "YOUR PASSWORD";

unsigned int channelID = YOUR ChannelID of Ambient; //ChannelID of Ambient AmbientのチャネルID
const char* writeKey = "YOUR WriteKey of Ambient"; //Writekey of Ambient Ambientのライトキー

//Calculate temperature, humidity and air pressure in floating point? 浮動小数点で温湿度・気圧を計算?
float tmp = 0.0;
float hum = 0.0;
float pressure = 0.0;

void setup() {
  // put your setup code here, to run once:
  M5.begin(); //Initialize M5StickC M5StickCを初期化
  delay(50); //Wait 50msecs 50ミリ秒待機

  mhz19.begin(32,33); //Initialize I2C of MHZ19 MHZ19のI2Cを初期化
  mhz19.setAutoCalibration(false); //AutoCalibration turn off 自動校正をしない
  delay(3000); //Wait 3000msecs 3000ミリ秒待機
  
  Wire.begin(0,26); //Initialize I2C of ENV2HAT ENV2HATのI2Cを初期化
  M5.Axp.ScreenBreath(10); //Decrease Lcd's Brightness 画面の輝度を下げる
  setCpuFrequencyMhz(160); //Set Cpu Frequency to 160MHz CPUの動作周波数を160MHzに
  M5.Lcd.fillScreen(BLACK); //Fill Screen Black 背景色を黒に
  M5.Lcd.setRotation(3); //Set Lcd's Direction 画面を横向き(左上)に
  M5.Lcd.setCursor(0, 2, 2); //Set Cursor to (0,2) and Assign Fonts 2 カーソルを(0,2)へ、フォントは2を指定
  M5.Lcd.println("Multi-Environmental-Sensor-System"); //Print to Lcds in"" 画面に””内の文章を表示
  
  if (!bme.begin(0x76)){  
      Serial.println("Could not find a valid BMP280 sensor, check wiring!");
      while (1);  //Error message if the BMP280 sensor was not detected? BMP280センサが検出されなかった場合のエラー表示?
  }

  WiFi.begin(ssid, password); //Starting connection of WiFi WiFi接続開始
  
  while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        M5.Lcd.print("."); //WiFi接続待ちの間500ミリ秒ごとに"."を表示
  }

  M5.Lcd.println("Successed!!");
  M5.Lcd.println("IP: "); //Print IPaddress IPアドレスを表示
  M5.Lcd.println(WiFi.localIP());

  ambient.begin(channelID, writeKey, &client); //Assign channelID and writekey and initialize ambient チャネルIDとライトキーを指定してAmbientの初期化
  
  delay(3000); //Wait 3000msecs 3000ミリ秒待機



  if(sht3x.get()==0){
    tmp = sht3x.cTemp;
    hum = sht3x.humidity; //Acquisition of temperature and humidity data from sht3x? sht3xからの温湿度データの取得?
  }

  float pressure = bme.readPressure(); //Calculating atmospheric pressure in floating point? 浮動小数点で大気圧を計算?
  int co2, temp; //Calculate CO2 concentration by integer? 整数でCO2濃度を計算?
  int i;
  unsigned int t; 
  co2 = mhz19.getPPM(); //Read datas from MH-Z19 MH-Z19からのデータ呼び出し?
  temp = mhz19.getTemperature();
  float vbat = M5.Axp.GetBatVoltage(); //Get the battery voltage of the M5StickC? M5StickCのバッテリー電圧を取得?

  M5.Lcd.fillScreen(BLACK); //Fill Screen Black(overwrite) 背景色を黒に上書き
  
  M5.Lcd.setCursor(0, 2, 1); //Set Cursor to (0,2) and Assign Fonts 1 カーソルを(0,2)へ、フォントは1を指定
  M5.Lcd.println("Multi-Environmental-Sensor-System"); //Print to Lcds in"" 画面に””内の文章を表示
  
  M5.Lcd.setCursor(0, 22, 2); //Set Cursor to (0,22) and Assign Fonts 2 カーソルを(0,22)へ、フォントは2を指定
  M5.Lcd.printf("Temp: %2.1fC Humi: %2.1f%%", tmp, hum); //Display of degrees Celsius and humidity in one decimal place 摂氏度および湿度を小数点以下1桁で表示

  M5.Lcd.setCursor(0, 42, 2); //Set Cursor to (0,42) and Assign Fonts 2 カーソルを(0,42)へ、フォントは2を指定
  M5.Lcd.printf("Pressure: %4.2fhPa", pressure / 100); //Atmospheric pressure in units of hPa to three decimal places 大気圧をhPaの単位で小数点以下3桁まで表示 
  
  delay(100); //Wait 100msecs 100ミリ秒待機

  if (!bme.begin(0x76)){  
      Serial.println("Could not find a valid BMP280 sensor, check wiring!");
      while (1); //Error message if the BMP280 sensor was not detected? BMP280センサが検出されなかった場合のエラー表示?
  }

  M5.Lcd.setCursor(0, 62, 2); //Set Cursor to (0,62) and Assign Fonts 2 カーソルを(0,62)へ、フォントは2を指定
  M5.Lcd.printf("CO2: %4dppm",co2); //CO2 concentration in integers in ppm CO2濃度をppmの単位で整数で表示
  
  delay(100); //Wait 100msecs 100ミリ秒待機
  
  M5.Lcd.setCursor(62, 10, 1); //Set Cursor to (62,10) and Assign Fonts 1 カーソルを(62,10)へ、フォントは1を指定
  M5.Lcd.printf("vbat: %4.2fV",vbat); //Battery voltage displayed in 2 decimal places バッテリー電圧を小数点以下2桁で表示

  //温湿度・気圧・CO2濃度・バッテリ電圧をAmbientに送信
  ambient.set(1, tmp);
  ambient.set(2, hum);
  ambient.set(3, co2);
  ambient.set(4, pressure / 100);
  ambient.set(5, vbat);

  ambient.send();

  delay(3000); //Wait 3000msecs 3000ミリ秒待機

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //Wakeup time from deep sleep ディープスリープからの復帰時間
  esp_deep_sleep_start(); //Start DEEP SLEEP MODE ディープスリープ開始

}

uint8_t setup_flag = 1;

void loop() {
  // put your main code here, to run repeatedly:
  
}

追記:上記コード内25行目、unsigned int channelID = YOUR ChannelID of Ambient;となっているところですが、ここは定数(数字)が直接入るため""(ダブルクォーテーション)は必要ありません。

0 件のコメント:

コメントを投稿