Jump to content




WIFI modem detector - finds and opens modem


15 replies to this topic

#1 BigSHinyToys

  • Members
  • 1,001 posts

Posted 29 March 2012 - 07: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
the section bellow this line (modemOn = openRednet()) shows a possible way to use this function.

#2 Ian-Moone

    The Germ

  • New Members
  • 124 posts
  • LocationLiverpool (no I am not a Scouser Im form Holland)

Posted 29 March 2012 - 07:10 AM

usefull :o/>
thanks

#3 Wolvan

  • New Members
  • 384 posts
  • LocationIn the TARDIS

Posted 29 March 2012 - 07: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?

#4 BigSHinyToys

  • Members
  • 1,001 posts

Posted 29 March 2012 - 07: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

#5 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 29 March 2012 - 10: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



#6 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 29 March 2012 - 11:28 AM

@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 


#7 BigSHinyToys

  • Members
  • 1,001 posts

Posted 29 March 2012 - 12:58 PM

View Postimmibis, on 29 March 2012 - 10:47 AM, said:

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"

View PostEspen, on 29 March 2012 - 11:28 AM, said:

@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.

#8 Wolvan

  • New Members
  • 384 posts
  • LocationIn the TARDIS

Posted 29 March 2012 - 01:41 PM

View PostBigSHinyToys, on 29 March 2012 - 12:58 PM, said:

View PostEspen, on 29 March 2012 - 11:28 AM, said:

@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.

#9 Wolvan

  • New Members
  • 384 posts
  • LocationIn the TARDIS

Posted 29 March 2012 - 02:04 PM

added and credited

#10 BigSHinyToys

  • Members
  • 1,001 posts

Posted 29 March 2012 - 02:57 PM

View PostWolvan, on 29 March 2012 - 02:04 PM, said:

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??

#11 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 29 March 2012 - 05: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/>

#12 Wolvan

  • New Members
  • 384 posts
  • LocationIn the TARDIS

Posted 29 March 2012 - 05:24 PM

View PostBigSHinyToys, on 29 March 2012 - 02:57 PM, said:

View PostWolvan, on 29 March 2012 - 02:04 PM, said:

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

#13 BigSHinyToys

  • Members
  • 1,001 posts

Posted 29 March 2012 - 06:30 PM

View PostWolvan, on 29 March 2012 - 05:24 PM, said:

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

#14 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 29 March 2012 - 07: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/>

#15 Wolvan

  • New Members
  • 384 posts
  • LocationIn the TARDIS

Posted 29 March 2012 - 07:49 PM

View PostBigSHinyToys, on 29 March 2012 - 06:30 PM, said:

View PostWolvan, on 29 March 2012 - 05:24 PM, said:

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

#16 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 30 March 2012 - 12: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.

View Postimmibis, on 29 March 2012 - 10:47 AM, said:

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







1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users