-
Notifications
You must be signed in to change notification settings - Fork 37
/
aktor.lua
149 lines (133 loc) · 3.53 KB
/
aktor.lua
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
print("start v.1")
manuel = 0
-------------------------------------------------------------------------------------------------function mqtt_start()
mqtt = mqtt.Client("WaschKueche", 0, "", "")
mqtt:on("connect", function(con)
print ("connected")
end)
mqtt:on("offline", function(con)
print ("reconnecting...")
print(node.heap())
tmr.alarm(2, 2000, 0, function()
mqtt:connect("192.168.0.43", 1883, 0)
end)
end)
mqtt:connect("192.168.0.43", 1883, 0, function(conn)
print("connected")
-- subscribe topic with qos = 0
mqtt:subscribe("/home/#",0, function(conn)
-- publish a message with data = my_message, QoS = 0, retain = 0
mqtt:publish("/home/light", "WaschKueche", 0, 0, function(conn)
print("sent")
end)
end)
end)
mqtt:on("message", function(conn, topic, data)
print(topic..":")
if data ~= nil then
print(data)
if(topic == "/home/light/keller") then
if (data == "ON") then
gpio.write(4, gpio.LOW)
end
if (data == "OFF") then
gpio.write(4, gpio.HIGH)
end
end
end
end)
-- wlan verbinden
wifi.sta.config("SSID", "PASSWORD")
print("wait")
-----------------------------------------------------
-- Code fuer befehle ueber TCP empfangen
-----------------------------------------------------
ipshow = 0 -- damit nach empfangen der ip tcp server startet
push = 0
change = 5
licht = 0 -- 0 = aus
tmr.alarm(0, 150, 1, function()
if wifi.sta.getip() == nil then
--print("wait\n")
elseif (ipshow == 0) then
print("SS Running v0.4")
print(wifi.sta.getip())
ipshow = 1
print("mqtt start")
--mqtt_start()
end
-----------------------------------------------------
-- Config fuer schalter !
-----------------------------------------------------
--schalterx(pin_relay,pin_eingang,"lisa1")
gpio.mode(7, gpio.HIGH)
gpio.write(7, gpio.HIGH)
schalter1 = gpio.read(7)
relay = 4
lampe_an = "\n status:"..gpio.read(relay)
lampe_visu = gpio.read(relay)
sid = "Lampe SZ Test"
--gpio.mode(relay,gpio.OUTPUT)
--print(schalter1)
if (schalter1 == 0 and push ~= schalter1) then
if (licht == 0) then
print("aus einschalten schalter1"..lampe_an.."\n")
licht = 1
make = 0
manuel = 1
xyc = 0
licht_value = nil
elseif (licht == 1) then
print("an ausschalten schalter1"..lampe_an.."\n")
licht = 0
make = 1
manuel = 1
xyc = 0
licht_value = nil
end
push = 0
elseif (schalter1 == 1 and push ~= schalter1) then
if (licht == 0) then
print("aus einschalten schalter1"..lampe_an.."\n")
licht = 1
manuel = 1
make = 0
xyc = 0
licht_value = nil
elseif (licht == 1) then
print("an ausschalten schalter1"..lampe_an.."\n")
licht = 0
make = 1
manuel = 1
xyc = 0
licht_value = nil
end
push = 1
end
end)
xyc = 0
tmr.alarm(2, 50, 1, function()
relay = 4
if(licht == 0 and xyc == 0) then
xyc = 1
--gpio.write(relay3,gpio.HIGH)
if (manuel == 1) then
--print("debug 203b manuel" ..lampe1.. " xyc: " ..xyc1)
manuel = 0
mqtt:publish("/home/light/keller","OFF", 0, 0, function(conn)
print("done")
end)
end
elseif (licht == 1 and xyc == 0) then
xyc = 1
--print("debug 214a" ..lampe1.. " xyc: " ..xyc1)
if (manuel == 1) then
manuel = 0
--print("debug 214b manuel" ..lampe1.. " xyc: " ..xyc1)
mqtt:publish("/home/light/keller","ON", 0, 0, function(conn)
print("done")
end)
end
--gpio.write(relay3, gpio.LOW)
end
end)