-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (67 loc) · 2.04 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from machine import Pin, unique_id
from network import WLAN
from time import sleep
from umqtt.simple import MQTTClient
from umqtt.simple import MQTTException
from sys import exit
from os import stat
from json import loads as json_loads
#
import ubinascii
config_file = "ratoni.json"
# Default alerted state is False
alerted = False
config_data = None
mqtt_user = None
mqtt_pass = None
if stat(config_file):
f = open(config_file, 'r')
config_data = json_loads(f.read(1024))
if "mqtt_broker" in config_data:
broker_address = config_data['mqtt_broker']
else:
broker_address = "iot.eclipse.org"
if "client_id" in config_data:
client_id = config_data['client_id']
else:
client_id = ubinascii.hexlify(unique_id())
if "mqtt_user" in config_data:
mqtt_user = config_data['mqtt_user']
if "mqtt_pass" in config_data:
mqtt_pass = config_data['mqtt_pass']
if "topic" in config_data:
mqtt_topic = config_data['topic']
else:
mqtt_topic = b"ratoni/jail"
if "pin" in config_data:
pin_id = config_data['pin_id']
else:
pin_id = 0
pin = Pin(pin_id, Pin.IN, Pin.PULL_UP)
wlan = WLAN()
while not wlan.isconnected():
print("Waiting for WLAN to be connected...")
sleep(1)
if mqtt_pass and mqtt_user:
c = MQTTClient(client_id, broker_address, user=mqtt_user, password=mqtt_pass)
print("MQTTClient will use user and password")
else:
c = MQTTClient(client_id, broker_address)
print("MQTTClient set to anonymous mode")
try:
c.connect()
except MQTTException:
print("[CRITICAL] Error connecting to MQTT broker... May have a problem with credentials! (MQTTException")
exit(1)
except TypeError:
print("[CRITICAL] Error connecting to MQTT broker... May have a problem with credentials! (TypeError)")
exit(1)
print("MQTTClient seems to be fine, waiting for some mice attack!")
while True:
#print("Pin is:" + str(pin.value()))
if not pin.value():
print("Circuit is closed. We might have a mouse!")
if not alerted:
c.publish(mqtt_topic, "ON")
alerted = True
sleep(2)