Wifi経由でArduino操作
IoTっぽいものが作りたかったので、Arduinoをスマホで操作してみました。
実行映像
Microsoft OneDrive - Access files anywhere. Create docs with free Office Online.
スマホ画面
配線は以下の通り。(トラ技2017年11月号付録のWi-fi Arduinoを使用)
コードもあげときます。
#define ECHOPIN 26 // エコー(受信側)ピン #define CTM 10 // HIGHの時間(μ秒) #define DX 12 int abc[] = {262, 294, 330, 349, 392, 440, 494, 523}; //ドレミの設定 //トーン設定(dx:ピン番号、hz:周波数、tm:ミリ時間) void mtone(int dx, int hz, long tm) { long t = millis(); long ns = 10000 / hz * 50; while (millis() - t < tm) { digitalWrite(dx, HIGH); delayMicroseconds(ns); digitalWrite(dx, LOW); delayMicroseconds(ns); } } #include <WiFi.h> const char* ssid = "ID"; const char* password = "パス"; WiFiServer server(80); void setup() { Serial.begin(115200); pinMode(2, OUTPUT) ; // デジタル出力に設定 pinMode(23, OUTPUT) ; // デジタル出力に設定 pinMode(18, OUTPUT) ; // デジタル出力に設定 pinMode(19, OUTPUT) ; // デジタル出力に設定 pinMode(DX, OUTPUT); // スピーカのデジタル出力宣言 pinMode(TRIGPIN, OUTPUT);// トリガーピンのデジタル出力設定 pinMode(ECHOPIN, INPUT); // エコーピンのデジタル入力設定 delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } int value = 0; void loop() { WiFiClient client = server.available(); // listen for incoming clients if (client) { // if you get a client, Serial.println("New Client."); // print a message out the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); client.println("<!DOCTYPE html>"); client.println("<html>"); client.println("<head>"); client.println("<meta name='viewport' content='initial-scale=1.5'>"); client.println("</head>"); client.println("<body>"); client.println("<form method='get'>"); client.println("<font size='4'>ESP-WROOM-32<br>"); client.println("Wi-Fi LED Switch</font><br>"); client.println("<br>"); client.println("<input type='submit' name=0 value='ON' style='background-color:#88ff88; color:red;'>"); client.println("<input type='submit' name=1 value='OFF' style='background-color:black; color:white;'>"); client.println("</form>"); client.println("</body>"); client.println("</html>"); // the content of the HTTP response follows the header: client.print("Click <a href=\"/H\">here</a> to turn the Red LED on.<br>"); client.print("Click <a href=\"/L\">here</a> to turn the Green LED on.<br>"); client.print("Click <a href=\"/R\">here</a> to turn the Blue LED on.<br>"); client.print("Click <a href=\"/A\">here</a> to turn the Red LED off.<br>"); client.print("Click <a href=\"/B\">here</a> to turn the Green LED off.<br>"); client.print("Click <a href=\"/C\">here</a> to turn the Blue LED off.<br>"); client.print("Click <a href=\"/D\">here</a> to turn the speaker up.<br>"); client.print("Click <a href=\"/E\">here</a> to turn the speaker down.<br>"); client.print("Click <a href=\"/F\">here</a> to turn the IO2 LED on.<br>"); client.print("Click <a href=\"/G\">here</a> to turn the IO2 LED off.<br>"); int val = analogRead(A4); client.println("<br>"); client.print ("Light = "); client.print (val); client.println("<br>"); int temp = analogRead(A5); client.print ("Temp = "); client.print (temp); client.println("<br>"); int dur; // 時間差(μ秒) float dis; // 距離(cm) digitalWrite(TRIGPIN, HIGH); delayMicroseconds(CTM); digitalWrite(TRIGPIN, LOW); dur = pulseIn(ECHOPIN, HIGH); // HIGHになる待ち時間の計測 dis = (float) dur * 0.017; // 音速による距離計算 client.print ("Distance = "); client.print(dis); client.println(" cm"); client.println("<br>"); // The HTTP response ends with another blank line: client.println(); // break out of the while loop: break; } else { // if you got a newline, then clear currentLine: currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } // Check if (currentLine.endsWith("GET /H")) { digitalWrite(23, HIGH); } if (currentLine.endsWith("GET /L")) { digitalWrite(19, HIGH); } if (currentLine.endsWith("GET /R")) { digitalWrite(18, HIGH); } if (currentLine.endsWith("GET /A")) { digitalWrite(23, LOW); } if (currentLine.endsWith("GET /B")) { digitalWrite(19, LOW); } if (currentLine.endsWith("GET /C")) { digitalWrite(18, LOW); } if (currentLine.endsWith("GET /F")) { digitalWrite(2, HIGH); } if (currentLine.endsWith("GET /G")) { digitalWrite(2, LOW); } if (currentLine.endsWith("GET /D")) { for (int i = 0; i < 8; i++) { mtone(DX, abc[i], 500); delay(50); } } if (currentLine.endsWith("GET /E")) { for (int i = 8; i > -1; i--) { mtone(DX, abc[i], 500); delay(50); } } } } // close the connection: client.stop(); Serial.println("Client Disconnected."); } }
お試しあれ!