From 720f59bc5d0d56b25e97ccc31e1ef7e07cbaf988 Mon Sep 17 00:00:00 2001 From: Luthics Date: Sat, 7 Jan 2023 21:27:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=98=E5=82=A8=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 ++ .vscode/extensions.json | 10 ++++ .vscode/settings.json | 16 ++++++ include/control.h | 2 + include/display.h | 5 ++ include/var.h | 5 ++ include/wd.h | 6 +++ include/web.h | 9 ++++ index.html | 47 ++++++++++++++++ lib/README | 46 ++++++++++++++++ platformio.ini | 20 +++++++ src/control.cpp | 19 +++++++ src/display.cpp | 30 +++++++++++ src/main.cpp | 47 ++++++++++++++++ src/wd.cpp | 18 +++++++ src/web.cpp | 117 ++++++++++++++++++++++++++++++++++++++++ test/README | 11 ++++ 17 files changed, 413 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 include/control.h create mode 100644 include/display.h create mode 100644 include/var.h create mode 100644 include/wd.h create mode 100644 include/web.h create mode 100644 index.html create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/control.cpp create mode 100644 src/display.cpp create mode 100644 src/main.cpp create mode 100644 src/wd.cpp create mode 100644 src/web.cpp create mode 100644 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..38ff813 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,16 @@ +{ + "files.associations": { + "array": "cpp", + "deque": "cpp", + "list": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "string_view": "cpp", + "initializer_list": "cpp", + "ranges": "cpp", + "regex": "cpp" + }, + "C_Cpp.clang_format_style": "{ BasedOnStyle: Chromium, UseTab: Never, IndentWidth: 4, TabWidth: 4, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false }" + +} \ No newline at end of file diff --git a/include/control.h b/include/control.h new file mode 100644 index 0000000..26a7b10 --- /dev/null +++ b/include/control.h @@ -0,0 +1,2 @@ +void ct_init(); +void ct_loop(); \ No newline at end of file diff --git a/include/display.h b/include/display.h new file mode 100644 index 0000000..e2cc3b2 --- /dev/null +++ b/include/display.h @@ -0,0 +1,5 @@ +#include +#include + +void ds_init(); +void ds_loop(); \ No newline at end of file diff --git a/include/var.h b/include/var.h new file mode 100644 index 0000000..e3d6f35 --- /dev/null +++ b/include/var.h @@ -0,0 +1,5 @@ +float gtemp(); +float gtemps(); +void stemps(float var); +String gip(); +void sip(String s); \ No newline at end of file diff --git a/include/wd.h b/include/wd.h new file mode 100644 index 0000000..64ea50e --- /dev/null +++ b/include/wd.h @@ -0,0 +1,6 @@ +#include +#include + +void wd_init(); +void wd_loop(); +float getTemp(); \ No newline at end of file diff --git a/include/web.h b/include/web.h new file mode 100644 index 0000000..1dae45b --- /dev/null +++ b/include/web.h @@ -0,0 +1,9 @@ +#include +#include +#include +#include "ESPAsyncWebServer.h" + +void web_init(); +void web_loop(); +void ref_ret(AsyncWebServerRequest* request, String html); +void sendWechat(float tmp); \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..46aecb2 --- /dev/null +++ b/index.html @@ -0,0 +1,47 @@ + + + + + + 高温预警器 + + + + +

当前温度:27.00℃

