Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <TinyGPSPlus.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #define redLED 4
- #define trigPin 5
- #define echoPin 18
- #define maxTrash 6 // in CM
- #define SOUND_SPEED 0.034 // in CM/Sec
- #define WIFI_SSID "network"//"Ghaidaa"
- #define WIFI_PASSWORD "123456789"//"Ghaidaa1632"
- #define BOT_TOKEN "5653695564:AAGB9tL88hoW_TZGhFijirJ5DDDxtxaRHEM"//5758548495:AAHYaV3NnmsFQo_1UjzBgcimSttmsZDp5Os"
- String chat_id = "473975732";//660897003";
- String lon;
- String latt;
- bool flag = 0;
- long duration;
- float distanceCm;
- bool Start = false;
- String stringMsg = "";
- char messageBuffer[70];
- //unsigned long bot_lasttime;
- //const unsigned long BOT_MTBS = 1000;
- //unsigned long interval = 60000; // 1 min
- unsigned long previousMillis = 0; // will store last time LED was updated
- const long interval = 10000; // interval at which to blink (milliseconds)
- TinyGPSPlus gps;
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- void setup() {
- Serial.begin(9600);
- Serial2.begin(9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(redLED, OUTPUT);
- digitalWrite(redLED, LOW);
- connectToWiFi();
- bot.sendMessage(chat_id, "Project Online");
- }
- void loop() {
- readUS_Data();
- readGPS_Data();
- if (distanceCm < 6) {
- digitalWrite(redLED, HIGH);
- sendTelegramMessage();
- }
- else if (distanceCm >= 6) {
- digitalWrite(redLED, LOW);
- }
- }
- void sendTelegramMessage() {
- bot.sendMessage(chat_id, messageBuffer);
- delay(5000);//interval
- }
- void readUS_Data() {
- if (millis() - previousMillis >= interval) {
- // save the last time you blinked the LED
- previousMillis = millis();
- // Clears the trigPin
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration = pulseIn(echoPin, HIGH);
- // Calculate the distance
- distanceCm = duration * SOUND_SPEED / 2;
- Serial.print("Distance (cm): ");
- Serial.println(distanceCm);
- }
- }
- void connectToWiFi() {
- Serial.print("Connecting to Wifi SSID ");
- Serial.print(WIFI_SSID);
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(500);
- }
- Serial.print("\n");
- Serial.print("WiFi connected. IP address: ");
- Serial.println(WiFi.localIP());
- Serial.print("Retrieving time: ");
- configTime(0, 0, "pool.ntp.org");
- time_t now = time(nullptr);
- while (now < 24 * 3600)
- {
- Serial.print(".");
- delay(100);
- now = time(nullptr);
- }
- Serial.println(now);
- }
- void readGPS_Data() {
- while (Serial2.available() > 0)
- if (gps.encode(Serial2.read()))
- displayInfo();
- if (millis() > 5000 && gps.charsProcessed() < 10) {
- Serial.println(F("No GPS detected: check wiring."));
- while (true);
- }
- }
- void displayInfo() {
- Serial.print(F("Location: "));
- if (gps.location.isValid()) {
- lon = String(gps.location.lng(), 6);
- latt = String(gps.location.lat(), 6);
- Serial.print("Lat: ");
- Serial.print(latt);
- Serial.print(F(","));
- Serial.print("Lng: ");
- Serial.print(lon);
- Serial.println();
- stringMsg = "http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=" + latt + "+" + lon;
- stringMsg.toCharArray(messageBuffer, 70);
- }
- else {
- Serial.println(F("INVALID"));
- stringMsg = "Invalid Location Data";
- stringMsg.toCharArray(messageBuffer, 70);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment