Új hozzászólás Aktív témák

  • layerke

    aktív tag

    válasz Dodi_30 #3993 üzenetére

    Több feladat egy skiccben:
    // Enable debug prints to serial monitor
    //#define MY_DEBUG

    // Enable and select radio type attached
    //#define MY_RADIO_NRF24
    #define MY_RADIO_RFM69

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

    // BME280 libraries and variables
    // Bosch BME280 Embedded Adventures MOD-1022 weather multi-sensor Arduino code
    // Written originally by Embedded Adventures
    // https://github.com/embeddedadventures/BME280
    #include <BME280_MOD-1022.h>

    #define MY_PARENT_NODE_ID 0
    #define TEMP_CHILD 0
    #define HUM_CHILD 1
    #define BAT_CHILD 2
    int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
    int oldBatteryPcnt = 0;

    // Sleep time between reads (in ms). Do not change this value as the forecast algorithm needs a sample every minute.
    const unsigned long SLEEP_TIME = 120000;

    float lastTemp;
    float lastHum;
    float lastBat;

    boolean metric;
    MyMessage tempMsg(TEMP_CHILD, V_TEMP);
    MyMessage humMsg(HUM_CHILD, V_HUM);
    MyMessage batMsg(BAT_CHILD, V_VOLTAGE);

    void setup() {
    // metric = getConfig().isMetric;
    Wire.begin(); // Wire.begin(sda, scl)
    // use the 1.1 V internal reference
    // pinMode(5, OUTPUT);
    #if defined(__AVR_ATmega2560__)
    analogReference(INTERNAL1V1);
    #else
    analogReference(INTERNAL);
    #endif
    }

    void presentation() {
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo("BME280 BAT furdo", "1.0");

    // Register sensors to gw (they will be created as child devices)
    present(TEMP_CHILD, S_TEMP);
    present(HUM_CHILD, S_HUM);
    present(BAT_CHILD, S_MULTIMETER);
    }

    // Loop
    void loop() {

    // digitalWrite(5, HIGH);
    // delay(10);
    Wire.begin();
    BME280.readCompensationParams();
    BME280.writeStandbyTime(tsb_0p5ms); // tsb = 0.5ms
    BME280.writeFilterCoefficient(fc_16); // IIR Filter coefficient 16
    BME280.writeOversamplingTemperature(os8x); // temperature x8
    BME280.writeOversamplingHumidity(os8x); // humidity x8

    BME280.writeMode(smNormal);

    while (1) {
    // Just to be sure, wait until sensor is done mesuring
    while (BME280.isMeasuring()) {
    }

    // Read out the data - must do this before calling the getxxxxx routines
    BME280.readMeasurements();

    float temperature = BME280.getTemperatureMostAccurate(); // must get temp first
    float humidity = BME280.getHumidityMostAccurate();

    Serial.println();
    Serial.print("Temperature = ");
    Serial.print(temperature);
    Serial.println(" C");
    Serial.print("Humidity = ");
    Serial.print(humidity);
    Serial.println(" %");
    int sensorValue = analogRead(BATTERY_SENSE_PIN);
    int batteryPcnt = sensorValue / 10;
    float batteryV = sensorValue * 0.003363075;
    #ifdef MY_DEBUG
    Serial.print("Battery Voltage: ");
    Serial.print(batteryV);
    Serial.println(" V");

    Serial.print("Battery percent: ");
    Serial.print(batteryPcnt);
    Serial.println(" %");
    #endif

    if (oldBatteryPcnt != batteryPcnt) {
    // Power up radio after sleep
    sendBatteryLevel(batteryPcnt);
    oldBatteryPcnt = batteryPcnt;
    }

    send(tempMsg.set(temperature, 2));
    lastTemp = temperature;

    send(humMsg.set(humidity, 1));
    lastHum = humidity;

    send(batMsg.set(batteryV, 2));
    lastBat = batteryV;

    sleep(SLEEP_TIME);

    }
    }

    [ Szerkesztve ]

Új hozzászólás Aktív témák