+
+ + +
+
+ + + \ No newline at end of file diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /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 a 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..2384d54 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,20 @@ +; 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:esp01_1m] +platform = espressif8266 +board = esp12e +framework = arduino +lib_deps = + ottowinter/ESPAsyncWebServer-esphome@^3.0.0 + paulstoffregen/OneWire@^2.3.7 + milesburton/DallasTemperature@^3.11.0 + bodmer/TFT_eSPI@^2.4.79 +monitor_speed = 115200 diff --git a/src/control.cpp b/src/control.cpp new file mode 100644 index 0000000..b8b6f4c --- /dev/null +++ b/src/control.cpp @@ -0,0 +1,19 @@ +#include +#include +#include + +int odd = 0; + +void ct_init() { + pinMode(D2, OUTPUT); + pinMode(D6, OUTPUT); +} + +void ct_loop() { + Serial.println(gtemp()); + if (gtemp() > gtemps()) { + digitalWrite(D2, HIGH); + } else { + digitalWrite(D2, LOW); + } +} \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp new file mode 100644 index 0000000..1feeaa1 --- /dev/null +++ b/src/display.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +TFT_eSPI tft = TFT_eSPI(); + +int a = D2; + +void ds_init() { + tft.init(); + tft.setRotation(2); + tft.fillScreen(TFT_BLACK); + tft.setCursor(0, 0, 3); + tft.setTextColor(TFT_RED, TFT_BLACK); +} + +void ds_loop() { + tft.fillScreen(TFT_BLACK); + tft.setCursor(0, 0, 2); + tft.setTextSize(1); + tft.println(gip()); + tft.setTextSize(3); + if (gtemp() > gtemps()) { + tft.setTextColor(TFT_RED, TFT_BLACK); + } else { + tft.setTextColor(TFT_BLUE, TFT_BLACK); + } + tft.print(gtemp()); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c54207f --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include + +float temp; +float temp_set = 28; +String ip; + +void setup() { + Serial.begin(115200); + web_init(); + wd_init(); + ds_init(); + ct_init(); +} + +void loop() { + temp = getTemp(); + web_loop(); + wd_loop(); + ds_loop(); + ct_loop(); + delay(100); +} + +float gtemp() { + return temp; +} + +float gtemps() { + return temp_set; +} + +void stemps(float var) { + temp_set = var; +} + +void sip(String s) { + ip = s; +} + +String gip() { + return ip; +} \ No newline at end of file diff --git a/src/wd.cpp b/src/wd.cpp new file mode 100644 index 0000000..7bfe15d --- /dev/null +++ b/src/wd.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +OneWire oneWire(D1); +DallasTemperature sensors(&oneWire); + +void wd_init() { + sensors.begin(); +} + +void wd_loop() { +} + +float getTemp() { + sensors.requestTemperatures(); + return sensors.getTempCByIndex(0); +} \ No newline at end of file diff --git a/src/web.cpp b/src/web.cpp new file mode 100644 index 0000000..324a9c2 --- /dev/null +++ b/src/web.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +AsyncWebServer server(80); +AsyncWebSocket ws("/ws"); + +const char* ssid = "Luthics' WLAN"; +const char* password = "qwerasdf"; + +void notFound(AsyncWebServerRequest* request) { + request->send(404, "text/plain", "Not found"); +} + +String indexhtml = + "高温预警器

当前温度:27.00℃

"; + +void onEventHandle(AsyncWebSocket* server, + AsyncWebSocketClient* client, + AwsEventType type, + void* arg, + uint8_t* data, + size_t len) { + if (type == WS_EVT_CONNECT) { + Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); + // client->printf("已成功连接"); + client->ping(); + } else if (type == WS_EVT_DISCONNECT) { + Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id()); + } else if (type == WS_EVT_ERROR) { + Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), + *((uint16_t*)arg), (char*)data); + } else if (type == WS_EVT_PONG) { + Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, + (len) ? (char*)data : ""); + } else if (type == WS_EVT_DATA) { + AwsFrameInfo* info = (AwsFrameInfo*)arg; + Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]:\n", server->url(), + client->id(), info->num, + (info->message_opcode == WS_TEXT) ? "text" : "binary", + info->index, info->index + len); + data[len] = 0; + Serial.printf("%s\n", (char*)data); + } +} + +void web_init() { + Serial.begin(115200); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + while (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.printf("WiFi Failed!"); + WiFi.begin(ssid, password); + } + + Serial.print("IP Address:"); + Serial.println(WiFi.localIP()); + sip(WiFi.localIP().toString()); + + ws.onEvent(onEventHandle); + server.addHandler(&ws); + + server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { + request->send(200, "text/html", indexhtml); + }); + + server.on("/temp", HTTP_POST, [](AsyncWebServerRequest* request) { + int tmp; + if (request->hasParam("temp", true)) { + tmp = request->getParam("temp", true)->value().toInt(); + stemps(tmp); + ref_ret(request, + "

Success. Back " + "after 1 " + "sencond.

"); + } else { + notFound(request); + } + }); + + server.onNotFound(notFound); + + server.begin(); +} + +void web_loop() { + ws.textAll(String(gtemp())); + // ws.cleanupClients(); + // sendWechat(getTemp()); +} + +void ref_ret(AsyncWebServerRequest* request, String html) { + request->send(200, "text/html", html); +} \ 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