From 611cde59f8363a27adef7b9ae845eb4e46e59102 Mon Sep 17 00:00:00 2001 From: "Misael.K" Date: Fri, 27 Sep 2024 21:59:43 -0300 Subject: [PATCH] Add scan_i2c and read_mac helpers --- esp32_espnow/helpers/read_mac/read_mac.ino | 35 +++++++++++++++ esp32_espnow/helpers/scan_i2c/scan_i2c.ino | 50 ++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 esp32_espnow/helpers/read_mac/read_mac.ino create mode 100644 esp32_espnow/helpers/scan_i2c/scan_i2c.ino diff --git a/esp32_espnow/helpers/read_mac/read_mac.ino b/esp32_espnow/helpers/read_mac/read_mac.ino new file mode 100644 index 0000000..7b61e33 --- /dev/null +++ b/esp32_espnow/helpers/read_mac/read_mac.ino @@ -0,0 +1,35 @@ +/* + Rui Santos & Sara Santos - Random Nerd Tutorials + Complete project details at https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/ + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +*/ +#include +#include + +void readMacAddress(){ + uint8_t baseMac[6]; + esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac); + if (ret == ESP_OK) { + Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n", + baseMac[0], baseMac[1], baseMac[2], + baseMac[3], baseMac[4], baseMac[5]); + } else { + Serial.println("Failed to read MAC address"); + } +} + +void setup(){ + Serial.begin(115200); + + WiFi.mode(WIFI_STA); + WiFi.STA.begin(); +} + +void loop(){ + + Serial.print("[DEFAULT] ESP32 Board MAC Address: "); + readMacAddress(); + delay(1000); + +} diff --git a/esp32_espnow/helpers/scan_i2c/scan_i2c.ino b/esp32_espnow/helpers/scan_i2c/scan_i2c.ino new file mode 100644 index 0000000..4eced92 --- /dev/null +++ b/esp32_espnow/helpers/scan_i2c/scan_i2c.ino @@ -0,0 +1,50 @@ +// error 5 at address 0x3B + +#include + +void setup() { + Serial.begin(115200); + Serial.println("SETUP"); + + Wire.setClock(100000); + delay(50); + Wire.begin(21, 22, 100000); // sda, sdl, freq + delay(50); + Wire.setTimeout(50000); //us +} + +void loop() { + byte error, address; + int nDevices; + + Serial.println("Scanning..."); + nDevices = 0; + for (address = 1; address <= 127; address++ ) { + Wire.beginTransmission(address); + error = Wire.endTransmission(true); + if (error == 0) { + Serial.print("I2C device found at address 0x"); + if (address < 16) Serial.print("0"); + Serial.println(address, HEX); + nDevices++; + } else if (error == 2) { + // ignore + } else if (error == 4) { + Serial.print("Unknown error at address 0x"); + if (address < 16) Serial.print("0"); + Serial.println(address, HEX); + } else { + Serial.print("error "); + Serial.print(error); + Serial.print(" at address 0x"); + if (address < 16) Serial.print("0"); + Serial.println(address, HEX); + } + } + if (nDevices == 0) { + Serial.println("No I2C devices found\n"); + } else { + Serial.println("done\n"); + } + delay(1000); +}