Hello I am in a trouble here. I have two DWM1000 anchors connected with their own esp32 and one tag. I want to do localization so I tried to send two distances from anchor 1 and anchor 2 to another esp32 which is main main controller board using ESP NOW. But here issue is when any of the one anchor is on then I received exact distance at main controller board but when I switch on both anchors then also I recieved both distance at main controller board but with a great error even in a negative value which is a matter of tension for me. Can anyone tell me why its happening. I am using library jremington’s library. Also I am providing codes of Anchor1, Anchor2 and main controller board. And also I am giving the image of recieving distance data at main controller board when both anchors are on.
Anchor 1 Code:
#include <SPI.h>
#include “DW1000Ranging.h”
#include “DW1000.h”
#include <WiFi.h>
#include <esp_now.h>
// ---------------------
// DWM1000 / DW1000 Setup
// ---------------------
char anchor_addr = “02:00:00:00:00:00:00:01”; // Unique anchor address
uint16_t Adelay = 16577; // Calibrated antenna delay
#define SPI_SCK 18
#define SPI_MISO 19
#define SPI_MOSI 23
#define DW_CS 4
const uint8_t PIN_RST = 25;
const uint8_t PIN_IRQ = 27;
const uint8_t PIN_SS = 4;
// ---------------------
// ESP-NOW Setup
// ---------------------
typedef struct struct_message {
int anchorID;
float distance;
} struct_message;
struct_message myData;
int anchorID = 1; // This is Anchor #1
// >>> REPLACE WITH ANCHOR #3’s MAC ADDRESS <<<
uint8_t mainControllerAddress = {0xCC, 0x7B, 0x5C, 0xF1, 0xF5, 0xBC};
// Optional: Callback to see if send was successful
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("Packet send status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? “Success” : “Fail”);
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println(“=== Anchor #1 ===”);
Serial.print("Antenna delay: ");
Serial.println(Adelay);
// Initialize DW1000
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
DW1000Ranging.initCommunication(PIN_RST, PIN_SS, PIN_IRQ);
DW1000.setAntennaDelay(Adelay);
DW1000Ranging.attachNewRange(newRange);
DW1000Ranging.attachNewDevice(newDevice);
DW1000Ranging.attachInactiveDevice(inactiveDevice);
// Start as anchor
DW1000Ranging.startAsAnchor(anchor_addr, DW1000.MODE_LONGDATA_RANGE_LOWPOWER, false);
// Initialize ESP-NOW
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}
esp_now_register_send_cb(onDataSent);
// Add main controller (Anchor #3) as a peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, mainControllerAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println(“Failed to add peer”);
return;
}
}
void loop() {
// Continuously process DW1000 ranging
DW1000Ranging.loop();
}
// Called by the library when a new distance is measured
void newRange() {
Serial.print(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX);
Serial.print(", ");
float dist = DW1000Ranging.getDistantDevice()->getRange();
Serial.println(dist);
// Prepare data and send via ESP-NOW
myData.anchorID = anchorID;
myData.distance = dist;
esp_now_send(mainControllerAddress, (uint8_t *)&myData, sizeof(myData));
}
// Called when a new device is discovered
void newDevice(DW1000Device *device) {
Serial.print("Device added: ");
Serial.println(device->getShortAddress(), HEX);
}
// Called when a device becomes inactive
void inactiveDevice(DW1000Device *device) {
Serial.print("Delete inactive device: ");
Serial.println(device->getShortAddress(), HEX);
}
Anchor 2 Code:
#include <SPI.h>
#include “DW1000Ranging.h”
#include “DW1000.h”
#include <WiFi.h>
#include <esp_now.h>
// ---------------------
// DWM1000 / DW1000 Setup
// ---------------------
char anchor_addr = “02:00:00:00:00:00:00:02”;
uint16_t Adelay = 16584;
#define SPI_SCK 18
#define SPI_MISO 19
#define SPI_MOSI 23
#define DW_CS 5
const uint8_t PIN_RST = 16;
const uint8_t PIN_IRQ = 17;
const uint8_t PIN_SS = 5;
// ---------------------
// ESP-NOW Setup
// ---------------------
typedef struct struct_message {
int anchorID;
float distance;
} struct_message;
struct_message myData;
int anchorID = 2; // This is Anchor #2
// >>> REPLACE WITH ANCHOR #3’s MAC ADDRESS <<<
uint8_t mainControllerAddress = {0xCC, 0x7B, 0x5C, 0xF1, 0xF5, 0xBC};
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("Packet send status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? “Success” : “Fail”);
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println(“=== Anchor #2 ===”);
Serial.print("Antenna delay: ");
Serial.println(Adelay);
// Initialize DW1000
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
DW1000Ranging.initCommunication(PIN_RST, PIN_SS, PIN_IRQ);
DW1000.setAntennaDelay(Adelay);
DW1000Ranging.attachNewRange(newRange);
DW1000Ranging.attachNewDevice(newDevice);
DW1000Ranging.attachInactiveDevice(inactiveDevice);
// Start as anchor
DW1000Ranging.startAsAnchor(anchor_addr, DW1000.MODE_LONGDATA_RANGE_LOWPOWER, false);
// Initialize ESP-NOW
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}
esp_now_register_send_cb(onDataSent);
// Add main controller (Anchor #3) as a peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, mainControllerAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println(“Failed to add peer”);
return;
}
}
void loop() {
DW1000Ranging.loop();
}
void newRange() {
Serial.print(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX);
Serial.print(", ");
float dist = DW1000Ranging.getDistantDevice()->getRange();
Serial.println(dist);
myData.anchorID = anchorID;
myData.distance = dist;
esp_now_send(mainControllerAddress, (uint8_t *)&myData, sizeof(myData));
}
void newDevice(DW1000Device *device) {
Serial.print("Device added: ");
Serial.println(device->getShortAddress(), HEX);
}
void inactiveDevice(DW1000Device *device) {
Serial.print("Delete inactive device: ");
Serial.println(device->getShortAddress(), HEX);
}
Main Controller Board:
#include <esp_now.h>
#include <WiFi.h>
// Define the same structure used by the anchor
typedef struct struct_message {
int anchorID;
float distance;
} struct_message;
// Callback function that runs when data is received
void onDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
struct_message receivedData;
memcpy(&receivedData, incomingData, sizeof(receivedData));
Serial.print(“Received from Anchor “);
Serial.print(receivedData.anchorID);
Serial.print(”: Distance = “);
Serial.print(receivedData.distance);
Serial.println(” m”);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}
// Register the receive callback
esp_now_register_recv_cb(onDataRecv);
}
void loop() {
// The main controller simply listens for incoming ESP-NOW packets.
// delay(1000);
}
So please anyone help me so that I can achieve my goal.