Guillermo Perez Guillen
Created February 18, 2017 © CC BY-NC

MONITORING A WEATHER STATION USING LoRa MODULE

With this project we are going to monitor modules of meteorological stations which we can place in ecological reserves or zoos.

BeginnerProtip1 hour795
MONITORING A WEATHER STATION USING LoRa MODULE

Things used in this project

Hardware components

LoRa Module
×1
Arduino UNO
Arduino UNO
×1
BMP085 Barometric Pressure Sensor
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Diagram

Electrical diagram of connections between the LoRa module and the Arduino board and the sensors BMP085 and DHT11

Code

Arduino

Arduino
Code to capture the data of the sensors DHT11 and BMP085 and to send them by the module SX1272 LoRa
// Include the SX1272 and SPI library: 
#include "SX1272.h"
#include <SPI.h>

#include "DHT.h"     // add the library with which our sensor works 
#include <Wire.h>

#define DHTPIN 13     // pin where we connect our sensor
#define DHTTYPE DHT11     // DHT 11 Sensor
#define BMP085_ADDRESS 0x77     // I2C address of BMP085
DHT dht(DHTPIN, DHTTYPE);     // pin we work and the type of sensor

const unsigned char OSS = 0;     // oversampling Setting
int ac1;     // calibration values 
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
long b5;     // b5 is calculated in BMP085 Get Temperature

int e;
char message1 [60];

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  
  // Power ON the module
  sx1272.ON();
  
  // Set transmission mode and print the result
  e = sx1272.setMode(4);
  Serial.println(F("Setting Mode: state "));
  Serial.println(e, DEC);
  
  // Select frequency channel
  e = sx1272.setChannel(CH_12_868);
  Serial.println(F("Setting Channel: state "));
  Serial.println(e, DEC);
  
  // Select output power (Max, High or Low)
  e = sx1272.setPower('H');
  Serial.println(F("Setting Power: state "));
  Serial.println(e, DEC);
  
  // Set the node address and print the result
  e = sx1272.setNodeAddress(2);
  Serial.println(F("Setting node address: state "));
  Serial.println(e, DEC);
  
  // Print a success message
  Serial.println(F("SX1272 successfully configured"));

  Serial.println("Sensors: DHT11 and BMP085:");
  dht.begin();
  Wire.begin();
  bmp085Calibration();
}

void loop()
{
  int h = dht.readHumidity();     // h in the float h - DHT11
  int t = dht.readTemperature();     // save reading variable DHT11
// calculation temperature - BMP085
  float temperature = bmp085GetTemperature(bmp085ReadUT());
 // barometric Pressure - BMP085
  float pressure = bmp085GetPressure(bmp085ReadUP());             
  float mmHg = pressure / 133.322368;
  float altitude = calcAltitude(pressure);     // uncompensated calculation in meters 
   
  if (isnan(t) || isnan(h))     // check if the variables are numbers indicated
  {
    Serial.println("Fallo al leer del sensor DHT"); 
  } else {
    lcd.print("HUMIDITY = ");     // relative Humidity Sensor DHT11 - LCD
    Serial.print(h);     // relative Humidity Sensor DHT11 - Serial
    Serial.print(" %\t");
    Serial.print("Temperatura: ");
    Serial.print(t);
    Serial.println(" *C\n");       
    Serial.print("Presion Atmosferica: ");
    Serial.print(mmHg, 2); 
    Serial.println(" mmHg\t");
    Serial.print("Temperatura: ");
    Serial.print(temperature, 2);     // temperature Sensor BMP085 - Serial
    Serial.println(" *C\t");
    Serial.print("Altitud: ");
    Serial.print(altitude, 2); 
    Serial.println(" metros");
    Serial.println();              
  }   
  delay(1000);
}
// load calibration values of pressure and temperature
void bmp085Calibration()              
{
  ac1 = bmp085ReadInt(0xAA);
  ac2 = bmp085ReadInt(0xAC);
  ac3 = bmp085ReadInt(0xAE);
  ac4 = bmp085ReadInt(0xB0);
  ac5 = bmp085ReadInt(0xB2);
  ac6 = bmp085ReadInt(0xB4);
  b1 = bmp085ReadInt(0xB6);
  b2 = bmp085ReadInt(0xB8);
  mb = bmp085ReadInt(0xBA);
  mc = bmp085ReadInt(0xBC);
  md = bmp085ReadInt(0xBE);
}
     // calculate the temperature in degrees Celsius
