Skip to content

Instantly share code, notes, and snippets.

@blippy
Created May 9, 2019 19:16
Show Gist options
  • Save blippy/5131d1ac01cb5e6bdacdef4512fb9966 to your computer and use it in GitHub Desktop.
Save blippy/5131d1ac01cb5e6bdacdef4512fb9966 to your computer and use it in GitHub Desktop.
Test wifi connectivity
import machine
import network
import time
# because we use onbaord GPIO, we invert the meaning of the pin
# so 0 turns it on, 1 turn it off
pin = machine.Pin(16, machine.Pin.OUT)
def poff(): pin.value(1)
def pon(): pin.value(0)
poff()
def tmain1():
wlan = network.WLAN(network.STA_IF)
try:
while True:
wlan.active(True)
try:
nets = wlan.scan()
pon()
except OSError:
poff()
wlan.active(False)
time.sleep_ms(500)
finally:
poff()
wlan.active(False)
tmain1()
@blippy
Copy link
Author

blippy commented May 9, 2019

This project tests to see if there's a Wifi connection within range. It is useful if you want to know if your device will be able to attach to a Wifi point, or if it is out of range.

The code is written in MicroPython for a nodemcu/ESP8226/etc.. Notes:

  • Transfer the file across to your ncodemcu.
  • in main.py add the line import tcw. You can, alternatively, run the program in uPython's repl, although you will probably want to try the device disconnected from your computer
  • Ensure that that nothing is attached to GPIO16 (aka D0), as the onboard LED is used to display the status of the connection
  • Power the device. The pic below shows 6V connected, with positive connected to Vin, and negative connected to GND. A 9V battery will also work. Alternative powering methods may also work
  • Roam around to see if there is a network. The LED will be lit if there is a network, and unlit otherwise. Allow a little time for the device to settle. It sometimes take a little while for scans to complete, and there is a 0.5s delay between scans.

The picture belows shows a setup. The LED is alight, demonstrating that there is Wifi within connectivity distance.

z_scanner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment