//Test12-chatGPT_OndoShitudoSwitchMomDisp // //#include #include #include #include "arduino_secrets.h" ///////please enter your sensitive data in the Secret tab/arduino_secrets.h char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) WiFiServer server(80); #define DHTPIN 11 // DHTセンサーのピン番号 #define DHTTYPE DHT11 // 使用するDHTセンサーのタイプ(DHT11, DHT22, etc.) #define LED_PIN1 LED_BUILTIN// LED 1のピン #define LED_PIN2 3 DHT dht(DHTPIN, DHTTYPE); bool ledState1 = false; bool ledState2 = false; int cnt; void setup() { Serial.begin(9600); dht.begin(); pinMode(LED_PIN1, OUTPUT); pinMode(LED_PIN2, OUTPUT); // WiFi接続の試行 Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println("WiFi connected."); server.begin(); Serial.println("Server started."); Serial.print("Use this URL to connect: http://"); Serial.print(WiFi.localIP()); Serial.println("/"); } void loop() { WiFiClient client = server.available(); if (client) { Serial.println("New Client."); String currentLine = ""; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); if (c == '\n') { if (currentLine.length() == 0) { client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); client.print(R"rawliteral( Arduino Temperature & Humidity

Temperature & Humidity

Counter: Loading...

Temperature: Loading... °C

Humidity: Loading... %

LED 1: OFF

LED 2: OFF

)rawliteral"); client.println(); break; } else { currentLine = ""; } } else if (c != '\r') { currentLine += c; } if (currentLine.endsWith("GET /sensor")) { float t = dht.readTemperature();//湿度を読み取る //t = Cnt; float h = dht.readHumidity();//温度を摂氏で読み取る //h = Cnt+1; cnt += 1; String json = "{\"counter\":" + String(cnt) + ",\"temperature\":" + String(t) + ",\"humidity\":" + String(h) + ",\"led1\":" + String(ledState1) + ",\"led2\":" + String(ledState2) + "}"; client.println("HTTP/1.1 200 OK"); client.println("Content-type:application/json"); client.println("Connection: close"); client.println(); client.println(json); Serial.println(json); break; } else { if (currentLine.indexOf("GET /toggleLED?led=1") >= 0) { int led = currentLine.substring(currentLine.indexOf('=') + 1).toInt();// = の次の文字抜き出し整数に if (led == 1) { digitalWrite(LED_PIN1, !digitalRead(LED_PIN1));//toggle sw if( digitalRead(LED_PIN1) ){ ledState1 = true; } else{ ledState1 = false; } } client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/plain"); client.println("Connection: close"); client.println(); client.println("OK"); break; } else if (currentLine.endsWith("GET /toggleLED?led=2")) { int led = currentLine.substring(currentLine.indexOf('=') + 1).toInt();// = の次の文字抜き出し整数に if (led == 2) { digitalWrite(LED_PIN2, !digitalRead(LED_PIN2));//toggle sw if( digitalRead(LED_PIN2) ){ ledState2 = true; } else{ ledState2 = false; } } client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/plain"); client.println("Connection: close"); client.println(); client.println("OK"); break; } } } } client.stop(); Serial.println("Client Disconnected."); } }