存储备份

This commit is contained in:
Luthics 2023-01-07 21:27:15 +08:00
commit 720f59bc5d
17 changed files with 413 additions and 0 deletions

5
.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

10
.vscode/extensions.json vendored Normal file
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"
]
}

16
.vscode/settings.json vendored Normal file
View File

@ -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 }"
}

2
include/control.h Normal file
View File

@ -0,0 +1,2 @@
void ct_init();
void ct_loop();

5
include/display.h Normal file
View File

@ -0,0 +1,5 @@
#include <SPI.h>
#include <TFT_eSPI.h>
void ds_init();
void ds_loop();

5
include/var.h Normal file
View File

@ -0,0 +1,5 @@
float gtemp();
float gtemps();
void stemps(float var);
String gip();
void sip(String s);

6
include/wd.h Normal file
View File

@ -0,0 +1,6 @@
#include <OneWire.h>
#include <DallasTemperature.h>
void wd_init();
void wd_loop();
float getTemp();

9
include/web.h Normal file
View File

@ -0,0 +1,9 @@
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include "ESPAsyncWebServer.h"
void web_init();
void web_loop();
void ref_ret(AsyncWebServerRequest* request, String html);
void sendWechat(float tmp);

47
index.html Normal file
View File

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>高温预警器</title>
<script>
var ws;
var temp = 28;
window.onload = function () {
if ('WebSocket' in window) {
ws = new WebSocket('ws://' + window.location.host + '/');
ws.onopen = function () {
document.getElementById('info').innerHTML +=
'WebSocket连接成功' + '<br>';
ws.send('connect ok!');
};
ws.onmessage = function (evt) {
document.getElementById('temp').innerHTML =
'当前温度:' + evt.data + '℃';
};
ws.onerror = function () {
document.getElementById('info').innerHTML +=
'通讯发送错误!' + '<br>';
};
ws.onclose = function () {
document.getElementById('info').innerHTML +=
'WebSocketTest连接已关闭' + '<br>';
};
} else {
document.getElementById('info').innerHTML =
'浏览器不支持 WebSocket';
}
};
</script>
</head>
<body>
<h1 id='temp'>当前温度27.00℃</h1>
<form action='temp' method='post'>
<input name='temp' id='tempi' />
<button name='submit' value='submit'>设置温度</button>
</form>
<div id=' info'></div>
</body>
</html>

46
lib/README Normal file
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 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 <Foo.h>
#include <Bar.h>
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

20
platformio.ini Normal file
View File

@ -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

19
src/control.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <Arduino.h>
#include <var.h>
#include <wd.h>
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);
}
}

30
src/display.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <SPI.h>
#include <TFT_eSPI.h>
#include <var.h>
#include <wd.h>
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());
}

47
src/main.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <Arduino.h>
#include <control.h>
#include <display.h>
#include <var.h>
#include <wd.h>
#include <web.h>
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;
}

18
src/wd.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <DallasTemperature.h>
#include <OneWire.h>
#include <wd.h>
OneWire oneWire(D1);
DallasTemperature sensors(&oneWire);
void wd_init() {
sensors.begin();
}
void wd_loop() {
}
float getTemp() {
sensors.requestTemperatures();
return sensors.getTempCByIndex(0);
}

117
src/web.cpp Normal file
View File

@ -0,0 +1,117 @@
#include <Arduino.h>
#include <var.h>
#include <wd.h>
#include <web.h>
#include <DNSServer.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
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 =
"<!DOCTYPE html><html><head><meta charset='UTF-8'><title>高温预警器</title><script>var ws;var "
"temp=28;window.onload=function(){if('WebSocket'in window){ws=new "
"WebSocket('ws://'+window.location.host+'/ws"
"');ws.onopen=function(){document.getElementById('info').innerHTML+='"
"WebSocket连接成功<br>';ws.send('connect "
"ok!')};ws.onmessage=function(evt){document.getElementById('temp')."
"innerHTML='当前温度:'+evt.data+'℃'};ws.onerror=function(){document."
"getElementById('info').innerHTML+='通讯发送错误!<br>'};ws.onclose="
"function(){document.getElementById('info').innerHTML+='"
"WebSocketTest连接已关闭<br>'}}else{document.getElementById('info')."
"innerHTML='浏览器不支持 WebSocket'}};</script></head><body><h1 "
"id='temp'>当前温度27.00℃</h1><form action='temp'method='post'><input "
"name='temp'id='tempi'/><button "
"name='submit'value='submit'>设置温度</button></form><div id=' "
"info'></div></body></html>";
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,
"<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" "
"content=\"1; "
"url='http://192.168.48.159/'\" /></head><body><h1>Success. Back "
"after 1 "
"sencond.</h1></body></html>");
} 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);
}

11
test/README Normal file
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