this code work fine
"
#include <Arduino_ESP32_OTA.h>
#define AOUT_PIN_MQ2 13 // ESP32's pin GPIO13 connected to AOUT pin of the MQ2 sensor
#define AOUT_PIN_FLAME 12 // ESP32's pin GPIO12 connected to AOUT pin of the flame sensor
#define BUZZER_PIN 5 // ESP32's pin GPIO5 connected to the buzzer
#define LED_RED_PIN 22 // ESP32's pin GPIO22 connected to the red LED
#define LED_GREEN_PIN 23 // ESP32's pin GPIO23 connected to the green LED
// const char ssid[] = "FAM_2.4"; // Wifi name
// const char pass[] = "gGcJca5D"; // Wifi Password
void setup() {
Serial.begin(115200);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_RED_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
ledcSetup(0, 5000, 8);
ledcAttachPin(BUZZER_PIN, 0);
digitalWrite(LED_RED_PIN, HIGH);
Serial.println("Warming up sensors...");
delay(20000); // 20 seconds warm-up
digitalWrite(LED_RED_PIN, LOW);
digitalWrite(LED_GREEN_PIN, HIGH);
Serial.println("Connected, Sensors are ready!");
}
void loop() {
// Read smoke (MQ2) sensor
int smokeValue = analogRead(AOUT_PIN_MQ2);
Serial.print("Smoke Value: ");
Serial.println(smokeValue);
// Read flame sensor from A0
int flameValue = analogRead(AOUT_PIN_FLAME);
Serial.print("Flame Value: ");
Serial.println(flameValue);
// Check for smoke and activate buzzer with different tones
if (smokeValue > 500) {
tone(BUZZER_PIN, 1000); // Adjust frequency for smoke detection
Serial.println("Smoke detected! Buzzer sounding (Smoke tone).");
digitalWrite(LED_RED_PIN, HIGH);
digitalWrite(LED_GREEN_PIN, LOW); // Alternate red and green LEDs
delay(3000); // Adjust duration for tone to be audible
noTone(BUZZER_PIN); // Turn off the buzzer
}
// Check for flame and activate buzzer with different tones
if (flameValue < 1000) {
tone(BUZZER_PIN, 900); // Adjust frequency for flame detection
digitalWrite(LED_RED_PIN, HIGH);
digitalWrite(LED_GREEN_PIN, LOW); // Alternate red and green LEDs
Serial.println("Flame detected! Buzzer sounding (Flame tone).");
} else {
noTone(BUZZER_PIN);
digitalWrite(LED_RED_PIN, LOW);
digitalWrite(LED_GREEN_PIN, HIGH);
}
delay(1000); // You can adjust the delay based on your application requirements
}
the reading is what i expect but for the when in the i upload the cloud iot code with the same functions as the code above.
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/40e93057-2b60-4791-99ff-3401699cfe61
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float mq2_sensor;
int flame_sensor;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <Arduino_ESP32_OTA.h>
#include <WiFi.h>
#include <WiFiClient.h>
#define AOUT_PIN_MQ2 13 // ESP32's pin GPIO13 connected to AOUT pin of the MQ2 sensor
#define AOUT_PIN_FLAME 12 // ESP32's pin GPIO12 connected to AOUT pin of the flame sensor
#define BUZZER_PIN 5 // ESP32's pin GPIO5 connected to the buzzer
#define LED_RED_PIN 22 // ESP32's pin GPIO22 connected to the red LED
#define LED_GREEN_PIN 23 // ESP32's pin GPIO23 connected to the green LED
void setup() {
// Initialize serial and wait for port to open:
Serial.begin();
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_RED_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
ledcSetup(0, 5000, 8);
ledcAttachPin(BUZZER_PIN, 0);
digitalWrite(LED_RED_PIN, HIGH);
Serial.println("Warming up sensors...");
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
digitalWrite(LED_RED_PIN, LOW);
digitalWrite(LED_GREEN_PIN, HIGH);
Serial.println("Connected, Sensors are ready!");
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Read smoke (MQ2) sensor
int smokeValue = analogRead(AOUT_PIN_MQ2);
Serial.print("Smoke Value: ");
Serial.println(smokeValue);
// Read flame sensor from A0
int flameValue = analogRead(AOUT_PIN_FLAME);
Serial.print("Flame Value: ");
Serial.println(flameValue);
// Check for smoke and activate buzzer with different tones
if (smokeValue > 500) {
tone(BUZZER_PIN, 1000); // Adjust frequency for smoke detection
Serial.println("Smoke detected! Buzzer sounding (Smoke tone).");
digitalWrite(LED_RED_PIN, HIGH);
digitalWrite(LED_GREEN_PIN, LOW); // Alternate red and green LEDs
delay(3000); // Adjust duration for tone to be audible
noTone(BUZZER_PIN); // Turn off the buzzer
}
// Check for flame and activate buzzer with different tones
if (flameValue < 1000) {
tone(BUZZER_PIN, 900); // Adjust frequency for flame detection
digitalWrite(LED_RED_PIN, HIGH);
digitalWrite(LED_GREEN_PIN, LOW); // Alternate red and green LEDs
Serial.println("Flame detected! Buzzer sounding (Flame tone).");
} else {
noTone(BUZZER_PIN);
digitalWrite(LED_RED_PIN, LOW);
digitalWrite(LED_GREEN_PIN, HIGH);
}
delay(1000); // You can adjust the delay based on your application requirements
// Your code here
}
The value is consistently zero, causing the flame and smoke actions to trigger. I know is not wiring because the first code works.