-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_bme280_mq_135.ino
73 lines (62 loc) · 2.07 KB
/
final_bme280_mq_135.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define BMP_SDA 21
#define BMP_SCL 22
#define BMP_ADDR 0x76
#define MQ_PIN A0
Adafruit_BMP280 bmp;
int mq135_pin = MQ_PIN;
char ssid[] = "prodigy"; // your network SSID (name)
char pass[] = "pritish3004"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
unsigned long myChannelNumber = 2091501; // replace with your channel number
const char * myWriteAPIKey = "0B20YOJSL9PBCS2G"; // replace with your Write API Key
WiFiClient client;
void setup() {
Serial.begin(115200);
Wire.begin(BMP_SDA, BMP_SCL);
if (!bmp.begin(BMP_ADDR)) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
float temperature = bmp.readTemperature();
float pressure = bmp.readPressure() / 100000;
float altitude = bmp.readAltitude(1013.25);
int sensorValue = analogRead(mq135_pin);
float voltage = sensorValue * (3.3 / 4095.0);
float mq135_ppm = (voltage - 0.22) / 0.1;
String postData = String("field1=") + String(temperature, 2) + "&" +
String("field2=") + String(pressure, 2) + "&" +
String("field5=") + String(altitude, 2) + "&" +
String("field3=") + String(mq135_ppm, 2);
HTTPClient http;
String url = "https://api.thingspeak.com/update?api_key=" + String(myWriteAPIKey) + "&" + postData;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Data sent to ThingSpeak successfully");
} else {
Serial.println("Error sending data to ThingSpeak");
}
http.end();
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Pressure: ");
Serial.println(pressure);
Serial.print("Altitude: ");
Serial.println(altitude);
Serial.print("MQ135 PPM: ");
Serial.println(mq135_ppm);
delay(10000);
}