Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
IOT Soil Moisture Level Reader code
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jair.canelo
IOT Soil Moisture Level Reader code
Commits
c53a4904
Commit
c53a4904
authored
Jan 16, 2025
by
jair.canelo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
f3e16d41
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
103 additions
and
0 deletions
+103
-0
Arduino IDE Source Code
Arduino IDE Source Code
+103
-0
No files found.
Arduino IDE Source Code
0 → 100644
View file @
c53a4904
#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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment