-
Notifications
You must be signed in to change notification settings - Fork 1
/
relay.py
48 lines (40 loc) · 1.1 KB
/
relay.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
# Micropython PIR Switch Control
# Erni Tron ernitron@gmail.com
# Copyright (c) 2016
import time
from machine import Pin
# The Relay Switch Class
class Relay():
# D8 GPIO15 Pin(15)
# D5 GPIO14 Pin(14)
# D0 GPIO0 Pin(0)
def __init__(self, p=14, sensor='relay', place='nowhere', server=''):
self.pin = Pin(p, Pin.OUT)
self.pin.value(0)
self.status = 0
self.count = 0
self.sensor = sensor
self.place = place
self.server = server
def get(self):
return self.status
def set(self, position):
if position != self.status:
self.pin.value(position)
self.status = position
self.count += 1
return (self.status, self.count)
def toggle(self):
self.set(1 - self.status)
def status(self):
T = {}
T['place'] = self.place
T['server'] = self.server
T['switch'] = self.status
T['temp'] = str(self.status)
T['count'] = self.count
T['sensor'] = self.sensor
T['date'] = time.time()
return T
# Initialize
relay = None