#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* serverAddress = "YourServerAddress"; // Address of your central server
const int serverPort = 80; // Port number of your server
const int sensorPin = 2; // Pin connected to the sensor
const int ledPin = 13; // Pin connected to the LED for visual indication
const int threshold = 800; // Threshold value for waste level detection
WiFiClient client;
void setup() {
Serial.begin(115200);
pinMode(sensorPin, INPUT);
pinMode(ledPin, OUTPUT);
connectToWiFi();
}
void loop() {
int wasteLevel = analogRead(sensorPin);
if (wasteLevel > threshold) {
digitalWrite(ledPin, HIGH); // Turn on LED if waste level is high
sendDataToServer(wasteLevel); // Send waste level data to server
} else {
digitalWrite(ledPin, LOW);
}
delay(1000);
}
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void sendDataToServer(int wasteLevel) {
if (client.connect(serverAddress, serverPort)) {
String postData = "waste_level=" + String(wasteLevel);
client.println("POST /updateWasteLevel HTTP/1.1");
client.println("Host: " + String(serverAddress));
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: " + String(postData.length()));
client.println();
client.print(postData);
Serial.println("Data sent to server: " + postData);
} else {
Serial.println("Connection to server failed");
}
client.stop();
}