Weather station ESP8266, BMP280

Odgovori
Message
Autoru
Korisnikov avatar
ebaketa
Početnik
Postovi: 46
Pridružio se: Sub Jan 13, 2018 4:40 pm
Lokacija: Vukovar
Kontakt:

Weather station ESP8266, BMP280

#1 Post od ebaketa » Ned Avg 18, 2019 3:05 pm

Vikend projekat! Malo programiranja i testiranja sensora. :)
  • ESP8266
  • OLED SH1106
  • BMP280
Slika
Slika
Slika

Kod: Obeleži sve

// *********************************************************
// Project:   weather station ESP8266
// Date:      17/08/2019
// Version:   0.0.1
// Author:    Elvis Baketa
// Description: 
// *********************************************************

#include <Arduino.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

// setup SH1106 OLED display constructor
U8G2_SH1106_128X64_NONAME_F_HW_I2C display(U8G2_R0, U8X8_PIN_NONE);

// setup BMP280 sensor constructor
Adafruit_BMP280 bmp280;

// temperature icon data
static const unsigned char tIcon[] U8X8_PROGMEM = {
  0x00, 0x03, 0xc0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x0e, 0x70, 0x00, 0x00, 0x0c, 0x30, 0x00, 
  0x00, 0x0c, 0x30, 0x00, 0x00, 0x0c, 0x30, 0x00, 0x00, 0x0c, 0x30, 0x00, 0x00, 0x0c, 0x30, 0x00, 
  0x00, 0x0c, 0x30, 0x00, 0x00, 0x0c, 0x30, 0x00, 0x00, 0x0c, 0x30, 0x00, 0x00, 0x0c, 0x30, 0x00, 
  0x00, 0x0c, 0x30, 0x00, 0x00, 0x0c, 0x30, 0x00, 0x00, 0x0c, 0x30, 0x00, 0x00, 0x0c, 0x30, 0x00, 
  0x00, 0x0d, 0xb0, 0x00, 0x00, 0x0d, 0xb0, 0x00, 0x00, 0x0d, 0xb0, 0x00, 0x00, 0x0d, 0xb0, 0x00, 
  0x00, 0x0d, 0xb0, 0x00, 0x00, 0x0d, 0xb0, 0x00, 0x00, 0x1d, 0xb8, 0x00, 0x00, 0x39, 0x9c, 0x00, 
  0x00, 0x33, 0xcc, 0x00, 0x00, 0x37, 0xec, 0x00, 0x00, 0x37, 0xec, 0x00, 0x00, 0x33, 0xcc, 0x00, 
  0x00, 0x39, 0x9c, 0x00, 0x00, 0x1c, 0x38, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x03, 0xc0, 0x00
};

// pressure icon data
static const unsigned char pIcon[] U8X8_PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf0, 0x00, 
  0x00, 0x3f, 0xfc, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x01, 0xe0, 0x07, 0x80, 0x03, 0xc1, 0x83, 0xc0, 
  0x07, 0x19, 0x98, 0xe0, 0x07, 0x18, 0x18, 0xe0, 0x0e, 0x00, 0x00, 0x70, 0x0c, 0xc0, 0x03, 0x30, 
  0x1c, 0xc4, 0x03, 0x38, 0x1c, 0x04, 0x00, 0x38, 0x18, 0x06, 0x00, 0x18, 0x19, 0x83, 0x01, 0x98, 
  0x19, 0x83, 0x81, 0x98, 0x18, 0x03, 0xc0, 0x18, 0x1c, 0xf3, 0xcf, 0x38, 0x1c, 0xf3, 0xcf, 0x38, 
  0x0c, 0xf1, 0x8f, 0x70, 0x0e, 0x78, 0x1e, 0x70, 0x07, 0x3e, 0x7c, 0xe0, 0x07, 0x1f, 0xf9, 0xe0, 
  0x03, 0xc7, 0xe3, 0xc0, 0x01, 0xe0, 0x0f, 0x80, 0x00, 0xf8, 0x1e, 0x00, 0x00, 0x3f, 0xfc, 0x00, 
  0x00, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

