Skip to content
Snippets Groups Projects
Commit 428f794b authored by ft-Demo's avatar ft-Demo
Browse files

commit by robo pro coding

parents
Branches master
No related tags found
No related merge requests found
{"name":"IoT_Surveillance_Camera_Motion_Detection","mode":"ADVANCED","uuid":"e2679fc1-bbd2-5142-2ec8-ad7e1cab3590"}
\ No newline at end of file
This diff is collapsed.
import base64
import json
import math
import threading
import time
from datetime import datetime
from fischertechnik.mqtt.Client import Client as MqttClient
from fischertechnik.mqtt.Constants import CONTROLLER_ID
from lib.camera import *
from lib.controller import *
snapshot = None
start_time = None
ts = None
threshold = None
surveillance_status = None
surveillance_period = None
def motion_callback(event):
global snapshot, start_time, ts, threshold, surveillance_status, surveillance_period
start_time = time.time()
surveillance_status = 1
motion_detector.add_detection_listener(motion_callback)
def blink():
global snapshot, start_time, ts, threshold, surveillance_status, surveillance_period
while True:
if surveillance_status == 1:
TXT_M_O5_led.set_brightness(512)
time.sleep(0.5)
TXT_M_O5_led.set_brightness(0)
time.sleep(0.5)
def image_callback(event):
global snapshot, start_time, ts, threshold, surveillance_status, surveillance_period
snapshot = "data:image/jpeg;base64," + base64.b64encode(event.value).decode("utf-8")
TXT_M_USB1_1_camera.add_change_listener("image", image_callback)
def log_data():
global snapshot, start_time, ts, threshold, surveillance_status, surveillance_period
print(''.join([str(x) for x in [surveillance_status, ' (', round(time.time() - start_time), 's)']]))
def datetimestamp():
global snapshot, start_time, ts, threshold, surveillance_status, surveillance_period
ts = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
return ts
TXT_M_USB1_2_microphone.start()
snapshot = ''
threshold = 70
surveillance_period = 60
surveillance_status = 0
start_time = time.time()
threading.Thread(target=blink, daemon=True).start()
MqttClient.getInstance().connect()
while True:
if (MqttClient.getInstance().is_connected()):
break
time.sleep(0.010)
while True:
if TXT_M_USB1_2_microphone.get_volume() > threshold:
start_time = time.time()
surveillance_status = 1
if surveillance_status == 1 and time.time() - start_time > surveillance_period:
surveillance_status = 0
if surveillance_status == 1:
MqttClient.getInstance().publish("/j1/txt/" + str(CONTROLLER_ID) +"/i/cam", '{{"ts":"{}","data":"{}"}}'.format(datetimestamp(), snapshot))
log_data()
time.sleep(1)
# auto generated content from camera configuration
from lib.controller import *
import fischertechnik.factories as txt_factory
TXT_M_USB1_1_camera.set_rotate(False)
TXT_M_USB1_1_camera.set_height(240)
TXT_M_USB1_1_camera.set_width(320)
TXT_M_USB1_1_camera.set_fps(15)
TXT_M_USB1_1_camera.start()
motion_detector = txt_factory.camera_factory.create_motion_detector(0, 0, 320, 240, 1)
TXT_M_USB1_1_camera.add_detector(motion_detector)
<xml type="camera" version="2"><item id="2" class="MotionDetector"><name>motion_detector</name><tolerance>1</tolerance><geometry><x>0</x><y>0</y><width>320</width><height>240</height></geometry></item></xml>
\ No newline at end of file
<xml xmlns="https://developers.google.com/blockly/xml" version="12">
<block type="txt4_controller" id="EI-]CpP2Xt@A:ytm?6={" x="-47" y="-609">
<mutation>
<arg type="Input" name="Input-0" value="I1"/>
<arg type="Input" name="Input-1" value="I2"/>
<arg type="Input" name="Input-2" value="I3"/>
<arg type="Input" name="Input-3" value="-"/>
<arg type="Output" name="Output-0" value="O5"/>
<arg type="Output" name="Output-1" value="-"/>
<arg type="Motor" name="Motor-0" value="M1"/>
<arg type="Motor" name="Motor-1" value="M2"/>
<arg type="Motor" name="Motor-2" value="-"/>
<arg type="Servomotor" name="Servomotor-0" value="-"/>
<arg type="Counter" name="Counter-0" value="C1"/>
<arg type="Counter" name="Counter-1" value="C2"/>
<arg type="Counter" name="Counter-2" value="-"/>
<arg type="I2C" name="I2C-0" value="I2C_1"/>
<arg type="I2C" name="I2C-1" value="-"/>
<arg type="USB" name="USB-0" value="USB1_1"/>
<arg type="USB" name="USB-1" value="USB1_2"/>
</mutation>
<field name="mode">0</field>
<field name="controller_name">TXT</field>
<field name="Input-0">I1</field>
<field name="Input-1">I2</field>
<field name="Input-2">I3</field>
<field name="Input-3">-</field>
<field name="Output-0">O5</field>
<field name="Output-1">-</field>
<field name="Motor-0">M1</field>
<field name="Motor-1">M2</field>
<field name="Motor-2">-</field>
<field name="Servomotor-0">-</field>
<field name="Counter-0">C1</field>
<field name="Counter-1">C2</field>
<field name="Counter-2">-</field>
<field name="I2C-0">I2C_1</field>
<field name="I2C-1">-</field>
<field name="USB-0">USB1_1</field>
<field name="USB-1">USB1_2</field>
<value name="Input-0">
<block type="mini_switch" id="ub;Gt!W(z5ygGJlL94Ji"/>
</value>
<value name="Input-1">
<block type="mini_switch" id="/pj_m.NVO6[led~!vZ{G"/>
</value>
<value name="Output-0">
<block type="led" id="0u{wddYS47RRLT1S:rNx"/>
</value>
<value name="Motor-0">
<block type="encodermotor" id="[jKNU%+@_!L1|[5fdYW7"/>
</value>
<value name="Motor-1">
<block type="encodermotor" id="T[fZxv3qalB[:KXZ*k~3"/>
</value>
<value name="Counter-0">
<block type="motor_step_counter" id="SlfO$BTMaHVBebfCIsDp">
<field name="instance_name">M1</field>
</block>
</value>
<value name="Counter-1">
<block type="motor_step_counter" id="^E[rf={,fU?klnGFn4}d">
<field name="instance_name">M2</field>
</block>
</value>
<value name="USB-0">
<block type="camera" id="pZV@vPJde2@.Q{1kCLfi"/>
</value>
<value name="USB-1">
<block type="microphone" id=")y6.P-%_I6tFP8{uSZtE"/>
</value>
</block>
</xml>
\ No newline at end of file
import fischertechnik.factories as txt_factory
txt_factory.init()
txt_factory.init_input_factory()
txt_factory.init_output_factory()
txt_factory.init_motor_factory()
txt_factory.init_counter_factory()
txt_factory.init_usb_factory()
txt_factory.init_camera_factory()
TXT_M = txt_factory.controller_factory.create_graphical_controller()
TXT_M_I1_mini_switch = txt_factory.input_factory.create_mini_switch(TXT_M, 1)
TXT_M_I2_mini_switch = txt_factory.input_factory.create_mini_switch(TXT_M, 2)
TXT_M_O5_led = txt_factory.output_factory.create_led(TXT_M, 5)
TXT_M_M1_encodermotor = txt_factory.motor_factory.create_encodermotor(TXT_M, 1)
TXT_M_M2_encodermotor = txt_factory.motor_factory.create_encodermotor(TXT_M, 2)
TXT_M_C1_motor_step_counter = txt_factory.counter_factory.create_encodermotor_counter(TXT_M, 1)
TXT_M_C1_motor_step_counter.set_motor(TXT_M_M1_encodermotor)
TXT_M_C2_motor_step_counter = txt_factory.counter_factory.create_encodermotor_counter(TXT_M, 2)
TXT_M_C2_motor_step_counter.set_motor(TXT_M_M2_encodermotor)
TXT_M_USB1_1_camera = txt_factory.usb_factory.create_camera(TXT_M, 1)
TXT_M_USB1_2_microphone = txt_factory.usb_factory.create_microphone(TXT_M, 2)
txt_factory.initialized()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment