
ESP32をTWSNMPシリーズのセンサーにする練習 まとめ
今朝は5時まで寝ました。3時が2日続いたので修正です。
昨日、ESP32から測定した温度をTWSNMP FCに送信するセンサーが完成しました。そのまとめです。
購入した機材は
センサーなどの部品が入って安いと思いましたが、前にRaspberry Pi 4のために買ったキットのセンサーを使ったのでちょっと無駄なもの買ってしまった気がします。
温度の測定は、Freenoveのthermistorのチュートリアルを参考にしました。

のような回路です。
この回路のまま、Wifiに接続すると温度が、-273℃になってしまいました。
の記事にWifiを使うと温度センサーを接続しているADC2が使えないということが書いてありました。別のピンに接続して解決できました。
Wifiの接続はチュートリアルに書いてある通りでOKでした。
時計をあわせるためのNTPは
を使いました。Aruino IDEには、このようなライブラリが豊富にあるようです。
syslogの送信は、
を参考にしました。
という組み合わせで、できたソフトは、
#include <WiFi.h>
#include <WiFiUdp.h>
#include <time.h>
#include "AsyncUDP.h"
#include "NTPClient.h"
#define PIN_ANALOG_IN 34
#define PIN_LED 2
const char *ssid_AP = "SSID";
const char *password_AP = "パスワード";
const IPAddress dst = IPAddress(192,168,1,250);
WiFiUDP udp;
NTPClient ntp(udp, "ntp.nict.jp", 32400, 60000);
AsyncUDP syslog;
int count = 0;
void setup() {
Serial.begin(115200);
pinMode(PIN_LED, OUTPUT);
delay(2000);
Serial.println("Setup start");
WiFi.begin(ssid_AP, password_AP);
Serial.println(String("Connecting to ") + ssid_AP);
int n = 0;
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(PIN_LED, n % 2 == 0 ? HIGH : LOW);
delay(500);
Serial.print(".");
}
Serial.println("\nConnected, IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Setup End");
ntp.begin();
}
void loop() {
char ts[128];
time_t t;
struct tm *tm;
AsyncUDPMessage msg;
ntp.update();
if (count % 60 == 0) {
int adcValue = analogRead(PIN_ANALOG_IN); //read ADC pin
double voltage = (float)adcValue / 4095.0 * 3.3; // calculate voltage
double Rt = 10 * voltage / (3.3 - voltage); //calculate resistance value of thermistor
double tempK = 1 / (1 / (273.15 + 25) + log(Rt / 10) / 3950.0); //calculate temperature (Kelvin)
double tempC = tempK - 273.15; //calculate temperature (Celsius)
Serial.printf("ADC value : %d,\tVoltage : %.2fV, \tTemperature : %.2fC\n", adcValue, voltage, tempC);
Serial.println(ntp.getFormattedTime());
t = ntp.getEpochTime();
tm = localtime(&t);
strftime(ts,sizeof(ts),"%FT%X+09:00",tm);
msg.printf("<%d>%s %s twESP32: temp=%.2f",21*8+6,ts,WiFi.localIP().toString(),tempC);
syslog.sendTo(msg,dst,514);
}
digitalWrite(PIN_LED, count % 2 == 0 ? HIGH : LOW);
delay(1000);
count++;
}
のような感じです。ライブラリが豊富なので簡単にできたと思います。
TWSNMP FCで受信した結果は、

です。
購入したESP32にはカメラがついているので、明日はカメラを使ってみようと思います。
明日に続く
いいなと思ったら応援しよう!
