WROOM-02とI2Cセンサで気象観測

ESP-WROOM-02 を使った リモート温度・湿度・気圧計の製作_takadaさん

の作成・実験

主要部品はWi-Fiモジュール(ESP-WROOM-02)、温度・湿度I2CセンサAM2321 と 気圧センサAE-SPC1000。

回路図

使用部品

http://www.palettesoft.co.jp/technology/pic/etc/press/pic_press.htm

PIC気圧計(SPC1000使用)

    1. ESP-WROOM-02      秋月 @550-
    2. AM2321                  秋月 @700-
    3. AE-Scp1000          (PIC気圧計用を流用)
    4. StepUP-DIP 3.3V   (PIC気圧計用を流用)
    5. ニッケル水素電池  (PIC気圧計用を流用)

スケッチ(プログラム)_takadaさん

*AM2321の部分は変更

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <Wire.h>

//SCP1000 Sensor's memory register addresses:
const int PRESSURE = 0x1F;      //3 most significant bits of pressure
const int PRESSURE_LSB = 0x20;  //16 least significant bits of pressure
const int TEMPERATURE = 0x21;   //16 bit temperature reading
const byte READ = 0b11111100;     // SCP1000's read command
const byte WRITE = 0b00000010;   // SCP1000's write command

//SCP1000 pins used for the connection with the sensor
// the other you need are controlled by the SPI library):
//const int dataReadyPin = 6;
const int chipSelectPin = 5;

// Specify data and clock connections and instantiate SHT1x object
//#define dataPin  2
//#define clockPin 4
//SHT1x sht1x(dataPin, clockPin);

// wifi
const char* ssid     = "rt500k-32b00b-1";
const char* password = "9910895a5fe09";

const char* host = "192.168.1.14";

void setup()
{

 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());

  //SCP100 start the SPI library:
  SPI.begin();
  // initalize the  data ready and chip select pins:
  // pinMode(dataReadyPin, INPUT);
  pinMode(chipSelectPin, OUTPUT);

  //Configure SCP1000 for low noise configuration:
  writeRegister(0x02, 0x2D);
  writeRegister(0x01, 0x03);
  writeRegister(0x03, 0x02);
  // give the sensor time to set up:
  delay(100);

   Serial.begin(115200); // Open serial connection to report values to host

   Wire.begin(2,4);  //AM2321
   Serial.print("humidity");  //AM2321
   Serial.print("\t");        //AM2321
   Serial.println("temperature");   //AM2321

   Serial.println("Starting up");


}

int value = 0;

void loop()
{
  delay(5000);
  ++value;

//AM2321  *SHT10

  byte rdptr[20];

  readAM2321(rdptr,8); //temp_c ⇔ humidity

  float humidity=(float)(rdptr[2]*256+rdptr[3])/10.0;
  float temp_c=(float)(rdptr[4]*256+rdptr[5])/10.0;
  Serial.print(temp_c,1);
  Serial.print("\t");
  Serial.println(humidity,1);

  delay(1000);


  //float temp_c;
  //float temp_f;
  //float humidity;

  // Read values from the sensor
  //temp_c = sht1x.readTemperatureC();
  //temp_f = sht1x.readTemperatureF();
  //humidity = sht1x.readHumidity();

  // Print the values to the serial port
  //Serial.print("Temperature: ");
  //Serial.print(temp_c, DEC);
  //Serial.print("C / ");
  //Serial.print(temp_f, DEC);
  //Serial.print("F. Humidity: ");
  //Serial.print(humidity);
  //Serial.println("%");


//SCP1000

 //Select High Resolution Mode
  writeRegister(0x03, 0x0A);

    delay(1000);
    //Read the temperature data
    int tempData = readRegister(0x21, 2);

    // convert the temperature to celsius and display it:
    float realTemp = (float)tempData / 20.0;
    Serial.print("Temp[℃]=");
    Serial.print(realTemp);


    //Read the pressure data highest 3 bits:
    byte  pressure_data_high = readRegister(0x1F, 1);
    pressure_data_high &= 0b00000111; //you only needs bits 2 to 0

    //Read the pressure data lower 16 bits:
    unsigned int pressure_data_low = readRegister(0x20, 2);
    //combine the two parts into one 19-bit number:
    long pressure = ((pressure_data_high << 16) | pressure_data_low) / 4;

    // display the temperature:
    Serial.println("\tPressure [Pa]=" + String(pressure));

  // Use WiFiClient class to create TCP connections
  Serial.print("connecting to ");
  Serial.println(host);

  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  //String url = "/tempera/tempera_log.php";
  String url = "/tempera/tempera_log.php";
         url = url + "?tempera=" +String( temp_c);
         url = url + "&humidi=" + String(humidity);
         url = url + "&puress=" + String(pressure);
  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");

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

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

void readAM2321(byte *rdptr, byte length )
{
  int i;
  byte  deviceaddress=0x5C;
  //step1
  Wire.beginTransmission(deviceaddress);
  Wire.write(0x00);
  Wire.endTransmission();
  delay(1);
  //step2
  Wire.beginTransmission(deviceaddress);
  Wire.write( 0x03);
  Wire.write(0x00);
  Wire.write(0x04);
  Wire.endTransmission();
  delay(2);
  //step3
  Wire.requestFrom(deviceaddress,length);
  delayMicroseconds(60);
  if(length <= Wire.available())
  {
    for(i=0;i<length;i++)
    {
      rdptr[i] = Wire.read();
    }
  }
}


//Read from or write to register from the SCP1000:
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
  byte inByte = 0;           // incoming byte from the SPI
  unsigned int result = 0;   // result to return
//  Serial.print(thisRegister, BIN);
//  Serial.print("\t");
  // SCP1000 expects the register name in the upper 6 bits
  // of the byte. So shift the bits left by two bits:
  thisRegister = thisRegister << 2;
  // now combine the address and the command into one byte
  byte dataToSend = thisRegister & READ;
//  Serial.println(thisRegister, BIN);
  // take the chip select low to select the device:
  digitalWrite(chipSelectPin, LOW);
  // send the device the register you want to read:
  SPI.transfer(dataToSend);
  // send a value of 0 to read the first byte returned:
  result = SPI.transfer(0x00);
  // decrement the number of bytes left to read:
  bytesToRead--;
  // if you still have another byte to read:
  if (bytesToRead > 0) {
    // shift the first byte left, then get the second byte:
    result = result << 8;
    inByte = SPI.transfer(0x00);
    // combine the byte you just got with the previous one:
    result = result | inByte;
    // decrement the number of bytes left to read:
    bytesToRead--;
  }
  // take the chip select high to de-select:
  digitalWrite(chipSelectPin, HIGH);
  // return the result:
  return(result);
}

// Sends a write command to SCP1000
 void writeRegister(byte thisRegister, byte thisValue) {

   // SCP1000 expects the register address in the upper 6 bits
   // of the byte. So shift the bits left by two bits:
   thisRegister = thisRegister << 2;

   // now combine the register address and the command into one byte:
   byte dataToSend = thisRegister | WRITE;

   // take the chip select low to select the device:
   digitalWrite(chipSelectPin, LOW);

   SPI.transfer(dataToSend); //Send register location
   SPI.transfer(thisValue);  //Send value to record into register

   // take the chip select high to de-select:
   digitalWrite(chipSelectPin, HIGH);
}

観測データのグラフ表示

シェアする

  • このエントリーをはてなブックマークに追加

フォローする