commit c81da2f834195f2b306cf229261d10a111c8f897 Author: MadobiNanami Date: Tue Jul 22 21:10:16 2025 +0800 new file: .gitignore new file: .vscode/extensions.json new file: .vscode/settings.json new file: UDP Tester.exe new file: debug.bak new file: debug.e new file: esp_relay_controller_0.1.stable_highActive.bin new file: include/README new file: lib/README new file: platformio.ini new file: src/main.cpp new file: test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.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/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c8daa7a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,108 @@ +{ + "C_Cpp_Runner.cCompilerPath": "C:/Users/FPCLinkYZT114/.platformio/packages/toolchain-xtensa/bin/xtensa-lx106-elf-gcc.exe", + "C_Cpp_Runner.cppCompilerPath": "C:/Users/FPCLinkYZT114/.platformio/packages/toolchain-xtensa/bin/xtensa-lx106-elf-g++.exe", + "C_Cpp_Runner.debuggerPath": "C:/Users/FPCLinkYZT114/.platformio/packages/toolchain-xtensa/bin/xtensa-lx106-elf-gdb.exe", + "C_Cpp_Runner.cStandard": "gnu17", + "C_Cpp_Runner.cppStandard": "gnu++17", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [ + "F:/源码/ESP8266/SmartPlugs_esp8266/include", + "F:/源码/ESP8266/SmartPlugs_esp8266/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/tools/sdk/include", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/cores/esp8266", + "C:/Users/FPCLinkYZT114/.platformio/packages/toolchain-xtensa/include", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lwip2/include", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/variants/nodemcu", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ArduinoOTA", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/DNSServer/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/EEPROM", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266AVRISP/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266HTTPClient/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266HTTPUpdateServer/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266LLMNR", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266NetBIOS", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266SSDP", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266SdFat/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266WebServer/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266WiFi/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266WiFiMesh/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266httpUpdate/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/ESP8266mDNS/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/Ethernet/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/FSTools", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/GDBStub/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/Hash/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/I2S/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/LittleFS/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/Netdump/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/SD/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/SDFS/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/SPI", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/SPISlave/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/Servo/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/SoftwareSerial/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/TFT_Touch_Shield_V2", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/Ticker/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/Wire", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/esp8266/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/lwIP_Ethernet/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/lwIP_PPP/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/lwIP_enc28j60/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/lwIP_w5100/src", + "C:/Users/FPCLinkYZT114/.platformio/packages/framework-arduinoespressif8266/libraries/lwIP_w5500/src", + "" + ], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "idf.portWin": "COM9", + "C_Cpp.errorSquiggles": "disabled", + "idf.adapterTargetName": "esp32s3" +} \ No newline at end of file diff --git a/UDP Tester.exe b/UDP Tester.exe new file mode 100644 index 0000000..11e0c28 Binary files /dev/null and b/UDP Tester.exe differ diff --git a/debug.bak b/debug.bak new file mode 100644 index 0000000..1d64137 Binary files /dev/null and b/debug.bak differ diff --git a/debug.e b/debug.e new file mode 100644 index 0000000..df90934 Binary files /dev/null and b/debug.e differ diff --git a/esp_relay_controller_0.1.stable_highActive.bin b/esp_relay_controller_0.1.stable_highActive.bin new file mode 100644 index 0000000..8ff9977 Binary files /dev/null and b/esp_relay_controller_0.1.stable_highActive.bin differ diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +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 usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +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/lib/README b/lib/README new file mode 100644 index 0000000..2593a33 --- /dev/null +++ b/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 executable file. + +The source code of each library should be placed in an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, 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 + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..d0ce2a6 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,22 @@ +; 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 + +[platform] +name = smartplugs_esp8266 +[env] +upload_speed = 921600 +monitor_encoding = utf-8 +monitor_speed = 115200 +framework = arduino + +[env:esp8266] +platform = espressif8266 +board = nodemcu +board_build.ldscript =eagle.flash.1m.ld \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..a6faedf --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include + +#define relay 0 +int ledPin = 2; +int UdpbeginPacketTest; +int UdpendPacketTest; +int digitalStatus; + +const char *ssid = "YZTRoomWifi"; +const char *password = "fpclink114roomwifi"; + +WiFiUDP Udp; +unsigned int localUdpPort = 443; +char incomingPacket[537]; +WiFiServer server(80); + +void setup(){ + Serial.begin(115200); + Serial.println(""); + Serial.println("Smart Socket ESP8266EX Firmware Ver0.1.stable Build24"); + Serial.println("Developed by Madobi Nanami"); + Serial.println("Personal site: https://nanami.tech"); + pinMode(ledPin,OUTPUT); + digitalWrite(ledPin,HIGH); + pinMode(relay,OUTPUT); + digitalWrite(relay,LOW); + Serial.print("Connecting to "); + Serial.print(ssid); + Serial.print(" with password: "); + Serial.println(password); + WiFi.begin(ssid,password); + while(WiFi.status() !=WL_CONNECTED){ + delay(500); + Serial.println("."); + } + Serial.println("Connected."); + + server.begin(); + Udp.begin(localUdpPort); + Serial.println("UDP server started. Listening on port 443"); + Serial.print("Use this address to connect: "); + Serial.println(WiFi.localIP()); +} + +void saveWiFiSettings(const char* ssid, const char* password) { + EEPROM.begin(512); + EEPROM.putString(0, ssid); + EEPROM.putString(32, password); + EEPROM.commit(); + EEPROM.end(); +} +void loadWiFiSettings(char* ssid, char* password) { + EEPROM.begin(512); + EEPROM.getString(0, ssid, 32); + EEPROM.getString(32, password, 32); + EEPROM.end(); +} + +void loop(){ + int packetSize = Udp.parsePacket(); + if(packetSize){ + Serial.printf("Received %d bytes from client: %s:%d\n",packetSize,Udp.remoteIP().toString()); + int len = Udp.read(incomingPacket,536); + if(len>0){ + incomingPacket[len] = 0; + Serial.printf("UDP packet contents: %s\n", incomingPacket); + if(strcmp(incomingPacket, "udp.test") == 0){ + UdpbeginPacketTest = Udp.beginPacket(Udp.remoteIP(),Udp.remotePort()); + if(UdpbeginPacketTest=1){ + Serial.println("Udp beginPacket test pass."); + } + Udp.write("udp test pass"); + UdpendPacketTest = Udp.endPacket(); + if(UdpendPacketTest=1){ + Serial.println("Udp endPacket test pass."); + } + return; + } + if(strcmp(incomingPacket, "device.read") == 0){ + Udp.beginPacket(Udp.remoteIP(),Udp.remotePort()); + Udp.write("[Chipset]ESP8266EX"); + Serial.println("chipset"); + Udp.write("[Manufacture]Espressif"); + Serial.println("manufacture"); + Udp.write("[DeviceType]relayController"); + Serial.println("devicetype"); + Udp.write("[FirmwareName]Smart Socket"); + Serial.println("firmwarename"); + Udp.write("[FirmwareVersion]0.1.stable"); + Serial.println("firmwareversion"); + Udp.write("[FirmwareBuildVer]24"); + Serial.println("firmwarebuildver"); + Udp.write("[Developer]Madobi Nanami"); + Serial.println("developer"); + Udp.write("[BuildDate]2024.08.07"); + Serial.println("builddate"); + delay(500); + Udp.write("[Command]EOF"); + Serial.println("eof"); + Udp.endPacket(); + return; + } + if(strcmp(incomingPacket, "led.read") == 0){ + digitalStatus = digitalRead(ledPin); + Udp.beginPacket(Udp.remoteIP(),Udp.remotePort()); + Udp.write(digitalStatus); + Udp.endPacket(); + return; + } + if(strcmp(incomingPacket, "relay.read") == 0){ + digitalStatus = digitalRead(relay); + Udp.beginPacket(Udp.remoteIP(),Udp.remotePort()); + Udp.write(digitalStatus); + Udp.endPacket(); + return; + } + if(strcmp(incomingPacket, "led.on") == 0){ + digitalWrite(ledPin,LOW); + Udp.beginPacket(Udp.remoteIP(),Udp.remotePort()); + Udp.write("led.on"); + Udp.endPacket(); + return; + } + if(strcmp(incomingPacket, "led.off") == 0){ + digitalWrite(ledPin,HIGH); + Udp.beginPacket(Udp.remoteIP(),Udp.remotePort()); + Udp.write("led.off"); + Udp.endPacket(); + return; + } + if(strcmp(incomingPacket, "relay.on") == 0){ + digitalWrite(relay,HIGH); + Udp.beginPacket(Udp.remoteIP(),Udp.remotePort()); + Udp.write("relay.on"); + Udp.endPacket(); + return; + } + if(strcmp(incomingPacket, "relay.off") == 0){ + digitalWrite(relay,LOW); + Udp.beginPacket(Udp.remoteIP(),Udp.remotePort()); + Udp.write("relay.off"); + Udp.endPacket(); + } + } + } +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/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