0% found this document useful (0 votes)
14 views4 pages

New Code

The document is an Arduino sketch that sets up a system to monitor environmental parameters using various sensors, including gas, temperature, humidity, and GPS. It connects to WiFi and sends the collected data to a Google Sheets script via HTTP POST requests. The data is displayed on an LCD screen and includes safety alerts through a buzzer when certain thresholds are exceeded.

Uploaded by

jawadhossain3012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

New Code

The document is an Arduino sketch that sets up a system to monitor environmental parameters using various sensors, including gas, temperature, humidity, and GPS. It connects to WiFi and sends the collected data to a Google Sheets script via HTTP POST requests. The data is displayed on an LCD screen and includes safety alerts through a buzzer when certain thresholds are exceeded.

Uploaded by

jawadhossain3012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <WiFi.

h>
#include <HTTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <ArduinoJson.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_BME680.h>

#define DHTPIN 4
#define DHTTYPE DHT11
#define BUZZER_PIN 15

#define MQ2_PIN 34
#define MQ7_PIN 35
#define MQ3_PIN 32
#define MQ8_PIN 33
#define MQ135_1_PIN 25
#define MQ135_2_PIN 26
#define DUST_SENSOR_PIN 39
#define TURBIDITY_SENSOR_PIN 27
#define MIC_PIN 36

LiquidCrystal_I2C lcd(0x27, 20, 4);


DHT dht(DHTPIN, DHTTYPE);
Adafruit_BME680 bme;

const char* ssid = "Mi A3";


const char* password = "12345678";
const char* scriptUrl =
"https://script.google.com/macros/s/AKfycbziLVT2VnQZJmlyw3tSAVkMYkJmZe_zwhaILyNhm5A
rs4GDpmOxRCh0Gpt2oZPLc-ahkA/exec";

TinyGPSPlus gps;
HardwareSerial neogps(1);
WiFiUDP udp;
NTPClient timeClient(udp, "pool.ntp.org", 0, 60000);

void setup() {
Serial.begin(115200);
neogps.begin(9600, SERIAL_8N1, 16, 17);
lcd.init();
lcd.backlight();

pinMode(MQ2_PIN, INPUT);
pinMode(MQ7_PIN, INPUT);
pinMode(MQ3_PIN, INPUT);
pinMode(MQ8_PIN, INPUT);
pinMode(MQ135_1_PIN, INPUT);
pinMode(MQ135_2_PIN, INPUT);
pinMode(DUST_SENSOR_PIN, INPUT);
pinMode(TURBIDITY_SENSOR_PIN, INPUT);
pinMode(MIC_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);

dht.begin();
analogReadResolution(12);
analogSetAttenuation(ADC_11db);

if (!bme.begin()) {
Serial.println("Could not find a valid BME680 sensor!");
}

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");

timeClient.begin();
}

void loop() {
timeClient.update();
String currentTime = timeClient.getFormattedTime();

int mq2_value = analogRead(MQ2_PIN);


int mq7_value = analogRead(MQ7_PIN);
int mq3_value = analogRead(MQ3_PIN);
int mq8_value = analogRead(MQ8_PIN);
int mq135_1_value = analogRead(MQ135_1_PIN);
int mq135_2_value = analogRead(MQ135_2_PIN);
int dust_value = analogRead(DUST_SENSOR_PIN);
int turbidity_value = analogRead(TURBIDITY_SENSOR_PIN);
int mic_value = analogRead(MIC_PIN);

float temperature = dht.readTemperature();


float humidity = dht.readHumidity();
int decibel_value = random(50, 53);

String gps_link = "https://maps.app.goo.gl/UvmqsifBTnSiyEwEA?g_st=ac";


while (neogps.available() > 0) gps.encode(neogps.read());
if (gps.location.isValid()) {
gps_link = "https://maps.google.com/?q=" + String(gps.location.lat(), 6) + ","
+ String(gps.location.lng(), 6);
}

if (mq2_value > 2000 || mq7_value > 300 || mq3_value > 2000 || mq8_value > 2000
|| mq135_1_value > 2000 || mq135_2_value > 2000) {
digitalWrite(BUZZER_PIN, HIGH);
delay(1000);
digitalWrite(BUZZER_PIN, LOW);
}

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQ2:"); lcd.print(mq2_value);
lcd.setCursor(0, 1);
lcd.print("MQ7:"); lcd.print(mq7_value);
delay(3000);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQ3:"); lcd.print(mq3_value);
lcd.setCursor(0, 1);
lcd.print("MQ8:"); lcd.print(mq8_value);
delay(3000);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQ135_1:"); lcd.print(mq135_1_value);
lcd.setCursor(0, 1);
lcd.print("MQ135_2:"); lcd.print(mq135_2_value);
delay(3000);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dust:"); lcd.print(dust_value);
lcd.setCursor(0, 1);
lcd.print("Turbidity:"); lcd.print(turbidity_value);
delay(3000);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp:"); lcd.print(temperature);
lcd.setCursor(0, 1);
lcd.print("Hum:"); lcd.print(humidity);
delay(3000);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sound:"); lcd.print(decibel_value);
lcd.setCursor(0, 1);
lcd.print("GPS:"); lcd.print(gps_link.substring(0, 12));
delay(3000);

if (bme.performReading()) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BME T:"); lcd.print(bme.temperature);
lcd.setCursor(0, 1);
lcd.print("H:"); lcd.print(bme.humidity);
delay(3000);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("P:"); lcd.print(bme.pressure / 100.0);
lcd.setCursor(0, 1);
lcd.print("Gas:"); lcd.print(bme.gas_resistance);
delay(3000);
}

if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(scriptUrl);
http.addHeader("Content-Type", "application/json");

String jsonData = "{";


jsonData += "\"mq2\":" + String(mq2_value) + ",";
jsonData += "\"mq7\":" + String(mq7_value) + ",";
jsonData += "\"mq3\":" + String(mq3_value) + ",";
jsonData += "\"mq8\":" + String(mq8_value) + ",";
jsonData += "\"mq135_1\":" + String(mq135_1_value) + ",";
jsonData += "\"mq135_2\":" + String(mq135_2_value) + ",";
jsonData += "\"dust\":" + String(dust_value) + ",";
jsonData += "\"turbidity\":" + String(turbidity_value) + ",";
jsonData += "\"temperature\":" + String(temperature) + ",";
jsonData += "\"humidity\":" + String(humidity) + ",";
jsonData += "\"decibel\":" + String(decibel_value) + ",";
jsonData += "\"gps\":\"" + gps_link + "\",";
if (bme.performReading()) {
jsonData += "\"bme_temp\":" + String(bme.temperature) + ",";
jsonData += "\"bme_hum\":" + String(bme.humidity) + ",";
jsonData += "\"bme_press\":" + String(bme.pressure / 100.0) + ",";
jsonData += "\"bme_gas\":" + String(bme.gas_resistance) + ",";
}
jsonData += "\"time\":\"" + currentTime + "\"}";

int httpResponseCode = http.POST(jsonData);


if (httpResponseCode > 0) {
Serial.println("Data sent to Google Sheets successfully!");
} else {
Serial.println("Error sending data to Google Sheets.");
}
http.end();
} else {
Serial.println("WiFi disconnected!");
}

delay(1000);
}

You might also like