SOS blinks on BME280+Electron+ThingSpeak

Dear all,

I follow this post for my 1st project – uploading sensor readings from Adafruit BME280 to ThingSpeak via Electron.

The code can be compiled successfully and now the readings have been uploaded to ThingSpeak.

However, the readings are all zeros.

What do I miss here?

Thanks for your help.

Here is the wiring:
CS pin --> Electron D5
SDI pin --> D2
SDO pin --> D3
SCK pin --> D4
GND pin --> GND
VIN pin --> 3V3

Here is my code:

    #include "ThingSpeak/ThingSpeak.h"
    #include "Adafruit_BME280/Adafruit_Sensor.h"
    #include "Adafruit_BME280/Adafruit_BME280.h"
    #define BME_SCK D4
    #define BME_MISO D3
    #define BME_MOSI D2
    #define BME_CS D5
    #define SEALEVELPRESSURE_HPA (1013.25)

    Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);


    /* Thingspeak */
    TCPClient client;
    unsigned long myChannelNumber = 122345;
    const char * myWriteAPIKey = "KU5J751FNAEFYAXL";

    void setup() {
    // Connect to ThingSpeak
        ThingSpeak.begin(client);
    // Wait for the sensor to stabilize
        delay(1000);
    
    // Update the 3 ThingSpeak fields with the new data
    ThingSpeak.setField(1, bme.readTemperature());
    ThingSpeak.setField(2, bme.readHumidity());
    ThingSpeak.setField(3, bme.readPressure() / 100.0F);

    // Write the fields that you've set all at once.
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

    // Give time for the message to reach ThingSpeak
    delay(5000);

    // Sleep for 15 minutes to save battery
    System.sleep(SLEEP_MODE_DEEP, 15 * 60);

    }

    void loop() {
        // This will run because the system is sleeping
    }

You seem to be missing this before first use of your sensor

  if (!bme.begin()) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1) Particle.process();
  }

And why are you using software SPI and not hardware SPI via A2~A5 and this constructor?

 Adafruit_BME280 bme(A2); // A2 is the default SS SlaveSelect or CS ChipSelect pin of HW SPI but you could use any 

Hello ScruffR:

Thanks for your help. It works based on the hardware SPI.

Any comments about my new modified code??

//SPI wiring
#define BME_CS A2
#define BME_SCK A3
#define BME_MISO A4
#define BME_MOSI A5

Adafruit_BME280 bme(BME_CS);

What is the right way to wiring BME280 based on I2C?
What is the difference between software and hardware SPI?

I see that I have use up 0.7 MB of quota just over night. Anything wrong in my code? I am currently uploading data to ThingSpeak every 15 mins all based on https://particle.hackster.io/monkbroc/from-0-to-iot-in-15-minutes-3e2607?ref=search&ref_id=electron&offset=4

Thanks a lot.

For I2C, you'd just write

Adafruit_BME280 bme;

And connect SDA to D0 and SCL to D1.

BTW, you wouldn't need your defines as the SPI pins also have their respective names SS, SCK, MOSI & MISO.

Hardware SPI is done by the controler by use of a dedicated interface that does its job without you actually controling the pins, but soft SPI does use bit banging to switch each pin (even the clock pin) on and of for each bit to send and reads each bit via seperate read statements.

If you use this instead you will use less data and even save on battery

void loop()
{
    /// move these lines out of setup() into loop() ///
    ThingSpeak.setField(1, bme.readTemperature());
    ThingSpeak.setField(2, bme.readHumidity());
    ThingSpeak.setField(3, bme.readPressure() / 100.0F);

    // Write the fields that you've set all at once.
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

    // Give time for the message to reach ThingSpeak
    for(uint ms=millis(); millis() - ms < 5000; Particle.process());

   System.sleep(WKP, RISING, 15 * 60, SLEEP_NETWORK_STANDBY);
}

since you don't need to renegotiate a connection with your cell towers each time your device wakes up.

I use this to wake my device via the SETUP button

  System.sleep(BTN, FALLING, dt, SLEEP_NETWORK_STANDBY);   // use SETUP BUTTON (20) to wake

Hello ScruffR:

Thanks again. I need to upgrade the firmware in order to use the SLEEP_NETWORK_STANDBY(). Now it works.

I am going to let it run over night to compare the data usage. I will let you know how it goes.

The I2C wiring is still not working based on your instruction – connecting SDA to D0 and SCL to D1.

Here is my code:

// I2C wiring
#define BME_MOSI D0
#define BME_SCK D1
 
Adafruit_BME280 bme; // I2C

I confuse the name on the BME280 board with SDA and SCL. Any comments?

You could try adding external 10k or 4k7 pull-up resistors to the D0 and D1.

1 Like

I am about to get I2C working by adding pull-up resistors. For the wiring, is it similar to this?

Yes that looks correct.

I checked this also and my50 ct:
I2C works with this PINs with/without resistors:

// I2C wiring
#define BME_MOSI D0
#define BME_SCK D1

//#define BME_SCK D4
//#define BME_MISO D3
//#define BME_MOSI D2
//#define BME_CS D5

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);

void setup() {
  Serial.begin(9600);
  Serial.println(F("BME280 test"));

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

By including this lib: Adafruit_BME280 (1.1.3)

@Postler, there are no circumstances where I2C will work without pullup resistors. With an Adafruit board or any other board, those resistors are typically on those boards.

I confirm: get the “more correct” values without additional resistances.
It was primarily about the PIN assignment: here we deviate from the Libary.

1 Like

a quick question: I switched to test the BME280 with SPI hardware and serial monitor opend but without any information - the photon breathing green after flashing this definitions which work by the other user “changks”:

// I2C wiring
//#define BME_MOSI D0
//#define BME_SCK D1

//SPI wiring
#define BME_SCK A3   // = SCK
#define BME_MISO A4  // = SD0
#define BME_MOSI A5  // = SDI
#define BME_CS A2    // = CS

//Adafruit_BME280 bme; // I2C
Adafruit_BME280 bme(BME_CS); // hardware SPI

@Postler, are you using the Adafruit BME280 breakout board?

nope, STK0151002635, optical like this

I tested also the sparkfun lib, but I got total wrong measurements.
I2C with adafruit lib works fine, so I switched to SPI to save my I2C.

@Postler, “save your I2C”? For SPI to work you will need to cut jumper SJ2 on the board. This jumper connects a pull-up resistor on the CS line which causes the BME280 to power up in I2C mode.

I wanted to save the allocation of the I2C, to offer a 1602 display for the values.
On my board is no SJ2 drawn or do you mean a programmatic intervention on the Libary?
I’m learning that I’d better have to buy the original board from Sparkfun or Adafruit.

@Postler, I2C is designed to handle up to 127 devices! You can have both your BME280 and the 1602 connected via I2C :wink:

1 Like

I´m proud to inform that I reached my checkpoint for today: my first OLED works with BME280 on I2C. If there is a sponsor, I try the remaining 120. :kissing_smiling_eyes:
The measured values are more likely from the leisure area.
This Infineon sensor is currently being advertised by hackster.io in a contest.

1 Like