Keywords: Connecting ESP8266 to WiFi, ESP8266, MicroPython, uPyCraft, Connecting ‘Thing – ESP8266’ to ‘Internet’, IoT
With ever growing Internet of Things industry a self-contained Wi-Fi networking capability MCU helps in understanding not only the data connectivity part but also the components involved in cloud. I being an Embedded engineer who worked on IoT products thought of putting my study and practical understanding of end to end concept which would help the readers to understand very easily.
For that I choose ESP8266 MCU which can perform a standalone application with WiFi connected in Stationary mode.
Prerequisites:
- ESP8266
- WiFi Router/Mobile Hotspot
- uPyCraftIDE
- Python
Software:
The station interface will allow us to connect ESP8266 board to the Wi-Fi network in home and get access to the Internet through it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
''' Author : Seetha Akella Purpose : Connect board to WiFi Language : MicroPython Hardware : ESP8266 ''' ''' The network module provides network drivers and routing configuration. ''' import network ''' The time module provides functions for getting 1. the current date and time 2. for sleeping ''' import time ''' class Pin - Control I/O pins The pin class has methods 1. to set the mode of pin (IN, OUT) 2. to get and set the digital logic level ''' from machine import Pin # Define a method to connect to a WiFi def do_connect(): ''' Constructor - WLAN Create a WLAN network interface object. Supported interfaces are 1. network.STA_IF (station aka client, connects to upstream WiFi access points) 2. network.AP_IF (access point, allows other WiFi clients to connect) ''' sta_if = network.WLAN(network.STA_IF) ''' Method - isconnected() 1. In case of STA mode, returns True if connected to a WiFi access point and has a valid IP address. 2. In AP mode returns True when a station is connected. Returns False otherwise. ''' if not sta_if.isconnected(): print('connecting to network...') ''' Method - active() Activate (“up”) or deactivate (“down”) nwk interface, if boolean argument is passed. Otherwise, query current state if no argument is provided. ''' sta_if.active(True) ''' Method - connect() Activate (“up”) or deactivate (“down”) nwk interface, if boolean argument is passed. Otherwise, query current state if no argument is provided. ''' sta_if.connect('SSID', 'Password') while not sta_if.isconnected(): sta_if.active(True) pass ''' Method - ifconfig() Get/set IP-level network interface parameters: IP address, subnet mask, gateway and DNS server. When called with no arguments, this method returns a 4-tuple with the above information. ''' print('network config:', sta_if.ifconfig()) led = Pin(2,Pin.OUT) while True: led.value(1) time.sleep(0.5) led.value(0) time.sleep(0.5) do_connect() time.sleep(1) |
Here is the snapshot of output for the above mentioned code.
THANK YOU and many more programs coming your way!
Please put your queries in comment section, I will get back to you with answers ASAP.
Please like and share Techawarey. Find and Like Techawarey on Facebook.😊