2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WebServerのサンプルを少しいじって温度の表示(M5NanoC6)

Last updated at Posted at 2024-10-29

x 過去ログを見よ!!
x 3.0.7
x パケロスするが気にするな!!
x なぜか、なんかいかモニターを開くと成功する(とうぜん抜き差ししてリセットを掛ける)

目的
最小限の小改造で温度をWeb表示
温度センサーは、スイッチサイエンスで売っている
seeedのGroveのV1.2のやつ

SSIDとパスワードは、修正する事


const char *ssid = "ご自宅のSSID";
const char *password = "ご自宅のパスワード";

この後、温度センサー系を合成する

合成とは、新しい攻撃系の魔法を作る時に使う。

元のプログラム




// Demo code for Grove - Temperature Sensor V1.1/1.2
// Loovee @ 2015-8-26

#include <math.h>

const int B = 4275;               // B value of the thermistor
const int R0 = 100000;            // R0 = 100k

//const int pinTempSensor = A0;     // Grove - Temperature Sensor connect to A0
const int pinTempSensor = 1;     // Grove - Temperature Sensor connect to G1


#if defined(ARDUINO_ARCH_AVR)
#define debug  Serial
#elif defined(ARDUINO_ARCH_SAMD) ||  defined(ARDUINO_ARCH_SAM)
#define debug  SerialUSB
#else
#define debug  Serial
#endif

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    int a = analogRead(pinTempSensor);

    //float R = 1023.0/a-1.0;
    //R = R0*R;
    float R,v1,a1,o1; 
    v1 = ((float)a) * 0.001; //電圧を求める
    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

    Serial.print("temperature = ");
    Serial.println(temperature);

    delay(100);
}





o_coq578.jpg

o_coq579.jpg

o_coq580.jpg

プログラム




//WebServer_temperature1_M5NanoC6_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);

const int led = 7;

void handleRoot() {
  digitalWrite(led, 1);


  //↓温度センサーの処理を開始

  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 = 1;     // Grove - Temperature Sensor connect to G1

  int a = analogRead(pinTempSensor);

  //float R = 1023.0/a-1.0;
  //R = R0*R;
  float R,v1,a1,o1; 
  v1 = ((float)a) * 0.001; //電圧を求める
  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", "hello from esp32!");
  server.send(200, "text/plain", data);
  digitalWrite(led, 0);
}

void handleNotFound() {
  digitalWrite(led, 1);
  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);
  digitalWrite(led, 0);
}

void setup(void) {
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  for(int i=0;i<10;i++){
    delay(500);
    Serial.print(".");
  }

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp32")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  server.handleClient();
  delay(2);  //allow the cpu to switch to other tasks
}


2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?