Sei sulla pagina 1di 4

//Ultrasonic Sensor HC-SR04 interfacing with Arduino.

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
const int rs = 12, en = 7, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int gasPin = A0; //GAS sensor output pin to Arduino analog A0 pin
const int buzzerPin = 8; //Buzzer is connected pin 13

int gasValue = 0;

#define RX 10
#define TX 11

// defining the pins


const int trigPin = 9;
const int echoPin = 6;
// defining variables
long duration;
int distance;

int countTrueCommand;
int countTimeCommand;
boolean found = false;

String AP = "moto"; // CHANGE ME


String PASS = "apple1234"; // CHANGE ME

String API = "BNP9FON14G9JM772"; // CHANGE ME


String HOST = "api.thingspeak.com";
String PORT = "80";
String field_gas = "field1";
String field_dustbin = "field2";

SoftwareSerial esp8266(RX, TX);

int dustBinStatus = 0, gasStatus = 0;

uint32_t nextlogTime = 0;

void setup() {

Serial.begin(9600); //Initialize serial port - 9600 bps


esp8266.begin(115200);
sendCommand("AT", 5, "OK");
sendCommand("AT+CWMODE=1", 5, "OK");
sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");

pinMode(buzzerPin, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication

lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;

if (distance < 50)


{
// Prints the distance on the Serial Monitor
lcd.clear();

if (distance >= 20)


{
lcd.print("Bin Empty:");
Serial.print("Bin Empty:");
dustBinStatus = 0;
}
else if (distance >= 10)
{
lcd.print("halfFilled:");
Serial.print("halfFilled:");
dustBinStatus = 50;
}
else if (distance >= 0)
{
lcd.print("AlmostFull:");
Serial.print("AlmostFull:");
dustBinStatus = 100;
}
lcd.print(distance);
Serial.print(distance);

lcd.setCursor(0, 1);
gasValue = analogRead(gasPin);
if (gasValue >= 300)
{
lcd.print("GasDetected:");
Serial.print("GasDetected:");
digitalWrite(buzzerPin, 1);
gasStatus = 1;
// delay(100);
// digitalWrite(buzzerPin,0);
}
else
{
lcd.print("Normal Air: ");
Serial.print("Normal Air: ");
digitalWrite(buzzerPin, 0);
gasStatus = 0;
}

lcd.print(gasValue);
Serial.println(gasValue);

if (millis() > nextlogTime)


{
// valSensor = getSensorData();
Serial.println("---------------GAS Status ------------");
String getData = "GET /update?api_key=" + API + "&" + field_gas + "=" +
String(gasStatus);
sendCommand("AT+CIPMUX=1", 5, "OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
sendCommand("AT+CIPSEND=0," + String(getData.length() + 4), 4, ">");
esp8266.println(getData); delay(500); countTrueCommand++;
sendCommand("AT+CIPCLOSE=0", 5, "OK");
Serial.println(gasStatus);
Serial.println("--------------------------------------");
Serial.println(" ");

Serial.println("---------------Dust Bin Status------------");


String getData_buzzerPin = "GET /update?api_key=" + API + "&" + field_dustbin +
"=" + String(dustBinStatus);
sendCommand("AT+CIPMUX=1", 5, "OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
sendCommand("AT+CIPSEND=0," + String(getData_buzzerPin.length() + 4), 4, ">");
esp8266.println(getData_buzzerPin); delay(500); countTrueCommand++;
sendCommand("AT+CIPCLOSE=0", 5, "OK");
Serial.println(dustBinStatus);
Serial.println("--------------------------------------");
Serial.println(" ");
nextlogTime = millis() + 5000;
}

delay(1000);
}

void sendCommand(String command, int maxTime, char readReplay[]) {


Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while (countTimeCommand < (maxTime * 1))
{
esp8266.println(command);//at+cipsend
if (esp8266.find(readReplay)) //ok
{
found = true;
break;
}

countTimeCommand++;
}

if (found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}

if (found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}

found = false;

delay(100);
}

Potrebbero piacerti anche