// altitude icon data
static const unsigned char aIcon[] U8X8_PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 
  0x00, 0x3f, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x7f, 0x86, 0x00, 0x00, 0x7f, 0x87, 0x00, 
  0x00, 0xff, 0xcf, 0x00, 0x01, 0xff, 0xcf, 0x80, 0x01, 0xff, 0x9f, 0xc0, 0x03, 0xff, 0xbf, 0xc0, 
  0x07, 0xff, 0x3f, 0xc0, 0x07, 0xff, 0x7f, 0xe0, 0x0f, 0xfe, 0x7f, 0xf0, 0x0f, 0xfe, 0xff, 0xf8, 
  0x1f, 0xfd, 0xff, 0xf8, 0x1f, 0xfd, 0xff, 0xf8, 0x3f, 0xfb, 0xff, 0xfc, 0x7f, 0xf3, 0xff, 0xfe, 
  0x7f, 0xf7, 0xff, 0xfe, 0x7f, 0xe7, 0xff, 0xff, 0x7f, 0xe7, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

// variable declaration
float bmp280temp = 0.00;
float bmp280pressure = 0.0;
float bmp280altitude = 0.0;
int dataToDisplay = 0;
int xPosition = 0;
int yPosition = 0;

// read data from sensor
void getSensorData(){
  bmp280temp = bmp280.readTemperature();
  Serial.print("Temperature = ");
  Serial.print(bmp280temp);
  Serial.println(" °C");
  bmp280pressure = bmp280.readPressure()/100;
  Serial.print("Pressure = ");
  Serial.print(bmp280pressure);
  Serial.println(" hPa");
  bmp280altitude = bmp280.readAltitude(1013.25);
  Serial.print("Approx altitude = ");
  Serial.print(bmp280altitude);
  Serial.println(" m");
  Serial.println();
}

// display data
void displayData(){
  xPosition = random(0, 7);
  yPosition = random(0, 31);
  switch (dataToDisplay) {
    case 0:
      display.clearBuffer();
      display.drawBitmap(xPosition, yPosition, 4, 32, tIcon);
      display.setFont(u8g2_font_profont22_tf);
      display.setCursor(xPosition + 36, yPosition + 22);
      display.print(bmp280temp);
      display.print(char(176));
      display.print("C");
      display.sendBuffer();
      dataToDisplay = 1;
      break;
    case 1:
      display.clearBuffer();
      display.drawBitmap(xPosition, yPosition, 4, 32, pIcon);
      display.setFont(u8g2_font_profont22_tf);
      display.setCursor(xPosition + 36, yPosition + 22);
      display.print(int(bmp280pressure));
      display.print("hPa");
      display.sendBuffer();
      dataToDisplay = 2;
      break;
    case 2:
      display.clearBuffer();
      display.drawBitmap(xPosition, yPosition, 4, 32, aIcon);
      display.setFont(u8g2_font_profont22_tf);
      display.setCursor(xPosition + 36, yPosition + 22);
      display.print(int(bmp280altitude));
      display.print("m");
      display.sendBuffer();
      dataToDisplay = 0;
      break;
  }
}

void setup() {
  // put your setup code here, to run once:
  // initialize serial communication
  Serial.begin(115200);
  Serial.println();
  Serial.println("serial communication, initialized");

  // initialize OLED SH1106 128x64 display
  display.begin();
  display.setContrast(63);
  Serial.println("display, initialized");

  // initialize sensor with i2c address 0x76
  bmp280.begin(0x76);
  bmp280.setSampling( Adafruit_BMP280::MODE_NORMAL,
                      Adafruit_BMP280::SAMPLING_X2,
                      Adafruit_BMP280::SAMPLING_X16,
                      Adafruit_BMP280::FILTER_X16,
                      Adafruit_BMP280::STANDBY_MS_500);
  Serial.println("sernsor, initialized");
  Serial.println();
}

void loop() {
  // put your main code here, to run repeatedly:
  getSensorData();
  displayData();
  delay(5000);
}

Odgovori

Ko je OnLine

Korisnika u ovom forumu: Nema registrovanih korisnika i 1 gost