From 413821ffe9baad00b4e9c8ac704d46c6b53230fd Mon Sep 17 00:00:00 2001 From: mia Date: Thu, 18 Sep 2025 21:29:58 +0200 Subject: [PATCH] Initial commit --- door-detector-arduino/checkMAC.ino | 41 +++++++++ .../door-detector-arduino.ino | 17 ++++ door-detector-receiver/.gitignore | 5 + .../.vscode/extensions.json | 10 ++ door-detector-receiver/include/README | 37 ++++++++ door-detector-receiver/lib/README | 46 ++++++++++ door-detector-receiver/platformio.ini | 16 ++++ door-detector-receiver/src/main.cpp | 63 +++++++++++++ door-detector-receiver/test/README | 11 +++ door-detector-sender/.gitignore | 5 + door-detector-sender/.vscode/extensions.json | 10 ++ door-detector-sender/include/README | 37 ++++++++ door-detector-sender/lib/README | 46 ++++++++++ door-detector-sender/platformio.ini | 16 ++++ door-detector-sender/src/main.cpp | 92 +++++++++++++++++++ door-detector-sender/test/README | 11 +++ readme.md | 27 ++++++ 17 files changed, 490 insertions(+) create mode 100644 door-detector-arduino/checkMAC.ino create mode 100644 door-detector-arduino/door-detector-arduino.ino create mode 100644 door-detector-receiver/.gitignore create mode 100644 door-detector-receiver/.vscode/extensions.json create mode 100644 door-detector-receiver/include/README create mode 100644 door-detector-receiver/lib/README create mode 100644 door-detector-receiver/platformio.ini create mode 100644 door-detector-receiver/src/main.cpp create mode 100644 door-detector-receiver/test/README create mode 100644 door-detector-sender/.gitignore create mode 100644 door-detector-sender/.vscode/extensions.json create mode 100644 door-detector-sender/include/README create mode 100644 door-detector-sender/lib/README create mode 100644 door-detector-sender/platformio.ini create mode 100644 door-detector-sender/src/main.cpp create mode 100644 door-detector-sender/test/README create mode 100644 readme.md diff --git a/door-detector-arduino/checkMAC.ino b/door-detector-arduino/checkMAC.ino new file mode 100644 index 0000000..4cf8fd7 --- /dev/null +++ b/door-detector-arduino/checkMAC.ino @@ -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 +#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(){ + 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(); +} + diff --git a/door-detector-arduino/door-detector-arduino.ino b/door-detector-arduino/door-detector-arduino.ino new file mode 100644 index 0000000..16e115d --- /dev/null +++ b/door-detector-arduino/door-detector-arduino.ino @@ -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); +// } diff --git a/door-detector-receiver/.gitignore b/door-detector-receiver/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/door-detector-receiver/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/door-detector-receiver/.vscode/extensions.json b/door-detector-receiver/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/door-detector-receiver/.vscode/extensions.json @@ -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" + ] +} diff --git a/door-detector-receiver/include/README b/door-detector-receiver/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/door-detector-receiver/include/README @@ -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 diff --git a/door-detector-receiver/lib/README b/door-detector-receiver/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/door-detector-receiver/lib/README @@ -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 +#include + +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 diff --git a/door-detector-receiver/platformio.ini b/door-detector-receiver/platformio.ini new file mode 100644 index 0000000..f40e0fc --- /dev/null +++ b/door-detector-receiver/platformio.ini @@ -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 diff --git a/door-detector-receiver/src/main.cpp b/door-detector-receiver/src/main.cpp new file mode 100644 index 0000000..59a759a --- /dev/null +++ b/door-detector-receiver/src/main.cpp @@ -0,0 +1,63 @@ +#include +#include + +// 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 +} \ No newline at end of file diff --git a/door-detector-receiver/test/README b/door-detector-receiver/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/door-detector-receiver/test/README @@ -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 diff --git a/door-detector-sender/.gitignore b/door-detector-sender/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/door-detector-sender/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/door-detector-sender/.vscode/extensions.json b/door-detector-sender/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/door-detector-sender/.vscode/extensions.json @@ -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" + ] +} diff --git a/door-detector-sender/include/README b/door-detector-sender/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/door-detector-sender/include/README @@ -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 diff --git a/door-detector-sender/lib/README b/door-detector-sender/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/door-detector-sender/lib/README @@ -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 +#include + +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 diff --git a/door-detector-sender/platformio.ini b/door-detector-sender/platformio.ini new file mode 100644 index 0000000..f40e0fc --- /dev/null +++ b/door-detector-sender/platformio.ini @@ -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 diff --git a/door-detector-sender/src/main.cpp b/door-detector-sender/src/main.cpp new file mode 100644 index 0000000..f38b54e --- /dev/null +++ b/door-detector-sender/src/main.cpp @@ -0,0 +1,92 @@ +#include +#include + +// 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); +} diff --git a/door-detector-sender/test/README b/door-detector-sender/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/door-detector-sender/test/README @@ -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 diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..a906492 --- /dev/null +++ b/readme.md @@ -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