# Part 3: Using WiFi ###### tags: `TA Stuff ESP32` `PyCom` `Heltec` `ESP32` To access the internet from your development board you can use the built-in WiFi on your **PyCom (FiPy & LoPy)**, **Heltec**, or **Generic ESP32**. In this tutorial, you will: + connect your development board to your home wireless router. + print the assigned IP address from the router to your board. + Try to use a TCP connection to a website and print the content of the page. As always, we'll continuously update this walkthrough. **Is there anything missing or unclear, or have you experienced some issue? Please add a comment.** You do this by highlighting the text and then you can write a comment on the highlighted part. You need to log in/create an account on hackMD first. :::info + **You should know that your development board works on 2.4 GHz and your home router should support it otherwise you can not connect to it. You can read more about it [*here*](https://www.actcorp.in/blog/difference-between-2.4ghz-and-5ghz-wifi).** + The WiFi connection methods are slightly different between **PyCom-Heltec** owners and **Generic ESP32** owners and that's because of the MicroPython version which is Pycom on PyCom and Heltec and Standard one on ESP32. So, based on your device follow one of the following sections: ::: ## Connecting **PyCom** and **Heltec** boards The following self-commented code could be copied to `main.py` and it first try to connect to your home WiFi then it tries to send an $HTTP$ request to $http://detectportal.firefox.com/$ and print the response on REPL. You should change **YourSSID** and **YourPassword** on line 14 (Fourteen) to your home WiFi name and password. ```python= def do_connect(): from network import WLAN import time import pycom import machine pycom.wifi_mode_on_boot(WLAN.STA) # choose station mode on boot wlan = WLAN() # get current object, without changing the mode # Set STA on soft rest if machine.reset_cause() != machine.SOFT_RESET: wlan.init(mode=WLAN.STA) # Put modem on Station mode if not wlan.isconnected(): # Check if already connected print("Connecting to WiFi...") # Connect with your WiFi Credential wlan.connect('YourSSID', auth=(WLAN.WPA2, 'YourPassword')) # Check if it is connected otherwise wait while not wlan.isconnected(): pass print("Connected to Wifi") time.sleep_ms(500) # Print the IP assigned by router print('network config:', wlan.ifconfig(id=0)) def http_get(url = 'http://detectportal.firefox.com/'): import socket # Used by HTML get request import time # Used for delay _, _, host, path = url.split('/', 3) # Separate URL request addr = socket.getaddrinfo(host, 80)[0][-1] # Get IP address of host s = socket.socket() # Initialise the socket s.connect(addr) # Try connecting to host address # Send HTTP request to the host with specific path s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) time.sleep(1) # Sleep for a second rec_bytes = s.recv(10000) # Receve response print(rec_bytes) # Print the response s.close() # Close connection # WiFi Connection do_connect() # HTTP request http_get() ``` :::info **We usually put the *do_connect()* function in our `boot.py` file so it will automatically connect to the internet whenever we reset the board.** ::: ## Connecting **ESP32** boards The following self-commented code could be copied to `main.py` and it first try to connect to your home WiFi then it tries to send an $HTTP$ request to $http://detectportal.firefox.com/$ and print the response on REPL. You should change **YourSSID** and **YourPassword** on line 7 (Seven) to your home WiFi name and password. ```python= def do_connect(): import network sta_if = network.WLAN(network.STA_IF) # Put modem on Station mode if not sta_if.isconnected(): # Check if already connected print('connecting to network...') sta_if.active(True) # Activate network interface sta_if.connect('YourSSID', 'YourPassword') # Your WiFi Credential # Check if it is connected otherwise wait while not sta_if.isconnected(): pass # Print the IP assigned by router print('network config:', sta_if.ifconfig()) def http_get(url = 'http://detectportal.firefox.com/'): import socket # Used by HTML get request import time # Used for delay _, _, host, path = url.split('/', 3) # Separate URL request addr = socket.getaddrinfo(host, 80)[0][-1] # Get IP address of host s = socket.socket() # Initialise the socket s.connect(addr) # Try connecting to host address # Send HTTP request to the host with specific path s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) time.sleep(1) # Sleep for a second rec_bytes = s.recv(10000) # Receve response print(rec_bytes) # Print the response s.close() # Close connection # WiFi Connection do_connect() # HTTP request http_get() ``` :::info **We usually put the *do_connect()* function in our `boot.py` file so it will automatically connect to the internet whenever we reset the board.** ::: *[REPL]: Read-Evaluate-Print Loop <style> .markdown-body code{ font-size: 1em !important; } .markdown-body .a{ font-size: 5em !important; } .markdown-body pre { background-color: #333; border: 1px solid #333 !important; color: #dfdfdf; font-weight: 600; } .token.operator, .token.entity, .token.url, .language-css .token.string, .style .token.string { background: #000; } .markdown-body table { display: table; padding: 1em; width: 100%; } .markdown-body table th, .markdown-body table td, .markdown-body table tr { border: none !important; } .markdown-body table tr { background-color: transparent !important; border-bottom: 1px solid rgba(0, 0, 0, 0.2) !important; } </style>