From 364733cbba3cad3076ab76d54a8097b1c066279f Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 10 May 2025 19:59:47 +0200 Subject: [PATCH] Initial commit --- .clang-format | 39 +++++++++++++++++++ README.org | 17 +++++++++ client/.gitignore | 3 ++ client/include/README | 37 ++++++++++++++++++ client/lib/README | 46 ++++++++++++++++++++++ client/platformio.ini | 27 +++++++++++++ client/src/main.cpp | 66 ++++++++++++++++++++++++++++++++ client/src/secrets.h.example | 4 ++ client/test/README | 11 ++++++ makefile | 33 ++++++++++++++++ server/.gitignore | 3 ++ server/include/README | 37 ++++++++++++++++++ server/lib/README | 46 ++++++++++++++++++++++ server/platformio.ini | 27 +++++++++++++ server/src/main.cpp | 74 ++++++++++++++++++++++++++++++++++++ server/src/secrets.h.example | 4 ++ server/test/README | 11 ++++++ 17 files changed, 485 insertions(+) create mode 100644 .clang-format create mode 100644 README.org create mode 100644 client/.gitignore create mode 100644 client/include/README create mode 100644 client/lib/README create mode 100644 client/platformio.ini create mode 100644 client/src/main.cpp create mode 100644 client/src/secrets.h.example create mode 100644 client/test/README create mode 100644 makefile create mode 100644 server/.gitignore create mode 100644 server/include/README create mode 100644 server/lib/README create mode 100644 server/platformio.ini create mode 100644 server/src/main.cpp create mode 100644 server/src/secrets.h.example create mode 100644 server/test/README diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..3afd345 --- /dev/null +++ b/.clang-format @@ -0,0 +1,39 @@ +# -*- yaml -*- + +--- +BasedOnStyle: WebKit +IndentWidth: 4 +--- +Language: Cpp + +AlignAfterOpenBracket: Align +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: true + +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortLambdasOnASingleLine: All + +AlwaysBreakTemplateDeclarations: Yes +IndentPPDirectives: BeforeHash + +BraceWrapping: + AfterEnum: false + AfterFunction: true + BeforeCatch: true + BeforeElse: true + BeforeLambdaBody: false + SplitEmptyRecord: false +BreakBeforeBraces: Custom +BreakInheritanceList: BeforeComma + +SpaceAfterTemplateKeyword: false +SpaceInEmptyBlock: false +NamespaceIndentation: None +FixNamespaceComments: true +Standard: c++20 +TabWidth: 4 +UseTab: AlignWithSpaces +... diff --git a/README.org b/README.org new file mode 100644 index 0000000..64ea10c --- /dev/null +++ b/README.org @@ -0,0 +1,17 @@ +* Powerbutton + +** Init + +platformio init --board esp32-c3-devkitm-1 + +** Build + +platformio run + +** Upload + +platformio run --target upload + +** Console + +platformio device monitor diff --git a/client/.gitignore b/client/.gitignore new file mode 100644 index 0000000..38d656e --- /dev/null +++ b/client/.gitignore @@ -0,0 +1,3 @@ +.pio + +src/secrets.h diff --git a/client/include/README b/client/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/client/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/client/lib/README b/client/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/client/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/client/platformio.ini b/client/platformio.ini new file mode 100644 index 0000000..b31839a --- /dev/null +++ b/client/platformio.ini @@ -0,0 +1,27 @@ +; 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 +board_build.mcu = esp32c3 +framework = arduino + +build_flags = + -DARDUINO_USB_CDC_ON_BOOT=1 + -DARDUINO_USB_MODE=1 + -DARDUINO_TINYUSB=1 + -DCORE_DEBUG_LEVEL=5 ; set the debug level (0-5, with 5 being the most verbose) + +lib_deps = + +monitor_filters = + esp32_exception_decoder + time ; add timestamp with milliseconds for each new line diff --git a/client/src/main.cpp b/client/src/main.cpp new file mode 100644 index 0000000..6174235 --- /dev/null +++ b/client/src/main.cpp @@ -0,0 +1,66 @@ +#include +#include +#include + +#include "secrets.h" + +// Default IP address = 192.168.4.1 + +#define HOST "192.168.4.1" +#define PORT 1234 + +void setup() +{ + Serial.begin(9600); + Serial.setDebugOutput(true); + + pinMode(2, OUTPUT); + + // Wait for a USB connection to be established + while (!Serial) + (void)0; + delay(1000); + Serial.println("Client booted!"); + + // Connect to button server + WiFi.begin(SSID, PASSWORD); + Serial.print("Connecting"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("\nConnected to AP"); +} + +void loop() +{ + digitalWrite(2, HIGH); + delay(500); + digitalWrite(2, LOW); + delay(500); + + Serial.println("ESP32 loopy!"); + + WiFiClient client; + if (client.connect(HOST, PORT)) { + client.print("button_pressed\n"); + Serial.println("Sent button press"); + + while (client.connected()) { + if (client.available()) { + // Wait for acknowledgment from the receiver + String response = client.readStringUntil('\n'); + if (response == "ACK") { + Serial.println("Received ACK"); + break; + } + } + } + + delay(2000); + client.stop(); + } + else { + Serial.println("Connection failed"); + } +} diff --git a/client/src/secrets.h.example b/client/src/secrets.h.example new file mode 100644 index 0000000..454930f --- /dev/null +++ b/client/src/secrets.h.example @@ -0,0 +1,4 @@ +#pragma once + +#define SSID "EXAMPLE" +#define PASSWORD "12345678" diff --git a/client/test/README b/client/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/client/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/makefile b/makefile new file mode 100644 index 0000000..4e95609 --- /dev/null +++ b/makefile @@ -0,0 +1,33 @@ +.PHONY: server server-log server-run client client-log client-run + +server: + @-cd server ; \ + platformio run --target upload ; \ + cd .. + +server-log: + @-cd server ; \ + platformio device monitor ; \ + cd .. + +server-run: + @-cd server ; \ + platformio run --target upload ; \ + platformio device monitor ; \ + cd .. + +client: + @-cd client ; \ + platformio run --target upload ; \ + cd .. + +client-log: + @-cd client ; \ + platformio device monitor ; \ + cd .. + +client-run: + @-cd client ; \ + platformio run --target upload ; \ + platformio device monitor ; \ + cd .. diff --git a/server/.gitignore b/server/.gitignore new file mode 100644 index 0000000..38d656e --- /dev/null +++ b/server/.gitignore @@ -0,0 +1,3 @@ +.pio + +src/secrets.h diff --git a/server/include/README b/server/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/server/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/server/lib/README b/server/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/server/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/server/platformio.ini b/server/platformio.ini new file mode 100644 index 0000000..b31839a --- /dev/null +++ b/server/platformio.ini @@ -0,0 +1,27 @@ +; 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 +board_build.mcu = esp32c3 +framework = arduino + +build_flags = + -DARDUINO_USB_CDC_ON_BOOT=1 + -DARDUINO_USB_MODE=1 + -DARDUINO_TINYUSB=1 + -DCORE_DEBUG_LEVEL=5 ; set the debug level (0-5, with 5 being the most verbose) + +lib_deps = + +monitor_filters = + esp32_exception_decoder + time ; add timestamp with milliseconds for each new line diff --git a/server/src/main.cpp b/server/src/main.cpp new file mode 100644 index 0000000..45ef07a --- /dev/null +++ b/server/src/main.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +#include "secrets.h" + +// Default IP address = 192.168.4.1 + +#define PORT 1234 +#define CHANNEL 11 +#define HIDDEN true +#define MAX_CONNECTION 1 + +WiFiServer server(PORT); + +void setup() +{ + Serial.begin(9600); + Serial.setDebugOutput(true); + + pinMode(2, OUTPUT); + + // Wait for a USB connection to be established + while (!Serial) + (void)0; + delay(3000); + Serial.println("Server booted!"); + + // Start button server + WiFi.softAP(SSID, PASSWORD, CHANNEL, HIDDEN, MAX_CONNECTION); + Serial.println("AP Started"); + server.begin(); +} + +void loop() +{ + digitalWrite(2, HIGH); + delay(500); + digitalWrite(2, LOW); + delay(500); + + Serial.println("ESP32 loopy!"); + + // ------------------------------------- + + WiFiClient client = server.available(); + if (client) { + Serial.println("Client connected"); + + // unsigned long start = 0; + while (client.connected()) { + if (client.available()) { + String msg = client.readStringUntil('\n'); + Serial.print("Received: "); + Serial.println(msg); + + // Do something with the message, like toggling an LED + + client.print("ACK\n"); + Serial.println("Sent ACK"); + // start = millis(); + } + + // // Kill lingering connections + // if (start != 0 && millis() - start < 1000) { + // Serial.println("Client kill.."); + // break; + // } + } + client.stop(); + Serial.println("Client disconnected"); + } +} diff --git a/server/src/secrets.h.example b/server/src/secrets.h.example new file mode 100644 index 0000000..454930f --- /dev/null +++ b/server/src/secrets.h.example @@ -0,0 +1,4 @@ +#pragma once + +#define SSID "EXAMPLE" +#define PASSWORD "12345678" diff --git a/server/test/README b/server/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/server/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