c010rblind3ngineer
Published © GPL3+

Arduino 'Can't Touch This' with D1 Mini ESP8266

Now it comes with Telegram app notification message!

IntermediateShowcase (no instructions)508
Arduino 'Can't Touch This' with D1 Mini ESP8266

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Analog Accelerometer: ADXL335
Adafruit Analog Accelerometer: ADXL335
×1
Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
×1
Buzzer
Buzzer
Passive
×1
5 mm LED: Red
5 mm LED: Red
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Schematics

Circuit:

Schematic:

Code

Code:

Arduino
To be uploaded to your Arduino Nano:
/* Arduino 'Can't Touch This' Version 2
   Components:
                - Arduino Nano
                - D1 Mini (ESP8266) module
                - ADXL335
                - Passive Buzzer
                - Push button tactile switch
                - Red LED
                - 220Ohm resistor
                - 330Ohm resistor
                - 10kOhm resistor
                - Breadboard
                - Some jumper wires

   Libraries:
                - https://arduino.esp8266.com/stable/package_esp8266com_index.json

   Created on 23 August 2022 by c010blind3ngineer
*/
#define trigPin 6    // this will be the pin that triggers the D1 Mini pin D1
#define buzzerPin 9
#define LEDpin 8
#define btnPin 7

int X_axis = A0;
int Y_axis = A1;
int Z_axis = A2;
const int deg_acc = 3;
boolean trigAlarm = false;
int x, y, z;
int t = 0;

int STILL[4];

void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(LEDpin, OUTPUT);
  pinMode(btnPin, INPUT);
  pinMode(trigPin, OUTPUT);
  Serial.begin(9600);
  while (digitalRead(btnPin) != HIGH) {}
}

void loop() {
  if (digitalRead(btnPin) == HIGH) {
    Serial.print("Calibrating");
    delay(500);
    int i = 0;    // reset 'i' counter
    while (i < 3) {   // read XYZ readings 3 times, it also gives the User time to stabilise the device properly
      Serial.print(".");
      STILL[0] = analogRead(X_axis);
      STILL[1] = analogRead(Y_axis);
      STILL[2] = analogRead(Z_axis);
      delay(500);
      i++;
    }
    digitalWrite(LEDpin, HIGH);
    tone(buzzerPin, 2000);
    delay(100);
    digitalWrite(LEDpin, LOW);
    noTone(buzzerPin);
    delay(100);
    digitalWrite(LEDpin, HIGH);
    tone(buzzerPin, 2000);
    delay(100);
    digitalWrite(LEDpin, LOW);
    noTone(buzzerPin);
    trigAlarm = false;
    t = 0;
    digitalWrite(trigPin, LOW);
  }
  // Read XYZ axis values
  x = analogRead(X_axis);
  y = analogRead(Y_axis);
  z = analogRead(Z_axis);

  // Check to see if the device is in the same position when the User set it initially
  if ((x > (STILL[0] - deg_acc)) && (x < (STILL[0] + deg_acc)) && (y > (STILL[1] - deg_acc)) && (y < (STILL[1] + deg_acc)) && (z > (STILL[2] - deg_acc)) && (z < (STILL[2] + deg_acc)) ) {
    // You can uncomment the lines below to see the XYZ values in the Serial Monitor
    //    Serial.print(x);
    //    Serial.print("\t");
    //    Serial.print(y);
    //    Serial.print("\t");
    //    Serial.print(z);
    //    Serial.println();
  }
  else {
    trigAlarm = true;
    while(t < 1){ // will only trigger pin 6 on the Arduino Nano once
      digitalWrite(trigPin, HIGH);  // trigger pin D1 on the D1 Mini (ESP8266) module
      t = 1;
    }
  }
  // Alarm goes off when someone moves the device out of position
  if (trigAlarm == true) {
    digitalWrite(LEDpin, HIGH);
    tone(buzzerPin, 2000);
    delay(100);
    digitalWrite(LEDpin, LOW);
    noTone(buzzerPin);
  }
  delay(100);
}

Code 2:

Arduino
To be uploaded to your D1 Mini ESP8266
/* Arduino 'Can't Touch This' Version 2
   Components:
                - Arduino Nano
                - D1 Mini (ESP8266) module
                - ADXL335
                - Passive Buzzer
                - Push button tactile switch
                - Red LED
                - 220Ohm resistor
                - 330Ohm resistor
                - 10kOhm resistor
                - Breadboard
                - Some jumper wires

   Libraries:
                - https://arduino.esp8266.com/stable/package_esp8266com_index.json

   Created on 23 August 2022 by c010blind3ngineer
*/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

#define btnPin D1

// Wifi network station credentials
#define WIFI_SSID "YOUR WIFI NETWORK"
#define WIFI_PASSWORD "YOUR WIFI PASSWORD"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "**********:***********************************"

// Use @myidbot (IDBot) 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 "**********"

X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);


void setup() {
  pinMode(btnPin, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH); // active LOW, means it will ON when it is LOW. We set it OFF (HIGH) it first.

  // attempt to connect to Wifi network:
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
  while (WiFi.status() != WL_CONNECTED)
  {
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
  }

  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  digitalWrite(LED_BUILTIN, HIGH);
  
  configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  time_t now = time(nullptr);
  while (now < 24 * 3600)
  {
    delay(100);
    now = time(nullptr);
  }
}

void loop() {
  if (digitalRead(btnPin) == HIGH) {
    bot.sendMessage(CHAT_ID, "Movement detected!", "");
  }
}

Repository link:

Credits

c010rblind3ngineer
5 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.