F.Anti

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.7 kB
6
Indexable
Never
#include <esp_now.h>
#include <WiFi.h>
#include "MQ135.h"

// Structure example to receive data
// Must match the sender structure
#define PIN_MQ135 34
MQ135 mq135_sensor = MQ135(PIN_MQ135);

int pinA0 = 32;
int pinD0 = 35;
int flameA0;
int flameD0;

uint32_t lastRecv;
bool wanring = false;
typedef struct struct_message {
    bool sos;
    bool light1;
    bool light2;
} struct_message;

// Create a struct_message called myData
struct_message myData;

byte buzzerPin = 23;
byte buttonPin = 22;
byte light1 = 5;
byte light1b = 21;
bool on1 = false;
byte light2 = 18;
byte light2b = 19;
bool on2 = false;
void beep()
{
  static uint32_t lastBeep;
  if(millis() > lastBeep + 200)
  {
    lastBeep = millis();
    digitalWrite(buzzerPin, !digitalRead(buzzerPin));
    }
  }


// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  lastRecv = millis();
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("sos: ");
  Serial.println(myData.sos);
  Serial.print("light1: ");
  Serial.println(myData.light1);
  Serial.print("light2: ");
  Serial.println(myData.light2);
  Serial.println();
  digitalWrite(2, !digitalRead(2));
}
 
void setup() {
  // Initialize Serial Monitor
  Serial.begin(115200);
  
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(2, OUTPUT);
  pinMode(pinA0, INPUT);
  pinMode(pinD0, INPUT);

  // led
  pinMode(4, OUTPUT); // sos
  digitalWrite(4, LOW);
  pinMode(light1, OUTPUT); // main
  digitalWrite(light1, LOW);
  pinMode(light2, OUTPUT); // room
  digitalWrite(light2, LOW);
}
 
void loop() {
  float ppm = mq135_sensor.getPPM(); //CBK_Gas, Chiso PPM.
  flameA0 = analogRead(pinA0);
  flameD0 = digitalRead(pinD0);

  if (myData.light1 == 1) {
    digitalWrite(light1, HIGH);
    digitalWrite(light2, HIGH);
  }

  if (myData.light2 == 1) {
    digitalWrite(light1, LOW);
    digitalWrite(light2, LOW);
  } 
  
  if (millis()> lastRecv + 500 || myData.sos == 1 || ppm >= 50 || flameA0 <= 1500)
  {
    wanring = true;
    }
  if (!digitalRead(buttonPin))
    wanring = false;
  if (wanring)
  {
    beep();
    digitalWrite(4, HIGH);
    delay(50);
    digitalWrite(4, LOW);
    delay(50);
    }
   else
   {
    digitalWrite(buzzerPin, LOW);
    }
  delay(50); 
}