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

Sending a rednet message with hostname lookup

Started by empirebuilder1, 08 April 2015 - 05:46 PM
empirebuilder1 #1
Posted 08 April 2015 - 07:46 PM
CC Noob here, while I'm sorta-kinda-OK with the code, this one's bugging me.

I am trying to set up a monitoring system for my factory. To start with, it will be using OpenCCSensors to monitor energy levels, and add coal to generators as needed. The computer monitoring energy levels will send those levels via rednet to a central PC, which will then display at-a-glance info. In the interest of portability since I may want to port this to a different server, I registered the hostname of both computers using rednet.host. The energy-monitoring computer then looks up the hostname of the central computer, and tries to send energy data every 5 seconds. However, therein lies my problem.

I can't figure out how to take the ID number generated by a lookup event - i.e, 5 - and put it into a rednet.send event. It wants a number - not a variable. Should I consider using the Modem API instead? I can hardcode the computer ID's in for now, but should I ever put it in a different place, it'll be hard and rather annoying to replace 20-25 variables on multiple PC's.
SquidDev #2
Posted 08 April 2015 - 07:58 PM
You should be able to do something like:


rednet.open("top")

local energyProviders = {rednet.lookup("energy")}

for _, id in pairs(energyProviders) do
	rednet.send(id, "getEnergy", "energy") -- Last param isn''t needed but useful
	print(rednet.receive("energy", 1)) -- Timeout after a while
end
Edited on 08 April 2015 - 05:58 PM
KingofGamesYami #3
Posted 08 April 2015 - 08:24 PM
It wants a number - not a variable.

I don't understand what you mean, variables can be numbers.
Lyqyd #4
Posted 08 April 2015 - 10:13 PM
Please post your code so we can see what's going on.
Square789 #5
Posted 09 April 2015 - 09:49 AM
It wants a number - not a variable.

I don't understand what you mean, variables can be numbers.
So, I don't unsterstand what you mean either, but
maybe you can use this function:

tonumber()
EDIT:
This changes a string like "1234554321" to 1234554321 so you have a number.
Edited on 09 April 2015 - 07:50 AM
Bomb Bloke #6
Posted 09 April 2015 - 10:56 AM
rednet.lookup() already returns numbers, so you can store the results directly into a variable and pass that straight to rednet.send() without needing to tonumber() them. The only reason one might have to do something like that is they did something in-between to mess with the type.

So yeah, assuming SquidDev's example doesn't help, it's not practical to comment much further without eg a pastebin link.