Skip to content
Snippets Groups Projects
Commit b064b636 authored by Dominik Alkhovik's avatar Dominik Alkhovik
Browse files

init

parents
No related branches found
No related tags found
No related merge requests found
#define PIR_MOTION_SENSOR_1 2
#define PIR_MOTION_SENSOR_2 3
const int soundPin = 4;
const int buzzerPin = 5;
const int buttonPin = 6;
int MotionState1; // the state of the Motion SENSOR 1 (PIR)
int MotionState2; // the state of the Motion SENSOR 2 (PIR)
int soundState;
int buzzerState = LOW;
int buttonState = LOW;
bool alarmEnabled = false;
int triggerEnabled = LOW;
void setup() {
pinMode(PIR_MOTION_SENSOR_1, INPUT);
pinMode(PIR_MOTION_SENSOR_2, INPUT);
pinMode(soundPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, OUTPUT);
digitalWrite(buzzerPin, buzzerState);
digitalWrite(buttonPin, buttonState);
Serial.begin(9600);
}
void loop() {
// Reading Data from Sensors
MotionState1 = digitalRead(PIR_MOTION_SENSOR_1);
MotionState2 = digitalRead(PIR_MOTION_SENSOR_2);
soundState = digitalRead(soundPin);
if (Serial.available() > 0) {
String msg = Serial.readString();
if (msg == "Enabled: True"){
alarmEnabled = true;
} else if (msg == "Enabled: False") {
alarmEnabled = false;
}
if (msg == "Triggered: True"){
triggerEnabled = HIGH;
} else if (msg == "Triggered: False") {
triggerEnabled = LOW;
}
}
// int triggerAlarm = LOW;
if (alarmEnabled) {
if (MotionState1 == 1){
triggerEnabled = HIGH;
}
if (MotionState2 == 1){
triggerEnabled = HIGH;
}
// if (soundState == HIGH){
// triggerAlarm = HIGH;
// Serial.println("Sound");
// }
}
if (triggerEnabled == HIGH){
// digitalWrite(buzzerPin, triggerAlarm);
//delay(3000);
Serial.println("Movement: True");
} else {
Serial.println("Movement: False");
}
// if (alarmEnabled) {
// Serial.println("Enabled: True");
// } else {
// Serial.println("Enabled: False");
// }
// digitalWrite(buzzerPin, triggerAlarm);
digitalWrite(buttonPin, triggerEnabled);
delay(1000);
}
\ No newline at end of file
This diff is collapsed.
cloud.py 0 → 100644
import os
import time
import paho.mqtt.client as mqtt
import json
import serial
import serial.tools.list_ports
THINGSBOARD_HOST = 'thingsboard.cs.cf.ac.uk'
ACCESS_TOKEN = 'Xh4fjhD3N8xsnc7QvlVx' # <== Insert your own access token here.
global ard
data_changed = True
# Check if we can successfully push a data to thingsboard
def on_publish(client,userdata,result):
print("Successful Publish")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
if not ard: return
print ('Topic: ' + msg.topic + '\nMessage: ' + str(msg.payload))
# Decode JSON request
data = json.loads(msg.payload)
# Check request method
if data['method'] == 'setValue':
params = data['params']
if 'enabled' in params:
# alarm_state['enabled'] = params['enabled']
ard.write(f"Enabled: {params['enabled']}".encode('utf-8'))
if 'triggered' in params:
# alarm_state['triggered'] = params['triggered']
ard.write(f"Triggered: {params['triggered']}".encode('utf-8'))
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc, *extra_params):
print('Connected to thingsboard with result code ' + str(rc))
# Data capture and upload interval in seconds. Less interval will eventually hang the DHT11.
INTERVAL = 1
alarm_state = {'enabled': False, 'triggered': False}
next_reading = time.time()
# Generate your client
client = mqtt.Client()
# Set access token
client.username_pw_set(ACCESS_TOKEN)
# Connect to ThingsBoard using default MQTT port and 60 seconds keepalive interval
client.connect(THINGSBOARD_HOST, 1883, 60)
# Register connect callback
client.on_connect = on_connect
# Register publish callback
client.on_publish = on_publish
# Registed publish message callback
client.on_message = on_message
# subscribe to RPC commands from the server - This will let you to control your buzzer.
client.subscribe('v1/devices/me/rpc/request/+')
client.loop_start()
try:
while not client.is_connected():
continue
ports = serial.tools.list_ports.comports()
portsList = []
for onePort in ports:
portsList.append(str(onePort))
print(str(onePort))
val = input("Select Port: COM")
for x in range(0,len(portsList)):
if portsList[x].startswith("COM" + str(val)):
com_port = "COM" + str(val)
print("----------")
ard = serial.Serial(com_port, 9600, timeout=0.1)
while True:
enabled = False
triggered = False
for i in range(2):
line = ard.readline().decode().strip()
if i == 0: triggered = "True" in line
else: enabled = "True" in line
if line: print(line)
alarm_state['enabled'] = enabled
alarm_state['triggered'] = triggered
# Sending humidity, temperature data and buzzer status to ThingsBoard
client.publish('v1/devices/me/telemetry', json.dumps(alarm_state), 1)
next_reading += INTERVAL
sleep_time = next_reading-time.time()
if sleep_time > 0:
time.sleep(sleep_time)
except KeyboardInterrupt:
client.loop_stop()
client.disconnect()
print ("Terminated.")
os._exit(0)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment