diff --git a/Example_Sketches/4_9_ESP_NOW/4_9_ESP_NOW.ino b/Example_Sketches/4_9_ESP_NOW/4_9_ESP_NOW.ino new file mode 100644 index 0000000..129d656 --- /dev/null +++ b/Example_Sketches/4_9_ESP_NOW/4_9_ESP_NOW.ino @@ -0,0 +1,114 @@ +/* + ESP-NOW Serial Example - Unicast transmission + Lucas Saavedra Vaz - 2024 + Send data between two ESP32s using the ESP-NOW protocol in one-to-one (unicast) configuration. + Note that different MAC addresses are used for different interfaces. + The devices can be in different modes (AP or Station) and still communicate using ESP-NOW. + The only requirement is that the devices are on the same Wi-Fi channel. + Set the peer MAC address according to the device that will receive the data. + + Example setup: + - Device 1: AP mode with MAC address F6:12:FA:42:B6:E8 + Peer MAC address set to the Station MAC address of Device 2 (F4:12:FA:40:64:4C) + - Device 2: Station mode with MAC address F4:12:FA:40:64:4C + Peer MAC address set to the AP MAC address of Device 1 (F6:12:FA:42:B6:E8) + + The device running this sketch will also receive and print data from any device that has its MAC address set as the peer MAC address. + To properly visualize the data being sent, set the line ending in the Serial Monitor to "Both NL & CR". +*/ + +#include "ESP32_NOW_Serial.h" +#include "MacAddress.h" +#include "WiFi.h" + +#include "esp_wifi.h" + +#define DEFAULT_MODE WIFI_MODE_STA +//#define DEFAULT_MODE WIFI_MODE_AP +#define DEFAULT_CHANNEL 1 + +// Set the MAC address of the device that will receive the data +const MacAddress macDev1({0x48, 0xE7, 0x29, 0x9A, 0xD6, 0x88}); +const MacAddress macDev2({0xE0, 0x5A, 0x1B, 0xD8, 0x8F, 0x14}); + +const char * wifiApSsid = "SoftAP"; +const char * wifiApPassword = "Password"; + +ESP_NOW_Serial_Class * nowSerial; + +void setup() +{ + int32_t channel; + wifi_interface_t interface; + MacAddress macLocal; + MacAddress macPeer; + wifi_mode_t mode; + + Serial.begin(115200); + Serial.printf("\r\n"); + Serial.printf("%s\r\n", __FILE__); + + // Determine the WiFi mode + mode = WiFi.getMode(); + if (mode == 0) + { + mode = DEFAULT_MODE; + if (mode == WIFI_MODE_AP) + WiFi.softAP(wifiApSsid, wifiApPassword); + else + WiFi.mode(mode); + } + Serial.printf("WiFi Mode: %s\r\n", (mode == WIFI_MODE_AP) ? "AP" : "Station"); + + // Determine the interface + interface = (mode == WIFI_MODE_AP) ? WIFI_IF_AP : WIFI_IF_STA; + + // Determine the WiFi channel + channel = WiFi.channel(); + if (channel == 0) + { + channel = DEFAULT_CHANNEL; + WiFi.setChannel(channel, WIFI_SECOND_CHAN_NONE); + } + Serial.printf("Channel: %d\r\n", channel); + + // Start WiFi if not already running + while (!(WiFi.STA.started() || WiFi.AP.started())) + { + delay(100); + } + + // Get the local MAC address + macLocal = (mode == WIFI_MODE_AP) ? WiFi.softAPmacAddress() : WiFi.macAddress(); + Serial.printf("MAC Address: %s\r\n", macLocal.toString().c_str()); + + // Determine the peer MAC address + macPeer = (macLocal == macDev1) ? macDev2 : macDev1; + + // Create the ESPNow serial object + nowSerial = new ESP_NOW_Serial_Class(macPeer, channel, interface); + + // Start the ESP-NOW communication + Serial.println("ESP-NOW communication starting..."); + nowSerial->begin(115200); + Serial.println("You can now send data to the peer device using the Serial Monitor.\n"); +} + +void loop() +{ + while (nowSerial->available()) + { + Serial.write(nowSerial->read()); + } + + while (Serial.available() && nowSerial->availableForWrite()) + { + if (nowSerial->write(Serial.read()) <= 0) + { + Serial.println("Failed to send data"); + break; + } + } + + delay(1); +} diff --git a/Example_Sketches/4_9_ESP_NOW/makefile b/Example_Sketches/4_9_ESP_NOW/makefile new file mode 100644 index 0000000..5c4c15f --- /dev/null +++ b/Example_Sketches/4_9_ESP_NOW/makefile @@ -0,0 +1,179 @@ +###################################################################### +# makefile +# +# Builds the example +###################################################################### + +########## +# Source files +########## + +EXAMPLE_SKETCH=4_9_ESP_NOW + +EXECUTABLES += example + +PARTITION_CSV_FILE=RTKEverywhere + +ifeq ($(OS),Windows_NT) +# Windows NT utilities +CLEAR=cls +COPY=copy +DELETE=rmdir /s +DIR_LISTING=dir +TERMINAL_APP= +TERMINAL_PORT="COM3" +TERMINAL_PARAMS= + +# Windows NT generic paths +USER_DIRECTORY_PATH=C:\Users\$(USERNAME) +ARDUINO_LIBRARY_PATH=$(USER_DIRECTORY_PATH)\Documents\Arduino\libraries +HOME_BOARD_PATH=$(USER_DIRECTORY_PATH)\AppData\Local\Arduino15\packages\esp32 +PATCH_SRC_PATH=Patch\ + +# Windows NT patch source paths +PARTITION_SRC_PATH=..\$(PARTITION_CSV_FILE).csv +PATCH_SRC_PATH=Patch\ + +# Windows NT patch destination paths +BLE_PATCH_DST_PATH=$(ARDUINO_LIBRARY_PATH)\ESP32_BleSerial\src\ +MBED_LIB_DEST_PATH=$(HOME_BOARD_PATH)\tools\esp32-arduino-libs\${{ env.ESP_IDF }}\esp32/lib\ +PARTITION_DST_PATH=$(HOME_BOARD_PATH)\hardware\esp32\$(ESP_CORE_VERSION)\tools\partitions\$(PARTITION_CSV_FILE).csv + +else +# Linux utilities +CLEAR=clear +COPY=cp +DELETE=rm -Rf +DIR_LISTING=ls +TERMINAL_APP=minicom +#TERMINAL_PORT="/dev/ttyACM1" +TERMINAL_PORT="/dev/ttyUSB0" +TERMINAL_PARAMS=-b 115200 -8 < /dev/tty + +# Linux generic paths +USER_DIRECTORY_PATH=~ +ARDUINO_LIBRARY_PATH=$(USER_DIRECTORY_PATH)/Arduino/libraries +ESP_IDF_PATH=$(HOME_BOARD_PATH)/tools/esp32-arduino-libs +HOME_BOARD_PATH=$(USER_DIRECTORY_PATH)/.arduino15/packages/esp32 + +# Linux patch source paths +PARTITION_SRC_PATH=../$(PARTITION_CSV_FILE).csv +PATCH_SRC_PATH=Patch/ + +# Linux patch destination paths +BLE_PATCH_DST_PATH=$(ARDUINO_LIBRARY_PATH)/ESP32_BleSerial/src/ +MBED_LIB_DEST_PATH=$(ESP_IDF_PATH)/$(ESP_IDF_VERSION)/esp32/lib/ +PARTITION_DST_PATH=$(HOME_BOARD_PATH)/hardware/esp32/$(ESP_CORE_VERSION)/tools/partitions/$(PARTITION_CSV_FILE).csv + +endif + +########## +# Buid all the sources - must be first +########## + +.PHONY: all + +all: $(EXECUTABLES) + +########## +# Add ESP32 board support +########## + +.PHONY: arduino-config + +arduino-config: + 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" + +########## +# Build an example +########## + +.PHONY: example + +example: build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin + +DEBUG_LEVEL=none +#DEBUG_LEVEL=debug +PSRAM=disabled +#PSRAM=enabled + +build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin: $(EXAMPLE_SKETCH).ino *.ino makefile + $(CLEAR) + arduino-cli compile --fqbn "esp32:esp32:esp32":DebugLevel=$(DEBUG_LEVEL),PSRAM=$(PSRAM) $< \ + --warnings default \ + --build-property build.partitions=$(PARTITION_CSV_FILE) \ + --build-property upload.maximum_size=6291456 \ + --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)\"" \ + --export-binaries + +########## +# Upload the example +########## + +ESPTOOL_PATH=~/Arduino/hardware/espressif/esp32/tools/esptool +#UPLOAD_SPEED=460800 +UPLOAD_SPEED=921600 +BOOT_LOADER_PATH=~/SparkFun/SparkFun_RTK_Firmware_Uploader/RTK_Firmware_Uploader/resource + +.PHONY: upload + +upload: build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin + python3 $(ESPTOOL_PATH)/esptool.py \ + --chip esp32 \ + --port $(TERMINAL_PORT) \ + --baud $(UPLOAD_SPEED) \ + --before default_reset \ + --after hard_reset \ + write_flash \ + --flash_mode dio \ + --flash_freq 80m \ + --flash_size detect \ + --compress \ + 0x1000 $(BOOT_LOADER_PATH)/RTK_Surveyor.ino.bootloader.bin \ + 0x8000 $(BOOT_LOADER_PATH)/RTK_Surveyor_Partitions_16MB.bin \ + 0xe000 $(BOOT_LOADER_PATH)/boot_app0.bin \ + 0x10000 $< + $(TERMINAL_APP) -D $(TERMINAL_PORT) $(TERMINAL_PARAMS) + +TERMINAL_PORT_2=/dev/ttyUSB1 + +.PHONY: upload2 + +upload2: build/esp32.esp32.esp32/$(EXAMPLE_SKETCH).ino.bin + python3 $(ESPTOOL_PATH)/esptool.py \ + --chip esp32 \ + --port $(TERMINAL_PORT_2) \ + --baud $(UPLOAD_SPEED) \ + --before default_reset \ + --after hard_reset \ + write_flash \ + --flash_mode dio \ + --flash_freq 80m \ + --flash_size detect \ + --compress \ + 0x1000 $(BOOT_LOADER_PATH)/RTK_Surveyor.ino.bootloader.bin \ + 0x8000 $(BOOT_LOADER_PATH)/RTK_Surveyor_Partitions_16MB.bin \ + 0xe000 $(BOOT_LOADER_PATH)/boot_app0.bin \ + 0x10000 $< + $(TERMINAL_APP) -D $(TERMINAL_PORT_2) $(TERMINAL_PARAMS) + +########## +# Terminal +########## + +.PHONY: terminal + +terminal: + $(TERMINAL_APP) -D $(TERMINAL_PORT) $(TERMINAL_PARAMS) + +.PHONY: terminal2 + +terminal2: + $(TERMINAL_APP) -D $(TERMINAL_PORT_2) $(TERMINAL_PARAMS) + +########## +# Clean up the example +########## + +clean: + $(DELETE) build