やりたいこと
Pythonでパワポのスライドショーを操作 - 滴了庵日録 で作成したPythonのWebアプリサーバに対して、Raspberry Pi Pico WからアクセスしてPowerPointを操作する。
開発環境
PlatformIOのArduinoフレームワークを使用する。platformio.iniは以下の通り。
[env:rpipicow] platform = https://github.com/maxgerhardt/platform-raspberrypi.git board = rpipicow framework = arduino
ソース
#include <Arduino.h> #include <WiFi.h> #include <HTTPClient.h> const char* SSID = WiFiのSSID const char* PASSWORD = WiFiパスワード const char* HOST = サーバーのホスト名またはIPアドレス void setup() { Serial.begin(115200); // WiFi接続 WiFi.begin(SSID, PASSWORD); Serial.print("Connecting to WiFi..."); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(" connected!"); } void loop() { // シリアル入力からコマンド送信 while(Serial.available()) { char c = Serial.read(); char *command = (char *)""; bool isValid = true; switch(c) { case 's': command = (char *)"start"; break; case 'e': command = (char *)"end"; break; case 'n': command = (char *)"next"; break; case 'p': command = (char *)"prev"; break; case 'b': command = (char *)"black"; break; case 'r': command = (char *)"resume"; break; case 't': command = (char *)"note"; break; case 'i': command = (char *)"pageinfo"; break; default: isValid = false; Serial.println("Unknown command"); break; } // コマンド送信 (HTTP GETリクエスト) if(isValid) { if (WiFi.status() == WL_CONNECTED) { HTTPClient http; static char url[64]; sprintf(url, "http://%s:5000/%s", HOST, command); http.begin(url); int httpCode = http.GET(); // GETリクエスト送信, レスポンスコード取得 if (httpCode > 0) { Serial.printf("HTTP Response code: %d\n", httpCode); // RP2040では、getString()がうまく動作しないため、以下のようにしてレスポンスボディを取得する // http.getString(); int len = http.getSize(); static char buffer[1024]; WiFiClient * stream = http.getStreamPtr(); int readLen = stream->readBytes(buffer, len); buffer[readLen] = '\0'; Serial.printf("Body=%s\n", buffer); } else { Serial.printf("GET failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } } } delay(100); }