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

WIFI modem detector - finds and opens modem

Started by BigSHinyToys, 29 March 2012 - 06:00 AM
BigSHinyToys #1
Posted 29 March 2012 - 08:00 AM
This code will open find a modem and open it.

add the full code to the top of your program and it will work or just use the function it is up to you.

line three can be substituted with
local listOfSides = { "top" , "bottom" , "front" , "left" , "right" , "back" }
but not recommended
Spoiler

-- wifi modem test --
local function openRednet()
local listOfSides = rs.getSides()
local listofPossibles = {}
local counter1 = 0
while true do
  counter1 = counter1 +1

  if peripheral.isPresent(tostring(listOfSides[counter1])) and peripheral.getType(listOfSides[counter1]) == "modem" then
   table.insert(listofPossibles,tostring(listOfSides[counter1]))
  end

  if counter1 == 6 and table.maxn(listofPossibles) == 0 then
   print("no wifi present")
   return nil
  end

  if counter1 == 6 and table.maxn(listofPossibles) ~= 0 then
   rednet.open(listofPossibles[1])
   return listofPossibles[1]
  end
end
end
modemOn = openRednet()
if modemOn == nil then
print("No WIFI Modem")
print("Will shutdown in 3 seconds")
sleep(3)
os.shutdown()
else
print("Opened wifi on "..modemOn.." side")
end
-- wifi modem test end --
the section bellow this line (modemOn = openRednet()) shows a possible way to use this function.
Ian-Moone #2
Posted 29 March 2012 - 08:10 AM
usefull :o/>/>
thanks
Wolvan #3
Posted 29 March 2012 - 08:33 AM
Made something like that in my rna API. Mind if I put in your code instead of mine?
Does this open bundled cable rednet too?
BigSHinyToys #4
Posted 29 March 2012 - 08:57 AM
it only works for wifi not cable

edit if it is useful to you please use it no need to credit me either
immibis #5
Posted 29 March 2012 - 11:47 AM
Really?
Also you missed "back".

-- Untested code
local function openRednet()
  for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
    if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
      rednet.open(side) 
      return side
    end
  end
  print("no wifi present")
end

modemSide = openRednet()
if modemSide == nil then
  print("No modem connected")
else
  print("Opened modem on "..modemSide)
end

Espen #6
Posted 29 March 2012 - 12:28 PM
@immibis
Come on, be nice. :o/>/>
Not everyone is on the same level.

On another note:
since one can never be sure if internal names might change between CC versions, it might be better to iterate over all sides using rs.getSides().
E.g. instead of …
for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do 
… it might be better to use this instead:
for _,side in ipairs(rs.getSides()) do 
BigSHinyToys #7
Posted 29 March 2012 - 01:58 PM
Really?
Also you missed "back".
– snipped code –

I had seen a lot of programs that say place WI FI modem on "left" or it wont work and already having written this thought it would be useful. So if you would like to post your code in your own topic GO ahead or else don't knock a noob that tried to help.
thank you for finding that problem. I have corrected it by adding "back"
@immibis
Come on, be nice. :o/>/>
Not everyone is on the same level.

On another note:
since one can never be sure if internal names might change between CC versions, it might be better to iterate over all sides using rs.getSides().
E.g. instead of …
for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do 
… it might be better to use this instead:
for _,side in ipairs(rs.getSides()) do 
i don't quite get what a Redstone function rs.getSides() has to do with rednet or side names? could you please explain how that

works.
Wolvan #8
Posted 29 March 2012 - 02:41 PM
@immibis
Come on, be nice. :o/>/>
Not everyone is on the same level.

On another note:
since one can never be sure if internal names might change between CC versions, it might be better to iterate over all sides using rs.getSides().
E.g. instead of …
for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do 
… it might be better to use this instead:
for _,side in ipairs(rs.getSides()) do 
i don't quite get what a Redstone function rs.getSides() has to do with rednet or side names? could you please explain how that

works.
I think rs.getSides() sends all side names into a table you can use. So you don't have to create the table.
Wolvan #9
Posted 29 March 2012 - 03:04 PM
added and credited
BigSHinyToys #10
Posted 29 March 2012 - 03:57 PM
added and credited
thanks
I got curious and tried rs.getSides() it is exactly what my table line does.so it can be swaped out if you want.
I was looking for information on rs.getSides() in help is it there and I'm just not finding it or is it a undocumented function??
Espen #11
Posted 29 March 2012 - 06:06 PM
@BigSHinyToys:
Yes, it returns all the valid redstone sides. So even if Dan would change the names of them, as long as he doesn't change the function name of getSides() it will always return all the sides without you having to fix your programs in the future. Also it's shorter to write, so all in all it's more "future-safe" and more compact.

It doesn't seem to be documented in the help page of "rs" though, but only in the one of "redstone", which is quite odd, because they are eqivalent and "rs" is just kind of an alias of "redstone". Also it doesn't explicitly say what getSides() does, but the name should give it away in this case. :o/>/>
Wolvan #12
Posted 29 March 2012 - 06:24 PM
added and credited
thanks
I got curious and tried rs.getSides() it is exactly what my table line does.so it can be swaped out if you want.
I was looking for information on rs.getSides() in help is it there and I'm just not finding it or is it a undocumented function??
Yeah I already swapped rs.getSides() with your table. Does my API work? Didn't have time to test
BigSHinyToys #13
Posted 29 March 2012 - 07:30 PM
Yeah I already swapped rs.getSides() with your table. Does my API work? Didn't have time to test
this line needs to be fixed in your api
local listOfSides = { rs.getSides() }

it should be
local listOfSides = rs.getSides()

this is because rs.getSide{} returns a table all we need to do is make a variable equal to it
example

table1 = {"a","b","c"}
table2 = table1
print(table2[1])
print(table2[2])
print(table2[3])
—- will return —-
a
b
c
Espen #14
Posted 29 March 2012 - 08:33 PM
But why waste a variable by assigning it to rs.getSides()?
I mean, as long as you don't want to make changes to the table holding the sides, but only ever want to read the sides from it, then you don't really need this…

local listOfSides = rs.getSides()

for key, value in pairs( listOfSides ) do
  -- Do something
end

You can just as well use it directly then, like this…

for key, value in pairs( rs.getSides() ) do
  -- Do something
end

Not telling how to do it, just trying to help keep the code smaller. So no offense, m'kay? :o/>/>
Wolvan #15
Posted 29 March 2012 - 08:49 PM
Yeah I already swapped rs.getSides() with your table. Does my API work? Didn't have time to test
this line needs to be fixed in your api
local listOfSides = { rs.getSides() }

it should be
local listOfSides = rs.getSides()

this is because rs.getSide{} returns a table all we need to do is make a variable equal to it
example

table1 = {"a","b","c"}
table2 = table1
print(table2[1])
print(table2[2])
print(table2[3])
—- will return —-
a
b
c
I fix it
Cloudy #16
Posted 30 March 2012 - 01:06 AM
I really applaud your effort. However the code below is much more efficient and should probably be used instead. I'd take note of how iteration through a table is performed.

Really?
Also you missed "back".

-- Untested code
local function openRednet()
  for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
	if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
	  rednet.open(side)
	  return side
	end
  end
  print("no wifi present")
end

modemSide = openRednet()
if modemSide == nil then
  print("No modem connected")
else
  print("Opened modem on "..modemSide)
end