float bmp085GetTemperature(unsigned int ut){   
  long x1, x2;
  x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  x2 = ((long)mc << 11)/(x1 + md);
  b5 = x1 + x2;
  float temp = ((b5 + 8)>>4);
  temp = temp /10;
  return temp;
}

long bmp085GetPressure(unsigned long up){     // calculation of pressure loaded
  long x1, x2, x3, b3, b6, p;
  unsigned long b4, b7;
  b6 = b5 - 4000;
  x1 = (b2 * (b6 * b6)>>12)>>11;     // calculation of B3
  x2 = (ac2 * b6)>>11;

  x3 = x1 + x2;
  b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  x1 = (ac3 * b6)>>13;     // calculation of B4
  x2 = (b1 * ((b6 * b6)>>12))>>16;  
  x3 = ((x1 + x2) + 2)>>2;
  b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  if (b7 < 0x80000000)
    p = (b7<<1)/b4;
  else
    p = (b7/b4)<<1;
  x1 = (p>>8) * (p>>8);
  x1 = (x1 * 3038)>>16;
  x2 = (-7357 * p)>>16;
  p += (x1 + x2 + 3791)>>4;
  long temp = p;
  return temp;
}

char bmp085Read(unsigned char address)     // read 1 byte of BMP085 'address'
{
  unsigned char data;
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();
  Wire.requestFrom(BMP085_ADDRESS, 1);
  while(!Wire.available());
  return Wire.read();
}

int bmp085ReadInt(unsigned char address)      // read 2 bytes of BMP085
{
  unsigned char msb, lsb;
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();
  Wire.requestFrom(BMP085_ADDRESS, 2);
  while(Wire.available()<2);
  msb = Wire.read();
  lsb = Wire.read();
  return (int) msb<<8 | lsb;
}

unsigned int bmp085ReadUT(){     // read uncompensated temperature value
  unsigned int ut;

Wire.beginTransmission(BMP085_ADDRESS);     // write 0x2E 
  Wire.write(0xF4);     // in the register 0xF4
  Wire.write(0x2E);
  Wire.endTransmission();
  delay(5);
  ut = bmp085ReadInt(0xF6);     // read 2 bytes of 0xF6 and 0xF7 records
  return ut;
}

unsigned long bmp085ReadUP(){     // read  uncompensated value - pressure
  unsigned char msb, lsb, xlsb;
  unsigned long up = 0; 
  Wire.beginTransmission(BMP085_ADDRESS);  
// write 0x34 + (OSS << 6) in the register 0xF4
  Wire.write(0xF4);
  Wire.write(0x34 + (OSS<<6));
  Wire.endTransmission();
  delay(2 + (3<<OSS));     // awaiting conversion
  msb = bmp085Read(0xF6);     // read 0xF6 (MSB) 0xF7 (LSB)
  lsb = bmp085Read(0xF7);     //  and 0xF8 records (XLSB)
  xlsb = bmp085Read(0xF8);
  up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
  return up;
}

void writeRegister(int deviceAddress, byte address, byte val) {
  Wire.beginTransmission(deviceAddress);     // transmission begins  
  Wire.write(address);     // sending registration address
  Wire.write(val);     // sending write value
  Wire.endTransmission();     // end transmission
}
int readRegister(int deviceAddress, byte address){
  int v;
  Wire.beginTransmission(deviceAddress);
  Wire.write(address);     // reading log
  Wire.endTransmission();
  Wire.requestFrom(deviceAddress, 1);     // reads a byte
  while(!Wire.available()) {                                                                              
  }
  v = Wire.read();
  return v;
}
float calcAltitude(float pressure){
  float A = pressure/101325;
  float B = 1/5.25588;
  float C = pow(A,B);
  C = 1 - C;
  C = C /0.0000225577;
  return C;
}

Credits

Guillermo Perez Guillen

Guillermo Perez Guillen

54 projects • 63 followers
Electronics and Communications Engineer (ECE): 12 prizes in Hackster / Hackaday Prize Finalist 2021-22-23 / 3 prizes in element14

Comments