Hello
Im pretty new at this but i was trying to build and code a weaterstation with Blynk. I have an Weimos D1 R1 with a DHT 22 Sensor with a MQ-135 Gas Sensor. Now i want to add a BMP 280 pressure sensor. I got a working code for my DHT222 and MQ and on another board for my BMP. How can i combine the two? Both codes are down here:
#define BLYNK_TEMPLATE_ID "e"
#define BLYNK_DEVICE_NAME "e"
#define BLYNK_AUTH_TOKEN "e"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "(...)"; // type your wifi name
char pass[] = "(...)"; // type your wifi password
int smokeA0 = A0;
int data = 0;
int sensorThres = 100;
#define DHTPIN D3 // Mention the digital pin where you connected
#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
void sendSensor(){
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println(t);
Blynk.virtualWrite(V1, h);
Blynk.virtualWrite(V0, t);
Serial.print("Temperature : ");
Serial.print(t);
Serial.print(" Humidity : ");
Serial.println(h);
if(t > 30){
// Blynk.email("", "Alert", "Temperature over 28C!");
Blynk.logEvent("temp_alert","Temp above 30 degree");
}
{
int data = analogRead(smokeA0);
Blynk.virtualWrite(V2, data);
Serial.print("Pin A0: ");
Serial.println(data);
}
}
void setup(){
pinMode(smokeA0, INPUT);
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
timer.setInterval(2500L, sendSensor);
}
void loop(){
Blynk.run();
timer.run();
}
And the BMP 280
#define BLYNK_TEMPLATE_ID "e"
#define BLYNK_DEVICE_NAME "BMP280"
#define BLYNK_AUTH_TOKEN ""
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = ""; // type your wifi name
char pass[] = ""; // type your wifi password
#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)
Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
BlynkTimer timer;
void sendSensor()
{
int temp =bmp.readTemperature();
float pressure= bmp.readPressure();
float altitude=bmp.readAltitude(1017);
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1017)); /* Adjusted to local forecast! */
Serial.println(" m"); //If you don't know it, modify it until you get your current altitude
Serial.println();
Blynk.virtualWrite(V3, temp);
Blynk.virtualWrite(V4, pressure);
Blynk.virtualWrite(V5, altitude); //The "1019.66" is the pressure(hPa) at sea level in day in your region
delay(2000);
}
void setup() {
Blynk.begin(auth, ssid, pass);
timer.setInterval(100L, sendSensor);
Serial.begin(115200);
while ( !Serial ) delay(100); // wait for native usb
Serial.println(F("BMP280 test"));
unsigned status;
//status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
status = bmp.begin(0x76);
if (!status) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
void loop()
{
Blynk.run();
timer.run();
}