├── .gitattributes ├── ESP32_UWB_Buzzer └── ESP32_UWB_Buzzer.ino └── ESP32_UWB_Button_Display └── ESP32_UWB_Button_Display.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /ESP32_UWB_Buzzer/ESP32_UWB_Buzzer.ino: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include "DW1000Ranging.h" 6 | 7 | #define buzzer 2 8 | 9 | #define ANCHOR_ADD "83:17:5B:D5:A9:9A:E2:9C" // Add your Anchor Address 10 | 11 | #define SPI_SCK 18 12 | #define SPI_MISO 19 13 | #define SPI_MOSI 23 14 | #define DW_CS 4 15 | #define CHANNEL 0 16 | 17 | const uint8_t PIN_RST = 27; // reset pin 18 | const uint8_t PIN_IRQ = 34; // irq pin 19 | const uint8_t PIN_SS = 4; // spi select pin 20 | 21 | 22 | void setup() 23 | { 24 | Serial.begin(115200); 25 | 26 | Serial.println("Code start"); 27 | 28 | // Set device as a Wi-Fi Station 29 | WiFi.mode(WIFI_STA); 30 | 31 | // Init ESP-NOW 32 | if (esp_now_init() != ESP_OK) { 33 | Serial.println("Error initializing ESP-NOW"); 34 | return; 35 | } 36 | 37 | pinMode(buzzer, OUTPUT); 38 | 39 | Serial.println("ESPNow/Basic/Slave Example"); 40 | //Set device in AP mode to begin with 41 | Serial.print("STA MAC: "); Serial.println(WiFi.macAddress()); 42 | // Init ESPNow with a fallback logic 43 | // InitESPNow(); 44 | // Once ESPNow is successfully Init, we will register for recv CB to 45 | // get recv packer info. 46 | esp_now_register_recv_cb(OnDataRecv); 47 | SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI); 48 | DW1000Ranging.initCommunication(PIN_RST, PIN_SS, PIN_IRQ); //Reset, CS, IRQ pin 49 | //define the sketch as anchor. It will be great to dynamically change the type of module 50 | DW1000Ranging.attachNewRange(newRange); 51 | DW1000Ranging.attachBlinkDevice(newBlink); 52 | DW1000Ranging.attachInactiveDevice(inactiveDevice); 53 | //Enable the filter to smooth the distance 54 | //DW1000Ranging.useRangeFilter(true); 55 | 56 | //we start the module as an anchor 57 | // DW1000Ranging.startAsAnchor("82:17:5B:D5:A9:9A:E2:9C", DW1000.MODE_LONGDATA_RANGE_ACCURACY); 58 | 59 | DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_RANGE_LOWPOWER, false); 60 | // DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_SHORTDATA_FAST_LOWPOWER); 61 | // DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_FAST_LOWPOWER); 62 | // DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_SHORTDATA_FAST_ACCURACY); 63 | // DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_FAST_ACCURACY); 64 | // DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_RANGE_ACCURACY); 65 | } 66 | 67 | // callback when data is recv from Master 68 | void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { 69 | char macStr[18]; 70 | snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", 71 | mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); 72 | Serial.print("Last Packet Recv from: "); Serial.println(macStr); 73 | Serial.print("Last Packet Recv Data: "); Serial.println(*data); 74 | Serial.println(""); 75 | if (*data == 0) 76 | play_tune(); 77 | } 78 | 79 | void loop() 80 | { 81 | 82 | DW1000Ranging.loop();// Handling UWB 83 | } 84 | void newRange() 85 | { 86 | Serial.print("from: "); 87 | Serial.print(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX); 88 | Serial.print("\t Range: "); 89 | Serial.print(DW1000Ranging.getDistantDevice()->getRange()); 90 | Serial.print(" m"); 91 | Serial.print("\t RX power: "); 92 | Serial.print(DW1000Ranging.getDistantDevice()->getRXPower()); 93 | Serial.println(" dBm"); 94 | } 95 | 96 | void newBlink(DW1000Device *device) 97 | { 98 | Serial.print("blink; 1 device added ! -> "); 99 | Serial.print(" short:"); 100 | Serial.println(device->getShortAddress(), HEX); 101 | } 102 | 103 | void inactiveDevice(DW1000Device *device) 104 | { 105 | Serial.print("delete inactive device: "); 106 | Serial.println(device->getShortAddress(), HEX); 107 | } 108 | 109 | void play_tune() 110 | { 111 | for (int i = 0; i < 5; i++) 112 | { 113 | digitalWrite(buzzer, HIGH); 114 | delay(100); 115 | digitalWrite(buzzer, LOW); 116 | delay(200); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /ESP32_UWB_Button_Display/ESP32_UWB_Button_Display.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "DW1000Ranging.h" 5 | #include 6 | #include 7 | #include 8 | #include "Simpletimer.h" 9 | 10 | Simpletimer timer; 11 | 12 | #define button 0 13 | 14 | // REPLACE WITH THE MAC Address of your receiver 15 | uint8_t broadcastAddress[] = {0x40, 0x91, 0x51, 0xB9, 0x5B, 0xAC}; 16 | 17 | #define SPI_SCK 18 18 | #define SPI_MISO 19 19 | #define SPI_MOSI 23 20 | #define DW_CS 4 21 | 22 | // OLED Configurations 23 | #define SCREEN_WIDTH 128 // OLED display width, in pixels 24 | #define SCREEN_HEIGHT 64 // OLED display height, in pixels 25 | #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) 26 | #define SCREEN_ADDRESS 0x3c 27 | Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 28 | 29 | 30 | // connection pins 31 | const uint8_t PIN_RST = 27; // reset pin 32 | const uint8_t PIN_IRQ = 34; // irq pin 33 | const uint8_t PIN_SS = 4; // spi select pin 34 | 35 | float distance; 36 | 37 | // Variable to store if sending data was successful 38 | String success; 39 | // Callback when data is sent 40 | void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { 41 | Serial.print("\r\nLast Packet Send Status:\t"); 42 | Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); 43 | if (status == 0) { 44 | success = "Delivery Success :)"; 45 | } 46 | else { 47 | success = "Delivery Fail :("; 48 | } 49 | } 50 | void callback() 51 | { 52 | uint8_t mydata = 0; 53 | mydata = digitalRead(0); 54 | esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &mydata, sizeof(mydata)); 55 | } 56 | 57 | void setup() 58 | { 59 | Serial.begin(115200); 60 | delay(1000); 61 | 62 | pinMode(button, INPUT); 63 | 64 | 65 | //init the configuration 66 | SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI); 67 | DW1000Ranging.initCommunication(PIN_RST, PIN_SS, PIN_IRQ); //Reset, CS, IRQ pin 68 | //define the sketch as anchor. It will be great to dynamically change the type of module 69 | DW1000Ranging.attachNewRange(newRange); 70 | DW1000Ranging.attachNewDevice(newDevice); 71 | DW1000Ranging.attachInactiveDevice(inactiveDevice); 72 | //Enable the filter to smooth the distance 73 | //DW1000Ranging.useRangeFilter(true); 74 | 75 | //we start the module as a tag 76 | DW1000Ranging.startAsTag("7D:00:22:EA:82:60:3B:9C", DW1000.MODE_LONGDATA_RANGE_LOWPOWER); 77 | if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { 78 | Serial.println(F("SSD1306 allocation failed")); 79 | for (;;); // Don't proceed, loop forever 80 | } 81 | 82 | // Show initial display buffer contents on the screen -- 83 | // the library initializes this with an Adafruit splash screen. 84 | display.display(); 85 | delay(2000); // Pause for 2 seconds 86 | display.clearDisplay(); 87 | 88 | // Draw a single pixel in white 89 | display.drawPixel(10, 10, SSD1306_WHITE); 90 | 91 | // Show the display buffer on the screen. You MUST call display() after 92 | // drawing commands to make them visible on screen! 93 | display.display(); 94 | delay(2000); 95 | Serial.println("Code start"); 96 | 97 | // Set device as a Wi-Fi Station 98 | WiFi.mode(WIFI_STA); 99 | 100 | // Init ESP-NOW 101 | if (esp_now_init() != ESP_OK) { 102 | Serial.println("Error initializing ESP-NOW"); 103 | return; 104 | } 105 | 106 | // Register peer 107 | esp_now_peer_info_t peerInfo; 108 | memset(&peerInfo, 0, sizeof(peerInfo)); 109 | for (int ii = 0; ii < 6; ++ii ) 110 | { 111 | peerInfo.peer_addr[ii] = (uint8_t) broadcastAddress[ii]; 112 | } 113 | peerInfo.channel = 0; 114 | peerInfo.encrypt = false; 115 | 116 | // Add peer 117 | if (esp_now_add_peer(&peerInfo) != ESP_OK) { 118 | Serial.println("Failed to add peer"); 119 | return; 120 | } 121 | 122 | // Once ESPNow is successfully Init, we will register for Send CB to 123 | // get the status of Trasnmitted packet 124 | esp_now_register_send_cb(OnDataSent); 125 | 126 | // timer intialisation 127 | timer.register_callback(callback); 128 | 129 | } 130 | 131 | void loop() 132 | { 133 | timer.run(2000); // Responsible for transmitting data via ESPNOW 134 | 135 | DW1000Ranging.loop(); // Handling UWB 136 | 137 | // Displaying Data on OLED Screen 138 | display.clearDisplay(); 139 | display.setTextSize(2); 140 | display.setCursor(20, 10); 141 | display.print("Tag Dist "); 142 | display.setTextSize(4); // Normal 1:1 pixel scale 143 | display.setTextColor(SSD1306_WHITE); // Draw white text 144 | display.setCursor(20, 35); // Start at top-left corner 145 | display.cp437(true); // Use full 256 char 'Code Page 437' font 146 | display.print(DW1000Ranging.getDistantDevice()->getRange()); 147 | display.display(); 148 | } 149 | 150 | 151 | void newRange() 152 | { 153 | Serial.print("from: "); 154 | Serial.print(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX); 155 | Serial.print("\t Range: "); 156 | Serial.print(DW1000Ranging.getDistantDevice()->getRange()); 157 | Serial.print(" m"); 158 | Serial.print("\t RX power: "); 159 | Serial.print(DW1000Ranging.getDistantDevice()->getRXPower()); 160 | Serial.println(" dBm"); 161 | distance = DW1000Ranging.getDistantDevice()->getRange(); 162 | 163 | 164 | } 165 | 166 | void newDevice(DW1000Device *device) 167 | { 168 | Serial.print("ranging init; 1 device added ! -> "); 169 | Serial.print(" short:"); 170 | Serial.println(device->getShortAddress(), HEX); 171 | } 172 | 173 | void inactiveDevice(DW1000Device *device) 174 | { 175 | Serial.print("delete inactive device: "); 176 | Serial.println(device->getShortAddress(), HEX); 177 | } 178 | --------------------------------------------------------------------------------