Initial commit

This commit is contained in:
mia 2025-09-18 21:29:58 +02:00
commit 413821ffe9
17 changed files with 490 additions and 0 deletions

View file

@ -0,0 +1,41 @@
/*
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 <WiFi.h>
#include <esp_wifi.h>
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(){
pinMode(8, OUTPUT);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.STA.begin();
Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
readMacAddress();
}
void loop(){
digitalWrite(8, 1);
delay(250);
digitalWrite(8, 0);
delay(250);
Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
readMacAddress();
}

View file

@ -0,0 +1,17 @@
// const int sensorPin = 2;
// bool sensorState = false;
//
// void setup() {
// pinMode(sensorPin, INPUT);
// Serial.begin(115200);
// }
//
// void loop() {
// sensorState = digitalRead(sensorPin);
// if (sensorState) {
// Serial.println("Open");
// } else {
// Serial.println("Closed");
// }
// delay(500);
// }

5
door-detector-receiver/.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

View file

@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

View file

@ -0,0 +1,37 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the convention is to give header files names that end with `.h'.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

View file

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into the executable file.
The source code of each library should be placed in a separate directory
("lib/your_library_name/[Code]").
For example, see the structure of the following example libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
Example contents of `src/main.c` using Foo and Bar:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
The PlatformIO Library Dependency Finder will find automatically dependent
libraries by scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

View file

@ -0,0 +1,16 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32-c3-devkitm-1]
platform = espressif32
board = esp32-c3-devkitm-1
framework = arduino
monitor_speed = 115200
debug_tool = cmsis-dap

View file

@ -0,0 +1,63 @@
#include <esp_now.h>
#include <WiFi.h>
// Same as in the sender code, doesn't really matter; kept here for completeness
typedef struct struct_message {
char a[32];
} struct_message;
struct_message myData;
#define LEDPIN 4
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.println(myData.a);
digitalWrite(LEDPIN, 1);
delay(100);
digitalWrite(LEDPIN, 0);
delay(200);
}
void setup() {
pinMode(4, OUTPUT);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
// Normal
digitalWrite(LEDPIN, 1);
delay(100);
digitalWrite(LEDPIN, 0);
delay(100);
// Long
digitalWrite(LEDPIN, 1);
delay(200);
digitalWrite(LEDPIN, 0);
delay(100);
// 2xShort
digitalWrite(LEDPIN, 1);
delay(50);
digitalWrite(LEDPIN, 0);
delay(50);
digitalWrite(LEDPIN, 1);
delay(50);
digitalWrite(LEDPIN, 0);
delay(50);
}
void loop() {
// stay silly :3
}

View file

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html

5
door-detector-sender/.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

View file

@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

View file

@ -0,0 +1,37 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the convention is to give header files names that end with `.h'.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

View file

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into the executable file.
The source code of each library should be placed in a separate directory
("lib/your_library_name/[Code]").
For example, see the structure of the following example libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
Example contents of `src/main.c` using Foo and Bar:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
The PlatformIO Library Dependency Finder will find automatically dependent
libraries by scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

View file

@ -0,0 +1,16 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32-c3-devkitm-1]
platform = espressif32
board = esp32-c3-devkitm-1
framework = arduino
monitor_speed = 115200
debug_tool = cmsis-dap

View file

@ -0,0 +1,92 @@
#include <esp_now.h>
#include <WiFi.h>
// Receiver MAC Address
uint8_t broadcastAddress[] = {0x34, 0xCD, 0xB0, 0xD0, 0x76, 0x40};
// Data to send, dose not matter in this code as any data will make the receiver blink
typedef struct struct_message {
char a[16];
} struct_message;
struct_message myData;
esp_now_peer_info_t peerInfo;
#define LEDPIN 0
#define REEDSENSORPIN 4
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
pinMode(REEDSENSORPIN, INPUT);
pinMode(LEDPIN, OUTPUT);
Serial.begin(115200);
Serial.println("Hello World!");
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
// 2xShort
digitalWrite(LEDPIN, 1);
delay(50);
digitalWrite(LEDPIN, 0);
delay(50);
digitalWrite(LEDPIN, 1);
delay(50);
digitalWrite(LEDPIN, 0);
delay(50);
// Long
digitalWrite(LEDPIN, 1);
delay(200);
digitalWrite(LEDPIN, 0);
delay(100);
// Normal
digitalWrite(LEDPIN, 1);
delay(100);
digitalWrite(LEDPIN, 0);
delay(100);
}
void loop() {
// Set values to send
strcpy(myData.a, "Heartbeat");
if (!digitalRead(REEDSENSORPIN)) {
digitalWrite(LEDPIN, 1);
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
} else {
digitalWrite(LEDPIN, 0);
}
delay(300);
}

View file

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html

27
readme.md Normal file
View file

@ -0,0 +1,27 @@
# ESP Door detector
## Hardware used
- 2x ESP32-C3 Supermini
- 1x Red LED
- 1x White LED
- 1x Generich Reed Sensor
- Resistors:
- 2x about 75Ω, I used the closest I had which was a 220Ω in series with a 100Ω Resistor (using a singe 100Ω Resistor each might also work, tho at lower brightness)
## Software used
- VScode with PlatformIO
## Install / Usage
- You can change the used pins at the top of each `main.cpp`, the defaults are
- **Receiver**
- LED `4`
- **Transmitter/Sender**
- LED `0`
- Reed Sensor `4`
- Upload door-detector-sender to the ESP that will be installed a Door
- Upload door-detector-receiver to the ESP that will be your indicator (for example besides your monitor)
- Test if everything works correctly **BEFORE** soldering the components, I reccomend to use a Oszylloscope to check if the receiving ESP pulses at about 3Hz (300ms) on pin 4
- If everything works correctly, you can then start by twisting together one 220Ω and one 100Ω Resistor
- Now Solder one end of the Resistor-packs to the shorter leg of each LED, then solder the other end to the Ground pin (G) on the ESP
- Continue by soldering the other Leg of each LED to the specified Pin above
- At this State you should be able to plug both ESPs in and see each one shortly blinking at boot. After a few seconds, the Receiver's LED should start to blink at 3Hz. If not, check if each ESPnd for any shorts on the boards