2025年1月6日 星期一

使用兩顆esp32實現ESP-NOW 1對1的單向的通訊程式





實作環境:利用兩台PC以及兩顆ESP32,分別執行Thonny,

接收端程式:

 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
import network
import espnow

# A WLAN interface must be active to send()/recv()
sta = network.WLAN(network.WLAN.IF_STA)
sta.active(True)
sta.disconnect()   # Because ESP8266 auto-connects to last Access Point

# Get the MAC address
mac_address = sta.config('mac')

# Convert the MAC address to a readable format
mac_address_readable = ':'.join(['{:02x}'.format(b) for b in mac_address])

print("MAC Address:", mac_address_readable)

e = espnow.ESPNow()
e.active(True)

while True:
    host, msg = e.recv()
    if msg:             # msg == None if timeout in recv()
        print(host, msg)
        if msg == b'end':
            break

僅執行接收程式的執行結果:
MPY: soft reboot
MAC Address: b0:a7:32:c0:c6:70

傳送程式:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import network
import espnow

# A WLAN interface must be active to send()/recv()
sta = network.WLAN(network.WLAN.IF_STA)  # Or network.WLAN.IF_AP
sta.active(True)
sta.disconnect()      # For ESP8266

e = espnow.ESPNow()
e.active(True)
peer = b'\xb0\xa7\x32\xc0\xc6\x70'   # MAC address of peer's wifi interface
e.add_peer(peer)      # Must add_peer() before send()

e.send(peer, "Starting...")
for i in range(100):
    e.send(peer, str(i), True)
e.send(peer, b'end')

