tf_extend.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import os,network,socket,time,machine,gc
  2. def cmd(args):
  3. if args[0]=='ifconfig':
  4. ifc=network.WLAN().ifconfig()
  5. print("IP: {}\tmask: {}\tgateway: {}\tDNS: {}".format(*ifc))
  6. return True
  7. elif args[0]=='host':
  8. if len(args)<2:
  9. print("syntax: host <domain.name>")
  10. return False
  11. print("host <{}> is at {}".format(args[1],socket.getaddrinfo(args[1],80)[0][-1][0]))
  12. return True
  13. elif args[0]=='connect':
  14. if len(args)<3:
  15. print("syntax: connect <ssid> <password>")
  16. return False
  17. w=network.WLAN(network.STA_IF)
  18. w.connect(args[1],args[2])
  19. print("connecting...",end=' ')
  20. time.sleep(3)
  21. print(w.ifconfig() if w.isconnected() else "not yet connected; try 'ifconfig' in a few seconds")
  22. return True
  23. elif args[0]=='scan':
  24. w=network.WLAN(network.STA_IF)
  25. print("scanning...")
  26. s=w.scan()
  27. if len(s)==0:
  28. print("no AP found")
  29. return True
  30. for i in s:
  31. print("ch: {}\tRSSI: {}\t{}\tSSID: {}".format(i[2],i[3],"open" if i[4]==0 else "",i[0]))
  32. return True
  33. elif args[0]=='freq':
  34. if len(args)==1 or args[1] in ("160","80"):
  35. if len(args)>1:
  36. machine.freq(int(args[1])*1000000)
  37. print("master cpu frequency {}MHz".format(machine.freq()//1000000))
  38. else:
  39. print("syntax: freq [ 160 | 80 ]")
  40. return True
  41. elif args[0]=='exec':
  42. if len(args)<2:
  43. print("syntax: exec <python-filename>")
  44. else:
  45. try:
  46. exec(open(args[1]).read(),globals(),globals())
  47. except OSError:
  48. print("file not found")
  49. return True
  50. elif args[0]=='free':
  51. print("memory used: {}\tmemory free:{}".format(gc.mem_alloc(),gc.mem_free()))
  52. return True
  53. elif args[0]=='help':
  54. print("==Extended commands")
  55. print(" connect <essid> <password> \tscan")
  56. print(" ifconfig \thost <domain.name>")
  57. print(" freq [ 160 | 80 ] \texec <python-filename>")
  58. print(" free")
  59. return True
  60. else:
  61. return False