# Arduino UNO R4 Wi-FiでWi-Fi通信 # ひとまず Arduino UNO R4 Wi-Fiを使えるようにする ::: success Raspberry Pi にArduino IDEをインストールすれば,Raspberry PiからArduino UNO R4 Wi-Fiをコーディングできます.Raspberry Pi 5はARMコアの64bitなので,Arduino IDEはレガシー版(1.8)を使ってください. ::: :::info ボードマネージャでArduino UNO R4のボードをインストールして下さい. ::: ## あとは以下のスケッチを実行 ```C++ #include "Arduino_LED_Matrix.h" #define LOOPSIZE 12 ArduinoLEDMatrix matrix; void setup() { Serial.begin(96000); matrix.begin(); } const uint32_t CIST_logo[][4] = { { 0x100f010, 0x82006fe3, 0x103204c0, 66 }, { 0x807808, 0x410037f1, 0x88190260, 66 }, { 0x403c04, 0x20809bf0, 0xc40c8130, 66 }, { 0x201e02, 0x1040cdf0, 0x62064098, 66 }, { 0x100f81, 0x20e6f0, 0x3103204c, 66 }, { 0x80080740, 0x8010f378, 0x18019026, 66 }, { 0x400c0320, 0x4008f9b4, 0xc80c013, 66 }, { 0x200e0110, 0x2004fcd2, 0x6406809, 66 }, { 0x100f0008, 0x1002fe61, 0x3203c04, 66 }, { 0x8078084, 0x17f30, 0x81901602, 66 }, { 0x403c042, 0x800bf9c, 0x40c80301, 66 }, { 0x201e021, 0x400dfc6, 0x20640980, 66 } }; void loop(){ for(int i=0;i<LOOPSIZE;i++){ matrix.loadFrame(CIST_logo[i]); delay(CIST_logo[i][3]); } } ``` 何が出るかな. ![image](https://hackmd.io/_uploads/ryb4e9epeg.png) LEDマトリクスの黄色部分の32bitのHEX表記 LEDマトリクスの水色部分の32bitのHEX表記 LEDマトリクスの緑色部分の32bitのHEX表記 の順で並んでいます. https://ledmatrix-editor.arduino.cc/ で描画し,右上の`</>`マークを押すとダウンロードできます. # Wi-Fi ```C++ #define SECRET_SSID "HSP" #define SECRET_PASS "2025oc0802" #include "WiFiS3.h" //#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) int keyIndex = 0; // your network key index number (needed only for WEP) int led = LED_BUILTIN; int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { Serial.begin(9600); // initialize serial communication pinMode(led, OUTPUT); // set the LED pin mode // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true); } String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } // attempt to connect to WiFi network: while (status != WL_CONNECTED) { Serial.print("Attempting to connect to Network named: "); Serial.println(ssid); // print the network name (SSID); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); } server.begin(); // start the web server on port 80 printWifiStatus(); // you're connected now, so print out the status } 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 to 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(); // the content of the HTTP response follows the header: client.print("<p style=\"font-size:7vw;\">Click <a href=\"/H\">here</a> turn the LED on<br></p>"); client.print("<p style=\"font-size:7vw;\">Click <a href=\"/L\">here</a> turn the LED off<br></p>"); // 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 to see if the client request was "GET /H" or "GET /L": if (currentLine.endsWith("GET /H")) { digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on } if (currentLine.endsWith("GET /L")) { digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off } } } // close the connection: client.stop(); Serial.println("client disconnected"); } } void printWifiStatus() { // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID()); // print your board's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); // print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm"); // print where to go in a browser: Serial.print("To see this page in action, open a browser to http://"); Serial.println(ip); } ``` 1. シリアルモニタにWebサーバ(Arduino)のIPアドレスが示されるので,それをメモする.以下の例では"192.168.11.6"です. 2. スマホでWebブラウザを立ち上げる. 3. ブラウザのURLに上記IPアドレスを入れる. 4. 以下のような画面で,LEDを点灯/消灯させる. ![image](https://hackmd.io/_uploads/HyCOHQC3ex.png) :::warning 注目するのはArduino UNO R4 Wi-Fiのボードに内蔵のLEDです. ::: <!-- [PC側の設定へ](https://hackmd.io/z-Zp_bfyRO2nQqWzd27nBA?view) --> # LEDマトリクスの制御とWi-Fi通信を組み合わせれば,スマホの操作でロゴを流す/止めるが出来るはず.