Last active
June 29, 2024 04:18
-
-
Save Timo614/8ec3dec5ac49887a698191177b410417 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <HTTPClient.h> | |
#include <Seeed_Arduino_SSCMA.h> | |
#include <Grove_LED_Bar.h> | |
//change to your ssid and password | |
const char* ssid = "Wifi SSID"; | |
const char* password = "Wifi Password"; | |
//change to your token and chatid | |
const String botToken = "00000:bot-token"; | |
const String chatId = "00000"; | |
// Threshold for detection | |
const int threshold = 70; | |
const int dataPin = D2; | |
const int clockPin = D1; | |
const int buzzerPin = D0; | |
SSCMA AI; | |
Grove_LED_Bar bar(dataPin, clockPin, 0, LED_BAR_10); | |
void setup() { | |
AI.begin(); | |
Serial.begin(9600); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Connecting to WiFi..."); | |
} | |
Serial.println("Connected"); | |
bar.begin(); | |
pinMode(buzzerPin, OUTPUT); // Buzzer on D0 | |
} | |
void loop() { | |
int peopleDetected = 0; | |
int highestConfidence = 0; | |
if (!AI.invoke()) { | |
for (int i = 0; i < AI.boxes().size(); i++) { | |
if (AI.boxes()[i].score > threshold) { | |
peopleDetected++; | |
if (AI.boxes()[i].score > highestConfidence) { | |
highestConfidence = AI.boxes()[i].score; | |
} | |
} | |
} | |
// Update LED bar | |
int ledsToLight = map(highestConfidence, 0, 100, 0, 10); | |
bar.setLevel(ledsToLight); | |
if (peopleDetected > 0) { | |
Serial.println("People detected"); | |
if (WiFi.status() == WL_CONNECTED) { | |
String message = "People Detected: " + String(peopleDetected) + "\Confidence: " + String(highestConfidence); | |
sendMessage(message); | |
} | |
// Trigger buzzer | |
digitalWrite(buzzerPin, HIGH); | |
delay(1000); // Buzzer delay | |
digitalWrite(buzzerPin, LOW); | |
} | |
delay(1000); // Add a delay to avoid rapid re-triggering | |
} | |
} | |
void sendMessage(String text) { | |
String url = "https://api.telegram.org/bot" + botToken + "/sendMessage"; | |
String payload = "{\"chat_id\":\"" + chatId + "\",\"text\":\"" + text + "\"}"; | |
HTTPClient http; | |
http.begin(url); | |
http.addHeader("Content-Type", "application/json"); | |
int statusCode = http.POST(payload); | |
if (statusCode == 200) { | |
Serial.println("Message sent successfully!"); | |
} else { | |
Serial.println("Failed to send message."); | |
} | |
http.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment