diff --git a/examples/application/soracom-gps-tracker/soracom-gps-tracker.ino b/examples/application/soracom-gps-tracker/soracom-gps-tracker.ino index ac0c4c2..8f909c0 100644 --- a/examples/application/soracom-gps-tracker/soracom-gps-tracker.ino +++ b/examples/application/soracom-gps-tracker/soracom-gps-tracker.ino @@ -9,17 +9,19 @@ // http://librarymanager#ArduinoJson 7.0.4 #include -#include #include #include +#define SEARCH_ACCESS_TECHNOLOGY (WioCellularNetwork::SearchAccessTechnology::LTEM) +#define LTEM_BAND (WioCellularNetwork::NTTDOCOMO_LTEM_BAND) static const char APN[] = "soracom.io"; + static const char HOST[] = "uni.soracom.io"; static constexpr int PORT = 23080; -static constexpr int INTERVAL = 1000 * 60 * 5; // [ms] -static constexpr int POWER_ON_TIMEOUT = 20000; // [ms] -static constexpr int RECEIVE_TIMEOUT = 10000; // [ms] +static constexpr int INTERVAL = 1000 * 60 * 5; // [ms] +static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms] +static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms] #define ABORT_IF_FAILED(result) \ do { \ @@ -28,9 +30,7 @@ static constexpr int RECEIVE_TIMEOUT = 10000; // [ms] static uint32_t MeasureTime = -INTERVAL; static String LatestGpsData; -static String LatestRegStatus; -static constexpr int PDP_CONTEXT_ID = 1; static constexpr int SOCKET_ID = 0; static JsonDocument JsonDoc; @@ -52,7 +52,10 @@ void setup(void) { WioCellular.begin(); ABORT_IF_FAILED(WioCellular.powerOn(POWER_ON_TIMEOUT)); - setupCellular(); + WioNetwork.config.searchAccessTechnology = SEARCH_ACCESS_TECHNOLOGY; + WioNetwork.config.ltemBand = LTEM_BAND; + WioNetwork.config.apn = APN; + WioNetwork.begin(); WioCellular.enableGrovePower(); GpsBegin(); @@ -67,49 +70,22 @@ void loop(void) { } if (millis() - MeasureTime >= INTERVAL) { - digitalWrite(LED_BUILTIN, HIGH); + if (WioNetwork.canCommunicate()) { + digitalWrite(LED_BUILTIN, HIGH); - JsonDoc.clear(); - if (measure(JsonDoc)) { - std::string jsonStr; - serializeJson(JsonDoc, jsonStr); + JsonDoc.clear(); + if (measure(JsonDoc)) { + send(JsonDoc); + } - send(reinterpret_cast(jsonStr.data()), jsonStr.size()); + digitalWrite(LED_BUILTIN, LOW); } - digitalWrite(LED_BUILTIN, LOW); - MeasureTime = millis(); } - WioCellular.doWork(10); -} - -static void setupCellular(void) { - Serial.println("### Setup cellular"); - - WioCellular.registerUrcHandler([](const std::string& response) -> bool { - if (response.compare(0, 8, "+CEREG: ") == 0) { - LatestRegStatus = response.substr(8).c_str(); - return true; - } - return false; - }); - ABORT_IF_FAILED(WioCellular.setEpsNetworkRegistrationStatusUrc(2)); - - std::vector pdpContexts; - ABORT_IF_FAILED(WioCellular.getPdpContext(&pdpContexts)); - - if (std::find_if(pdpContexts.begin(), pdpContexts.end(), [](const WioCellularModule::PdpContext& pdpContext) { - return pdpContext.apn == APN; - }) - == pdpContexts.end()) { - ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(0)); - ABORT_IF_FAILED(WioCellular.setPdpContext({ PDP_CONTEXT_ID, "IP", APN, "0.0.0.0", 0, 0, 0 })); - ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(1)); - } - - Serial.println("### Completed"); + Serial.flush(); + WioCellular.doWork(10); // Spin } static bool measure(JsonDocument& doc) { @@ -117,8 +93,6 @@ static bool measure(JsonDocument& doc) { doc["uptime"] = millis() / 1000; - doc["regStatus"] = LatestRegStatus.c_str(); - int index[5]; index[0] = LatestGpsData.indexOf(','); index[1] = index[0] >= 0 ? LatestGpsData.indexOf(',', index[0] + 1) : -1; @@ -147,37 +121,43 @@ static bool measure(JsonDocument& doc) { return true; } -static bool send(const void* data, size_t size) { - bool result = true; - +static bool send(const JsonDocument& doc) { Serial.println("### Sending"); - if (result) { - Serial.print("Connecting "); - Serial.print(HOST); - Serial.print(":"); - Serial.println(PORT); - if (WioCellular.openSocket(PDP_CONTEXT_ID, SOCKET_ID, "TCP", HOST, PORT, 0) != WioCellularResult::Ok) { - Serial.println("ERROR: Failed to open socket"); - result = false; - } + int socketId; + if (WioCellular.getSocketUnusedConnectId(WioNetwork.config.pdpContextId, &socketId) != WioCellularResult::Ok) { + Serial.println("ERROR: Failed to get unused connect id"); + return false; } + Serial.print("Connecting "); + Serial.print(HOST); + Serial.print(":"); + Serial.println(PORT); + if (WioCellular.openSocket(WioNetwork.config.pdpContextId, socketId, "TCP", HOST, PORT, 0) != WioCellularResult::Ok) { + Serial.println("ERROR: Failed to open socket"); + return false; + } + + bool result = true; + if (result) { Serial.print("Sending "); - printData(Serial, data, size); + std::string str; + serializeJson(doc, str); + printData(Serial, str.data(), str.size()); Serial.println(); - if (WioCellular.sendSocket(SOCKET_ID, data, size) != WioCellularResult::Ok) { + if (WioCellular.sendSocket(socketId, str.data(), str.size()) != WioCellularResult::Ok) { Serial.println("ERROR: Failed to send socket"); result = false; } } - static uint8_t recvData[1500]; + static uint8_t recvData[WioCellular.RECEIVE_SOCKET_SIZE_MAX]; size_t recvSize; if (result) { Serial.println("Receiving"); - if (WioCellular.receiveSocket(SOCKET_ID, recvData, sizeof(recvData), &recvSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) { + if (WioCellular.receiveSocket(socketId, recvData, sizeof(recvData), &recvSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) { Serial.println("ERROR: Failed to receive socket"); result = false; } else { @@ -186,7 +166,7 @@ static bool send(const void* data, size_t size) { } } - if (WioCellular.closeSocket(SOCKET_ID) != WioCellularResult::Ok) { + if (WioCellular.closeSocket(socketId) != WioCellularResult::Ok) { Serial.println("ERROR: Failed to close socket"); result = false; } diff --git a/examples/cellular/shell/shell.ino b/examples/cellular/shell/shell.ino index 2f7a11d..e31cbc6 100644 --- a/examples/cellular/shell/shell.ino +++ b/examples/cellular/shell/shell.ino @@ -9,17 +9,18 @@ // https://github.com/matsujirushi/ntshell 0.3.1 #include -#include #include #include // Natural Tiny Shell #include // Natural Tiny Shell #include +#define SEARCH_ACCESS_TECHNOLOGY (WioCellularNetwork::SearchAccessTechnology::LTEM) +#define LTEM_BAND (WioCellularNetwork::NTTDOCOMO_LTEM_BAND) static const char APN[] = "soracom.io"; -static constexpr int POWER_ON_TIMEOUT = 20000; // [ms] +static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms] +static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms] -static constexpr int PDP_CONTEXT_ID = 1; static constexpr int SOCKET_ID = 0; static ntshell_t Shell; @@ -96,32 +97,14 @@ void setup(void) { Serial.printf("... %lu[ms]\n", millis() - start); } - Serial.printf("Set URC settings\n"); - WioCellular.setEpsNetworkRegistrationStatusUrc(2); - - Serial.printf("Check PDP context\n"); - std::vector pdpContexts; - WioCellularResult result; - if ((result = WioCellular.getPdpContext(&pdpContexts)) != WioCellularResult::Ok) { - Serial.printf("ERROR: %d\n", static_cast(result)); - abort(); - } - - if (std::find_if(pdpContexts.begin(), pdpContexts.end(), [](const WioCellularModule::PdpContext &pdpContext) { - return pdpContext.apn == APN; - }) - == pdpContexts.end()) { - Serial.printf("Set PDP context\n"); - WioCellular.setPhoneFunctionality(0); - if ((result = WioCellular.setPdpContext({ PDP_CONTEXT_ID, "IP", APN, "0.0.0.0", 0, 0, 0 })) != WioCellularResult::Ok) { - Serial.printf("ERROR: %d\n", static_cast(result)); - abort(); - } - WioCellular.setPhoneFunctionality(1); - } + WioNetwork.config.searchAccessTechnology = SEARCH_ACCESS_TECHNOLOGY; + WioNetwork.config.ltemBand = LTEM_BAND; + WioNetwork.config.apn = APN; + WioNetwork.begin(); } void loop(void) { + Serial.flush(); WioCellular.doWork(10); // Spin ntshell_execute_nb(&Shell); } @@ -262,7 +245,7 @@ static int CommandPdpContext(int argc, char **argv) { static int CommandSocket(int argc, char **argv) { std::vector statuses; - WioCellular.getSocketStatus(PDP_CONTEXT_ID, &statuses); + WioCellular.getSocketStatus(WioNetwork.config.pdpContextId, &statuses); Serial.printf("Socket Statuses:\n"); for (const auto &status : statuses) { @@ -283,7 +266,7 @@ static int CommandSocketOpen(int argc, char **argv) { return 1; } - WioCellular.openSocket(PDP_CONTEXT_ID, SOCKET_ID, argv[1], argv[2], atoi(argv[3]), 0); + WioCellular.openSocket(WioNetwork.config.pdpContextId, SOCKET_ID, argv[1], argv[2], atoi(argv[3]), 0); return 0; } @@ -326,7 +309,7 @@ static int CommandSocketSendReceive(int argc, char **argv) { static char data[1500]; size_t dataSize; - if (WioCellular.receiveSocket(SOCKET_ID, data, sizeof(data), &dataSize, 5000) != WioCellularResult::Ok) { + if (WioCellular.receiveSocket(SOCKET_ID, data, sizeof(data), &dataSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) { Serial.printf("RECEIVE ERROR\n"); return 0; } diff --git a/examples/cellular/transparent/transparent.ino b/examples/cellular/transparent/transparent.ino index e66d7cd..a831321 100644 --- a/examples/cellular/transparent/transparent.ino +++ b/examples/cellular/transparent/transparent.ino @@ -7,7 +7,7 @@ #include #include -static constexpr int POWER_ON_TIMEOUT = 20000; // [ms] +static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms] void setup(void) { Serial.begin(115200); diff --git a/examples/soracom/soracom-connectivity-diagnostics/soracom-connectivity-diagnostics.ino b/examples/soracom/soracom-connectivity-diagnostics/soracom-connectivity-diagnostics.ino index 1230ea4..f75ca10 100644 --- a/examples/soracom/soracom-connectivity-diagnostics/soracom-connectivity-diagnostics.ino +++ b/examples/soracom/soracom-connectivity-diagnostics/soracom-connectivity-diagnostics.ino @@ -15,7 +15,7 @@ #include #include -static constexpr int POWER_ON_TIMEOUT = 20000; // [ms] +static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms] #define CONSOLE Serial #define ABORT_IF_FAILED(result) \ @@ -134,6 +134,7 @@ static void pingToSoracomNetwork(void) { ABORT_IF_FAILED(executeCommand("AT+QPING=1,\"pong.soracom.io\",3,3", 300000)); const auto start = millis(); while (pingResponse.size() < 3 + 1) { + Serial.flush(); WioCellular.doWork(timeout - (millis() - start)); if (millis() - start >= timeout) break; } diff --git a/examples/soracom/soracom-uptime-httpclient/soracom-uptime-httpclient.ino b/examples/soracom/soracom-uptime-httpclient/soracom-uptime-httpclient.ino index 9961601..13a7b44 100644 --- a/examples/soracom/soracom-uptime-httpclient/soracom-uptime-httpclient.ino +++ b/examples/soracom/soracom-uptime-httpclient/soracom-uptime-httpclient.ino @@ -10,21 +10,22 @@ // http://librarymanager#ArduinoHttpClient 0.6.1 #include -#include #include -#include #include #include #include +#define SEARCH_ACCESS_TECHNOLOGY (WioCellularNetwork::SearchAccessTechnology::LTEM) +#define LTEM_BAND (WioCellularNetwork::NTTDOCOMO_LTEM_BAND) static const char APN[] = "soracom.io"; + static const char HOST[] = "metadata.soracom.io"; static const char PATH[] = "/v1/subscriber/tags"; static constexpr int PORT = 80; -static constexpr int INTERVAL = 1000 * 60 * 15; // [ms] -static constexpr int POWER_ON_TIMEOUT = 20000; // [ms] -static constexpr int RECEIVE_TIMEOUT = 10000; // [ms] +static constexpr int INTERVAL = 1000 * 60 * 5; // [ms] +static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms] +static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms] #define ABORT_IF_FAILED(result) \ do { \ @@ -60,63 +61,52 @@ void setup(void) { WioCellular.begin(); ABORT_IF_FAILED(WioCellular.powerOn(POWER_ON_TIMEOUT)); - setupCellular(); + WioNetwork.config.searchAccessTechnology = SEARCH_ACCESS_TECHNOLOGY; + WioNetwork.config.ltemBand = LTEM_BAND; + WioNetwork.config.apn = APN; + WioNetwork.begin(); digitalWrite(LED_BUILTIN, LOW); } void loop(void) { - digitalWrite(LED_BUILTIN, HIGH); - - JsonDoc.clear(); - if (generateRequestBody(JsonDoc)) { - std::string jsonStr; - serializeJson(JsonDoc, jsonStr); - Serial.println(jsonStr.c_str()); - - HttpResponse response = httpRequest(TcpClient, HOST, PORT, PATH, "PUT", "application/json", jsonStr.c_str()); - - Serial.println("Header(s):"); - for (auto header : response.headers) { - Serial.print(" "); - Serial.print(header.first.c_str()); - Serial.print(" : "); - Serial.print(header.second.c_str()); - Serial.println(); + if (WioNetwork.canCommunicate()) { + digitalWrite(LED_BUILTIN, HIGH); + + JsonDoc.clear(); + if (generateRequestBody(JsonDoc)) { + std::string jsonStr; + serializeJson(JsonDoc, jsonStr); + Serial.println(jsonStr.c_str()); + + HttpResponse response = httpRequest(TcpClient, HOST, PORT, PATH, "PUT", "application/json", jsonStr.c_str()); + + Serial.println("Header(s):"); + for (auto header : response.headers) { + Serial.print(" "); + Serial.print(header.first.c_str()); + Serial.print(" : "); + Serial.print(header.second.c_str()); + Serial.println(); + } + Serial.print("Body: "); + Serial.println(response.body.c_str()); + + if (response.statusCode == 200) { + JsonDoc.clear(); + deserializeJson(JsonDoc, response.body.c_str()); + // Output the IMSI field as an example of how to use the response + Serial.print("Response imsi> "); + Serial.print(JsonDoc["imsi"].as()); + Serial.println(); + } } - Serial.print("Body: "); - Serial.println(response.body.c_str()); - - if (response.statusCode == 200) { - JsonDoc.clear(); - deserializeJson(JsonDoc, response.body.c_str()); - // Output the IMSI field as an example of how to use the response - Serial.print("Response imsi> "); - Serial.print(JsonDoc["imsi"].as()); - Serial.println(); - } - } - digitalWrite(LED_BUILTIN, LOW); - delay(INTERVAL); -} - -static void setupCellular(void) { - Serial.println("### Setup cellular"); - - std::vector pdpContexts; - ABORT_IF_FAILED(WioCellular.getPdpContext(&pdpContexts)); - - if (std::find_if(pdpContexts.begin(), pdpContexts.end(), [](const WioCellularModule::PdpContext& pdpContext) { - return pdpContext.apn == APN; - }) - == pdpContexts.end()) { - ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(0)); - ABORT_IF_FAILED(WioCellular.setPdpContext({ PDP_CONTEXT_ID, "IP", APN, "0.0.0.0", 0, 0, 0 })); - ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(1)); + digitalWrite(LED_BUILTIN, LOW); } - Serial.println("### Completed"); + Serial.flush(); + WioCellular.doWorkUntil(INTERVAL); } /** diff --git a/examples/soracom/soracom-uptime-lp/soracom-uptime-lp.ino b/examples/soracom/soracom-uptime-lp/soracom-uptime-lp.ino index c1548fe..9897a56 100644 --- a/examples/soracom/soracom-uptime-lp/soracom-uptime-lp.ino +++ b/examples/soracom/soracom-uptime-lp/soracom-uptime-lp.ino @@ -9,11 +9,13 @@ // http://librarymanager#ArduinoJson 7.0.4 #include -#include #include #include +#define SEARCH_ACCESS_TECHNOLOGY (WioCellularNetwork::SearchAccessTechnology::LTEM) +#define LTEM_BAND (WioCellularNetwork::NTTDOCOMO_LTEM_BAND) static const char APN[] = "soracom.io"; + static const char HOST[] = "uni.soracom.io"; static constexpr int PORT = 23080; @@ -29,9 +31,6 @@ static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms] if ((result) != WioCellularResult::Ok) abort(); \ } while (0) -static constexpr int PDP_CONTEXT_ID = 1; -static constexpr int SOCKET_ID = 0; - static SemaphoreHandle_t CellularWorkSem; static SemaphoreHandle_t CellularStartSem; static SemaphoreHandle_t MeasureSem; @@ -65,7 +64,10 @@ void setup(void) { WioCellular.begin(); ABORT_IF_FAILED(WioCellular.powerOn(POWER_ON_TIMEOUT)); - setupCellular(); + WioNetwork.config.searchAccessTechnology = SEARCH_ACCESS_TECHNOLOGY; + WioNetwork.config.ltemBand = LTEM_BAND; + WioNetwork.config.apn = APN; + WioNetwork.begin(); assert(xTimerStart(xTimerCreate("CellularStart", pdMS_TO_TICKS(START_DELAY), pdFALSE, CellularStartSem, semaphoreGiveTimerHandler), 0) == pdPASS); @@ -73,6 +75,7 @@ void setup(void) { } void loop(void) { + Serial.flush(); const auto activatedMember = xQueueSelectFromSet(QueueSet, portMAX_DELAY); assert(activatedMember); @@ -94,37 +97,14 @@ void loop(void) { JsonDoc.clear(); if (measure(JsonDoc)) { - std::string jsonStr; - serializeJson(JsonDoc, jsonStr); - ABORT_IF_FAILED(WioCellular.powerOn(POWER_ON_TIMEOUT)); - send(reinterpret_cast(jsonStr.data()), jsonStr.size()); + send(JsonDoc); } } digitalWrite(LED_BUILTIN, LOW); } -static void setupCellular(void) { - Serial.println("### Setup cellular"); - - ABORT_IF_FAILED(WioCellular.setEpsNetworkRegistrationStatusUrc(2)); - - std::vector pdpContexts; - ABORT_IF_FAILED(WioCellular.getPdpContext(&pdpContexts)); - - if (std::find_if(pdpContexts.begin(), pdpContexts.end(), [](const WioCellularModule::PdpContext& pdpContext) { - return pdpContext.apn == APN; - }) - == pdpContexts.end()) { - ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(0)); - ABORT_IF_FAILED(WioCellular.setPdpContext({ PDP_CONTEXT_ID, "IP", APN, "0.0.0.0", 0, 0, 0 })); - ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(1)); - } - - Serial.println("### Completed"); -} - static bool measure(JsonDocument& doc) { Serial.println("### Measuring"); @@ -135,37 +115,43 @@ static bool measure(JsonDocument& doc) { return true; } -static bool send(const void* data, size_t size) { - bool result = true; - +static bool send(const JsonDocument& doc) { Serial.println("### Sending"); - if (result) { - Serial.print("Connecting "); - Serial.print(HOST); - Serial.print(":"); - Serial.println(PORT); - if (WioCellular.openSocket(PDP_CONTEXT_ID, SOCKET_ID, "TCP", HOST, PORT, 0) != WioCellularResult::Ok) { - Serial.println("ERROR: Failed to open socket"); - result = false; - } + int socketId; + if (WioCellular.getSocketUnusedConnectId(WioNetwork.config.pdpContextId, &socketId) != WioCellularResult::Ok) { + Serial.println("ERROR: Failed to get unused connect id"); + return false; + } + + Serial.print("Connecting "); + Serial.print(HOST); + Serial.print(":"); + Serial.println(PORT); + if (WioCellular.openSocket(WioNetwork.config.pdpContextId, socketId, "TCP", HOST, PORT, 0) != WioCellularResult::Ok) { + Serial.println("ERROR: Failed to open socket"); + return false; } + bool result = true; + if (result) { Serial.print("Sending "); - printData(Serial, data, size); + std::string str; + serializeJson(doc, str); + printData(Serial, str.data(), str.size()); Serial.println(); - if (WioCellular.sendSocket(SOCKET_ID, data, size) != WioCellularResult::Ok) { + if (WioCellular.sendSocket(socketId, str.data(), str.size()) != WioCellularResult::Ok) { Serial.println("ERROR: Failed to send socket"); result = false; } } - static uint8_t recvData[1500]; + static uint8_t recvData[WioCellular.RECEIVE_SOCKET_SIZE_MAX]; size_t recvSize; if (result) { Serial.println("Receiving"); - if (WioCellular.receiveSocket(SOCKET_ID, recvData, sizeof(recvData), &recvSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) { + if (WioCellular.receiveSocket(socketId, recvData, sizeof(recvData), &recvSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) { Serial.println("ERROR: Failed to receive socket"); result = false; } else { @@ -174,7 +160,7 @@ static bool send(const void* data, size_t size) { } } - if (WioCellular.closeSocket(SOCKET_ID) != WioCellularResult::Ok) { + if (WioCellular.closeSocket(socketId) != WioCellularResult::Ok) { Serial.println("ERROR: Failed to close socket"); result = false; } diff --git a/examples/soracom/soracom-uptime-tcpclient/soracom-uptime-tcpclient.ino b/examples/soracom/soracom-uptime-tcpclient/soracom-uptime-tcpclient.ino index 724d0ee..0693668 100644 --- a/examples/soracom/soracom-uptime-tcpclient/soracom-uptime-tcpclient.ino +++ b/examples/soracom/soracom-uptime-tcpclient/soracom-uptime-tcpclient.ino @@ -9,17 +9,19 @@ // http://librarymanager#ArduinoJson 7.0.4 #include -#include #include #include +#define SEARCH_ACCESS_TECHNOLOGY (WioCellularNetwork::SearchAccessTechnology::LTEM) +#define LTEM_BAND (WioCellularNetwork::NTTDOCOMO_LTEM_BAND) static const char APN[] = "soracom.io"; + static const char HOST[] = "uni.soracom.io"; static constexpr int PORT = 23080; -static constexpr int INTERVAL = 1000 * 60 * 15; // [ms] -static constexpr int POWER_ON_TIMEOUT = 20000; // [ms] -static constexpr int RECEIVE_TIMEOUT = 10000; // [ms] +static constexpr int INTERVAL = 1000 * 60 * 5; // [ms] +static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms] +static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms] #define ABORT_IF_FAILED(result) \ do { \ @@ -49,42 +51,28 @@ void setup(void) { WioCellular.begin(); ABORT_IF_FAILED(WioCellular.powerOn(POWER_ON_TIMEOUT)); - setupCellular(); + WioNetwork.config.searchAccessTechnology = SEARCH_ACCESS_TECHNOLOGY; + WioNetwork.config.ltemBand = LTEM_BAND; + WioNetwork.config.apn = APN; + WioNetwork.begin(); digitalWrite(LED_BUILTIN, LOW); } void loop(void) { - digitalWrite(LED_BUILTIN, HIGH); - - JsonDoc.clear(); - if (measure(JsonDoc)) { - std::string jsonStr; - serializeJson(JsonDoc, jsonStr); - - send(reinterpret_cast(jsonStr.data()), jsonStr.size()); - } - - digitalWrite(LED_BUILTIN, LOW); - delay(INTERVAL); -} - -static void setupCellular(void) { - Serial.println("### Setup cellular"); + if (WioNetwork.canCommunicate()) { + digitalWrite(LED_BUILTIN, HIGH); - std::vector pdpContexts; - ABORT_IF_FAILED(WioCellular.getPdpContext(&pdpContexts)); + JsonDoc.clear(); + if (measure(JsonDoc)) { + send(JsonDoc); + } - if (std::find_if(pdpContexts.begin(), pdpContexts.end(), [](const WioCellularModule::PdpContext& pdpContext) { - return pdpContext.apn == APN; - }) - == pdpContexts.end()) { - ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(0)); - ABORT_IF_FAILED(WioCellular.setPdpContext({ PDP_CONTEXT_ID, "IP", APN, "0.0.0.0", 0, 0, 0 })); - ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(1)); + digitalWrite(LED_BUILTIN, LOW); } - Serial.println("### Completed"); + Serial.flush(); + WioCellular.doWorkUntil(INTERVAL); } static bool measure(JsonDocument& doc) { @@ -97,27 +85,27 @@ static bool measure(JsonDocument& doc) { return true; } -static bool send(const void* data, size_t size) { - bool result = true; - +static bool send(const JsonDocument& doc) { Serial.println("### Sending"); - if (result) { - Serial.print("Connecting "); - Serial.print(HOST); - Serial.print(":"); - Serial.println(PORT); - if (!TcpClient.connect(HOST, PORT)) { - Serial.println("ERROR: Failed to open socket"); - result = false; - } + Serial.print("Connecting "); + Serial.print(HOST); + Serial.print(":"); + Serial.println(PORT); + if (!TcpClient.connect(HOST, PORT)) { + Serial.println("ERROR: Failed to open socket"); + return false; } + bool result = true; + if (result) { Serial.print("Sending "); - printData(Serial, data, size); + std::string str; + serializeJson(doc, str); + printData(Serial, str.data(), str.size()); Serial.println(); - if (TcpClient.write(reinterpret_cast(data), size) != size) { + if (TcpClient.write(reinterpret_cast(str.data()), str.size()) != str.size()) { Serial.println("ERROR: Failed to send socket"); result = false; } @@ -136,7 +124,7 @@ static bool send(const void* data, size_t size) { } } - static uint8_t recvData[1500]; + static uint8_t recvData[WioCellular.RECEIVE_SOCKET_SIZE_MAX]; int recvSize; if (result) { recvSize = TcpClient.read(recvData, sizeof(recvData)); diff --git a/examples/soracom/soracom-uptime/soracom-uptime.ino b/examples/soracom/soracom-uptime/soracom-uptime.ino index 81e1cb9..c8128a9 100644 --- a/examples/soracom/soracom-uptime/soracom-uptime.ino +++ b/examples/soracom/soracom-uptime/soracom-uptime.ino @@ -1,5 +1,5 @@ /* - * soracom-uptime.ino + * soracom-uptime-wionetwork.ino * Copyright (C) Seeed K.K. * MIT License */ @@ -9,25 +9,19 @@ // http://librarymanager#ArduinoJson 7.0.4 #include -#include #include #include +#define SEARCH_ACCESS_TECHNOLOGY (WioCellularNetwork::SearchAccessTechnology::LTEM) +#define LTEM_BAND (WioCellularNetwork::NTTDOCOMO_LTEM_BAND) static const char APN[] = "soracom.io"; + static const char HOST[] = "uni.soracom.io"; static constexpr int PORT = 23080; -static constexpr int INTERVAL = 1000 * 60 * 15; // [ms] -static constexpr int POWER_ON_TIMEOUT = 20000; // [ms] -static constexpr int RECEIVE_TIMEOUT = 10000; // [ms] - -#define ABORT_IF_FAILED(result) \ - do { \ - if ((result) != WioCellularResult::Ok) abort(); \ - } while (0) - -static constexpr int PDP_CONTEXT_ID = 1; -static constexpr int SOCKET_ID = 0; +static constexpr int INTERVAL = 1000 * 60 * 5; // [ms] +static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms] +static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms] static JsonDocument JsonDoc; @@ -43,50 +37,33 @@ void setup(void) { Serial.println(); Serial.println("Startup"); - digitalWrite(LED_BUILTIN, HIGH); WioCellular.begin(); - ABORT_IF_FAILED(WioCellular.powerOn(POWER_ON_TIMEOUT)); + if (WioCellular.powerOn(POWER_ON_TIMEOUT) != WioCellularResult::Ok) abort(); - setupCellular(); - - digitalWrite(LED_BUILTIN, LOW); + WioNetwork.config.searchAccessTechnology = SEARCH_ACCESS_TECHNOLOGY; + WioNetwork.config.ltemBand = LTEM_BAND; + WioNetwork.config.apn = APN; + WioNetwork.begin(); } void loop(void) { - digitalWrite(LED_BUILTIN, HIGH); + if (WioNetwork.canCommunicate()) { + digitalWrite(LED_BUILTIN, HIGH); - JsonDoc.clear(); - if (measure(JsonDoc)) { - std::string jsonStr; - serializeJson(JsonDoc, jsonStr); + JsonDoc.clear(); + if (measure(JsonDoc)) { + send(JsonDoc); + } - send(reinterpret_cast(jsonStr.data()), jsonStr.size()); + digitalWrite(LED_BUILTIN, LOW); } - digitalWrite(LED_BUILTIN, LOW); - delay(INTERVAL); + Serial.flush(); + WioCellular.doWorkUntil(INTERVAL); } -static void setupCellular(void) { - Serial.println("### Setup cellular"); - - std::vector pdpContexts; - ABORT_IF_FAILED(WioCellular.getPdpContext(&pdpContexts)); - - if (std::find_if(pdpContexts.begin(), pdpContexts.end(), [](const WioCellularModule::PdpContext& pdpContext) { - return pdpContext.apn == APN; - }) - == pdpContexts.end()) { - ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(0)); - ABORT_IF_FAILED(WioCellular.setPdpContext({ PDP_CONTEXT_ID, "IP", APN, "0.0.0.0", 0, 0, 0 })); - ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(1)); - } - - Serial.println("### Completed"); -} - -static bool measure(JsonDocument& doc) { +static bool measure(JsonDocument &doc) { Serial.println("### Measuring"); doc["uptime"] = millis() / 1000; @@ -96,37 +73,43 @@ static bool measure(JsonDocument& doc) { return true; } -static bool send(const void* data, size_t size) { - bool result = true; - +static bool send(const JsonDocument &doc) { Serial.println("### Sending"); - if (result) { - Serial.print("Connecting "); - Serial.print(HOST); - Serial.print(":"); - Serial.println(PORT); - if (WioCellular.openSocket(PDP_CONTEXT_ID, SOCKET_ID, "TCP", HOST, PORT, 0) != WioCellularResult::Ok) { - Serial.println("ERROR: Failed to open socket"); - result = false; - } + int socketId; + if (WioCellular.getSocketUnusedConnectId(WioNetwork.config.pdpContextId, &socketId) != WioCellularResult::Ok) { + Serial.println("ERROR: Failed to get unused connect id"); + return false; + } + + Serial.print("Connecting "); + Serial.print(HOST); + Serial.print(":"); + Serial.println(PORT); + if (WioCellular.openSocket(WioNetwork.config.pdpContextId, socketId, "TCP", HOST, PORT, 0) != WioCellularResult::Ok) { + Serial.println("ERROR: Failed to open socket"); + return false; } + bool result = true; + if (result) { Serial.print("Sending "); - printData(Serial, data, size); + std::string str; + serializeJson(doc, str); + printData(Serial, str.data(), str.size()); Serial.println(); - if (WioCellular.sendSocket(SOCKET_ID, data, size) != WioCellularResult::Ok) { + if (WioCellular.sendSocket(socketId, str.data(), str.size()) != WioCellularResult::Ok) { Serial.println("ERROR: Failed to send socket"); result = false; } } - static uint8_t recvData[1500]; + static uint8_t recvData[WioCellular.RECEIVE_SOCKET_SIZE_MAX]; size_t recvSize; if (result) { Serial.println("Receiving"); - if (WioCellular.receiveSocket(SOCKET_ID, recvData, sizeof(recvData), &recvSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) { + if (WioCellular.receiveSocket(socketId, recvData, sizeof(recvData), &recvSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) { Serial.println("ERROR: Failed to receive socket"); result = false; } else { @@ -135,7 +118,7 @@ static bool send(const void* data, size_t size) { } } - if (WioCellular.closeSocket(SOCKET_ID) != WioCellularResult::Ok) { + if (WioCellular.closeSocket(socketId) != WioCellularResult::Ok) { Serial.println("ERROR: Failed to close socket"); result = false; } @@ -147,8 +130,8 @@ static bool send(const void* data, size_t size) { } template -void printData(T& stream, const void* data, size_t size) { - auto p = static_cast(data); +void printData(T &stream, const void *data, size_t size) { + auto p = static_cast(data); for (; size > 0; --size, ++p) stream.write(0x20 <= *p && *p <= 0x7f ? *p : '.'); diff --git a/extras/experimental/soracom-uptime-wionetwork/soracom-uptime-wionetwork.ino b/extras/experimental/soracom-uptime-wionetwork/soracom-uptime-wionetwork.ino deleted file mode 100644 index b8727dd..0000000 --- a/extras/experimental/soracom-uptime-wionetwork/soracom-uptime-wionetwork.ino +++ /dev/null @@ -1,138 +0,0 @@ -/* - * soracom-uptime-wionetwork.ino - * Copyright (C) Seeed K.K. - * MIT License - */ - -//////////////////////////////////////////////////////////////////////////////// -// Libraries: -// http://librarymanager#ArduinoJson 7.0.4 - -#include -#include -#include - -static const char APN[] = "soracom.io"; -static const char HOST[] = "uni.soracom.io"; -static constexpr int PORT = 23080; - -static constexpr int INTERVAL = 1000 * 60 * 15; // [ms] -static constexpr int POWER_ON_TIMEOUT = 20000; // [ms] -static constexpr int RECEIVE_TIMEOUT = 10000; // [ms] - -static JsonDocument JsonDoc; - -void setup(void) { - Serial.begin(115200); - { - const auto start = millis(); - while (!Serial && millis() - start < 5000) { - delay(2); - } - } - Serial.println(); - Serial.println(); - - Serial.println("Startup"); - - WioCellular.begin(); - if (WioCellular.powerOn(POWER_ON_TIMEOUT) != WioCellularResult::Ok) abort(); - - WioNetwork.config.apn = APN; - WioNetwork.config.searchAccessTechnology = WioNetwork.SearchAccessTechnology::LTEM; - WioNetwork.config.ltemBand = WioNetwork.NTTDOCOMO_LTEM_BAND; - // WioNetwork.config.ltemBand = WioNetwork.KDDI_LTEM_BAND; - WioNetwork.begin(); -} - -void loop(void) { - if (WioNetwork.canCommunicate()) { - digitalWrite(LED_BUILTIN, HIGH); - - JsonDoc.clear(); - if (measure(JsonDoc)) { - send(JsonDoc); - } - - digitalWrite(LED_BUILTIN, LOW); - } - - Serial.flush(); - const auto start = millis(); - while (millis() - start < INTERVAL) { - WioCellular.doWork(INTERVAL - (millis() - start)); - } -} - -static bool measure(JsonDocument &doc) { - Serial.println("### Measuring"); - - doc["uptime"] = millis() / 1000; - - Serial.println("### Completed"); - - return true; -} - -static bool send(const JsonDocument &doc) { - Serial.println("### Sending"); - - int socketId; - if (WioCellular.getSocketUnusedConnectId(WioNetwork.config.pdpContextId, &socketId) != WioCellularResult::Ok) { - Serial.println("ERROR: Failed to get unused connect id"); - return false; - } - - Serial.print("Connecting "); - Serial.print(HOST); - Serial.print(":"); - Serial.println(PORT); - if (WioCellular.openSocket(WioNetwork.config.pdpContextId, socketId, "TCP", HOST, PORT, 0) != WioCellularResult::Ok) { - Serial.println("ERROR: Failed to open socket"); - return false; - } - bool result = true; - - if (result) { - Serial.print("Sending "); - std::string str; - serializeJson(doc, str); - printData(Serial, str.data(), str.size()); - Serial.println(); - if (WioCellular.sendSocket(socketId, str.data(), str.size()) != WioCellularResult::Ok) { - Serial.println("ERROR: Failed to send socket"); - result = false; - } - } - - static uint8_t recvData[1500]; - size_t recvSize; - if (result) { - Serial.println("Receiving"); - if (WioCellular.receiveSocket(socketId, recvData, sizeof(recvData), &recvSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) { - Serial.println("ERROR: Failed to receive socket"); - result = false; - } else { - printData(Serial, recvData, recvSize); - Serial.println(); - } - } - - if (WioCellular.closeSocket(socketId) != WioCellularResult::Ok) { - Serial.println("ERROR: Failed to close socket"); - result = false; - } - - if (result) - Serial.println("### Completed"); - - return result; -} - -template -void printData(T &stream, const void *data, size_t size) { - auto p = static_cast(data); - - for (; size > 0; --size, ++p) - stream.write(0x20 <= *p && *p <= 0x7f ? *p : '.'); -} diff --git a/extras/tools/inspection/inspection.ino b/extras/tools/inspection/inspection.ino index 96bfbc4..4c5c1d8 100644 --- a/extras/tools/inspection/inspection.ino +++ b/extras/tools/inspection/inspection.ino @@ -14,7 +14,7 @@ using namespace ace_button; -static constexpr int POWER_ON_TIMEOUT = 20000; // [ms] +static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms] static const char* CELLULAR_REVISION = "BG770AGLAAR02A05_JP_01.200.01.200";