ESP8266-01 and BME280 using GPIO 0 & 2 for I2c

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
youngst
 
Posts: 3
Joined: Sat Mar 26, 2022 1:18 am

ESP8266-01 and BME280 using GPIO 0 & 2 for I2c

Post by youngst »

I want to use the little ESP8266-01 to send BME280 data. I have got it to work using a feather huzzah ESD8266, but I really want to use these little devices and for the life of me I can't find where or what to change to use GPIO0 and GPIO2 for SDA and SCL pinouts.

I have loaded an I2C detect and all is fine @ 0x76, when I utilize the wire.h library and wire.begin(0, 2); but it will not work in my sketch. I think it has to do with the autodetect adafruit is using but I don't know for sure, NOOB.

Can someone help?

User avatar
rpiloverbd
 
Posts: 198
Joined: Mon Nov 29, 2021 8:13 am

Re: ESP8266-01 and BME280 using GPIO 0 & 2 for I2c

Post by rpiloverbd »

Which library are you using? If you upload the default example, does your circuit work?

User avatar
youngst
 
Posts: 3
Joined: Sat Mar 26, 2022 1:18 am

Re: ESP8266-01 and BME280 using GPIO 0 & 2 for I2c

Post by youngst »

This is what I'm trying to do. I think the only thing I have added is the wire.h and wire.begin(0, 2); from the original from Rui Santos sketch. It works fine with the huzzah esp8266, "without the Wire.h and wire.begin(0, 2)" but I need to know how to get Adafruit_bme to look for scl and sda at GPIO0 and GPIO2 instead of the "default" GPIO4 and GPIO5.

Code: Select all

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp8266-esp-now-wi-fi-web-server/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/
#include <Wire.h>
#include <espnow.h>
#include <ESP8266WiFi.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>

// Set your Board ID (ESP32 Sender #1 = BOARD_ID 1, ESP32 Sender #2 = BOARD_ID 2, etc)
#define BOARD_ID 3

Adafruit_BME280 bme; 

//MAC Address of the receiver 
uint8_t broadcastAddress[] = {0x4C, 0x75, 0x25, 0x0B, 0x20, 0x96};

//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
    int id;
    float temp;
    float hum;
    float bar;
    int readingId;
} struct_message;

//Create a struct_message called myData
struct_message myData;

unsigned long previousMillis = 0;   // Stores last time temperature was published
const long interval = 10000;        // Interval at which to publish sensor readings

unsigned int readingId = 0;

// Insert your SSID
constexpr char WIFI_SSID[] = "youngst123";

int32_t getWiFiChannel(const char *ssid) {
  if (int32_t n = WiFi.scanNetworks()) {
    for (uint8_t i=0; i<n; i++) {
      if (!strcmp(ssid, WiFi.SSID(i).c_str())) {
        return WiFi.channel(i);
      }
    }
  }
  return 0;
}

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

float readTemperature() {
  float t = bme.readTemperature();
  return t;
}

float readHumidity() {
  float h = bme.readHumidity();
  return h;
}

float readPressure() {
  float g = bme.readPressure();
  return g;
}

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0){
    Serial.println("Delivery success");
  }
  else{
    Serial.println("Delivery fail");
  }
}
 
void setup() {
  
  //Init Serial Monitor
  Serial.begin(115200);
  
  Wire.begin(0, 2);
  initBME(); 

  // Set device as a Wi-Fi Station and set channel
  WiFi.mode(WIFI_STA);

  int32_t channel = getWiFiChannel(WIFI_SSID);

  WiFi.printDiag(Serial); // Uncomment to verify channel number before
  wifi_promiscuous_enable(1);
  wifi_set_channel(channel);
  wifi_promiscuous_enable(0);
  WiFi.printDiag(Serial); // Uncomment to verify channel change after

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);

  esp_now_register_send_cb(OnDataSent);
  
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
 
void loop() {
    unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // Save the last time a new reading was published
    previousMillis = currentMillis;
    //Set values to send
    myData.id = BOARD_ID;
    myData.temp = readTemperature();
    myData.hum = readHumidity();
    myData.bar = readPressure();
    //myData.readingId = readingId++;
     
    esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));

    Serial.print("loop");
  }
}

User avatar
youngst
 
Posts: 3
Joined: Sat Mar 26, 2022 1:18 am

Re: ESP8266-01 and BME280 using GPIO 0 & 2 for I2c

Post by youngst »

Ok, Ok I must have read all "a lot" of the forums. Must have seen a 100 different people say they ended up with BMP280 instead of the BME280 that they thought they had and thought that aint me and kept on a looking. Thats exactly what the problem was, ME. If anyone is having the same sort of problem, please look at this page http://www.raypcb.com/bmp280-vs-bme280/ it will help determine which 280 sensor you have or check out the data sheet.

Locked
Please be positive and constructive with your questions and comments.

Return to “Wireless: WiFi and Bluetooth”