first and working ethernet

This commit is contained in:
2025-07-26 21:48:13 -04:00
commit 473f9013c0
58 changed files with 3405 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
from machine import UART, Pin
from mqtt_client import MQTTClient
from ch9120 import CH9120
import time
# CH9120
MODE = 1 #0:TCP Server 1:TCP Client 2:UDP Server 3:UDP Client
GATEWAY = (192, 168, 1, 1) # GATEWAY
TARGET_IP = (192, 168, 1, 10) # TARGET_IP
LOCAL_IP = (192, 168, 1, 200) # LOCAL_IP
SUBNET_MASK = (255,255,255,0) # SUBNET_MASK
LOCAL_PORT1 = 1000 # LOCAL_PORT1
TARGET_PORT = 2000 # TARGET_PORT
BAUD_RATE = 115200 # BAUD_RATE
uart1 = UART(1, baudrate=9600, tx=Pin(20), rx=Pin(21))
def ch9120_configure():
# Configure
global uart1
ch9120 = CH9120(uart1)
ch9120.enter_config() # enter configuration mode
ch9120.set_mode(MODE) # 0:TCP Server 1:TCP Client 2:UDP Server 3:UDP Client
ch9120.set_localIP(LOCAL_IP)
ch9120.set_subnetMask(SUBNET_MASK)
ch9120.set_gateway(GATEWAY)
ch9120.set_localPort(LOCAL_PORT1)
ch9120.set_targetIP(TARGET_IP)
ch9120.set_targetPort(TARGET_PORT)
ch9120.set_baudRate(BAUD_RATE)
ch9120.exit_config() # exit configuration mode
# Clear cache and reconfigure uart1
uart1.read(uart1.any())
time.sleep(0.5)
uart1 = UART(1, baudrate=115200, tx=Pin(20), rx=Pin(21))
if __name__ == "__main__":
ch9120_configure()
while True:
time.sleep(0.1)
while uart1.any() > 0:
rxData1 = uart1.read(uart1.any())
uart1.write(rxData1)
print(rxData1.decode('utf8'))

View File

@@ -0,0 +1,74 @@
from machine import UART, Pin
import time
class CH9120:
def __init__(self, uart):
self.uart = uart
self.MODE = 1 #0:TCP Server 1:TCP Client 2:UDP Server 3:UDP Client
self.GATEWAY = (192, 168, 11, 1) # GATEWAY
self.TARGET_IP = (47, 92, 129, 18) # TARGET_IP
self.LOCAL_IP = (192, 168, 10, 200) # LOCAL_IP
self.SUBNET_MASK = (255,255,252,0) # SUBNET_MASK
self.LOCAL_PORT = 1000 # LOCAL_PORT1
self.TARGET_PORT = 1883 # TARGET_PORT
self.BAUD_RATE = 115200 # BAUD_RATE
self.CFG = Pin(18, Pin.OUT,Pin.PULL_UP)
self.RST = Pin(19, Pin.OUT,Pin.PULL_UP)
def enter_config(self):
print("begin")
self.RST.value(1)
self.CFG.value(0)
time.sleep(0.5)
def exit_config(self):
self.uart.write(b'\x57\xab\x0D')
time.sleep(0.1)
self.uart.write(b'\x57\xab\x0E')
time.sleep(0.1)
self.uart.write(b'\x57\xab\x5E')
time.sleep(0.1)
self.CFG.value(1)
time.sleep(0.1)
print("end")
def set_mode(self,MODE):
self.MODE = MODE
self.uart.write(b'\x57\xab\x10' + self.MODE.to_bytes(1, 'little'))#Convert int to bytes
time.sleep(0.1)
def set_localIP(self,LOCAL_IP):
self.LOCAL_IP = LOCAL_IP
self.uart.write(b'\x57\xab\x11' + bytes(self.LOCAL_IP))#Converts the int tuple to bytes
time.sleep(0.1)
def set_subnetMask(self,SUBNET_MASK):
self.SUBNET_MASK = SUBNET_MASK
self.uart.write(b'\x57\xab\x12' + bytes(self.SUBNET_MASK))
time.sleep(0.1)
def set_gateway(self,GATEWAY):
self.GATEWAY = GATEWAY
self.uart.write(b'\x57\xab\x13' + bytes(self.GATEWAY))
time.sleep(0.1)
def set_localPort(self,LOCAL_PORT):
self.LOCAL_PORT = LOCAL_PORT
self.uart.write(b'\x57\xab\x14' + self.LOCAL_PORT.to_bytes(2, 'little'))
time.sleep(0.1)
def set_targetIP(self,TARGET_IP):
self.TARGET_IP = TARGET_IP
self.uart.write(b'\x57\xab\x15' + bytes(self.TARGET_IP))
time.sleep(0.1)
def set_targetPort(self,TARGET_PORT):
self.TARGET_PORT = TARGET_PORT
self.uart.write(b'\x57\xab\x16' + self.TARGET_PORT.to_bytes(2, 'little'))
time.sleep(0.1)
def set_baudRate(self,BAUD_RATE):
self.BAUD_RATE = BAUD_RATE
self.uart.write(b'\x57\xab\x21' + self.BAUD_RATE.to_bytes(4, 'little'))
time.sleep(0.1)