Spresense LTE拡張ボードを使って、センサーデータをsoracom harvestに送信しています。
LTEUDPの使い方のところで不明な点がありますので、教えてください。
開発はArduinoIDEで行なっています。
UDPでのデータ送信のテストスケッチを添付しました。データは正常に受信できています。
APIドキュメントを読んだ限りでは、LTEUDPクラスのendPacketメソッドを実行すれば、パケットが送信されると思ったのですが、それでは送信してくれませんでした。色々、試行錯誤した結果、LTEClientクラスのconnectメソッドを呼ぶとデータが送信されるようになりました。connectメソッドは本来不要だと思いますが、間違っていますでしょうか?
よろしくお願いします。
// libraries
/* include the LTE library */
#include <LTE.h>
/* include the LTEUDP library */
#include <LTEUDP.h>
// APN data
#define LTE_APN "soracom.io" // APN Name
#define LTE_USER_NAME "sora" // APN username
#define LTE_PASSWORD "sora" // APN password
/*
* Network 接続先
* ENDPOINT_IP はカンマ区切りで記載
*/
//#define ENDPOINT_URL "harvest.soracom.io" // for SORACOM Harvest
#define ENDPOINT_URL "unified.soracom.io" //for SORACOM Unified Endpoint
//#define ENDPOINT_IP 100,127,111,111
//#define ENDPOINT_PORT 8514 // for SORACOM Harvest
#define ENDPOINT_PORT 23080 // for SORACOM Unified Endpoint
//#define ENDPOINT_PORT 8888
#define LOCAL_PORT 8888
char server[] = "harvest.soracom.io";
// initialize the library instance
LTE lteAccess;
LTEScanner scannerNetworks;
LTEUDP soraUDP;
LTEClient client;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("LTE networks scanner");
while (true) {
if (lteAccess.begin() == LTE_SEARCHING) {
if (lteAccess.attach(LTE_APN, LTE_USER_NAME, LTE_PASSWORD) == LTE_READY) {
Serial.println("attach succeeded.");
break;
}
Serial.println("An error occurred, shutdown and try again.");
lteAccess.shutdown();
sleep(1);
}
}
// Assigned IP address
IPAddress address = lteAccess.getIPAddress();
Serial.print("IP address: ");
Serial.println(address);
// currently connected carrier
Serial.print("Current carrier: ");
Serial.println(scannerNetworks.getCurrentCarrier());
long test = -1234567L;
uint8_t sendBytes[4] = {0x00, 0x00, 0x00, 0x00};
sendBytes[3] = uint8_t(test & 0xff);
sendBytes[2] = uint8_t((test >> 8) & 0xff);
sendBytes[1] = uint8_t((test >> 16) & 0xff);
sendBytes[0] = uint8_t((test >> 24) & 0xff);
Serial.print("-----senddata: ");
Serial.print(sendBytes[0], HEX);
Serial.print(sendBytes[1], HEX);
Serial.print(sendBytes[2], HEX);
Serial.print(sendBytes[3], HEX);
Serial.println("");
if (soraUDP.beginPacket(ENDPOINT_URL, ENDPOINT_PORT) == 1){
Serial.println(F("UDP OK"));
soraUDP.write(sendBytes[0]);
soraUDP.write(sendBytes[1]);
soraUDP.write(sendBytes[2]);
soraUDP.write(sendBytes[3]);
if (soraUDP.endPacket() == 1) {
Serial.println(F("Data send OK"));
}
else {
Serial.println(F("Data send NG"));
}
}
else {
Serial.println(F("UDP NG"));
}
soraUDP.stop();
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
client.stop();
lteAccess.shutdown();
}
void loop() {
// put your main code here, to run repeatedly:
}