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

add comments

parent a6e33339
No related branches found
No related tags found
No related merge requests found
#include <Wire.h>
#include "rgb_lcd.h"
#define PIR_MOTION_SENSOR_1 5
const int buttonPin = 6;
const int buzzerPin = 8;
#define PIR_MOTION_SENSOR_1 5 // the digital pin of the PIR sensor
const int buzzerPin = 8; // digital pin number of the buzzer
int MotionState1; // the state of the Motion SENSOR (PIR)
int buzzerState = LOW;
int buttonState = LOW;
int buzzerState = LOW; // the state of the buzzer
bool alarmEnabled = false;
bool alarmEnabled = false;
int triggerEnabled = LOW;
rgb_lcd lcd;
rgb_lcd lcd; // initialise lcd screen
void setup() {
// setup pin modes
......@@ -78,10 +75,9 @@ void loop() {
Serial.println("Triggered: False");
}
// write to buzzer and button outputs
// write to buzzer outputs
if ((millis()/500) % 3 != 0) {
digitalWrite(buzzerPin, triggerEnabled);
// digitalWrite(buttonPin, triggerEnabled);
} else {
digitalWrite(buzzerPin, LOW);
}
......
......@@ -5,84 +5,6 @@
"mobileOrder": null,
"configuration": {
"widgets": {
"5c9bc29a-87d2-0033-49c6-f57607cd08e3": {
"isSystemType": true,
"bundleAlias": "cards",
"typeAlias": "entities_table",
"type": "latest",
"title": "Entities table",
"sizeX": 15,
"sizeY": 13,
"row": 0,
"col": 2,
"config": {
"timewindow": {
"realtime": {
"interval": 1000,
"timewindowMs": 86400000
},
"aggregation": {
"type": "NONE",
"limit": 200
}
},
"showTitle": true,
"backgroundColor": "rgb(255, 255, 255)",
"color": "rgba(0, 0, 0, 0.87)",
"padding": "4px",
"settings": {
"enableSearch": true,
"displayPagination": true,
"defaultPageSize": 10,
"defaultSortOrder": "entityName",
"displayEntityName": true,
"displayEntityType": true,
"enableSelectColumnDisplay": true,
"enableStickyHeader": true,
"enableStickyAction": true,
"reserveSpaceForHiddenAction": "true",
"displayEntityLabel": false,
"useRowStyleFunction": false
},
"title": "Entities table",
"dropShadow": true,
"enableFullscreen": true,
"titleStyle": {
"fontSize": "16px",
"fontWeight": 400,
"padding": "5px 10px 5px 10px"
},
"useDashboardTimewindow": false,
"showLegend": false,
"datasources": [
{
"type": "entity",
"name": null,
"entityAliasId": "9d73b2e8-7028-b54e-6378-f90b93b4ff53",
"filterId": null,
"dataKeys": [
{
"name": "triggered",
"label": "triggered",
"type": "timeseries",
"color": "#2196f3",
"settings": {},
"_hash": 0.6446102890820327
},
{
"name": "enabled",
"label": "enabled",
"type": "timeseries",
"color": "#4caf50",
"settings": {},
"_hash": 0.58092539334787
}
]
}
]
},
"id": "5c9bc29a-87d2-0033-49c6-f57607cd08e3"
},
"264b7c41-ccc2-6e17-de39-f27eebb0da75": {
"isSystemType": true,
"bundleAlias": "control_widgets",
......@@ -105,7 +27,7 @@
"getValueMethod": "getValue",
"setValueMethod": "setValue",
"showOnOffLabels": true,
"title": "Burglar System Enabled",
"title": "Burglar System Armed",
"retrieveValueMethod": "timeseries",
"parseValueFunction": "return data ? true : false;",
"convertValueFunction": "return {enabled: value};",
......@@ -283,29 +205,23 @@
"layouts": {
"main": {
"widgets": {
"5c9bc29a-87d2-0033-49c6-f57607cd08e3": {
"sizeX": 7,
"sizeY": 6,
"row": 0,
"col": 0
},
"264b7c41-ccc2-6e17-de39-f27eebb0da75": {
"sizeX": 5,
"sizeY": 2,
"row": 3,
"col": 7
"col": 0
},
"60006959-405c-1f3b-8e24-8a2b92492df5": {
"sizeX": 3,
"sizeY": 2,
"row": 3,
"col": 12
"col": 5
},
"c63ef02b-bea2-768b-b0af-67de6e8a6f11": {
"sizeX": 8,
"sizeY": 3,
"row": 0,
"col": 7
"col": 0
}
},
"gridSettings": {
......
......@@ -8,6 +8,7 @@ import serial.tools.list_ports
THINGSBOARD_HOST = 'thingsboard.cs.cf.ac.uk'
ACCESS_TOKEN = 'Xh4fjhD3N8xsnc7QvlVx' # <== Insert your own access token here.
# Initialise global variables
global ard
data_changed = True
......@@ -19,24 +20,30 @@ def on_publish(client,userdata,result):
def on_message(client, userdata, msg):
global data_changed
# Handle nothing if Arduino has not been initialised
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']
# Logic if message includes "enabled" data
if 'enabled' in params:
if alarm_state['enabled'] != params['enabled']:
data_changed = True
alarm_state['enabled'] = params['enabled']
print(f"Testing enabled {params['enabled']}")
# Send message to Arduino
ard.write(f"Enabled: {params['enabled']}".encode('utf-8'))
# Set triggered to false if enabled is set to false
if not params['enabled']:
ard.write(f"_Triggered: False".encode('utf-8'))
alarm_state['triggered'] = False
# Logic if message includes "triggered" data
if 'triggered' in params:
# Send message to Arduino
ard.write(f"Triggered: {params['triggered']}".encode('utf-8'))
# The callback for when the client receives a CONNACK response from the server.
......@@ -65,42 +72,51 @@ client.subscribe('v1/devices/me/rpc/request/+')
client.loop_start()
try:
# Wait till the program has connected to the ThingsBoard server
while not client.is_connected():
continue
# Find serial ports
ports = serial.tools.list_ports.comports()
portsList = []
for onePort in ports:
portsList.append(str(onePort))
print(str(onePort))
# Instruct user to choose the port the Arduino is connected to
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("----------")
# Connect to Arduino
ard = serial.Serial(com_port, 9600, timeout=0.1)
# Set Initial Variables to Arduino (in the case they have
# persisted since the last use)
ard.write("Triggered: False".encode('utf-8'))
ard.write("Enabled: False".encode('utf-8'))
while True:
triggered = alarm_state['triggered']
# Read output from Arduino
line = ard.readline().decode().strip()
print(f"ReadLine: {line}")
if 'Triggered' in line:
new_triggered = "True" in line
# Check if data has actually changed
if triggered != new_triggered:
data_changed = True
# Set data to new data
alarm_state['triggered'] = new_triggered
print (f"Triggered: {alarm_state['triggered']}")
print (f"Enabled: {alarm_state['enabled']}")
# Sending humidity, temperature data and buzzer status to ThingsBoard
# If data has changed, publish data to ThingsBoard server
if data_changed:
client.publish('v1/devices/me/telemetry', json.dumps(alarm_state), 1)
# Reset data and wait interval
data_changed = False
next_reading += INTERVAL
sleep_time = next_reading-time.time()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment