Commit c53a4904 authored by jair.canelo's avatar jair.canelo

Add new file

parent f3e16d41
#include <WiFi.h> // Updated to <WiFi.h> for ESP32
#include <ThingSpeak.h>
// Wi-Fi credentials
const char* ssid = "HometelecomDX1A2G"; // Replace with your Wi-Fi SSID
const char* password = "qg2r2cdJG3fnr79t"; // Replace with your Wi-Fi Password
// ThingSpeak details
unsigned long myChannelNumber = 2808525; // Your ThingSpeak channel number
const char* myWriteAPIKey = "T9ZPZQW4H3N2LL83"; // Your ThingSpeak Write API Key
// Wi-Fi client and server
WiFiServer server(80); // HTTP server running on port 80
WiFiClient client;
// Pin definitions
#define SOIL_SENSOR_PIN A0 // Analog pin for soil moisture sensor
#define BUZZER 5 // Pin for buzzer
// Threshold and sensor variables
int threshold = 450; // Moisture threshold value
int soilMoistureValue = 0; // Raw soil moisture value
int soilMoisturePercent = 0; // Soil moisture percentage
bool buzzerState = false; // State of the buzzer
void setup() {
pinMode(BUZZER, OUTPUT);
Serial.begin(115200); // Start Serial Monitor
// Connect to Wi-Fi
Serial.print("Connecting to Wi-Fi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Start HTTP server
server.begin();
// Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
// Handle HTTP client connections
WiFiClient client = server.available();
if (client) {
Serial.println("New client connected");
String request = client.readStringUntil('\r');
Serial.println("Request: " + request);
client.flush();
// Respond to client
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n";
response += "Soil Moisture Value: " + String(soilMoistureValue) + "\n";
response += "Soil Moisture Percentage: " + String(soilMoisturePercent) + "%";
response += "Buzzer State: " + String(buzzerState ? "ON" : "OFF");
client.print(response);
client.stop();
Serial.println("Response sent");
}
// Read soil moisture value
soilMoistureValue = analogRead(SOIL_SENSOR_PIN);
// Convert to percentage
soilMoisturePercent = map(soilMoistureValue, 0, 1023, 0, 100);
// Debugging output
Serial.print("Soil Moisture Value: ");
Serial.println(soilMoistureValue);
Serial.print("Soil Moisture Percentage: ");
Serial.println(soilMoisturePercent);
// Check moisture threshold and control buzzer
if (soilMoistureValue < threshold) {
digitalWrite(BUZZER, HIGH); // Turn on buzzer
Serial.println("Soil is too dry!");
} else {
digitalWrite(BUZZER, LOW); // Turn off buzzer
}
// Send data to ThingSpeak
ThingSpeak.setField(1, soilMoisturePercent);
int response = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (response == 200) {
Serial.println("Data sent to ThingSpeak successfully!");
} else {
Serial.print("Error sending data to ThingSpeak. HTTP Error code: ");
Serial.println(response);
}
// Delay before the next iteration (minimize client requests and ThingSpeak updates)
delay(15000);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment