Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- numeric_relays.lua
- -- 1) Map the incoming number string to a specific relay and side
- local numberToRelay = {
- ["1"] = { relayName = "redstone_relay_0", side = "front" },
- ["2"] = { relayName = "redstone_relay_1", side = "front" },
- ["3"] = { relayName = "redstone_relay_2", side = "front" },
- ["4"] = { relayName = "redstone_relay_3", side = "front" },
- ["5"] = { relayName = "redstone_relay_4", side = "front" },
- ["6"] = { relayName = "redstone_relay_5", side = "front" }
- }
- -- (Adjust these names and sides to match your actual setup)
- -- 2) Turn off all relays at startup
- local function turnOffAll()
- for _, info in pairs(numberToRelay) do
- local relay = peripheral.wrap(info.relayName)
- if relay then
- relay.setOutput(info.side, false)
- end
- end
- end
- turnOffAll()
- -- Keep track of the currently active number so we can turn it off
- local currentNumber = nil
- local function activateNumber(newNumber)
- -- If there's a current number active, turn that off first
- if currentNumber and numberToRelay[currentNumber] then
- local oldInfo = numberToRelay[currentNumber]
- local oldRelay = peripheral.wrap(oldInfo.relayName)
- if oldRelay then
- oldRelay.setOutput(oldInfo.side, false)
- end
- end
- -- Turn on the newly received number's relay
- local info = numberToRelay[newNumber]
- if info then
- local relay = peripheral.wrap(info.relayName)
- if relay then
- relay.setOutput(info.side, true)
- currentNumber = newNumber
- print("Activated number: "..newNumber)
- else
- print("No relay found for: "..newNumber)
- end
- else
- print("Unknown number: "..newNumber)
- end
- end
- -- 3) Connect to the Node.js WebSocket server
- local wsUrl = "ws://kev.unoriginal.studio:8080"
- print("Connecting to "..wsUrl)
- http.websocketAsync(wsUrl)
- -- 4) Event loop for handling WebSocket messages
- while true do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "websocket_success" then
- local ws = p2
- print("Connected to the server!")
- -- Optionally send a message from CC
- ws.send("Hello from CC!")
- elseif event == "websocket_failure" then
- print("Failed to connect:", p2)
- break
- elseif event == "websocket_message" then
- -- p2 = ws handle, p3 = the text message
- local incoming = p3 -- e.g. "1", "2", ...
- print("Received: "..incoming)
- activateNumber(incoming) -- pass it directly, so "1" stays "1" (string)
- elseif event == "websocket_closed" then
- print("WebSocket closed.")
- break
- end
- end
- print("Exiting program.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement