Giter Site home page Giter Site logo

erriez / erriezbmx280 Goto Github PK

View Code? Open in Web Editor NEW
6.0 4.0 4.0 3.42 MB

BMP280 / BME280 temperature/pressure/humidity sensor library for Arduino

Home Page: https://github.com/Erriez/ErriezArduinoLibrariesAndSketches

License: MIT License

C++ 100.00%
bmp280 bme280 temperature pressure humidity sensor library arduino example i2c

erriezbmx280's People

Contributors

erriez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

erriezbmx280's Issues

Forced mode

One item your documentation doesn't touch on is when using forced mode, the BME goes to sleep after taking readings, and if it isn't woken up again (easiest method given the functionality of the library is to use setSampling again) the subsequent readings will all be the same, it doesn't update.

Abnormal values after some time

First of all, thank your very much for sharing your work.

I would like to return the values from the BME280 when I issue a command in a Telegram chat. I am not sure if I need to update those values every 1 second in the loop() function, then send them to me when I issued the command. I tried capturing the values from the sensor only when the command is issued but after leaving the module unattended for some minutes, the values were not correct anymore (like 623.39 hPa instead of 1020.71 hPa).

My project is comprised of a Wemos D1, a PIR and a BME238 sensor, powered by USB. The PIR triggers and interrupt and a message is sent to a Telegram channel; the BME238 captures temperature, humidity, pressure and altitude and these values are sent to me when I type "/status". It is a basic surveillance/weather station that is placed in my backyard. My only doubt is how to correctly retrieve the values from the BME238.

I would appreciate your feedback on how to improve my code : )

/*******************************************************************
  Wemos D1 Mini
  HC-SR501 PIR output to D7: send message to Telegram channel upon interrupt
  BME280 to D1 D2: sends weather data to Telegram chat upon request with "/status"
  TODO: send periodically to Thingspeak

  Using library ESP8266WiFi at version 1.0 in folder: E:\arduino-1.8.13\portable\packages\esp8266\hardware\esp8266\3.0.2\libraries\ESP8266WiFi
  Using library UniversalTelegramBot at version 1.3.0 in folder: E:\arduino-1.8.13\portable\sketchbook\libraries\UniversalTelegramBot
  Using library ArduinoJson at version 6.19.4 in folder: E:\arduino-1.8.13\portable\sketchbook\libraries\ArduinoJson
  Using library BME280 at version 3.0.0 in folder: E:\arduino-1.8.13\portable\sketchbook\libraries\BME280
  Using library Wire at version 1.0 in folder: E:\arduino-1.8.13\portable\packages\esp8266\hardware\esp8266\3.0.2\libraries\Wire
  Using library SPI at version 1.0 in folder: E:\arduino-1.8.13\portable\packages\esp8266\hardware\esp8266\3.0.2\libraries\SPI
 *******************************************************************/

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

// Wifi network station credentials
#define WIFI_SSID "blablabla"
#define WIFI_PASSWORD "blablabla"
#define BOT_TOKEN "blablabla" // Telegram BOT Token (Get from Botfather)

const unsigned long BOT_MTBS = 1000; // mean time between scan messages

X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime; // last time messages' scan has been done

#define CHANNEL_ID "blablabla"
const int motionSensor = 13; //D7
boolean motion = false;
boolean pir_active = false;

#include <Wire.h>
#include <ErriezBMX280.h>

// Adjust sea level for altitude calculation
#define SEA_LEVEL_PRESSURE_HPA      1026.25

// Create BMX280 object I2C address 0x76 or 0x77
ErriezBMX280 bmx280 = ErriezBMX280(0x76);

float temperature, humidity, pressure, altitude;

unsigned long previousMillis = 0;

void IRAM_ATTR detectsMovement() {
  Serial.println("movement detected");
  motion = true;
}

void handleNewMessages(int numNewMessages)
{
  Serial.print("handleNewMessages ");
  Serial.println(numNewMessages);

  for (int i = 0; i < numNewMessages; i++)
  {
    String chat_id = bot.messages[i].chat_id;
    String text = bot.messages[i].text;

    String from_name = bot.messages[i].from_name;
    if (from_name == "")
      from_name = "Guest";

    if (text == "/enpir")
    {
      pir_active = true;
      bot.sendMessage(chat_id, "PIR activated", "");
    }

    if (text == "/dispir")
    {
      pir_active = false;
      bot.sendMessage(chat_id, "PIR deactivated", "");
    }

    if (text == "/status")
    {
      String stat = "Rssi: " + String(WiFi.RSSI());
      stat += "\nPIR: " + String(pir_active);
      stat += "\nTemp: " + String(temperature) + " C";
      stat += "\nHum: " + String(humidity) + " %";
      stat += "\nPres: " + String(pressure) + " hPa";
      stat += "\nAlt: " + String(altitude) + " m";
      bot.sendMessage(chat_id, stat, "Markdown");
    }

    if (text == "/start")
    {
      String welcome = "Welcome, " + from_name + ".\n";
      welcome += "Commands list:\n\n";
      welcome += "/enpir : to switch the pir ON\n";
      welcome += "/dispir : to switch the pir OFF\n";
      welcome += "/status : Returns current status of pir\n";
      bot.sendMessage(chat_id, welcome, "Markdown");
    }
  }
}