執行結果(接收端):
MPY: soft reboot
MAC Address: b0:a7:32:c0:c6:70
b'\xb0\xa72\xc0\xc6\xbc' b'Starting...'
b'\xb0\xa72\xc0\xc6\xbc' b'0'
b'\xb0\xa72\xc0\xc6\xbc' b'1'
b'\xb0\xa72\xc0\xc6\xbc' b'2'
b'\xb0\xa72\xc0\xc6\xbc' b'3'
b'\xb0\xa72\xc0\xc6\xbc' b'4'
b'\xb0\xa72\xc0\xc6\xbc' b'5'
b'\xb0\xa72\xc0\xc6\xbc' b'6'
b'\xb0\xa72\xc0\xc6\xbc' b'7'
b'\xb0\xa72\xc0\xc6\xbc' b'8'
b'\xb0\xa72\xc0\xc6\xbc' b'9'
b'\xb0\xa72\xc0\xc6\xbc' b'10'
b'\xb0\xa72\xc0\xc6\xbc' b'11'
b'\xb0\xa72\xc0\xc6\xbc' b'12'
b'\xb0\xa72\xc0\xc6\xbc' b'13'
b'\xb0\xa72\xc0\xc6\xbc' b'14'
b'\xb0\xa72\xc0\xc6\xbc' b'15'
b'\xb0\xa72\xc0\xc6\xbc' b'16'
b'\xb0\xa72\xc0\xc6\xbc' b'17'
b'\xb0\xa72\xc0\xc6\xbc' b'18'
b'\xb0\xa72\xc0\xc6\xbc' b'19'
b'\xb0\xa72\xc0\xc6\xbc' b'20'
b'\xb0\xa72\xc0\xc6\xbc' b'21'
b'\xb0\xa72\xc0\xc6\xbc' b'22'
b'\xb0\xa72\xc0\xc6\xbc' b'23'
b'\xb0\xa72\xc0\xc6\xbc' b'24'
b'\xb0\xa72\xc0\xc6\xbc' b'25'
b'\xb0\xa72\xc0\xc6\xbc' b'26'
b'\xb0\xa72\xc0\xc6\xbc' b'27'
b'\xb0\xa72\xc0\xc6\xbc' b'28'
b'\xb0\xa72\xc0\xc6\xbc' b'29'
b'\xb0\xa72\xc0\xc6\xbc' b'30'
b'\xb0\xa72\xc0\xc6\xbc' b'31'
b'\xb0\xa72\xc0\xc6\xbc' b'32'
b'\xb0\xa72\xc0\xc6\xbc' b'33'
b'\xb0\xa72\xc0\xc6\xbc' b'34'
b'\xb0\xa72\xc0\xc6\xbc' b'35'
b'\xb0\xa72\xc0\xc6\xbc' b'36'
b'\xb0\xa72\xc0\xc6\xbc' b'37'
b'\xb0\xa72\xc0\xc6\xbc' b'38'
b'\xb0\xa72\xc0\xc6\xbc' b'39'
b'\xb0\xa72\xc0\xc6\xbc' b'40'
b'\xb0\xa72\xc0\xc6\xbc' b'41'
b'\xb0\xa72\xc0\xc6\xbc' b'42'
b'\xb0\xa72\xc0\xc6\xbc' b'43'
b'\xb0\xa72\xc0\xc6\xbc' b'44'
b'\xb0\xa72\xc0\xc6\xbc' b'45'
b'\xb0\xa72\xc0\xc6\xbc' b'46'
b'\xb0\xa72\xc0\xc6\xbc' b'47'
b'\xb0\xa72\xc0\xc6\xbc' b'48'
b'\xb0\xa72\xc0\xc6\xbc' b'49'
b'\xb0\xa72\xc0\xc6\xbc' b'50'
b'\xb0\xa72\xc0\xc6\xbc' b'51'
b'\xb0\xa72\xc0\xc6\xbc' b'52'
b'\xb0\xa72\xc0\xc6\xbc' b'53'
b'\xb0\xa72\xc0\xc6\xbc' b'54'
b'\xb0\xa72\xc0\xc6\xbc' b'55'
b'\xb0\xa72\xc0\xc6\xbc' b'56'
b'\xb0\xa72\xc0\xc6\xbc' b'57'
b'\xb0\xa72\xc0\xc6\xbc' b'58'
b'\xb0\xa72\xc0\xc6\xbc' b'59'
b'\xb0\xa72\xc0\xc6\xbc' b'60'
b'\xb0\xa72\xc0\xc6\xbc' b'61'
b'\xb0\xa72\xc0\xc6\xbc' b'62'
b'\xb0\xa72\xc0\xc6\xbc' b'63'
b'\xb0\xa72\xc0\xc6\xbc' b'64'
b'\xb0\xa72\xc0\xc6\xbc' b'65'
b'\xb0\xa72\xc0\xc6\xbc' b'66'
b'\xb0\xa72\xc0\xc6\xbc' b'67'
b'\xb0\xa72\xc0\xc6\xbc' b'68'
b'\xb0\xa72\xc0\xc6\xbc' b'69'
b'\xb0\xa72\xc0\xc6\xbc' b'70'
b'\xb0\xa72\xc0\xc6\xbc' b'71'
b'\xb0\xa72\xc0\xc6\xbc' b'72'
b'\xb0\xa72\xc0\xc6\xbc' b'73'
b'\xb0\xa72\xc0\xc6\xbc' b'74'
b'\xb0\xa72\xc0\xc6\xbc' b'75'
b'\xb0\xa72\xc0\xc6\xbc' b'76'
b'\xb0\xa72\xc0\xc6\xbc' b'77'
b'\xb0\xa72\xc0\xc6\xbc' b'78'
b'\xb0\xa72\xc0\xc6\xbc' b'79'
b'\xb0\xa72\xc0\xc6\xbc' b'80'
b'\xb0\xa72\xc0\xc6\xbc' b'81'
b'\xb0\xa72\xc0\xc6\xbc' b'82'
b'\xb0\xa72\xc0\xc6\xbc' b'83'
b'\xb0\xa72\xc0\xc6\xbc' b'84'
b'\xb0\xa72\xc0\xc6\xbc' b'85'
b'\xb0\xa72\xc0\xc6\xbc' b'86'
b'\xb0\xa72\xc0\xc6\xbc' b'87'
b'\xb0\xa72\xc0\xc6\xbc' b'88'
b'\xb0\xa72\xc0\xc6\xbc' b'89'
b'\xb0\xa72\xc0\xc6\xbc' b'90'
b'\xb0\xa72\xc0\xc6\xbc' b'91'
b'\xb0\xa72\xc0\xc6\xbc' b'92'
b'\xb0\xa72\xc0\xc6\xbc' b'93'
b'\xb0\xa72\xc0\xc6\xbc' b'94'
b'\xb0\xa72\xc0\xc6\xbc' b'95'
b'\xb0\xa72\xc0\xc6\xbc' b'96'
b'\xb0\xa72\xc0\xc6\xbc' b'97'
b'\xb0\xa72\xc0\xc6\xbc' b'98'
b'\xb0\xa72\xc0\xc6\xbc' b'99'
b'\xb0\xa72\xc0\xc6\xbc' b'end'

沒有留言:

張貼留言