#include <WiFi.h>
#include <HTTPClient.h>
// Define pins for sensors and output devices
const int sensor1Pin = 34; // GPIO for first sensor (potentiometer)
const int sensor2Pin = 35; // GPIO for second sensor (potentiometer)
const int sensor3Pin = 32; // GPIO for third sensor (potentiometer)
const int buzzerPin = 33; // GPIO for buzzer
const int ledPin = 25; // GPIO for LED (optional)
// WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// ThingSpeak settings
const char* apiKey = "your_THINGSPEAK_WRITE_API_KEY";
const String baseUrl = "https://api.thingspeak.com/update?api_key=" + String(apiKey);
void setup() {
Serial.begin(115200);
// Initialize sensor pins
pinMode(sensor1Pin, INPUT);
pinMode(sensor2Pin, INPUT);
pinMode(sensor3Pin, INPUT);
// Initialize output pins
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
void loop(){
// Read sensor values
int sensor1Value = analogRead(sensor1Pin);
int sensor2Value = analogRead(sensor2Pin);
int sensor3Value = analogRead(sensor3Pin);
// Print sensor values to Serial Monitor
Serial.print("Sensor 1: ");
Serial.print(sensor1Value);
Serial.print(" Sensor 2: ");
Serial.print(sensor2Value);
Serial.print(" Sensor 3: ");
Serial.println(sensor3Value);
// Send data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = baseUrl + "&field1=" + String(sensor1Value) +
"&field2=" + String(sensor2Value) +
"&field3=" + String(sensor3Value);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
}
http.end();
}
// Check sensor values and trigger alerts
if (sensor1Value > 1000) {
digitalWrite(ledPin, HIGH); // Turn on LED
tone(buzzerPin, 1000); // Activate buzzer
} else if (sensor2Value > 1000) {
digitalWrite(ledPin, LOW); // Turn off LED
noTone(buzzerPin); // Deactivate buzzer
} else if (sensor3Value > 1000) {
digitalWrite(ledPin, LOW); // Turn off LED
noTone(buzzerPin); // Deactivate buzzer
}
delay(20000); // Delay for updating ThingSpeak
}