Skip to content

Commit b97743f

Browse files
authored
Merge pull request #25 from LeeLeahy2/4-15-esp-now-client
4-15: Add ESP-NOW client to test 4-13
2 parents 9c6050f + 8bf2b7e commit b97743f

File tree

2 files changed

+328
-0
lines changed

2 files changed

+328
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**********************************************************************
2+
4_15_ESP_NOW_Client.ino
3+
4+
Example sketch to test ESP-NOW mode with example 4_13_Muliple_WiFi_Users
5+
**********************************************************************/
6+
7+
#include "ESP32_NOW_Serial.h"
8+
#include "MacAddress.h"
9+
#include "WiFi.h"
10+
11+
#include "esp_wifi.h"
12+
13+
bool RTK_CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC = false;
14+
15+
const int32_t channelList[] =
16+
{
17+
1,
18+
8,
19+
};
20+
const int32_t channelCount = sizeof(channelList) / sizeof(channelList[0]);
21+
int32_t channelIndex;
22+
int32_t channel;
23+
24+
// Set the MAC address of the device that will receive the data
25+
const MacAddress macPeer({0xE0, 0x5A, 0x1B, 0xD8, 0x8F, 0x14});
26+
27+
const wifi_interface_t interface = WIFI_IF_STA;
28+
const char * testData = "ESP-NOW test data\r\n";
29+
30+
ESP_NOW_Serial_Class * nowSerial;
31+
32+
//*********************************************************************
33+
// Entry point for the application
34+
void setup()
35+
{
36+
Serial.begin(115200);
37+
Serial.printf("\r\n");
38+
Serial.printf("%s\r\n", __FILE__);
39+
}
40+
41+
//*********************************************************************
42+
// Idle loop for core 1 of the application
43+
void loop()
44+
{
45+
static uint32_t wifiConnectMsec;
46+
47+
// Connect to WiFi
48+
if (!nowSerial)
49+
{
50+
// Set the mode
51+
WiFi.mode(WIFI_MODE_STA);
52+
esp_wifi_set_protocol(interface,
53+
WIFI_PROTOCOL_11B
54+
| WIFI_PROTOCOL_11G
55+
| WIFI_PROTOCOL_11N);
56+
57+
// Set the WiFi channel
58+
channel = channelList[channelIndex++];
59+
if (channelIndex >= channelCount)
60+
channelIndex = 0;
61+
WiFi.setChannel(channel, WIFI_SECOND_CHAN_NONE);
62+
63+
// Start WiFi if not already running
64+
Serial.printf("Starting WiFi\r\n");
65+
while (!WiFi.STA.started())
66+
delay(100);
67+
68+
// Create the ESPNow serial object
69+
esp_now_init();
70+
nowSerial = new ESP_NOW_Serial_Class(macPeer, channel, interface);
71+
if (nowSerial)
72+
{
73+
Serial.printf("ESP-NOW started\r\n");
74+
nowSerial->begin(115200);
75+
wifiConnectMsec = millis();
76+
}
77+
}
78+
79+
// Send the ESP-NOW test data
80+
else if (nowSerial->availableForWrite())
81+
{
82+
size_t bytesWritten;
83+
84+
Serial.printf("Sending ESP-NOW data\r\n");
85+
bytesWritten = nowSerial->write((uint8_t *)testData, strlen(testData));
86+
Serial.printf("bytesWritten: %ld\r\n", bytesWritten);
87+
if (bytesWritten > 0)
88+
nowSerial->flush();
89+
else
90+
Serial.println("Failed to send data");
91+
92+
// Done with the ESP-NOW connection
93+
disconnect();
94+
delay(1000);
95+
}
96+
97+
// Timeout the ESP-NOW link
98+
else
99+
{
100+
if ((millis() - wifiConnectMsec) >= 500)
101+
disconnect();
102+
}
103+
}
104+
105+
//*********************************************************************
106+
// Break ESP-NOW link
107+
void disconnect()
108+
{
109+
Serial.printf("Breaking ESP-NOW Link\r\n");
110+
// Break the ESP-NOW link
111+
if (nowSerial)
112+
{
113+
esp_now_deinit();
114+
nowSerial->end();
115+
delete nowSerial;
116+
nowSerial = nullptr;
117+
}
118+
119+
// Stop WiFi
120+
WiFi.mode((wifi_mode_t)0);
121+
delay(100);
122+
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
######################################################################
2+
# makefile
3+
#
4+
# Builds the example
5+
######################################################################
6+
7+
##########
8+
# Source files
9+
##########
10+
11+
EXAMPLE_SKETCH=4_15_ESP_NOW_Client
12+
13+
EXECUTABLES += example
14+
15+
PARTITION_CSV_FILE=RTKEverywhere
16+
17+
ifeq ($(OS),Windows_NT)
18+
# Windows NT utilities
19+
CLEAR=cls
20+
COPY=copy
21+
DELETE=rmdir /s
22+
DIR_LISTING=dir
23+
TERMINAL_APP=
24+
TERMINAL_PORT="COM3"
25+
TERMINAL_PARAMS=
26+
27+
# Windows NT generic paths
28+
USER_DIRECTORY_PATH=C:\Users\$(USERNAME)
29+
ARDUINO_LIBRARY_PATH=$(USER_DIRECTORY_PATH)\Documents\Arduino\libraries
30+
HOME_BOARD_PATH=$(USER_DIRECTORY_PATH)\AppData\Local\Arduino15\packages\esp32
31+
PATCH_SRC_PATH=Patch\
32+
33+
# Windows NT patch source paths
34+
PARTITION_SRC_PATH=..\$(PARTITION_CSV_FILE).csv
35+
PATCH_SRC_PATH=Patch\
36+
37+
# Windows NT patch destination paths
38+
BLE_PATCH_DST_PATH=$(ARDUINO_LIBRARY_PATH)\ESP32_BleSerial\src\
39+
MBED_LIB_DEST_PATH=$(HOME_BOARD_PATH)\tools\esp32-arduino-libs\${{ env.ESP_IDF }}\esp32/lib\
40+
PARTITION_DST_PATH=$(HOME_BOARD_PATH)\hardware\esp32\$(ESP_CORE_VERSION)\tools\partitions\$(PARTITION_CSV_FILE).csv
41+
42+
else
43+
# Linux utilities
44+
CLEAR=clear
45+
COPY=cp
46+
DELETE=rm -Rf
47+
DIR_LISTING=ls
48+
TERMINAL_APP=minicom
49+
#TERMINAL_PORT="/dev/ttyACM1"
50+
TERMINAL_PORT="/dev/ttyUSB0"
51+
TERMINAL_PARAMS=-b 115200 -8 < /dev/tty
52+
53+
# Linux generic paths
54+
USER_DIRECTORY_PATH=~
55+
ARDUINO_LIBRARY_PATH=$(USER_DIRECTORY_PATH)/Arduino/libraries
56+
ESP_IDF_PATH=$(HOME_BOARD_PATH)/tools/esp32-arduino-libs
57+
HOME_BOARD_PATH=$(USER_DIRECTORY_PATH)/.arduino15/packages/esp32
58+
59+
# Linux patch source paths
60+
PARTITION_SRC_PATH=../$(PARTITION_CSV_FILE).csv
61+
PATCH_SRC_PATH=Patch/
62+
63+
# Linux patch destination paths
64+
BLE_PATCH_DST_PATH=$(ARDUINO_LIBRARY_PATH)/ESP32_BleSerial/src/
65+
MBED_LIB_DEST_PATH=$(ESP_IDF_PATH)/$(ESP_IDF_VERSION)/esp32/lib/
66+
PARTITION_DST_PATH=$(HOME_BOARD_PATH)/hardware/esp32/$(ESP_CORE_VERSION)/tools/partitions/$(PARTITION_CSV_FILE).csv
67+
68+
endif
69+
70+
##########
71+
# Buid all the sources - must be first
72+
##########
73+
74+
.PHONY: all
75+
76+
all: $(EXECUTABLES)
77+
78+
##########
79+
# Add ESP32 board support
80+
##########
81+
82+
.PHONY: arduino-config
83+
84+
arduino-config:
85+
arduino-cli config init --overwrite --additional-urls "https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json,https://espressif.github.io/arduino-esp32/package_esp32_dev_index.json"
86+
87+
##########
88+
# Build an example
89+
##########
90+
91+
.PHONY: example
92+
93+
example: build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin
94+
95+
DEBUG_LEVEL=none
96+
#DEBUG_LEVEL=debug
97+
PSRAM=disabled
98+
#PSRAM=enabled
99+
100+
build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin: $(EXAMPLE_SKETCH).ino *.ino makefile
101+
$(CLEAR)
102+
arduino-cli compile --fqbn "esp32:esp32:esp32":DebugLevel=$(DEBUG_LEVEL),PSRAM=$(PSRAM) $< \
103+
--warnings default \
104+
--build-property build.partitions=$(PARTITION_CSV_FILE) \
105+
--build-property upload.maximum_size=6291456 \
106+
--build-property "compiler.cpp.extra_flags=-MMD -c \"-DPOINTPERFECT_TOKEN=$(POINTPERFECT_TOKEN)\" \"-DFIRMWARE_VERSION_MAJOR=$(FIRMWARE_VERSION_MAJOR)\" \"-DFIRMWARE_VERSION_MINOR=$(FIRMWARE_VERSION_MINOR)\" \"-DENABLE_DEVELOPER=$(ENABLE_DEVELOPER)\"" \
107+
--export-binaries
108+
109+
##########
110+
# Upload the example
111+
##########
112+
113+
ESPTOOL_PATH=~/Arduino/hardware/espressif/esp32/tools/esptool
114+
#UPLOAD_SPEED=460800
115+
UPLOAD_SPEED=921600
116+
BOOT_LOADER_PATH=~/SparkFun/SparkFun_RTK_Firmware_Uploader/RTK_Firmware_Uploader/resource
117+
118+
.PHONY: upload
119+
120+
upload: build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin
121+
python3 $(ESPTOOL_PATH)/esptool.py \
122+
--chip esp32 \
123+
--port $(TERMINAL_PORT) \
124+
--baud $(UPLOAD_SPEED) \
125+
--before default_reset \
126+
--after hard_reset \
127+
write_flash \
128+
--flash_mode dio \
129+
--flash_freq 80m \
130+
--flash_size detect \
131+
--compress \
132+
0x1000 $(BOOT_LOADER_PATH)/RTK_Surveyor.ino.bootloader.bin \
133+
0x8000 $(BOOT_LOADER_PATH)/RTK_Surveyor_Partitions_16MB.bin \
134+
0xe000 $(BOOT_LOADER_PATH)/boot_app0.bin \
135+
0x10000 $<
136+
$(TERMINAL_APP) -D $(TERMINAL_PORT) $(TERMINAL_PARAMS)
137+
138+
TERMINAL_PORT_2=/dev/ttyUSB1
139+
140+
.PHONY: upload2
141+
142+
upload2: build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin
143+
python3 $(ESPTOOL_PATH)/esptool.py \
144+
--chip esp32 \
145+
--port $(TERMINAL_PORT_2) \
146+
--baud $(UPLOAD_SPEED) \
147+
--before default_reset \
148+
--after hard_reset \
149+
write_flash \
150+
--flash_mode dio \
151+
--flash_freq 80m \
152+
--flash_size detect \
153+
--compress \
154+
0x1000 $(BOOT_LOADER_PATH)/RTK_Surveyor.ino.bootloader.bin \
155+
0x8000 $(BOOT_LOADER_PATH)/RTK_Surveyor_Partitions_16MB.bin \
156+
0xe000 $(BOOT_LOADER_PATH)/boot_app0.bin \
157+
0x10000 $<
158+
$(TERMINAL_APP) -D $(TERMINAL_PORT_2) $(TERMINAL_PARAMS)
159+
160+
TERMINAL_PORT_3=/dev/ttyUSB2
161+
162+
.PHONY: upload3
163+
164+
upload3: build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin
165+
python3 $(ESPTOOL_PATH)/esptool.py \
166+
--chip esp32 \
167+
--port $(TERMINAL_PORT_3) \
168+
--baud $(UPLOAD_SPEED) \
169+
--before default_reset \
170+
--after hard_reset \
171+
write_flash \
172+
--flash_mode dio \
173+
--flash_freq 80m \
174+
--flash_size detect \
175+
--compress \
176+
0x1000 $(BOOT_LOADER_PATH)/RTK_Surveyor.ino.bootloader.bin \
177+
0x8000 $(BOOT_LOADER_PATH)/RTK_Surveyor_Partitions_16MB.bin \
178+
0xe000 $(BOOT_LOADER_PATH)/boot_app0.bin \
179+
0x10000 $<
180+
$(TERMINAL_APP) -D $(TERMINAL_PORT_3) $(TERMINAL_PARAMS)
181+
182+
##########
183+
# Terminal
184+
##########
185+
186+
.PHONY: terminal
187+
188+
terminal:
189+
$(TERMINAL_APP) -D $(TERMINAL_PORT) $(TERMINAL_PARAMS)
190+
191+
.PHONY: terminal2
192+
193+
terminal2:
194+
$(TERMINAL_APP) -D $(TERMINAL_PORT_2) $(TERMINAL_PARAMS)
195+
196+
.PHONY: terminal3
197+
198+
terminal3:
199+
$(TERMINAL_APP) -D $(TERMINAL_PORT_3) $(TERMINAL_PARAMS)
200+
201+
##########
202+
# Clean up the example
203+
##########
204+
205+
clean:
206+
$(DELETE) build

0 commit comments

Comments
 (0)