-
Notifications
You must be signed in to change notification settings - Fork 1
/
internet_lamp_server.lua
174 lines (147 loc) · 3.97 KB
/
internet_lamp_server.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
require "cord"
LED = require("led")
brd = LED:new("GP0")
local pin = storm.io.D4
print("\ninternet lamp.\n")
brd:flash(4)
ipaddr = storm.os.getipaddr()
ipaddrs = string.format("%02x%02x:%02x%02x:%02x%02x:%02x%02x::%02x%02x:%02x%02x:%02x%02x:%02x%02x",
ipaddr[0],
ipaddr[1],ipaddr[2],ipaddr[3],ipaddr[4],
ipaddr[5],ipaddr[6],ipaddr[7],ipaddr[8],
ipaddr[9],ipaddr[10],ipaddr[11],ipaddr[12],
ipaddr[13],ipaddr[14],ipaddr[15])
print("ip addr", ipaddrs)
print("node id", storm.os.nodeid())
cport = 49160--49152
csock = storm.net.udpsocket(1526,
function(payload, from, port)
payload = storm.mp.unpack(payload)
if payload[1] == "setLight" then
print("$$$")
brd:flash(1)
if payload[2] == true then
lamp_on_p = 1
lamp_on()
print("light on")
else
lamp_on_p = 0
lamp_off()
print("light off")
end
end
end)
servicesock = storm.net.udpsocket(1525,
function(payload, from, port)
end)
local svc_manifest = {id="Team Mac and Cheese",
setLight={s="setBoolean", desc="Turn light on or off"}
}
local msg = storm.mp.pack(svc_manifest)
storm.os.invokePeriodically(5*storm.os.SECOND, function()
print("broadcasting service")
storm.net.sendto(servicesock, msg, "ff02::1", 1525)
end)
storm.io.set_mode(storm.io.OUTPUT, pin)
--local a='0',b='0',c='0'
local lamp_on_p = 0
local strobe_on_p = 0
local delays = {1000, 500, 250, 100, 50, 25, 10}
delay_index = 1
local n = #delays
storm.io.set_mode(storm.io.OUTPUT, storm.io.D4)
function lamp_on ()
storm.io.set(storm.io.HIGH, pin)
end
function lamp_off ()
storm.io.set(storm.io.LOW, pin)
end
function change_delay_g(milliseconds)
delay_g = storm.os.MILLISECOND*milliseconds
print(storm.os.SECOND/delay_g, "times per second (global)")
end
change_delay = change_delay_g
local delay_g
change_delay(delays[1])
local strobe
local count = 0
function x(n)
delay_index = n
end
function strobe_on ()
local delay = delay_g
function change_delay_l(milliseconds)
delay = storm.os.MILLISECOND*milliseconds
print(storm.os.SECOND/delay, "times per second (local)")
end
change_delay = change_delay_l
strobe = cord.new(function ()
local c = count
count = count + 1
while true do
delay = delays[delay_index]*storm.os.MILLISECOND
if not delay then
delay = 500*storm.os.MILLISECOND
end
storm.io.set(storm.io.HIGH, pin)
cord.await(storm.os.invokeLater, delay)
storm.io.set(storm.io.LOW, pin)
cord.await(storm.os.invokeLater, delay)
end
end)
print("strobe handle = ", strobe)
return change
end
function strobe_off ()
print("strobe_off()")
print("strobe = ", strobe)
if strobe then
cord.cancel(strobe)
end
change_delay = change_delay_g
print("end\n")
end
-- create echo server as handler
--server = function()
--[[ ssock = storm.net.udpsocket(7,
function(payload, from, port)
print("$$$")
brd:flash(1)
if payload == "l1" then
lamp_on_p = 1
lamp_on()
print("light on")
elseif payload == "l0" then
lamp_on_p = 0
lamp_off()
print("light off")
elseif payload == "s1" then
print("strobe on")
if strobe_on_p ~= 1 then
strobe_on()
strobe_on_p = 1
end
elseif payload == "s0" then
print("strobe off")
if strobe_on_p ~= 0 then
strobe_off()
strobe_on_p = 0
lamp_off()
end
elseif payload == "f" then
delay_index = (delay_index % n) + 1
--change_delay(delays[delay_index])
print("delay index = ", delay_index)
else
print("Err: invalid command:", payload)
end
print(storm.net.sendto(ssock, payload, from, cport))
brd:flash(1)
end)
end
]]--
--server() -- every node runs the echo server
-- enable a shell
sh = require "stormsh"
sh.start()
cord.enter_loop() -- start event/sleep loop