#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define WATER 2
#define MOISTURE 0
#define SPEAKER_PIN 4
#define GAS_SENSOR 27
#define minVal -5
#define maxVal 3
Adafruit_MPU6050 mpu;
sensors_event_t event;
/*variables*/
int xsample = 0;
int ysample = 0;
int zsample = 0;
/*Macros*/
#define samples 50
// #define maxVal 20 // max change limit
// #define minVal -20 // min change limit
const char* serverName = "https://kofdpsa4ivfnfvw6s5b2v4yrk40boyof.lambda-url.ap-south-1.on.aws/";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(GAS_SENSOR, INPUT);
pinMode(SPEAKER_PIN, OUTPUT);
// Wire.begin(16, 17);
// lcd.init();
// lcd.backlight();
Serial.println("Initialize MPU6050");
while (!mpu.begin()) {
Serial.println("MPU6050 not connected!");
delay(1000);
}
Serial.println("MPU6050 ready!");
xsample /= samples; // taking avg for x
ysample /= samples; // taking avg for y
zsample /= samples; // taking avg for z
setupWifi();
}
void loop() {
// put your main code here, to run repeatedly:
int16_t j = analogRead(WATER) * 100 / 4095;
int16_t i = analogRead(MOISTURE);
int16_t gas_value = analogRead(GAS_SENSOR) * 100 / 4095;
String msg = i < 2165 ? "WET" : i > 3135 ? "DRY" : "OK";
Serial.print(F("Smoke level: "));
Serial.print(gas_value);
Serial.println(F("%"));
Serial.print(F("Water level: "));
Serial.print(j);
Serial.println(F("%"));
Serial.print(F("Moisture: "));
Serial.println(msg);
if (j >= 70 || i < 2165 || gas_value > 20) {
tone(SPEAKER_PIN, 100);
delay(100);
noTone(SPEAKER_PIN);
}
lcd.clear();
lcd.setCursor(0, 0); // move cursor the first row
lcd.print(F("Moisture: "));
lcd.print(msg);
lcd.setCursor(0, 1); // move cursor the second row
lcd.print(F("Water level: "));
lcd.print(j);
lcd.println(F("%"));
lcd.setCursor(0, 2); // move cursor the third row
lcd.print(F("Gas: "));
// lcd.print(gas_value);
mpu.getAccelerometerSensor()->getEvent(&event);
int value1 = event.acceleration.x; // reading x out
int value2 = event.acceleration.y; //reading y out
int value3 = event.acceleration.z; //reading z out
int xValue = xsample - value1; // finding change in x
int yValue = ysample - value2; // finding change in y
int zValue = zsample - value3; // finding change in z
int eqd = 0;
if (xValue < minVal || xValue > maxVal || yValue < minVal || yValue > maxVal || zValue < minVal || zValue > maxVal)
Serial.println("Earthquake Alert");
eqd = 1;
sendData(gas_value, eqd, msg, j);
delay(1000); // this speeds up the simulation
}
void setupWifi() {
lcd.setCursor(0, 0); // move cursor the first row
lcd.print("Connecting to ");
lcd.setCursor(0, 1);
lcd.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
lcd.clear();
lcd.setCursor(0, 0); // move cursor the first row
lcd.print("Wifi connected!");
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
lcd.clear();
}
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
lcd.setCursor(15, 1);
lcd.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void sendData(int gas_v, int eq_v, String moist_v, int water_v) {
Serial.println("Sending data...");
WiFiClient client;
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "text/plain");
String data = "{\"gas\":";
data.concat(gas_v);
data.concat(",\"eq\":");
data.concat(eq_v );
data.concat(",\"moisture\":\"");
data.concat(moist_v);
data.concat("\",\"water\":");
data.concat(water_v );
data.concat(",\"app\":\"eq\"");
data.concat("}");
int httpResponseCode = http.POST(data);
if (httpResponseCode > 0) {
String response = http.getString(); //Get the response to the request
Serial.println(httpResponseCode); //Print return code
Serial.println(response); //Print request answer
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}