#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Galaxy A10s4563";
const char* password = "uxbw68830";
const int sensor1Pin = 2; // Sensor ya karibu zaidi na mlango
const int sensor2Pin = 3; // Sensor ya nje zaidi
const int goodButtonPin = 4;
const int badButtonPin = 5;
int peopleCount = 0;
bool sensor1State = LOW;
bool sensor2State = LOW;
void setup() {
Serial.begin(9600);
pinMode(sensor1Pin, INPUT);
pinMode(sensor2Pin, INPUT);
pinMode(goodButtonPin, INPUT);
pinMode(badButtonPin, INPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Kuhesabu Watu
bool newSensor1State = digitalRead(sensor1Pin);
bool newSensor2State = digitalRead(sensor2Pin);
if (newSensor1State != sensor1State) {
sensor1State = newSensor1State;
if (sensor1State == HIGH && sensor2State == LOW) {
peopleCount++;
delay(500); // Debounce delay
}
}
if (newSensor2State != sensor2State) {
sensor2State = newSensor2State;
if (sensor2State == HIGH && sensor1State == LOW) {
peopleCount--;
delay(500); // Debounce delay
}
}
if (peopleCount >= 10) { // Mfano: ikiwa watu 10 wameingia, tuma taarifa
sendDataToServer("count", String(peopleCount));
peopleCount = 0; // Reset counter baada ya kutuma taarifa
}
// Kukusanya Maoni
if (digitalRead(goodButtonPin) == HIGH) {
sendDataToServer("feedback", "Good");
delay(1000); // Debounce delay
}
if (digitalRead(badButtonPin) == HIGH) {
sendDataToServer("feedback", "Bad");
delay(1000); // Debounce delay
}
}
void sendDataToServer(String dataType, String dataValue) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String serverUrl;
String httpRequestData;
if (dataType == "count") {
serverUrl = "http://your_django_server_ip/api/update-count/";
httpRequestData = "count=" + dataValue;
} else if (dataType == "feedback") {
serverUrl = "http://your_django_server_ip/api/receive-feedback/";
httpRequestData = "feedback=" + dataValue;
}
http.begin(serverUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Error in WiFi connection");
}
}