Notifications
Clear all

BME/BMP 280  

Page 2 / 3
  RSS

frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 490
2019-11-25 11:49 am  

@pugwash

Ok, I think I found the problem, which may lay with your BME280 sensor or the delays causing a timeout, because if I comment out the code as below, and provide hard coded float literals instead, the receiver produces the correct results, otherwise I get zeros like you do.  Could your module or the delays be causing a timeout?

#include <Wire.h>
#include <Adafruit_Sensor.h>
//#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10); // CE, CSN         
const uint64_t address = 01;     //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.

//Adafruit_BME280 bme; // Create the bme object

typedef struct Message{
  float temperature;
  float pressure;
  float altitude;
  float humidity;  
  }bme280data;

bme280data myObject; // declare the message object

unsigned long previousTime;
unsigned long currentTime;
unsigned long delayTime = 5000; // 5 second interval

void setup() {
  
  Serial.begin(9600);
  Serial.println("Starting....");
  
   //Initialise the nrf24l01
  
  radio.begin();                  //Starting the Wireless communication
  radio.openWritingPipe(address); //Setting the address where we will send the data
  radio.setPALevel(RF24_PA_MIN);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
  radio.stopListening();          //This sets the module as transmitter

  // Initialise bme280

  /*if (!bme.begin(0x76)) {
  Serial.println("Could not find a valid BME280 sensor, check wiring!");
  while (1);
  }*/
  previousTime = millis();
}

void loop() {

  currentTime = millis();
  
  if(currentTime >= previousTime + delayTime){
    myObject.temperature = 1.750;
    myObject.pressure = 2.750;
    myObject.altitude = 3.750;
    myObject.humidity = 4.750;
  
    Serial.println(myObject.temperature); // for testing purposes only
    Serial.println(myObject.pressure);
    Serial.println(myObject.altitude);
    Serial.println(myObject.humidity);
    Serial.println(sizeof(bme280data));
    //const char text[] = "Your Button State is HIGH";
    radio.write(&myObject, sizeof(myObject));
  
    previousTime = currentTime;
  
  }
}

 

 


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2019-11-25 11:50 am  

Eureka!! It works!

It was the wiring, your last post made me recheck how I had wired this initially.

Just need to change (9, 10) to (10, 9)!

Thanks for your patience, and I also learned something new "data type structures". 


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 490
2019-11-25 12:04 pm  

@pugwash

Cool, glad you got it working!
Welcome to the world of OOP in Arduino! 🙂

 


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2019-11-25 12:10 pm  

@frogandtoad

Sometimes the journey is more exciting than reaching the destination!

Phase 2 will be to include this in a network of multiple sensors using the RF24network libraries.

That should be the easy part, as the basic groundwork is complete.


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2019-11-25 12:14 pm  
Posted by: @frogandtoad

@pugwash

Cool, glad you got it working!
Welcome to the world of OOP in Arduino! 🙂

 

I didn't realise that OOP was an option in the world of Arduino.

It is something I have used before in both Python and Java programming, but I was under the illusion that C++ in the Arduino world was limited to the instruction set detailed on the original Arduino Language Reference website.

What a mistake?? ? ? ? 


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 490
2019-11-25 12:29 pm  

@pugwash

Indeed, the Arduino reference doesn't tell the full story.. some implementations support quite a lot more, and most libraries are written in OOP if you take a look at their source code.

Why not write your own? 

namespace pugwash {
  class Sensor {
    private:
      float Temperature;
      float Pressure;
      float Altitude;
      float Humidity; 
    public:
      Sensor() : Temperature(), Pressure(), Altitude(), Humidity() {}
      Sensor(float temperature, float pressure, float altitude, float humidity)
              : Temperature(temperature), Pressure(pressure), Altitude(altitude), Humidity(humidity) {}
      float getTemperature() {
       return Temperature;
      }
      float getPressure() {
       return Pressure;
      }
      float getAltitude() {
       return Altitude;
      } 
      float getHumidity() {
       return Humidity;
      }   
   };
 }
 
pugwash::Sensor CoolSensor(1.24f, 2.567f, 3.12f, 2.227f);

void setup() {
  Serial.begin(9600);

  Serial.println(CoolSensor.getTemperature());
  Serial.println(CoolSensor.getPressure());
  Serial.println(CoolSensor.getAltitude());
  Serial.println(CoolSensor.getHumidity());

  Serial.println("Size of Sensor: " + String (sizeof(CoolSensor)));
 }

void loop() {

 }

?

ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2019-11-25 12:59 pm  

@frogandtoad

I did start a thread #post-4070 several months ago, on the subject of creating libraries, but it received very little resonance. The reason this interested me was that I quite like to read and write to registers without relying on third-party libraries, I feel much happier addressing them myself. But when the next piece of junk arrived from China I just got distracted.

