bangnaga

Tongkat Telegram Bot

Nov 8th, 2020 (edited)
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. #include <Adafruit_ADXL345_U.h>
  2. Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
  3. uint8_t i = 0;
  4. uint8_t NUM_BLINKS = 3;
  5.  
  6. // Tap settings
  7. uint8_t TAP_THRESH = 200; // 48 = 3g
  8. uint8_t TAP_DUR = 16;     // 10ms
  9. uint8_t TAP_LATENT = 16;  // 20ms
  10. uint8_t TAP_WINDOW = 128; // 160ms
  11.  
  12. // Free Fall settings
  13. uint8_t FF_THRESH = 8;    // (5 - 9) recommended
  14. uint8_t FF_DUR = 64;      // (20 - 70) recommended
  15.  
  16. #include <ESP8266WiFi.h>
  17. #include <WiFiClientSecure.h>
  18. #include <UniversalTelegramBot.h>
  19. #include <ArduinoJson.h>
  20.  
  21. const char* ssid = "MKI R&D";
  22. const char* password = "rahasia123";
  23.  
  24. // Initialize Telegram BOT
  25. #define BOTtoken "1421194712:AAHOLi1e9XlOguG3ptSTcWDF3ZVMjQ4lo6E"  // your Bot Token (Get from Botfather)
  26.  
  27. // Use @myidbot to find out the chat ID of an individual or a group
  28. // Also note that you need to click "start" on a bot before it can
  29. // message you
  30. #define CHAT_ID "1118241931"
  31.  
  32. WiFiClientSecure client;
  33. UniversalTelegramBot bot(BOTtoken, client);
  34.  
  35. const int motionSensor = 14; // PIR Motion Sensor
  36. bool motionDetected = false;
  37.  
  38. // Indicates when motion is detected
  39. void ICACHE_RAM_ATTR detectsMovement() {
  40.   //Serial.println("MOTION DETECTED!!!");
  41.   motionDetected = true;
  42. }
  43.  
  44. void setup() {
  45.   //Serial.begin(115200);
  46.   // Check sensor initialization
  47.   #ifndef ESP8266
  48.     while (!Serial); // for Leonardo/Micro/Zero
  49.   #endif
  50.  
  51.   // Join the serial bus at 9600 baud
  52.   Serial.begin(9600);
  53.   Serial.println("Accelerometer Test");
  54.  
  55.   if (!accel.begin())
  56.   {
  57.     Serial.println("No ADXL345 detected ... Check your wiring.");
  58.     while (1);
  59.   }
  60.   else
  61.     Serial.println("Accelerometer is ready.");
  62.  
  63.   // Initialize LED pin
  64.   pinMode(LED_BUILTIN, OUTPUT);
  65.  
  66.   // Enable full resolution on ADXL
  67.   accel.setRange(ADXL345_RANGE_16_G);
  68.  
  69.   // Set data rate
  70.   accel.setDataRate(ADXL345_DATARATE_100_HZ);
  71.  
  72.    // Disable interrupts before configuration
  73.   accel.writeRegister(ADXL345_REG_INT_ENABLE, 0b00000000);
  74.  
  75.   // Set tap threshold
  76.   accel.writeRegister(ADXL345_REG_THRESH_TAP, TAP_THRESH);
  77.  
  78.   // Set tap duration
  79.   accel.writeRegister(ADXL345_REG_DUR, TAP_DUR);
  80.  
  81.   // Set tap latency
  82.   accel.writeRegister(ADXL345_REG_LATENT, TAP_LATENT);
  83.  
  84.   // Set tap window
  85.   accel.writeRegister(ADXL345_REG_WINDOW, TAP_WINDOW);
  86.  
  87.   // Set free fall threshold
  88.   accel.writeRegister(ADXL345_REG_THRESH_FF, FF_THRESH);
  89.  
  90.   // Set free fall duration
  91.   accel.writeRegister(ADXL345_REG_TIME_FF, FF_DUR);
  92.  
  93.   // Map all interrupts to INT1 pin
  94.   accel.writeRegister(ADXL345_REG_INT_MAP, 0b00000000);
  95.  
  96.   // Enable x-, y-, and z- participation in tap detection
  97.   accel.writeRegister(ADXL345_REG_TAP_AXES, 0b00000111);
  98.  
  99.   // Enable Double Tap and Free Fall interrupts
  100.   accel.writeRegister(ADXL345_REG_INT_ENABLE, 0b00100100);
  101.   client.setInsecure();
  102.  
  103.   // PIR Motion Sensor mode INPUT_PULLUP
  104.   pinMode(motionSensor, INPUT_PULLUP);
  105.   // Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
  106.   attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
  107.  
  108.   // Attempt to connect to Wifi network:
  109.   Serial.print("Connecting Wifi: ");
  110.   Serial.println(ssid);
  111.  
  112.   WiFi.mode(WIFI_STA);
  113.   WiFi.begin(ssid, password);
  114.  
  115.   while (WiFi.status() != WL_CONNECTED) {
  116.     Serial.print(".");
  117.     delay(500);
  118.   }
  119.  
  120.   Serial.println("");
  121.   Serial.println("WiFi connected");
  122.   Serial.print("IP address: ");
  123.   Serial.println(WiFi.localIP());
  124.  
  125.   bot.sendMessage(CHAT_ID, "Bot started up", "");
  126. }
  127.  
  128. void loop() {
  129.   detect();
  130.   /*bot.sendMessage(CHAT_ID, " Test!!", "");
  131.   if(motionDetected){
  132.     bot.sendMessage(CHAT_ID, "Motion detected!!", "");
  133.     Serial.println("Motion Detected");
  134.     motionDetected = false;
  135.   }
  136.   */
  137. }
  138.  
  139. void detect(void)
  140. {
  141.     // Detect Free Fall or Double Tap
  142.     // Bit clears after it is read
  143.     if ((accel.readRegister(ADXL345_REG_INT_SOURCE) & 0b00000100) ||
  144.         (accel.readRegister(ADXL345_REG_INT_SOURCE) & 0b00100000))
  145.   {      
  146.    
  147.     for (i = NUM_BLINKS; i > 0; i--)
  148.     {
  149.       digitalWrite(LED_BUILTIN, HIGH);
  150.       delay(500);
  151.       digitalWrite(LED_BUILTIN, LOW);
  152.       delay(500);
  153.     }
  154.     bot.sendMessage(CHAT_ID, " Tongkat Jatuh!", "");
  155.   }
  156. }
  157.  
Advertisement
Add Comment
Please, Sign In to add comment