実現したいこと
ESP32でAWS Iot coreに接続して、1分毎にデータを送信するプログラムを作成しています。
発生している問題・分からないこと
Json化したトピックデータを作成して送信しているのですが、
Payload sizeが203バイトのデータなら、正常にAWSのMQTTクライアントテストの#ワイルドカード受信で正常受信を確認しています。
その後、もっとデータ量を増やそうとして、Payload sizeを260バイトにあげると、ワイルドカード設定でもデータが受信できなくなります。
エラーメッセージ
error
1モニター出力結果はこちらです。 2 3#ssids 4go*****ain,Fd****** 520*****v6,a****** 6testwifi,12345678 7接続を試みています: g********in パスワード長: 9 8. 9WiFiに接続しました。 10IPアドレス: 192.168.86.174 11AWS IoTエンドポイント: am5*********ts.iot.us-east-1.amazonaws.com 12デバイス名: *****Device0009 13CA証明書ファイルを読み込みました。 14CA証明書の内容の長さ: 1187 15証明書の長さ: 1220 16プライベートキーの長さ: 1675 17Payload size: 258 18AWS IoTへのデータ送信に失敗しました。 19Payload size: 258 20AWS IoTへのデータ送信に失敗しました。 21Payload size: 258 22AWS IoTへのデータ送信に失敗しました。
該当のソースコード
cpp
1#include "AWSPub.h"2#include <WiFiClientSecure.h>3#include <PubSubClient.h>4#include "FS.h"5#include "SPIFFS.h"6#include "SPIFFSRead.h"7#include "payload.h"8 9// AWS IoT設定10//const char* aws_endpoint = "YOUR_AWS_IOT_ENDPOINT"; // AWS IoTエンドポイント11//const char* deviceName = "YOUR_DEVICE_NAME";12String gAwsEndpoint = "";13String gDeviceName = "";14const int aws_port = 8883;15 16String sanitizeEndpoint(String endpoint);17 18 19// AWS IoT設定(エンドポイント、認証情報など)20// 以前の設定をここに移動21 22WiFiClientSecure net;23PubSubClient client(net);24 25 26void readAWSDeviceName() {27 if(!SPIFFS.begin(true)){28 Serial.println("SPIFFSの初期化に失敗しました。");29 return;30 }31 32 // AWS設定ファイルを開く33 File configFile = SPIFFS.open("/aws_devicename.conf", "r");34 if (!configFile) {35 Serial.println("AWS設定ファイルを開くことができませんでした。");36 return;37 }38 39 // ファイルからデバイス名とエンドポイントを読み込む40 //String awsDeviceName, awsEndpoint;41 while(configFile.available()) {42 String line = configFile.readStringUntil('\n');43 line.trim();44 int separatorIndex = line.indexOf(':');45 if (separatorIndex != -1) {46 String key = line.substring(0, separatorIndex);47 String value = line.substring(separatorIndex + 1);48 49 if (key == "AWS_DEVICE_NAME") {50 gDeviceName = value;51 } else if (key == "AWS_IOT_ENDPOINT") {52 gAwsEndpoint = value;53 }54 }55 }56 configFile.close();57 58 // 変数に読み込んだ値を設定59 //const char* aws_endpoint = awsEndpoint.c_str();60 //const char* deviceName = awsDeviceName.c_str();61 //awsEndpoint = sanitizeEndpoint(awsEndpoint);62 63 //aws_endpoint = awsEndpoint.c_str();64 //deviceName = awsDeviceName.c_str();65 Serial.println("AWS IoTエンドポイント: " + gAwsEndpoint);66 Serial.println("デバイス名: " + gDeviceName);67}68 69 70String gCaContent = "";71String gCertificateContent = "";72String gPrivateKeyContent = "";73 74 75void setupAWSIoT() {76 77 if(!SPIFFS.begin(true)){78 Serial.println("SPIFFSの初期化に失敗しました。");79 return;80 }81 82 readAWSDeviceName();83 84 if(!SPIFFS.begin(true)){85 Serial.println("SPIFFSの初期化に失敗しました。");86 return;87 }88 89 90 // Amazon Root CA 1証明書を読み込む91 File ca = SPIFFS.open("/key/AmazonRootCA1.pem", "r");92 if (!ca) {93 Serial.println("CA証明書ファイルの読み込みに失敗しました。");94 } else {95 Serial.println("CA証明書ファイルを読み込みました。");96 //String caContent = "";97 while (ca.available()) {98 gCaContent += char(ca.read());99 }100 Serial.print("CA証明書の内容の長さ: ");101 Serial.println(gCaContent.length()); // 読み込んだCA証明書の内容の長さを表示102 103 net.setCACert(gCaContent.c_str());104 ca.close();105 }106 107 if(!SPIFFS.begin(true)){108 Serial.println("SPIFFSの初期化に失敗しました。");109 return;110 }111 112 113 // 拡張子に基づいて証明書とキーを読み込む114 gCertificateContent = SPIFFSRead::readFirstFileWithExtension(".cert.pem");115 gPrivateKeyContent = SPIFFSRead::readFirstFileWithExtension(".private.key");116 117 // 読み込んだ内容でセットアップ118 if(gCertificateContent.length() > 0 && gPrivateKeyContent.length() > 0) {119 net.setCertificate(gCertificateContent.c_str());120 net.setPrivateKey(gPrivateKeyContent.c_str());121 122 // 内容の長さを出力する123 Serial.print("証明書の長さ: ");124 Serial.println(gCertificateContent.length());125 Serial.print("プライベートキーの長さ: ");126 Serial.println(gPrivateKeyContent.length());127 } else {128 Serial.println("証明書またはプライベートキーが見つかりませんでした。");129 }130 131 //client.setClient(net);132 // AWS IoTエンドポイントの設定133 client.setServer(gAwsEndpoint.c_str(), aws_port);134 135 //client.setKeepAlive(60);136 137 138 // タスクの作成と開始139 xTaskCreatePinnedToCore(140 sendDataToAWS, /* タスク関数 */141 "SendAWSTask", /* タスク名 */142 10000, /* スタックサイズ */143 NULL, /* タスクパラメータ */144 1, /* 優先度 */145 NULL, /* タスクハンドル */146 0 /* コアID */147 );148}149 150// AWSへデータを送信するタスク151void sendDataToAWS(void * parameter){152 153 for(;;){ // 無限ループ154 if (!client.connected()) {155 while (!client.connect(gDeviceName.c_str())) {156 delay(1000);157 }158 }159 160 // 現在の時刻をミリ秒で取得161 unsigned long currentTime = millis();162 163 #if 1164 // ペイロードを動的に生成165 String payload = createJsonPayload(166 gDeviceName, 167 "デバイスID", 168 "所属組織", 169 currentTime, 170 "センサー情報", 171 "ボタン状態", 172 "定時監視データ", 173 "予備データ1", 174 "予備データ2"175 );176 177 Serial.print("Payload size: ");178 Serial.println(payload.length()); // ペイロードのサイズを出力179 // 生成されたペイロードをAWS IoTに送信180 if(client.publish("topic/path", payload.c_str())){181 Serial.println("AWS IoTにデータを送信しました。");182 } else {183 Serial.println("AWS IoTへのデータ送信に失敗しました。");184 }185 186 #endif187 188 #if 0189 // データをAWS IoTに送信190 String payload = "{\"temperature\": 25.5}";191 client.publish("topic/path", payload.c_str());192 Serial.println("AWS IoTにデータを送信しました。");193 #endif194 195 //delay(60000); // 1分ごとに送信196 delay(30000); // 30secごとに送信197 }198}199 200
cpp
1#include <ArduinoJson.h> // JSON操作のためのライブラリをインクルード2#include "payload.h" // ヘッダーファイルをインクルード3 4// JSONペイロードを作成する関数5String createJsonPayload(6 const String& deviceName, 7 const String& deviceId, 8 const String& organization, 9 unsigned long timestamp, 10 const String& sensor, 11 const String& button, 12 const String& routineCheck, 13 const String& spare1, 14 const String& spare2 15 ) {16 StaticJsonDocument<512> jsonDoc; // JSONドキュメントを作成17 18 // データをJSONドキュメントに追加19 jsonDoc["deviceName"] = deviceName;20 jsonDoc["deviceId"] = deviceId;21 jsonDoc["organization"] = organization;22 jsonDoc["timestamp"] = timestamp;23 jsonDoc["sensor"] = sensor;24 jsonDoc["button"] = button;25 jsonDoc["routineCheck"] = routineCheck;26 #if 127 jsonDoc["spare1"] = spare1;28 jsonDoc["spare2"] = spare2;29 #endif30 31 String payload;32 serializeJson(jsonDoc, payload); // JSONオブジェクトを文字列に変換33 34 return payload; // 作成したペイロードを返す35}36
試したこと・調べたこと
上記の詳細・結果
jsonファイル内の
jsonDoc["spare1"] = spare1;
jsonDoc["spare2"] = spare2;
この2つをコメントアウトすると、203バイトとなり、これはAWSに正常にパブリッシュできることは確認できています。
chatGPTによると、1通信で最大128kバイトまで送信できると回答があるのですが、それにしては少なすぎるなと感じています。
githubのソースコードリンクはこちらです。
補足
特になし

0 コメント