ESP8266 implements TCP control LED and setting WIFI password
INDUSTRIAL LCD DISPLAYS / IGBT MODULES DISTRIBUTOR

Infineon / Mitsubishi / Fuji / Semikron / Eupec / IXYS

ESP8266 implements TCP control LED and setting WIFI password

Posted Date: 2024-01-19

Video demonstration of Station B: https://www.bilibili.com/video/BV11t4y1m79r/?vd_source=089e9c9189991f80be4b4886098da65b#reply151108474896

The code main.py can be passed into the ESP8266 micropython firmware through the web page. How to transfer and find information by yourself. I also forgot how to transfer. This article is just to record the code cases I have written. Easy to find later.

import usocket #Import socket package import machine import network import time LED=machine.Pin(2, machine.Pin.OUT) LED.value(1) WIFI_flag=1; is_flag=0 ssid="CMCC-lin----- " pwd="linjia@15120613059" ap_ip="" st_ip="" while True: if WIFI_flag == 1: ap = network.WLAN(network.AP_IF) ap.active(True) ap.config(essid='ESP8266_WIFI' ) ap.config(authmode=0) print("ESP8266_IP:",ap.ifconfig()(0)+",Port:23")#Get IP information s=usocket.socket() s.bind(('' ,23)) #Listen to local port 23 s.listen(5) ''' if WIFI_flag == 0: #Connect to hotspot WIFI sta_if=network.WLAN(network.STA_IF) sta_if.active(True) #sta_if.scan() # Scan the currently connectable WiFi name sta_if.connect(ssid,pwd) # Set the name and connection password of the WiFi to be connected print("SSID:",ssid+"nPassword:",pwd) print("Linking WIFI..." ) while True: if sta_if.isconnected()==True: break else : time.sleep(10) print("Linking WIFI error.Please reset device and input right SSID or PAWD again!n") print("ESP8266 link WIFI status:",sta_if.isconnected())#Check whether the connection is successful print("ESP8266_IP:",sta_if.ifconfig()(0)+",Port:80")#Get IP information s=usocket.socket() s .bind(('',80)) #Listen to local port 23 s.listen(5) ''' print("waiting...") c,a=s.accept() print("one device link!" ) c.send("Hi,Welcome micropython of ESP8266n") while True: client_data=c.recv(1024)#Received are bytes print("client_data:",client_data) if client_data==b"": # s.listen(5) print("waiting...") c,a=s.accept() print("one device link!") c.send("Hi,Welcome micropython of ESP8266n") print("client :",client_data) #Output the received message if client_data==b"ON":#Judge the byte LED.value(0)#Turn on the light print("+++ LED OPEN +++") c.send( "+++ LED OPEN +++n") if client_data==b"OFF":#Judge bytes, add b in front of the string to force type conversion to bytes LED.value(1)#Turn off the light print(" --- LED CLOSE ---") c.send("Hi,Welcome ESP8266 micropythonn") c.send("--- LED CLOSE ---n") if client_data(0:5)==b"SSID :": #Determine the WIFI name to be connected ssid=client_data(5:) ssid=bytes.decode(ssid)#Convert bytes into string print("ssid:",ssid) if client_data(0:5)== b"PAWD:": #Judge WIFI password pwd=client_data(5:) pwd=bytes.decode(pwd)#Convert bytes into string print("password:",pwd) if client_data==b"CXIP": app = network.WLAN(network.AP_IF) sta = network.WLAN(network.STA_IF) print("STA_IP:",app.ifconfig()(0)+",Port:23n") print("AP_IP:", sta.ifconfig()(0)+",Port:23n") c.send("nPort:23,STA_IP:") c.send(sta.ifconfig()(0)) c.send("nPort:23 ,AP_IP:") c.send(app.ifconfig()(0)) if client_data==b"WIFI_AP": WIFI_flag=1 print("--- WIFI_AP ---") for i in range(1): LED.value(0)#Turn on the light time.sleep(0.5) LED.value(1)#Turn off the light time.sleep(0.5) ap = network.WLAN(network.AP_IF) ap.active(True) ap.config( essid='ESP8266_WIFI') ap.config(authmode=0) print("ESP8266_IP:",ap.ifconfig()(0)+",Port:23")#Get IP information print("waiting...") for i in range(2): LED.value(0)#Turn on the light time.sleep(0.5) LED.value(1)#Turn off the light time.sleep(0.5) c,a=s.accept() print(" one device link!") c.send("Hi,Welcome micropython of ESP8266n") for i in range(3): LED.value(0)#turn on time.sleep(0.5) LED.value(1)#off Light time.sleep(0.5) if client_data==b"WIFI_ST": WIFI_flag=0 print("--- WIFI_ST ---") for i in range(1): LED.value(0)#Turn on the light time. sleep(0.5) LED.value(1)#Turn off the light time.sleep(0.5) #Connect to hotspot WIFI sta_if=network.WLAN(network.STA_IF) sta_if.active(True) #sta_if.scan()# Scan the current available connections WiFi name sta_if.connect(ssid,pwd) # Set the name and connection password of the WiFi to be connected print("SSID:",ssid+"nPassword:",pwd) print("Linking WIFI...") while True: if sta_if.isconnected()==True: is_flag=1 break else : time.sleep(10) print("Linking WIFI error.Please reset device and input right SSID or PAWD again!n") print("ESP8266 link WIFI status: ",sta_if.isconnected())#Check whether the connection is successful print("ESP8266_IP:",sta_if.ifconfig()(0)+",Port:23")#Get IP information print("waiting...") for i in range(2): LED.value(0)#Turn on the light time.sleep(0.5) LED.value(1)#Turn off the light time.sleep(0.5) c,a=s.accept() print("one device link!") c.send("Hi,Welcome micropython of ESP8266n") for i in range(3): LED.value(0)#Turn on the light time.sleep(0.5) LED.value(1)#Turn off the light time.sleep(0.5)
#ESP8266 #implements #TCP #control #LED #setting #WIFI #password