#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <HX711.h>
#include <DHT.h>
#include <ESP32Servo.h>
// Pin definitions
#define DHTPIN 4
#define DHTTYPE DHT22
#define SERVO_PIN 18
#define LOADCELL_DOUT_PIN 21
#define LOADCELL_SCK_PIN 22
#define TRIG_PIN 5
#define ECHO_PIN 19
// WiFi credentials
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* password = ""; // Replace with your WiFi Password
// API and Certificate
const char* apiToken = "abc123XYZ456"; // Replace with your API Token
const char* serverUrl = "https://smart-feeder-server.vercel.app/"; // Replace with your domain
// Objects for sensors and actuators
DHT dht(DHTPIN, DHTTYPE);
HX711 scale;
Servo myServo;
// Constants
const float targetWeight = 1000; // Target weight in grams
const float lowThreshold = 15.0; // Distance in cm for "low"
const float midThreshold = 10.0; // Distance in cm for "mid"
const float fullThreshold = 5.0; // Distance in cm for "full"
void setup() {
Serial.begin(115200);
// Connect to WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
// Initialize sensors and servo
dht.begin();
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale();
scale.tare();
myServo.attach(SERVO_PIN);
myServo.write(0); // Start with servo open
// Initialize ultrasonic sensor
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
float readUltrasonicSensor() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2; // Convert to cm
return distance;
}
String getFoodLevel(float distance) {
if (distance > lowThreshold) {
return "Low";
} else if (distance > midThreshold) {
return "Mid";
} else {
return "Full";
}
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
long weight = scale.get_units(10);
float foodLevelDistance = readUltrasonicSensor();
String foodLevel = getFoodLevel(foodLevelDistance);
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.printf("Temperature: %.2f°C, Humidity: %.2f%%, Weight: %ldg, Food Level: %s\n",
temperature, humidity, weight, foodLevel.c_str());
if (weight >= targetWeight) {
myServo.write(0);
Serial.println("Servo closed (target weight reached)");
} else {
myServo.write(90);
Serial.println("Servo opened (weight below target)");
}
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin( serverUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", String("Bearer ") + apiToken);
String payload = "{\"temperature\": " + String(temperature) +
", \"humidity\": " + String(humidity) +
", \"weight\": " + String(weight) +
", \"foodLevel\": \"" + foodLevel + "\"}";
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.println("Response from server: " + http.getString());
} else {
Serial.printf("Error sending data: %d\n", httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi not connected!");
}
delay(2000); // Delay for stability
}