#include <WiFi.h>
#include <Wire.h>
#include <ThingSpeak.h>
// WiFi credentials
const char *ssid = "Wokwi-GUEST";
const char *password = "";
// ThingSpeak API key and channel ID
const char *apiKey = "7XBVCDI0PI6266G3";
const long channelID = 2410153; // Replace with your ThingSpeak channel ID
// Pin for LM35 Temperature Sensor
const int temperatureSensorPin = 35; // Analog pin for LM35 Temperature Sensor
// Pins for LED and Buzzer
const int ledPin = 13; // Digital pin for LED
const int buzzerPin = 12; // Digital pin for Buzzer
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Set up pins
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Read analog value from the LM35 temperature sensor
int temperatureValue = analogRead(temperatureSensorPin);
// Convert the temperature value to degrees Celsius
float voltage = (temperatureValue / 4095.0) * 3300.0; // LM35 linearly scales 10mV per degree Celsius
float temperatureCelsius = voltage / 10.0;
// Print values to the serial monitor
Serial.print("Temperature (C): ");
Serial.println(temperatureCelsius);
// If temperature is above a certain threshold, send data to ThingSpeak and activate LED and buzzer
if (temperatureCelsius > 25.0) { // Adjust the threshold based on your specific conditions
// Send data to ThingSpeak
int status = ThingSpeak.writeField(channelID, 1, temperatureCelsius, apiKey);
// Check if data was sent successfully
if (status == 200) {
Serial.println("Data sent to ThingSpeak successfully!");
} else {
Serial.print("Error sending data to ThingSpeak. HTTP status code: ");
Serial.println(status);
}
// Activate LED and buzzer
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
}
// Delay for stability
delay(10000); // Increase the delay to 10 seconds
}