2016年1月28日木曜日

BME280に変えてみる 

DHT11でモニターが走ってサーバーにもデータを送ってくるようになったが、手元にBME280があったのでこれも試してみることにする。 これも”Todotaniのはやり物Log”のESP-WROOM-02でI2Cセンサーを使用するを参考にさせていただき、Embedded Adventureのコードを利用してみた。 単にArduinoでBME280からデータを引っ張り出して、シリアルモニターに出すプログラムなので、不必要なところをコメントアウトして、DHT11用のコードのESP-WROOM-02の部分をコピペで作ってみました。
当然かもしれませんが、問題なく動きますので、気圧データもとれる此方を、折角なので、基板に収めて室内に暫く侍らしてみることにします。
ソースを見れば分かるように、BME280からデータを引っぱり出すまでに少し時間が掛かる(終わるまてループで待っている)ので、リポートする時間がぴったり1時間毎になりません。 15時から21時までの6時間で13秒程遅れているので、常に遅れる方向にズレるのであれば、delayの時間を調整して、出来るだけ正確に一時間毎にリポートするようにする必要があります。 deep sleepにすれば解決するのかな?
 
BME280はちょっとコストが高いのと、裸の基板の上に載っているので、実際にどんな形状の筐体に収めるのか、ちょっと工夫が要ると思う。

もう少しコストが抑えられないかなー、、。

======================== WeatherMonitorBME280.ino ==============================
/*
All rights reserved.

Contact us at source [at] embeddedadventures.com
www.embeddedadventures.com

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

- Neither the name of Embedded Adventures nor the names of its contributors
  may be used to endorse or promote products derived from this software
  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.

*/

// BME280 MOD-1022 weather multi-sensor Arduino demo
// Written originally by Embedded Adventures

#include
#include
extern "C" {
  #include "user_interface.h"
}

#include

// ESP8266 related codes added by t.Yamada
const char* ssid = "Buffalo-G-4CC0"; // SSID to access network
const char* password = "65rhwrit3nnee"; // password to connect to the network
const char* host = "192.168.1.3"; // host address
const char* Identifier = "MyRoom";  // Identifier to locate the monitor
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);


// Arduino needs this to pring pretty numbers
void printFormattedFloat(float x, uint8_t precision) {
char buffer[10];

  dtostrf(x, 7, precision, buffer);
  Serial.print(buffer);

}

// Global declaration to use for WiFi
float t;
float h;
float p;

//print out the measurements
//and set the most accurete values to global variables - added by T.Yamada
void printCompensatedMeasurements(void) {

float temp, humidity,  pressure, pressureMoreAccurate;
double tempMostAccurate, humidityMostAccurate, pressureMostAccurate;
char buffer[80];

  temp      = BME280.getTemperature();
  humidity  = BME280.getHumidity();
  pressure  = BME280.getPressure();
 
  pressureMoreAccurate = BME280.getPressureMoreAccurate();  // t_fine already calculated from getTemperaure() above
 
  t = tempMostAccurate     = BME280.getTemperatureMostAccurate();
  h = humidityMostAccurate = BME280.getHumidityMostAccurate();
  p = pressureMostAccurate = BME280.getPressureMostAccurate();
  Serial.println("                Good  Better    Best");
  Serial.print("Temperature  ");
  printFormattedFloat(temp, 2);
  Serial.print("         ");
  printFormattedFloat(tempMostAccurate,2);
  Serial.println();
 
  Serial.print("Humidity     ");
  printFormattedFloat(humidity, 2);
  Serial.print("         ");
  printFormattedFloat(humidityMostAccurate,2);
  Serial.println();

  Serial.print("Pressure     ");
  printFormattedFloat(pressure, 2);
  Serial.print(" ");
  printFormattedFloat(pressureMoreAccurate,2);
  Serial.print(" ");
  printFormattedFloat(pressureMostAccurate,2);
  Serial.println();
}


// setup wire and serial

void setup()
{
  Wire.begin();
  Serial.begin(115200);

  wifi_set_sleep_type(LIGHT_SLEEP_T);  // couldn't use deep sleep....
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid); 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
 
}

// main loop

