So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By bendeer
#62733 Hi all,

I'm fighting with my nodemcu board for a few days. I can't force it to read using i2c bus from BME280 sensor. DHT22 is working fine. Could someone take a look at my code and advise me what's wrong with it?

Thank you

Code: Select all#define debug 1 // debug on/off

#include <DHT.h>
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C

// replace with your channel's thingspeak API key,
  String apiKey = "api key";

// WiFi Details
  const char* ssid = "ssid";
  const char* password = "pass";
  const char* server = "api.thingspeak.com";
 
// GPIO05 / D1 - zdefiniuj PIN DHT
  #define DHTPIN 13
 
// zdefiniuj typ czujnika 
  #define DHTTYPE DHT22
  DHT dht(DHTPIN, DHTTYPE);

// PIN15 / D8 - zdefuniuj pin dla Dallas
  #define ONE_WIRE_BUS 15
  OneWire oneWire(ONE_WIRE_BUS);
  DallasTemperature sensors(&oneWire);

double node3DewPoint;
 
WiFiClient client;

void setup() {               
  Serial.begin(115200);
  delay(10);
  dht.begin();
  //I2C Test
  Serial.println("Starting I2C !");
  Wire.begin(0, 2);
  for (byte address = 1; address < 127; address++) {
    Wire.beginTransmission(address);
    byte error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
    }
    else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.println(address,HEX);
    }   
  }
  Serial.println("ESP to Thingspeak"); //Print a message
  sensors.begin();
 
  WiFi.begin(ssid, password);
 
  Serial.println();
  Serial.println();
  Serial.print("Logowanie do: ");
  Serial.println(ssid);
   
  WiFi.begin(ssid, password);
   
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Zalogowano do WiFi");
 
}
 
void loop()
{

   float h = dht.readHumidity();
   float t = dht.readTemperature();
   float g = sensors.getTempCByIndex(0);
   float temp = bme.readTemperature();
   float pres = bme.readPressure() / 100.0F;
   float altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
   float hum = bme.readHumidity();
   float node3DewPoint = dewPointAccurate(temp, hum);

  // Serial Debug wlacz/wylacz na poczatku skryptu
  if (debug == 1)
  {
   
     Serial.print("Temperatura powietrza [st. C]: ");
     Serial.print(t);
     Serial.print("\n");
     Serial.print("Wilgotnosc [%]: ");
     Serial.print(h);
     Serial.print("\n");
     Serial.print("Temperatura gruntu [st. C]: ");
     Serial.print(g);
     Serial.print("\n");
     Serial.print("BME280 Temperatura [st. C]: ");
     Serial.print(temp);
     Serial.print("\n");
     Serial.print("BME280 Wilgotnosc [%]: ");
     Serial.print(hum);
     Serial.print("\n");
     Serial.print("BME280 Cisnienie [hPa]: ");
     Serial.print(pres);
     Serial.print("\n");
     Serial.print("BME280 Wysokosc [m]: ");
     Serial.print(altitude);
     Serial.print("\n");
     Serial.print("Dew point: ");
     Serial.print(node3DewPoint);
     Serial.print("\n");
     Serial.println(sensors.getTempCByIndex(0));
     Serial.print("\n");
     Serial.print("\n");
     Serial.print("\n");
     Serial.print("\n");
     Serial.print("\n");
     Serial.print("\n");
     Serial.print("\n");
     Serial.print("\n");
     Serial.println("**** wysylam do TS");
     Serial.println("-------------------------------------------");
  }
 
  if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
    String postStr = apiKey;
        // DHT
           postStr +="&field1=";
           postStr += String(t);
           postStr +="&field2=";
           postStr += String(h);
       // Dallas
           postStr +="&field3=";
           postStr += String(g);
       // BME280
           postStr += "&field4=";
           postStr += String(temp);
           postStr += "&field5=";
           postStr += String(hum);
           postStr += "&field6=";
           postStr += String(pres);
           postStr += "&field7=";
           postStr += String(altitude);
       // DewPoint
           postStr += "&field8=";
           postStr += String(node3DewPoint);
           postStr += "\r\n\r\n";
 
     client.print("POST /update HTTP/1.1\n");
     client.print("Host: api.thingspeak.com\n");
     client.print("Connection: close\n");
     client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
     client.print("Content-Type: application/x-www-form-urlencoded\n");
     client.print("Content-Length: ");
     client.print(postStr.length());
     client.print("\n\n");
     client.print(postStr);         
   
  }
  client.stop();
   
  Serial.println("Odczyt... kolejny za 30 sekund");   
  // Zdefiniuj opoznienie odczytu
  delay(30000); 
}

/*-----( Declare User-written Functions )-----*/
// dewPoint function NOAA
// reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
// reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
//

double dewPointAccurate (double celsius, double humidity)
{
  // (1) Saturation Vapor Pressure = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  RHS += log10(1013.124); // NB = 1013 = absolute air pressure from BME280 sensor!!!!???????????????

  // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  double VP = pow(10, RHS - 3) * humidity;

  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP / 0.61078); // temp var
  return (241.88 * T) / (17.558 - T);
}
/* ==== END Functions ==== */