yimean 發表於 2020-8-7 18:27:12

WiFi程式碼基礎問題

各位版上的大大日安,
小弟是超新手,剛買了AI生醫感測健康大應用,是旗標出的。
書上是用拼圖式教學,所以他把一些東西模組化了。正因為如此,所以學起來的觀念很不扎實。
而且說真的如果對架構孰悉了,Key in跟Copy and past應該會比拼圖快。(我個人的觀點,不知道有沒有使用很久的大大有不一樣的看法?)
所以,我還是比較傾向回去看程式碼。我對於程式架構的兩個區塊已經有初步的認識。
我的硬體是ESP8266
下方是我目前的程式碼
我的問題如下:


#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "data/webpages.h"

unsigned long TCon;
int BAC;
ESP8266WebServer _esp8266WebServer(80);

void handleRoot() {
#ifndef WEBPAGE_IN_PROGMEM
_esp8266WebServer.send(200, "text/html", mainPage);
#else
_esp8266WebServer.send_P(200, PSTR("text/html"), mainPage);
#endif
}

void handleNotFound() {
#ifndef WEBPAGE_IN_PROGMEM
_esp8266WebServer.send(404, "text/html", errorPage);
#else
_esp8266WebServer.send_P(404, PSTR("text/html"), errorPage);
#endif
}

void handleSetting() {
#ifndef WEBPAGE_IN_PROGMEM
_esp8266WebServer.send(200, "text/html", settingPage);
#else
_esp8266WebServer.send_P(200, PSTR("text/html"), settingPage);
#endif
}

void BAC_return() {
_esp8266WebServer.send(200, u8"text/plain", String(BAC));
}


// setup() 會先被執行且只會執行一次
void setup() {
pinMode(A0, INPUT);

TCon = millis();
while (!WiFi.softAP(u8"alcohol", u8"12345678", 1,false));
_esp8266WebServer.on("/measure", BAC_return);
_esp8266WebServer.on("/", handleRoot);
_esp8266WebServer.onNotFound(handleNotFound);
_esp8266WebServer.on("/setting", handleSetting);
_esp8266WebServer.begin();

}

// loop() 裡面的程式會不斷重複執行
void loop() {
_esp8266WebServer.handleClient();
if (millis() - TCon > 100) {
    BAC = analogRead(A0);
    TCon = millis();
}

}

我的問題如下:
1.#include "data/webpages.h"這一行的意思,是不是說去目前的路徑下的一個叫data的資料夾,找webpages.h這個檔案?

2.在大部分的情況下,以下這些東西是真的需要了解他,還是說只要照抄就好?
void handleRoot() {
#ifndef WEBPAGE_IN_PROGMEM
_esp8266WebServer.send(200, "text/html", mainPage);
#else
_esp8266WebServer.send_P(200, PSTR("text/html"), mainPage);
#endif
}

void handleNotFound() {
#ifndef WEBPAGE_IN_PROGMEM
_esp8266WebServer.send(404, "text/html", errorPage);
#else
_esp8266WebServer.send_P(404, PSTR("text/html"), errorPage);
#endif
}

void handleSetting() {
#ifndef WEBPAGE_IN_PROGMEM
_esp8266WebServer.send(200, "text/html", settingPage);
#else
_esp8266WebServer.send_P(200, PSTR("text/html"), settingPage);
#endif
}

void BAC_return() {
_esp8266WebServer.send(200, u8"text/plain", String(BAC));
}

3.在大部分的情況下,是不是只要修改其中的Wifi ID與密碼,還有第2行的BAC_return就可以了?然後/measure以及/setting這些是路徑名稱嗎?如果是的話這些路徑會存在哪邊呢?
while (!WiFi.softAP(u8"alcohol", u8"12345678", 1,false));
_esp8266WebServer.on("/measure", BAC_return);
_esp8266WebServer.on("/", handleRoot);
_esp8266WebServer.onNotFound(handleNotFound);
_esp8266WebServer.on("/setting", handleSetting);
_esp8266WebServer.begin();

以上懇請高手大大指導,感恩。

babyfish0226 發表於 2020-8-9 18:18:47

我沒看過這本,我用一般程式開發的習慣提供您想法:
1.一般是在專案或特定Framework的路徑下,若沒封裝打包讓人看不見的話,最簡單的方式,可以搜尋一下webpages.h這支檔案,就可以確認了

2.主要是#ifndef WEBPAGE_IN_PROGMEM 這一段,應該會有個地方記錄WEBPAGE_IN_PROGMEM的參數,視該參數有沒有存在然後跑不同的呼叫方式,基本上應該照用就可以了,若想了解的話,可以看一下他帶入的PSTR是做了什麼事

3.沒看到他function的寫法無法確定,看起來使用/像路徑,一般可能是呼叫網路API的代入網址位置不同,這部分後端會開規格書,另外也可能只是參數,至於WiFi,自己直接下去測試您WiFi的帳密就知道啦

yimean 發表於 2020-8-10 20:28:17

babyfish0226 發表於 2020-8-9 18:18
我沒看過這本,我用一般程式開發的習慣提供您想法:
1.一般是在專案或特定Framework的路徑下,若沒封裝打包 ...

收到,感恩大大指導。
頁: [1]
查看完整版本: WiFi程式碼基礎問題