参考
x 過去ログを見よ!!
x 3.1.0
x なぜか、なんかいか「ツール」-「シリアルモニター」を開くと成功する
目的
最小限の小改造で温度をWeb表示
温度センサーは、スイッチサイエンスで売っている(または、RSコンポーネンツ)
seeedのGroveのV1.2のやつ
SSIDとパスワードは、修正する事
const char *ssid = "ご自宅のSSID";
const char *password = "ご自宅のパスワード";
ローカルドメインを使用する
http://esp32.local/
プログラム
//WebServer_temperature1_M5StampS3_1
//ヘッダー
#include <WiFi.h>
#include <NetworkClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <math.h> //logの計算等で使う
//定義
const char *ssid = "ご自宅のSSID";
const char *password = "ご自宅のパスワード";
WebServer server(80);
void handleRoot() {
//↓温度センサーの処理を開始
static uint32_t msg_count = 0;
static char data[256 + 1];
int B = 4275; // B value of the thermistor
int R0 = 100000; // R0 = 100k
int pinTempSensor = 15; // Grove - Temperature Sensor connect to G15
int a = analogRead(pinTempSensor);
//float R = 1023.0/a-1.0;
//R = R0*R;
float R, v1, a1, o1;
v1 = ((float)a)*(3.3/4096.0); //電圧を求める
a1 = v1 / 100000.0; //電流を求める
o1 = 5.0 / a1; //全体の抵抗を求める
R = o1 - 100000.0; //全体の抵抗から検出抵抗を引く
float temperature = 1.0 / (log(R / R0) / B + 1 / 298.15) - 273.15; // convert to temperature via datasheet
snprintf(data, sizeof(data), "temperature = %d #%lu", (int)temperature, msg_count++);
//↑温度センサーの処理が終了
//転送
server.send(200, "text/plain", data);
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
//初期化
void setup(void) {
//WiFiの初期化
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
//シリアルの初期化
Serial.begin(9600);
Serial.println("");
//シリアルの待ちが0.5*9
//delay(3000);//決め打ちなので、おかしかったら調整してね! wifiの待ち0.5*6
for (int i = 0; i < (9 + 6); i++) {
delay(500); //接続待ち
Serial.print(".");
}//for
//ローカルドメインネームサーバーの設定
MDNS.begin("esp32"); delay(2000);//決め打ちなので、おかしかったら調整してね!
//接続情報の表示
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//割り込みに登録↓ start
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
//割り込みに登録↑ end
server.begin(); //webサービスの初期化
Serial.println("HTTP server started");
}//setup
//メインループ
void loop(void) {
server.handleClient();
delay(2); //allow the cpu to switch to other tasks
}//loop