first and compiling freertos
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
from machine import UART, Pin
|
||||
from mqtt_client import MQTTClient
|
||||
from ch9120 import CH9120
|
||||
import time
|
||||
|
||||
# MQTT
|
||||
CLIENT_ID = "Waveshare_RP2040_ETH"
|
||||
SUBSCRIBE_TOPIC = "test_topic1"
|
||||
PUBLISH_TOPIC = "test_topic2"
|
||||
|
||||
# CH9120
|
||||
MODE = 1 #0:TCP Server 1:TCP Client 2:UDP Server 3:UDP Client
|
||||
GATEWAY = (192, 168, 1, 1) # GATEWAY
|
||||
TARGET_IP = (47, 92, 129, 18) # 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 = 1883 # TARGET_PORT
|
||||
BAUD_RATE = 115200 # BAUD_RATE
|
||||
|
||||
uart1 = UART(1, baudrate=9600, tx=Pin(20), rx=Pin(21))
|
||||
|
||||
def ch9120_configure():
|
||||
global uart1
|
||||
ch9120 = CH9120(uart1)
|
||||
ch9120.enter_config() # enter configuration mode
|
||||
ch9120.set_mode(MODE)
|
||||
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()
|
||||
mqtt_client = MQTTClient(uart1)
|
||||
mqtt_client.ClientID = CLIENT_ID # Set ClientID
|
||||
mqtt_client.connect() # Connect to MQTT server
|
||||
mqtt_client.subscribe(SUBSCRIBE_TOPIC) # Subscribe to topic:test_topic1
|
||||
|
||||
mqtt_client.send_heartbeat()
|
||||
last_heartbeat_time = time.time()
|
||||
time.sleep_ms(60) # Sending the first heartbeat
|
||||
uart1.read() # Clear unnecessary data
|
||||
|
||||
while True:
|
||||
rxData = uart1.read()
|
||||
if rxData is not None:
|
||||
topic, message = mqtt_client.extract_data(rxData) # Parse the received data
|
||||
if topic == SUBSCRIBE_TOPIC:
|
||||
print("Topic:", topic)
|
||||
print("Message:", message)
|
||||
mqtt_client.publish(PUBLISH_TOPIC, message) # Send received data to topic:test_topic2
|
||||
|
||||
current_time = time.time()
|
||||
if current_time - last_heartbeat_time >= 30:
|
||||
mqtt_client.send_heartbeat() # Send a heartbeat every 30 seconds
|
||||
last_heartbeat_time = current_time
|
||||
time.sleep_ms(60) # Waiting for the server to respond
|
||||
if not mqtt_client.check_heartbeat_response():
|
||||
while True:
|
||||
print("Reconnecting...")
|
||||
mqtt_client = MQTTClient(uart1)
|
||||
mqtt_client.ClientID = CLIENT_ID # Set ClientID
|
||||
mqtt_client.connect() # Connect to MQTT server
|
||||
mqtt_client.subscribe(SUBSCRIBE_TOPIC) # Subscribe to topic:test_topic1
|
||||
time.sleep_ms(200) # Waiting for the server to respond
|
||||
uart1.read() # Clear unnecessary data
|
||||
mqtt_client.send_heartbeat() # Sending the first heartbeat
|
||||
last_heartbeat_time = current_time # Clear unnecessary data
|
||||
time.sleep_ms(60) # Waiting for the server to respond
|
||||
if mqtt_client.check_heartbeat_response():
|
||||
print("Reconnection successful!")
|
||||
break
|
||||
|
||||
time.sleep_ms(20)
|
||||
|
||||
|
||||
74
docs/examples/eth_code/Python/RP2040-ETH-MQTT/ch9120.py
Normal file
74
docs/examples/eth_code/Python/RP2040-ETH-MQTT/ch9120.py
Normal 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)
|
||||
|
||||
68
docs/examples/eth_code/Python/RP2040-ETH-MQTT/mqtt_client.py
Normal file
68
docs/examples/eth_code/Python/RP2040-ETH-MQTT/mqtt_client.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from machine import UART
|
||||
import time
|
||||
import ubinascii
|
||||
|
||||
class MQTTClient:
|
||||
def __init__(self, uart):
|
||||
self.uart = uart
|
||||
self.ClientID = "Waveshare_RP2040_ETH"
|
||||
self.connect_message = bytearray([
|
||||
0x10, # MQTT control packet type (CONNECT)
|
||||
0x11, # Remaining Length in the fixed header
|
||||
0x00, 0x04, # Length of the UTF-8 encoded protocol name
|
||||
0x4D, 0x51, 0x54, 0x54, # MQTT string "MQTT"
|
||||
0x04, # Protocol Level (MQTT 3.1.1)
|
||||
0x02, # Connect Flags: Clean Session, No Will, No Will Retain, QoS = 0, No Will Flag, Keep Alive = 60 seconds
|
||||
0x00, 0x3C # Keep Alive Time in seconds
|
||||
])
|
||||
|
||||
def connect(self):
|
||||
byte_array = bytes(self.ClientID, "utf-8")
|
||||
length = len(byte_array)
|
||||
self.connect_message.extend(length.to_bytes(2, 'big')) # Length of the Client ID
|
||||
self.connect_message.extend(byte_array) # Client ID
|
||||
self.connect_message[1] = len(self.connect_message) - 2 # Change Length
|
||||
self.uart.write(bytes(self.connect_message))
|
||||
|
||||
def publish(self, topic, message):
|
||||
publish_message = bytearray([
|
||||
0x30, 0x11, # MQTT control packet type (PUBLISH)
|
||||
0x00, 0x0A # Length of the topic name
|
||||
])
|
||||
publish_message.extend(bytes(topic, "utf-8")) # Topic
|
||||
publish_message.extend(bytes(message, "utf-8")) # Message content
|
||||
publish_message[1] = len(publish_message) - 2 # Change Length
|
||||
publish_message[3] = len(bytes(topic, "utf-8")) # Change Length
|
||||
self.uart.write(bytes(publish_message))
|
||||
|
||||
def subscribe(self, topic):
|
||||
subscribe_message = bytearray([
|
||||
0x82, 0x0A, # MQTT control packet type (SUBSCRIBE)
|
||||
0x00, 0x01 # Remaining length
|
||||
])
|
||||
byte_array = bytes(topic, "utf-8")
|
||||
length = len(byte_array)
|
||||
subscribe_message.extend(length.to_bytes(2, 'big')) # Length of the topic name
|
||||
subscribe_message.extend(byte_array) # Topic
|
||||
subscribe_message.extend(bytes([0x00])) # qos
|
||||
subscribe_message[1] = len(subscribe_message) - 2 # Change Length
|
||||
self.uart.write(bytes(subscribe_message))
|
||||
|
||||
def send_heartbeat(self):
|
||||
heartbeat_message = bytearray([0xC0, 0x00])# Heartbeat message to keep the connection alive
|
||||
self.uart.write(heartbeat_message)
|
||||
|
||||
def check_heartbeat_response(self):
|
||||
response = self.uart.read()# Check for PINGRESP message
|
||||
if response == bytes([0xD0, 0x00]):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def extract_data(self, rxData):
|
||||
rxArray = bytearray()
|
||||
rxArray.extend(rxData)
|
||||
topic = rxArray[4:4 + rxArray[3]].decode('utf-8')
|
||||
message = rxArray[4 + rxArray[3]:rxArray[1] + 2].decode('utf-8')
|
||||
return topic, message
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user