昨日試したSSL通信のサンプルコードです。
ほぼほぼコピペですがsetup()の中身は自分用に少し変えています。
サンプルコード
#include <M5Core2.h> #include <WiFi.h> #include <HTTPClient.h> #define USE_SERIAL Serial const char* root_ca= \ "-----BEGIN CERTIFICATE-----\n" "MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n" "ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n" "b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n" "MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n" "b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n" "ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n" "9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n" "IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n" "VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n" "93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n" "jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n" "AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n" "A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n" "U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n" "N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n" "o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n" "5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n" "rqXRfboQnoZsG4q5WTP468SQvvG5\n" "-----END CERTIFICATE-----"; // (2) void setup() { String config_ini; String ssid; String password; unsigned short ausIdx = 0; /* 初期化 */ M5.begin(); /* SD初期化 */ SD.begin(); Serial.begin(115200); delay(10); /* 文字サイズを変更 */ M5.Lcd.setTextSize(1); /* ファイルオープン */ File datFile = SD.open("/set/config.ini"); if( datFile ) { M5.Lcd.println("File open successful"); Serial.println("File open successful"); /* サイズ分ループ */ while( datFile.available() ) { config_ini = config_ini + datFile.readString(); } /* ファイルクローズ */ datFile.close(); } else { M5.Lcd.println("File open error hello.txt"); } /* SSID取得 */ config_ini = config_ini.substring(config_ini.indexOf("#SSID\r\n") + 7); ssid = config_ini.substring(0, config_ini.indexOf("\r\n")); /* パスワード取得 */ config_ini = config_ini.substring(config_ini.indexOf("#SSID_PASS\r\n") + 12); password = config_ini.substring(0, config_ini.indexOf("\r\n")); /* connect to WiFi */ M5.Lcd.print("Connecting to YOUR_SSID "); Serial.println("Connecting to YOUR_SSID "); /* wifi初期化 */ WiFi.begin(ssid.c_str(), password.c_str()); while (WiFi.status() != WL_CONNECTED) { delay(500); M5.Lcd.print("."); } M5.Lcd.println(" CONNECTED"); Serial.println(" CONNECTED"); /* disconnect WiFi as it's no longer needed */ // WiFi.disconnect(true); // WiFi.mode(WIFI_OFF); } void loop() { while (WiFi.status() == WL_CONNECTED) { HTTPClient http; USE_SERIAL.print("[HTTP] begin...\n"); http.begin("https://qiita.com/robots.txt", root_ca); // (3) USE_SERIAL.print("[HTTP] GET...\n"); int httpCode = http.GET(); if (httpCode > 0) { USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); USE_SERIAL.println(payload); } } else { USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } delay(10000); }
私の場合はWiFiのSSIDとPASSはiniファイルに格納して読みだしてます。
昔の記事ですが参考までに。。
m5stack-build.hatenablog.com