void setup()
{
  delay(500);
  Serial.begin(115200);
  Serial.println();

  // Initialize I2C bus
  Wire.begin();
  Wire.setClock(400000);

  // Initialize sensor
  while (!bmx280.begin()) {
    Serial.println(F("Error: Could not detect sensor"));
    delay(3000);
  }

  // Print sensor type
  Serial.print(F("\nSensor type: "));
  switch (bmx280.getChipID()) {
    case CHIP_ID_BMP280:
      Serial.println(F("BMP280\n"));
      break;
    case CHIP_ID_BME280:
      Serial.println(F("BME280\n"));
      break;
    default:
      Serial.println(F("Unknown\n"));
      break;
  }

  // Set sampling - Recommended modes of operation
  //
  // Weather
  //  - forced mode, 1 sample / minute
  //  - pressure ×1, temperature ×1, humidity ×1
  //  - filter off
  //
  // Humidity sensing
  //  - forced mode, 1 sample / second
  //  - pressure ×0, temperature ×1, humidity ×1
  //  - filter off
  //
  // Indoor navigation
  //  - normal mode, t standby = 0.5 ms
  //  - pressure ×16, temperature ×2, humidity ×1
  //  - filter coefficient 16
  //
  // Gaming
  //  - forced mode, t standby = 0.5 ms
  //  - pressure ×1, temperature ×1, humidity ×1
  //  - filter off
  bmx280.setSampling(BMX280_MODE_NORMAL,    // SLEEP, FORCED, NORMAL
                     BMX280_SAMPLING_X16,   // Temp:  NONE, X1, X2, X4, X8, X16
                     BMX280_SAMPLING_X16,   // Press: NONE, X1, X2, X4, X8, X16
                     BMX280_SAMPLING_X16,   // Hum:   NONE, X1, X2, X4, X8, X16 (BME280)
                     BMX280_FILTER_X16,     // OFF, X2, X4, X8, X16
                     BMX280_STANDBY_MS_500);// 0_5, 10, 20, 62_5, 125, 250, 500, 1000


  // PIR Motion Sensor
  pinMode(motionSensor, INPUT);
  // Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
  attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);

  // attempt to connect to Wifi network:
  configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
  secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
  Serial.print("Connecting to Wifi SSID ");
  Serial.print(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.print("\nWiFi connected. IP address: ");
  Serial.println(WiFi.localIP());

  // Check NTP/Time, usually it is instantaneous and you can delete the code below.
  Serial.print("Retrieving time: ");
  time_t now = time(nullptr);
  while (now < 24 * 3600)
  {
    Serial.print(".");
    delay(100);
    now = time(nullptr);
  }
  Serial.println(now);

  //  bot.sendMessage(chat_id, "/start", "");
}

void loop()
{

  // Check for Telegram messages
  if (millis() - bot_lasttime > BOT_MTBS)
  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while (numNewMessages)
    {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }

    bot_lasttime = millis();
  }

  // When interrupt is triggered by motion, don't send message if PIR is set as inactive
  if (motion && pir_active) {
    bot.sendMessage(CHANNEL_ID, "/photo", "");
    motion = false;
  } else {
    motion = false;
  }

// Update BME280 values every 1 seconds
  if (millis() - previousMillis >= 1000) {

    previousMillis = millis();

    temperature = bmx280.readTemperature();
    Serial.print(F("Temperature: "));
    Serial.print(temperature);
    Serial.println(" C");

    if (bmx280.getChipID() == CHIP_ID_BME280) {
      humidity = bmx280.readHumidity();
      Serial.print(F("Humidity:    "));
      Serial.print(humidity);
      Serial.println(" %");
    }

    pressure = bmx280.readPressure() / 100.0F;
    Serial.print(F("Pressure:    "));
    Serial.print(pressure);
    Serial.println(" hPa");

    altitude = bmx280.readAltitude(SEA_LEVEL_PRESSURE_HPA);
    Serial.print(F("Altitude:    "));
    Serial.print(altitude);
    Serial.println(" m");
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.