/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp-now-esp32-arduino-ide/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <esp_now.h>
#include <WiFi.h>
#include <MQ135.h>
int analogMq135 = 16;
int buzz = 19;
MQ135 mq135_sensor(analogMq135);
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x64, 0xe8, 0x33, 0x5d, 0x0f, 0x84};
// Structure example to send data
// Must match the receiver structure
typedef struct message {
int a; // Nilai smoke sensor
bool b; // Apakah udh lewatin treshold rokok
bool c; // buzzer / led nyala or nah
} message;
message airqual;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
pinMode(analogMq135, INPUT);
pinMode(buzz,OUTPUT);
// 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;
}
esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
int16_t ValuePPMSensor = analogRead(analogMq135);
int ppmValueMapped = (ValuePPMSensor/4.095);
if (ppmValueMapped > 100) {
strcpy(message.a, "THIS IS A CHAR");
message.b = true;
message.c = true;
// Kirim message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &message, sizeof(message));
}
delay(1000); // delya pembacaan sensor
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1