#include #include #include #include #include #define JST 3600*9 // 日本の標準時のグリニッチ標準時との時差をJSTと定義 const char ssid[] = "Buffalo-G-2C6E"; const char password[] = "-----------"; const char* mqtt_server = "192.168.1.66"; String COLOR1 = "red"; String COLOR2 = "blue"; float tpary[1000]; time_t ttary[1000]; String datestr; char dstr[20] = "abcdefghij1234"; int maxpt = 999; float dpa[10]; int kp = 0; int dp; // 時刻表示ために追加された 定義部 time_t t; // time_tg型の変数 tを定義 struct tm *tm; // 現在時刻を格納する構造体を定義、ポインタを使用する static const char *wd[7] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"}; // 曜日を表示するための0~7の配列にSunからSatにセットします WiFiClient espClient; PubSubClient client(espClient); WebServer server(80); IPAddress ipadr; int n = 0; int bp = 0; unsigned char tmp2_adr = 0x48; long lastMsg = 0; char msg[150]; int value = 0; i2clcd qlcd(0x3E, 3, 0x20); //具体的な処理を行うためのオブジェクトを作る float get_tmp102(unsigned char tmp0_adr) {//温度センサを読み取る関数 Wire.requestFrom(tmp0_adr, 2); while (Wire.available() < 2) { } int tmpin = Wire.read() * 16; tmpin = tmpin + (Wire.read() >> 4); return 0.0625 * tmpin; } void handleRoot() { handleRootmsg(COLOR2, COLOR2); } void handleon() { digitalWrite(5, HIGH); // GET /H turns the LED on handleRootmsg(COLOR1, COLOR1); } void handleoff() { digitalWrite(5, LOW); // GET /L turns the LED off handleRootmsg(COLOR2, COLOR2); } void handleRootmsg( String cr1, String cr2) { String temp = ""; char tempa[600]; int sec = millis() / 1000; int min = sec / 60; int hr = min / 60; float tvalue = get_tmp102(tmp2_adr); dp = kp - 1; for (int i = 0; i <= 9; i++) { if (dp == 0) { dp = maxpt; } dpa[i] = tpary[dp]; Serial.println("data "); Serial.println(i); Serial.println(dpa[i]); Serial.println(tpary[dp]); } // 時刻表示のために追加された処理 time(&t); // システムクロックから時刻を導き出し変数に経過時間としてセット // t = time(NULL); 戻り値を受け取る場合は引数はnullする。同じ結果になる tm = localtime(&t); // 構造体に日時の各メンバーが利用可能な方にセットする snprintf(tempa, 600, "\ \ \
\ Denshi Club ESP32 test\ \ \

Temperature from ESP32

\

%04d/%02d/%02d(%s) %02d:%02d:%02d:

\

Uptime: %2d:%2d:%2d:

\

Current Temperature: %8.2f

\

Temperature course:

\

(1 -- 5):%6.2f:%6.2f:%6.2f:%6.2f:%6.2f

\

(6 -- 10):%6.2f:%6.2f:%6.2f:%6.2f:%6.2f

\ \
", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, wd[tm->tm_wday], tm->tm_hour, tm->tm_min, tm->tm_sec, // 時刻表所 構造体の各メンバを年月日曜日時分秒に分け表示 hr, min % 60, sec % 60, tvalue, dpa[0], dpa[1], dpa[2], dpa[3], dpa[4], dpa[5], dpa[6], dpa[7], dpa[8], dpa[9] ); temp = tempa; temp += "
"; temp += "

Click here to turn "; temp += "the LED on pin 5 on.

"; temp += "

Click here to turn the LED on pin 5 off.

"; temp += "
"; temp += ""; temp += "
"; temp += ""; temp += "", server.send(200, "text/html", temp); } 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/HTML", message); } void setup(void) { qlcd.init_lcd(); //LCDモジュールの初期化処理 qlcd.i2cprint("start:"); pinMode(5, OUTPUT); Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); ipadr = WiFi.localIP(); Serial.println(ipadr); qlcd.lcdcu_set(0, 1); qlcd.i2cprint(String(ipadr[0]) + "." + String(ipadr[1]) + "."); qlcd.i2cprint(String(ipadr[2]) + "." + String(ipadr[3])); server.on("/", handleRoot); server.on("/H", handleon); server.on("/L", handleoff); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp"); client.setServer(mqtt_server, 1883); client.setCallback(callback); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println("CallBack"); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection... "); // Attempt to connect if (client.connect("ESP32Client01")) { Serial.println("connected"); // Once connected, publish an announcement... client.publish("ESP32/01/temp", "from Arduino ----reconnect"); client.subscribe("ESP32/01/temp");// ... and resubscribe } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 2 seconds"); delay(2000); } } } void loop(void) { if (!client.connected()) { reconnect(); Serial.println("reconnect"); } client.loop(); server.handleClient(); if (0 == (millis() % 2000)) { float gettmpd = get_tmp102(tmp2_adr); snprintf (msg, 75, "%lf", gettmpd); client.publish("ESP32/01/temp", msg); // Serial.print(" Received " + client.subscribe("ESP32/01/temp")); qlcd.lcdcu_set(0, 0); // LCDモジュールのカーソルを先頭に n = n + 1; tpary[kp] = gettmpd; ttary[kp] = time(NULL); Serial.println("****kp***"); Serial.println(kp); Serial.println(tpary[kp]); tm = localtime(&ttary[kp]); Serial.print("date data "); snprintf(msg, 75, "%04d/%02d/%02d(%s)%02d:%02d:%02d:", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, wd[tm->tm_wday], tm->tm_hour, tm->tm_min, tm->tm_sec); Serial.println(msg); client.publish("ESP32/01/temp", msg); // snprintf (msg, 75, "%lf", tpary[kp]); Serial.print("Publish message: "); Serial.println(msg); client.publish("ESP32/01/temp", msg); // kp = kp + 1; if (kp > maxpt) { kp = 0; } qlcd.i2cprint(String(n)); // カウンタの値をLCDへ表示 qlcd.i2cprint(":"); qlcd.i2cprint("temp2="); // 見出しをLCDへ表示 qlcd.i2cprint(String(gettmpd)); //測定温度を表示 } }