Here are my first and last feeble attempts at it. Of no particular use, but I was just finding out what libraries were all about and how to employ them myself. Even at my age ?, I am always keen to learn something new!


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 490
2019-11-26 10:16 am  

@pugwash

Cool... I'll check out that thread soon enough.

I have missed many threads, because I hadn't been around for a while, since I helped Bill with some initial forum testing, but as of recently, hoping to see and participate a lot more.


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 490
2019-11-26 10:29 am  

@pugwash

OK, I had a quick look, but you haven't included all of the files in your last post there... it's missing the implementation file for the class, or is that the part you're having trouble with?  Have any of the recent examples I posted helped? If not, let me know what problems you're having and I will try to help.


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2019-11-26 10:42 am  
Posted by: @frogandtoad

@pugwash

OK, I had a quick look, but you haven't included all of the files in your last post there... it's missing the implementation file for the class, or is that the part you're having trouble with?  Have any of the recent examples I posted helped? If not, let me know what problems you're having and I will try to help.

I wasn't having any troubles, they seemed to work "as is"!

I thought though, that I only needed a .cpp and .h files for the libraries to work. What exactly am I missing?


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 490
2019-11-26 10:51 am  

@pugwash

Ok, so that code you embedded directly in your post, is that what you weren't able to attach?  Is that what you meant?  No, .cpp and .h files are all you need.

 


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2019-11-26 11:07 am  
Posted by: @frogandtoad

@pugwash

Ok, so that code you embedded directly in your post, is that what you weren't able to attach?  Is that what you meant?  No, .cpp and .h files are all you need.

 

I think we are talking at cross purposes here!

The archive.zip file was just a demonstration of my first feeble attempt at building my own library. Nothing more, nothing less. Only posted for possible positive or negative criticism. ? 


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 490
2019-11-26 11:49 am  

@pugwash

No, I was talking about your other post... haven't looked at the .zip file yet 🙂

Anyway, here's how you could have written the code in your old post:

CostomLed.ino

#include "Led.h"

#define ledPin 13

pugwash::Led Led1(ledPin, OUTPUT);

void setup() {
  Serial.begin(9600);
}

void loop() {
  Led1.ledOn();
  delay(1000);
  
  Led1.ledOff();
  delay(1000);
 }

Led.h

#ifndef PUGWASH_LED_H
#define PUGWASH_LED_H

#include "Arduino.h"

namespace pugwash {
  class Led  {
    private:
      byte LedPin;
    public:
      Led(byte pin, byte mode);
    
      void ledOn();
      void ledOff();
   };
 }

#endif

Led.cpp

#include "Led.h"

namespace pugwash {
  Led::Led(byte pin, byte mode) : LedPin(pin) {
    pinMode(LedPin, mode);
   }

  void Led::ledOn() {
    digitalWrite(LedPin, HIGH);
   }

  void Led::ledOff() {
    digitalWrite(LedPin, LOW);
   }
 }

The reason for the namespace is so your personal library names do not clash or interfere with any other library names, because a namespace introduces a new scope, so all libraries should have their own distinct namespace.


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2019-11-27 9:24 am  

@frogandtoad

I hope you enjoyed your first day in the new job. Me, the last time I set an alarm clock was last January or February, 48 years of work was enough!!

Now I need to pick your brains again:

typedef struct Message{
  float temperature;
  float pressure;
  float altitude;
  float humidity;
  byte checksum;  
  }bme280data;

bme280data myObject; // declare the message object

So I have got this object "myObject". It is 17 bytes long and I need the first 16 bytes in an array to calculate the checksum. I tried for several hours but to no avail. Perhaps you can help. It is obviously possible, as somewhere in the RF24 library there is a routine that extracts the bytes to send them sequentially to the receiver.

 


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 1 year ago
Posts: 997
2019-11-27 9:48 am  

As for the BME280 module. The vendors claim an altitude accuracy of +/- 1 metre. I find this a bit far fetched (understatement). I have had this running for about 48 hours, although the air pressure reading is pretty accurate (compared to local weather station), I now find myself at an altitude of 179m above mean sea level and I know I am actually only about 25m above.

Back in my youth, when I flew in Chipmunks with the ATC, the pilot always zeroed out his altimeter before take-off. Therefore I think I need to chain two of these modules together.

One module with SEALEVELPRESSURE_HPA set to 1013 hPa, and the other module with SEALEVELPRESSURE_HPA set to the actual air pressure that I am getting from module 1, compensated for 25m of altitude.

Any thoughts welcome!!


ReplyQuote
Page 2 / 3