I am using this web server template
Random Nerds
and the Cactus.IO library to allow the use of 2 BME280's on the same I2C line
Cactus Link
the issue is that I have only been successful 1 in 20 times (more like 1:50) to get this to upload to a Wemos D1 and work.
When I tried to change a few words in the display to get things aligned, I get errors in the upload.
when I comment out loop() and just run the sensors, it works great, every time.
Linux Ubuntu IDE 1.8.5
Wemos D1 mini
2 BME280 sensors.
// http://randomnerdtutorials.com/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide/
// Cactus library : http://static.cactus.io/downloads/library/bme280/cactus_io_BME280_I2C.zip
#include <Wire.h>
#include "cactus_io_BME280_I2C.h"
#include <ESP8266WiFi.h>
// Create two BME280 instances
BME280_I2C bme1(0x77); // I2C using default 0x77
BME280_I2C bme2(0x76); // I2C using address 0x76
const char* ssid = "*****";
const char* password = "***********";
char temp1String[7];
char rh1String[7];
char pres1String[8];
char temp2String[7];
char rh2String[7];
char pres2String[8];
float temp1, rh1, pres1 , temp2, rh2, pres2;
WiFiServer server(80); // Web Server on port 80
void setup() {// ++++++++++++++++++++++++++++++++ SETUP +++++++++++++++++++++
Serial.begin(9600);
delay(10);
Serial.println("Multi Bosch BME280 Barometric Pressure - Humidity - Temp Sensor | cactus.io");
if (!bme1.begin()) {
Serial.println("Could not find a First BME280 sensor, check wiring!");
while (1);
}
if (!bme2.begin()) {
Serial.println("Could not find a Second BME280 sensor, check wiring!");
while (1);
}
// Connecting to WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Starting the web server
server.begin();
Serial.println("Web server running. Waiting for the ESP IP...");
delay(10000);
Serial.println(WiFi.localIP()); // Print ESP IP address
// ============================= READ BME ===================
delay(1000);
bme1.readSensor();
temp1 = (bme1.getTemperature_F());
rh1 = (bme1.getHumidity());
pres1 = (bme1.getPressure_MB());
pres1 = pres1 / 33.864 ;
delay(1000);
bme2.readSensor();
temp2 = (bme2.getTemperature_F());
rh2 = (bme2.getHumidity());
pres2 = (bme2.getPressure_MB());
pres2 = pres2 / 33.864 ; // to get inched of mercury
// print one line of data from sensors to verfiy they are on-line
delay(1000);
dtostrf(temp1, 2, 2, temp1String);
dtostrf( rh1, 2, 2, rh1String);
dtostrf(pres1, 2, 2, pres1String);
dtostrf(temp2, 2, 2, temp2String);
dtostrf( rh2, 2, 2, rh2String);
dtostrf(pres2, 2, 2, pres2String);
Serial.println("from set-up");
Serial.print("BME1 - ");
Serial.print(temp2String); Serial.print("*F2\t");
Serial.print(rh2String); Serial.print("%rh2\t");
Serial.print(pres2String); Serial.println(" IN HG2\t");
Serial.print("BME1 - ");
Serial.print(temp1String); Serial.print("*F\t");
Serial.print(rh1String); Serial.print("%rh\t");
Serial.print(pres1String); Serial.println(" IN HG\t");
Serial.println(WiFi.localIP()); // Print ESP IP address
Serial.println("--- END SETUP ---");
} // ==================== END SETUP =============
void getBME() { //++++++++++++++++++++++ BME +++++++++++++++++++
bme1.readSensor();
temp1 = (bme1.getTemperature_F());
rh1 = (bme1.getHumidity());
pres1 = (bme1.getPressure_MB());
pres1 = pres1 / 33.864 ;
delay(1000);
bme2.readSensor();
temp2 = (bme2.getTemperature_F());
rh2 = (bme2.getHumidity());
pres2 = (bme2.getPressure_MB());
pres2 = pres2 / 33.864 ;
delay(1000);
dtostrf(temp1, 2, 2, temp1String); // digits to string
dtostrf( rh1, 2, 2, rh1String);
dtostrf(pres1, 2, 2, pres1String);
Serial.print("BME1 - ");
Serial.print(temp1String); Serial.print("*F\t"); // print string to verify
Serial.print(rh1String); Serial.print("%rh\t");
Serial.print(pres1String); Serial.println(" IN HG\t");
dtostrf(temp2, 2, 2, temp2String);
dtostrf( rh2, 2, 2, rh2String);
dtostrf(pres2, 2, 2, pres2String);
Serial.print("BME1 - ");
Serial.print(temp2String); Serial.print("*F2\t");
Serial.print(rh2String); Serial.print("%rh2\t");
Serial.print(pres2String); Serial.println(" IN HG2\t");
}
void loop() { // ++++++++++++++++++++++ LOOP +++++++++++++++++
//getBME();
WiFiClient client = server.available(); // Listenning for new clients
if (client) {
Serial.println("New client");
// bolean to locate when the http request ends
boolean blank_line = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && blank_line) {
getBME();
//test to see if client is looking
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// actual web page that displays temperature
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head></head><body><h1>Daves Sensors</h1>");
client.println("<h3>Unit 1 Temp : ");
client.println(temp1String);
client.println("*F</h3><h3>Unit 1 Humidity : ");
client.println(rh1String);
client.println("%RH</h3><h3>Unit 1 Pres : ");
client.println(pres1String);
client.println("in HG</h3><h3>Unit 2 temp : ");
client.println(temp2String);
client.println("*F</h3><h3>Unit 2 Humidity : ");
client.println(rh2String);
client.println("%RH</h3><h3>Unit 2 Pres : ");
client.println(pres2String);
client.println("in HG</h3></body></html>");
break;
}
if (c == '\n') {
// when starts reading a new line
blank_line = true;
}
else if (c != '\r') {
// when finds a character on the current line
blank_line = false;
}
}
}
// closing the client connection
delay(1);
client.stop();
Serial.println("Client disconnected.");
}
} // ============= END OF LOOP ===========