bangnaga icon

Tongkat Telegram Bot

bangnaga | PRO | 06/23/25 06:34:23 AM UTC (Edited) | 0 ⭐ | 216 👁️ | Never ⏰ | []
C++ |

4.17 KB

|

None

|

0 👍

/

0 👎

#include <Adafruit_ADXL345_U.h>
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
uint8_t i = 0;
uint8_t NUM_BLINKS = 3;
 
// Tap settings
uint8_t TAP_THRESH = 200; // 48 = 3g
uint8_t TAP_DUR = 16;     // 10ms
uint8_t TAP_LATENT = 16;  // 20ms
uint8_t TAP_WINDOW = 128; // 160ms
 
// Free Fall settings
uint8_t FF_THRESH = 8;    // (5 - 9) recommended
uint8_t FF_DUR = 64;      // (20 - 70) recommended
 
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
 
const char* ssid = "MKI R&D";
const char* password = "rahasia123";
 
// Initialize Telegram BOT
#define BOTtoken "1421194712:AAHOLi1e9XlOguG3ptSTcWDF3ZVMjQ4lo6E"  // your Bot Token (Get from Botfather)
 
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "1118241931"
 
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
 
const int motionSensor = 14; // PIR Motion Sensor
bool motionDetected = false;
 
// Indicates when motion is detected
void ICACHE_RAM_ATTR detectsMovement() {
  //Serial.println("MOTION DETECTED!!!");
  motionDetected = true;
}
 
void setup() {
  //Serial.begin(115200);
  // Check sensor initialization
  #ifndef ESP8266
    while (!Serial); // for Leonardo/Micro/Zero
  #endif
 
  // Join the serial bus at 9600 baud
  Serial.begin(9600);
  Serial.println("Accelerometer Test");
 
  if (!accel.begin())
  {
    Serial.println("No ADXL345 detected ... Check your wiring.");
    while (1);
  }
  else
    Serial.println("Accelerometer is ready.");
 
  // Initialize LED pin 
  pinMode(LED_BUILTIN, OUTPUT);
 
  // Enable full resolution on ADXL
  accel.setRange(ADXL345_RANGE_16_G);
 
  // Set data rate
  accel.setDataRate(ADXL345_DATARATE_100_HZ);
 
   // Disable interrupts before configuration
  accel.writeRegister(ADXL345_REG_INT_ENABLE, 0b00000000);
 
  // Set tap threshold
  accel.writeRegister(ADXL345_REG_THRESH_TAP, TAP_THRESH);
 
  // Set tap duration
  accel.writeRegister(ADXL345_REG_DUR, TAP_DUR);
 
  // Set tap latency
  accel.writeRegister(ADXL345_REG_LATENT, TAP_LATENT);
 
  // Set tap window
  accel.writeRegister(ADXL345_REG_WINDOW, TAP_WINDOW);
 
  // Set free fall threshold
  accel.writeRegister(ADXL345_REG_THRESH_FF, FF_THRESH);
 
  // Set free fall duration
  accel.writeRegister(ADXL345_REG_TIME_FF, FF_DUR);
 
  // Map all interrupts to INT1 pin
  accel.writeRegister(ADXL345_REG_INT_MAP, 0b00000000);
 
  // Enable x-, y-, and z- participation in tap detection
  accel.writeRegister(ADXL345_REG_TAP_AXES, 0b00000111);
 
  // Enable Double Tap and Free Fall interrupts
  accel.writeRegister(ADXL345_REG_INT_ENABLE, 0b00100100);
  client.setInsecure();
 
  // PIR Motion Sensor mode INPUT_PULLUP
  pinMode(motionSensor, INPUT_PULLUP);
  // Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
  attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
 
  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  bot.sendMessage(CHAT_ID, "Bot started up", "");
}
 
void loop() {
  detect();
  /*bot.sendMessage(CHAT_ID, " Test!!", "");
  if(motionDetected){
    bot.sendMessage(CHAT_ID, "Motion detected!!", "");
    Serial.println("Motion Detected");
    motionDetected = false;
  }
  */
}
 
void detect(void)
{ 
    // Detect Free Fall or Double Tap
    // Bit clears after it is read
    if ((accel.readRegister(ADXL345_REG_INT_SOURCE) & 0b00000100) ||
        (accel.readRegister(ADXL345_REG_INT_SOURCE) & 0b00100000))
  {      
    
    for (i = NUM_BLINKS; i > 0; i--)
    {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(500);
      digitalWrite(LED_BUILTIN, LOW);
      delay(500);
    }
    bot.sendMessage(CHAT_ID, " Tongkat Jatuh!", "");
  }
}
 

Comments