This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
plazter's profile picture

Need code and how to put it!

Started by plazter, 12 March 2012 - 06:51 PM
plazter #1
Posted 12 March 2012 - 07:51 PM
Hello peepz!
i've hitted a wall..
i got 2 computer wired up with rednet(wireless)
and want to use my "Main" Computer to be able to work as a "switch" on my greenhouse's light and door.
So if some 1 of you guys would be so gentle to give me an explanation on how to set them up to work that way and a code i would
really appreciate it!!

Best Regards Plazter
Liraal #2
Posted 12 March 2012 - 08:01 PM
function receiver()
while true do
local _,b=rednet.receive()
local tmp=loadstring( b )
tmp()
end
end

and on the sending terminal type

rednet.send(id, command) e.g. rednet.send(11,"rs.setOutput(\"back\", true")
pieiscool32 #3
Posted 12 March 2012 - 08:40 PM
Hello peepz!
i've hitted a wall..
i got 2 computer wired up with rednet(wireless)
and want to use my "Main" Computer to be able to work as a "switch" on my greenhouse's light and door.
So if some 1 of you guys would be so gentle to give me an explanation on how to set them up to work that way and a code i would
really appreciate it!!

Best Regards Plazter

Can you use proper english please, because what looks better? this:

H3y P3eP1ez! M3h N33Dz C0d3! W&Nt N0w Plz.

or this:

Hey, I am having a problem, can you help me with my code. (link to code) Thank you for your help.

So for the sake of me having to read a mix of numbers and letters, please use spell check and proper english, thank you.
Jan #4
Posted 12 March 2012 - 09:09 PM
while true do
local _,b=rednet.receive()
local tmp=loadstring( b )
tmp()
end
end

You need to save Liraal's program on the receiver as some name, and run it.
What it basicly does, is waiting for a rednet message, and storing it in 'b'
After that the script converts the string, into lua code, and saves that function as tmp
Then it executes tmp()
(tmp containes the code the turtle received)


So basicly the turtle now executes everything it receives, also lua commands like 'rs.setOutput("front",true)'

(NOTE: do not forget to activate the two modems by using rednet.open("right") for example.)

Now you need to send the commands. in the controllercomputer, type 'lua'

Then open the port if you haven't already.

> rednet.broadcast("Some command in a string")
i. e.
> rednet.broadcast("rs.setOuput('front',true)")

Done!

(there a multiple ways to do this, and this was an advanced way)
Liraal #5
Posted 12 March 2012 - 09:14 PM
This is the overcomplicated advanced version: :mellow:/>/>

(But it still requires you to open the ports, for how to see above)

--sender program
function yN()
local n=1
while true do
local x, y=term.getCursorPos()
term.clearLine()
if n==1 then write("[YES]   NO") else write (" YES   [NO]") end
term.setCursorPos(x, y)
a, b=os.pullEvent()
while a~="key" do a, b=os.pullEvent() end
if b==203 and n==2 then n=1 end
if b==205 and n==1 then n=2 end
if b==28 then print("") break end
end
if n==1 then return true end
if n==2 then return false end
return false
end
function sender()
while true do
print("Type your command")
local a=read()
if a=="exit" then print("Quitting...") break end
print("Do you want to send to specific ID?")
if yN() then
local b=read()
rednet.send(a,;)/>/>
else
rednet.broadcast(a)
end
print("your command has been sent")
end
return true
end

And Jan, thanks for explaining this.
Jan #6
Posted 12 March 2012 - 09:22 PM
Here is a simpler way (haven't tested yet)


The server program is in this case the computer at the door/light
all – are comments.

Program: server
rednet.open("right") – opens the modem on the right side. you may use another side if you wish.
while true do – Keep repeating this while true (forever)
from,message = rednet.receive() – wait for message and store the message and receiver
if message=="on" then
print("Putting lights on")
rs.setOutput("front",true) – You can change the side. rs stands for redstone
elseif message=="off" then
print("Putting lights off")
rs.setOutput("front",false)
else
print("Unknown command received") –
end
end – the end of the while loop

On the controller program make these two programs:

Program lighton
rednet.open("right")
rednet.broadcast("on") – sends a message to all computers that are in 'receiving'-mode

Program lightoff
rednet.open("right")
rednet.broadcast("off") – sends off instead of off

If there are any errors, please post them, because I haven;t tested yet ;)/>/>

EDIT: Ninja :mellow:/>/>
Liraal #7
Posted 12 March 2012 - 09:24 PM
There always is a simpler way, my goal is to find the harder :mellow:/>/>
plazter #8
Posted 12 March 2012 - 09:30 PM
function receiver()
while true do
local _,b=rednet.receive()
local tmp=loadstring( b )
tmp()
end
end

and on the sending terminal type

rednet.send(id, command) e.g. rednet.send(11,"rs.setOutput("back", true")

Now that i made a program that i called receiver its keep saying false? oO
Liraal #9
Posted 12 March 2012 - 09:33 PM
because that is actually a function, if you make a program, you need to add at the end 'receiver()' and it should work.
Jan #10
Posted 12 March 2012 - 09:33 PM
There always is a simpler way, my goal is to find the harder :mellow:/>/>
Yea I like your program. You can litterally do everthing with the target, and you never have to 'update' the script of the receiver to support more commands or something.
plazter #11
Posted 12 March 2012 - 09:46 PM
gna try ninja's or what hes called :mellow:/>/>
- tho will i need the "program lightoff" in my code? xD