DorianH935

Untitled

Jun 24th, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. -- client.lua
  2.  
  3. -- Function to find and open the modem
  4. local function openModem()
  5. local sides = {"left", "right", "top", "bottom", "front", "back"}
  6. for _, side in ipairs(sides) do
  7. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  8. rednet.open(side)
  9. return true
  10. end
  11. end
  12. return false
  13. end
  14.  
  15. -- Open the modem
  16. if not openModem() then
  17. print("No modem found!")
  18. return
  19. end
  20.  
  21. local paired = false
  22.  
  23. -- Function to pair with the host
  24. local function pairWithHost(pairingKey)
  25. rednet.broadcast(pairingKey, "pairing")
  26. local senderId, response, protocol = rednet.receive("pairing_response", 30)
  27. if protocol == "pairing_response" and response == "success" then
  28. paired = true
  29. print("Paired with host.")
  30. else
  31. print("Pairing failed or timed out.")
  32. end
  33. end
  34.  
  35. -- Main loop
  36. while true do
  37. if not paired then
  38. print("Enter pairing key (4-digit hex): ")
  39. local pairingKey = read()
  40. pairWithHost(pairingKey)
  41. else
  42. local senderId, command, protocol = rednet.receive("command")
  43. if protocol == "command" then
  44. local handle = io.popen(command)
  45. local result = handle:read("*a")
  46. handle:close()
  47. rednet.send(senderId, result, "response")
  48. end
  49. end
  50. end
  51.  
Advertisement
Add Comment
Please, Sign In to add comment