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

Using CCsensors and Wireless Modem

Started by gustavowizard, 08 June 2014 - 10:24 PM
gustavowizard #1
Posted 09 June 2014 - 12:26 AM
Hello guys,

Im trying to set up a simple programs here to make 1 computer with a CCsensor (Tank) + Wireless Modem to get Amount data from tanks near and send to another computer, far away (less than 64 blocks for modem reach) that will display it on a monitor. Here what i did so far, the send code seams ok but i dont know if i made the receive code right, its just showing crazy numbers on the monitor instead of the Amount.


SEND PROGRAM:


os.loadAPI("ocs/apis/sensor")
local modem = peripheral.wrap("bottom")
local sendOnChannel = 1
local replyChannel = 2
prox = sensor.wrap("top")
while true do
term.setCursorPos(1,3)
moreDetails = prox.getTargetDetails("3,0,0")
print(moreDetails.Tanks[1].Amount)
term.setCursorPos(1,4)
moreDetails = prox.getTargetDetails("-3,0,0")
print(moreDetails.Tanks[1].Amount)
modem.transmit(2,1,moreDetails.Tanks[1].Amount)
end


RECEIVE/DISPLAY PROGRAM:


local monitor = peripheral.wrap("top")
local modem = peripheral.wrap("back")
modem.open(2)
modem.open(1)
monitor.clear()
while true do
monitor.setCursorPos(1,1)
local messageArguments ={os.pullEvent("modem_message")}
monitor.write(messageArguments)
end


http://pastebin.com/ZmS9NTmE

http://pastebin.com/N1q3sDVD

i also tried to put (moreDetails.Tanks[1].Amount) instead of (messageArguments), dont work too
Lyqyd #2
Posted 09 June 2014 - 12:54 AM
You're capturing the event in a table, then printing the table. It's probably printing something like "table: hexdigits". You need to actually index the value from the messageArguments that you want. I believe printing messageArguments[5] will do what you're looking for.
gustavowizard #3
Posted 09 June 2014 - 04:34 AM
You're capturing the event in a table, then printing the table. It's probably printing something like "table: hexdigits". You need to actually index the value from the messageArguments that you want. I believe printing messageArguments[5] will do what you're looking for.

lol it worked but from where you put that '5' out? what does it means?

thanks
Lyqyd #4
Posted 09 June 2014 - 04:37 AM
You're putting the arguments from a modem message event into a table, so you need to index out the message argument. You look up the details for that event and see that the fourth returned value is the message. Since the event name takes up an additional value at the beginning, you add one, so the value you want is at index 5.
gustavowizard #5
Posted 09 June 2014 - 05:07 AM
cool it works but when i use a 3rd computer it wont work, i guess i need to use rednet for more than 2 computers right?
Lyqyd #6
Posted 09 June 2014 - 05:12 AM
You'd have to post the code you're using on all three, but you should still be able to use the raw modem calls just fine.
gustavowizard #7
Posted 09 June 2014 - 08:26 PM
yes, but how the receive computer will distinguish bethween the messages of computer 1 and 2 that are sending? that code i cant do but i know that i will have to change the lines here right?

local messageArguments ={os.pullEvent("modem_message")}
monitor.write(messageArguments)
Bomb Bloke #8
Posted 10 June 2014 - 02:19 AM
The rednet API has a fairly sensible method of determining where messages came from: Loosely put, each system only expects to receive messages on a channel equal to its own ID number, and when sending, specifies that ID as the expected reply channel. I tend to replicate this when using the modem API.

Rather than hard-coding the channels into my scripts, I generally have them start by sending a broadcast which only my server(s) will respond to (eg "Hello Bomb's server!"). The clients can inspect the responses to get the server's actual ID number(s), which they then use directly from there. ComputerCraft 1.6 added an "official" way of doing this under rednet with its lookup system.

However, a rather more trivial method of dealing with the matter would be to have the clients send a table to the server instead of just a single bit of data. Eg:

modem.transmit(2,1,{os.getComputerID(), moreDetails.Tanks[1].Amount})

The server can then do something like this:

local messageArguments ={os.pullEvent("modem_message")}
monitor.write("Client "..messageArguments[5][1].." specified an amount of "..messageArguments[5][2].."." )

… where "messageArguments[5]" is the fifth bit of info in the modem_message event, that being the table which was sent. messageArguments[5][1] is hence the first index of that table - the ID - and messageArguments[5][2] is the tank amount.
Edited on 15 June 2014 - 01:59 AM
gustavowizard #9
Posted 10 June 2014 - 09:48 PM
im gonna try this code in a sec… thank you both again
Edited on 10 June 2014 - 07:50 PM
gustavowizard #10
Posted 14 June 2014 - 09:35 PM
its not working for me im trying with rednet now, but with rednet i cant make it display the

moreDetails.Tanks[1].Amount
Bomb Bloke #11
Posted 15 June 2014 - 04:00 AM
Can't really comment on what you're doing wrong without seeing the code you're currently trying to use. You've not updated the content behind your Pastebin links.