void loop()
{

  uint8_t chipID;
 
  Serial.println("Welcome to the BME280 MOD-1022 weather multi-sensor test sketch!");
  Serial.println("Embedded Adventures (www.embeddedadventures.com)");
  chipID = BME280.readChipId();
 
  // find the chip ID out just for fun
  Serial.print("ChipID = 0x");
  Serial.print(chipID, HEX);
 

  // need to read the NVM compensation parameters
  BME280.readCompensationParams();
 
  // Need to turn on 1x oversampling, default is os_skipped, which means it doesn't measure anything
  BME280.writeOversamplingPressure(os1x);  // 1x over sampling (ie, just one sample)
  BME280.writeOversamplingTemperature(os1x);
  BME280.writeOversamplingHumidity(os1x);
  /*
  // example of a forced sample.  After taking the measurement the chip goes back to sleep
  BME280.writeMode(smForced);
  while (BME280.isMeasuring()) {
    Serial.println("Measuring...");
    delay(50);
  }
  Serial.println("Done!");
 
  // read out the data - must do this before calling the getxxxxx routines
  BME280.readMeasurements();
  Serial.print("Temp=");
  Serial.println(BME280.getTemperature());  // must get temp first
  Serial.print("Humidity=");
  Serial.println(BME280.getHumidity());
  Serial.print("Pressure=");
  Serial.println(BME280.getPressure());
  Serial.print("PressureMoreAccurate=");
  Serial.println(BME280.getPressureMoreAccurate());  // use int64 calculcations
  Serial.print("TempMostAccurate=");
  Serial.println(BME280.getTemperatureMostAccurate());  // use double calculations
  Serial.print("HumidityMostAccurate=");
  Serial.println(BME280.getHumidityMostAccurate()); // use double calculations
  Serial.print("PressureMostAccurate=");
  Serial.println(BME280.getPressureMostAccurate()); // use double calculations
  */
  // Example for "indoor navigation"
  // We'll switch into normal mode for regular automatic samples
 
  BME280.writeStandbyTime(tsb_0p5ms);        // tsb = 0.5ms
  BME280.writeFilterCoefficient(fc_16);      // IIR Filter coefficient 16
  BME280.writeOversamplingPressure(os16x);    // pressure x16
  BME280.writeOversamplingTemperature(os2x);  // temperature x2
  BME280.writeOversamplingHumidity(os1x);     // humidity x1
 
  BME280.writeMode(smNormal);
  char temp[80];
  while (1) {
    while (BME280.isMeasuring()) {
      delay(50);
    }
   
    // read out the data - must do this before calling the getxxxxx routines
    BME280.readMeasurements();
    printCompensatedMeasurements();

    // Use WiFiClient class to create TCP connection
    Serial.print("connecting to ");
    Serial.println(host);
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)){
      Serial.println("connection failed");
      return;
    }
    String url = "/weather/weather_log.php";
           url += "?location=" + String(Identifier); // ID to identify location
           url += "&temperature="+String(t);
           url += "&humidity=" +String(h);
           url += "&pressure=" +String(p);
    Serial.print("Requesting URL: ");
    Serial.println("GET "+url);

    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                  "Host: " + host + "\r\n" +
                  "Connection: close\r\n\r\n");
    delay(50);
   
    // Read all the lines of the reply from server and print them to Serial
    Serial.println("Returned message from server: ");
    while(client.available()){
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }
 
    Serial.println();
    Serial.println("closing connection");

    //ESP8266 Deep Sleap mode
    //1:μ秒での復帰までのタイマー時間設定 (60分)  2:復帰するきっかけの設定(モード設定)
    //ESP.deepSleep(1 * 60 * 1000 * 1000 , WAKE_RF_DEFAULT); 

    //deepsleepモード移行までのダミー命令
    // delay(1000);
    delay (60*60*1000-800);  // 60 min delay
  }
}

0 件のコメント:

php のインストールの確認

phpって最初のfacebook書くときに使われたみたいで、それなりに歴史のある言語で、私も2006年位から使っていますが、CLIで使う事はあまり無いので、apacheとの連携のトラブル(mod_phpのバージョンの齟齬)などは気になりますが、拡張モジュールのインストールの問題に...