自学内容网 自学内容网

arduino-i2c-sht4x

https://github.com/Sensirion/arduino-i2c-sht4x

/*
 * THIS FILE IS AUTOMATICALLY GENERATED
 *
 * Generator:     sensirion-driver-generator 0.40.0
 * Product:       sht4x
 * Model-Version: 2.1.1
 */
/*
 * Copyright (c) 2024, Sensirion AG
 * All rights reserved.
 *
 * 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 Sensirion AG 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.
 */
#include <Arduino.h>
#include <SensirionI2cSht4x.h>
#include <Wire.h>

// macro definitions
// make sure that we use the proper definition of NO_ERROR
#ifdef NO_ERROR
#undef NO_ERROR
#endif
#define NO_ERROR 0

SensirionI2cSht4x sensor;

static char errorMessage[64];
static int16_t error;

void setup() {

    Serial.begin(115200);
    while (!Serial) {
        delay(100);
    }
    Wire.begin();
    sensor.begin(Wire, SHT45_I2C_ADDR_44);

    sensor.softReset();
    delay(10);
    uint32_t serialNumber = 0;
    error = sensor.serialNumber(serialNumber);
    if (error != NO_ERROR) {
        Serial.print("Error trying to execute serialNumber(): ");
        errorToString(error, errorMessage, sizeof errorMessage);
        Serial.println(errorMessage);
        return;
    }
    Serial.print("serialNumber: ");
    Serial.print(serialNumber);
    Serial.println();
}

bool enOutput = false;
int loopTime = 3000;

void printData(char aPrecision, float aTemperature, float aHumidity){
      Serial.print(aPrecision);
      Serial.print(": ");
      Serial.print("aTemperature: ");
      Serial.print(aTemperature);
      Serial.print("\t");
      Serial.print("aHumidity: ");
      Serial.print(aHumidity);
      Serial.println();
}

void readOnce(){
      float aTemperature = 0.0;
      float aHumidity = 0.0;
      error = sensor.measureHighPrecision(aTemperature, aHumidity);
      if (error != NO_ERROR) {
          Serial.print("Error trying to execute measureHighPrecision(): ");
          errorToString(error, errorMessage, sizeof errorMessage);
          Serial.println(errorMessage);
          return;
      }
      printData('H', aTemperature, aHumidity);
}

/*
void changeLooptime(int rloopTime){
  if (rloopTime>=48 && rloopTime <=57){
    loopTime = (rloopTime - '0') * 1000;
  }
}
*/
void changeLooptime(String strloopTime){
  //Serial.println(strloopTime);
  //loopTime = stoi(strloopTime);
  //Serial.println(strloopTime);

  if (strloopTime.length() == 0){
    Serial.print("current looptime is ");
    Serial.print(loopTime,DEC);
    Serial.println(" ms.");
    return;
  }else{
    int i = 0;
    for (char c : strloopTime) {
          // Checking if the element is number
          if (c >= '0' && c <= '9') {
              i = i * 10 + (c - '0');
          }
          // Otherwise print bad output
          else {
              Serial.println("number is not right!");
              return;
          }
      }
    loopTime = i;
  }
}

void loop() {

    float aTemperature = 0.0;
    float aHumidity = 0.0;

    // d for disable; e for enable
    // enOutput
    if (loopTime >= 100){
      for (int i = 0; i < loopTime/100; i++){
        int incomingByte = 0;
        if (Serial.available() > 0) {
          incomingByte = Serial.read();
          Serial.print("I received: ");
          Serial.println(incomingByte, DEC);
          switch(incomingByte) {
            case 'd':
              enOutput = false;
              break;
            case 'e':
              enOutput = true;
              break;
            case 'r':
              readOnce();
              break;
              case 't':
              changeLooptime(Serial.readString());
              break;
            default:
              Serial.println("e to enable; d to disable; r to read; t### change looptime(ms)");
              break;
          }
        }
        delay(100);
      }
      //Serial.println(loopTime % 1000, DEC);
      delay(loopTime % 100);
    }else{
      delay(loopTime);
    }

    if ( enOutput == true ){
      error = sensor.measureLowestPrecision(aTemperature, aHumidity);
      if (error != NO_ERROR) {
          Serial.print("Error trying to execute measureLowestPrecision(): ");
          errorToString(error, errorMessage, sizeof errorMessage);
          Serial.println(errorMessage);
          return;
      }
      printData('L', aTemperature, aHumidity);

      error = sensor.measureMediumPrecision(aTemperature, aHumidity);
      if (error != NO_ERROR) {
          Serial.print("Error trying to execute measureMediumPrecision(): ");
          errorToString(error, errorMessage, sizeof errorMessage);
          Serial.println(errorMessage);
          return;
      }
      printData('M', aTemperature, aHumidity);

      error = sensor.measureHighPrecision(aTemperature, aHumidity);
      if (error != NO_ERROR) {
          Serial.print("Error trying to execute measureHighPrecision(): ");
          errorToString(error, errorMessage, sizeof errorMessage);
          Serial.println(errorMessage);
          return;
      }
      printData('H', aTemperature, aHumidity);

      //delay(loopTime);
    }
}


原文地址:https://blog.csdn.net/mlxxqq/article/details/138251329

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!