Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Arduino:
- #include <Keypad.h>
- const byte ROWS = 4;
- const byte COLS = 3;
- char hexaKeys[ROWS][COLS] = {
- {'1', '2', '3'},
- {'4', '5', '6'},
- {'7', '8', '9'},
- {'*', '0', '#'}
- };
- byte rowPins[ROWS] = {9, 8, 7, 6};
- byte colPins[COLS] = {5, 4, 3};
- Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
- int led = 13;
- int pirSensor1 = 10;
- int pirSensor2 = 11;
- int pirSensor3 = 12;
- int input1 = 0;
- int input2 = 0;
- int input3 = 0;
- int area1DeactivationCode = 123;
- int area2DeactivationCode = 456;
- int area3DeactivationCode = 789;
- int getCode()
- {
- int code = 0;
- char key = customKeypad.getKey();
- while(key != '#')
- {
- switch (key)
- {
- case NO_KEY:
- break;
- case '0': case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8': case '9':
- Serial.print("debug: Entered key: ");Serial.println(key);
- code = code * 10 + (key - '0');
- break;
- case '*':
- code = 0;
- break;
- }
- key = customKeypad.getKey();
- }
- Serial.print("debug: Confirmed code: ");Serial.println(code);
- return code;
- }
- void setup(){
- pinMode(led, OUTPUT);
- pinMode(pirSensor1, INPUT);
- pinMode(pirSensor2, INPUT);
- pinMode(pirSensor3, INPUT);
- Serial.begin(9600);
- }
- void loop(){
- input1 = digitalRead(pirSensor1);
- input2 = digitalRead(pirSensor2);
- input3 = digitalRead(pirSensor3);
- if(input1 == 1){
- digitalWrite(led, HIGH);
- Serial.println("Area 1: Alarm activated");
- int code = 0;
- do{
- code = getCode();
- } while(code != area1DeactivationCode);
- digitalWrite(led, LOW);
- Serial.print("Area 1: Alarm deactivated with code: ");Serial.println(code);
- }
- if(input2 == 1){
- digitalWrite(led, HIGH);
- Serial.println("Area 2: Alarm activated");
- int code = 0;
- do{
- code = getCode();
- } while(code != area2DeactivationCode);
- digitalWrite(led, LOW);
- Serial.print("Area 2: Alarm deactivated with code: ");Serial.println(code);
- }
- if(input3 == 1){
- digitalWrite(led, HIGH);
- Serial.println("Area 3: Alarm activated");
- int code = 0;
- do{
- code = getCode();
- } while(code != area3DeactivationCode);
- digitalWrite(led, LOW);
- Serial.print("Area 3: Alarm deactivated with code: ");Serial.println(code);
- }
- }
- Java:
- package main;
- import java.time.LocalDateTime;
- public class Event {
- private int id;
- private String name;
- private LocalDateTime createdAt;
- public Event(){
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public LocalDateTime getCreatedAt() {
- return createdAt;
- }
- public void setCreatedAt(LocalDateTime createdAt) {
- this.createdAt = createdAt;
- }
- @Override
- public String toString() {
- return "Event [id=" + id + ", name=" + name + ", createdAt=" + createdAt + "]";
- }
- }
- package main;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import java.nio.charset.StandardCharsets;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.time.Duration;
- import java.time.LocalDateTime;
- import java.util.concurrent.TimeUnit;
- public class Main {
- public static Connection getMySQLConnection() throws SQLException {
- String url = "jdbc:mysql://localhost:3306/alarm";
- String username = "root";
- String password = "";
- return DriverManager.getConnection(url, username, password);
- }
- public static void main(String[] args) {
- new Thread()
- {
- public void run() {
- while(true){
- try {
- Thread.sleep(5000);
- Connection connection = getMySQLConnection();
- Statement statement = connection.createStatement();
- ResultSet resultSet;
- Event event = null;
- resultSet = statement.executeQuery("SELECT * FROM events ORDER BY created_at DESC LIMIT 1");
- while(resultSet.next()) {
- event = new Event();
- event.setId(resultSet.getInt("id"));
- event.setName(resultSet.getString("name"));
- event.setCreatedAt(resultSet.getTimestamp("created_at").toLocalDateTime());
- }
- if(event != null && event.getName().contains("Alarm activated") &&
- TimeUnit.MILLISECONDS.toSeconds(Duration.between(event.getCreatedAt(), LocalDateTime.now()).toMillis()) > 20){
- String area = event.getName().substring(0, 6);
- statement.executeUpdate("INSERT INTO events (id, name) VALUES (0, 'SOT team dispatched to " + area + "')");
- }
- connection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
- try (BufferedReader br = new BufferedReader(new InputStreamReader(
- new FileInputStream("C:\\Users\\Dobi\\Desktop\\output.txt"), StandardCharsets.UTF_8))) {
- String line;
- while (true) {
- Thread.sleep(500);
- line = br.readLine();
- if (line != null && !line.startsWith("debug")) {
- Connection connection = getMySQLConnection();
- Statement statement = connection.createStatement();
- statement.executeUpdate("INSERT INTO events (id, name) VALUES (0, '" + line + "')");
- connection.close();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement