Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
matsujirushi committed Oct 2, 2024
1 parent 79693cd commit f05d320
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 441 deletions.
104 changes: 42 additions & 62 deletions examples/application/soracom-gps-tracker/soracom-gps-tracker.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
// http://librarymanager#ArduinoJson 7.0.4

#include <Adafruit_TinyUSB.h>
#include <algorithm>
#include <WioCellular.h>
#include <ArduinoJson.h>

#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 { \
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -67,58 +70,29 @@ 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<const uint8_t*>(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<WioCellularModule::PdpContext> 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) {
Serial.println("### Measuring");

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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down
41 changes: 12 additions & 29 deletions examples/cellular/shell/shell.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
// https://github.com/matsujirushi/ntshell 0.3.1

#include <Adafruit_TinyUSB.h>
#include <algorithm>
#include <nrfx_power.h>
#include <ntshell.h> // Natural Tiny Shell
#include <util/ntopt.h> // Natural Tiny Shell
#include <WioCellular.h>

#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;
Expand Down Expand Up @@ -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<WioCellularModule::PdpContext> pdpContexts;
WioCellularResult result;
if ((result = WioCellular.getPdpContext(&pdpContexts)) != WioCellularResult::Ok) {
Serial.printf("ERROR: %d\n", static_cast<int>(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<int>(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);
}
Expand Down Expand Up @@ -262,7 +245,7 @@ static int CommandPdpContext(int argc, char **argv) {

static int CommandSocket(int argc, char **argv) {
std::vector<WioCellularModule::SocketStatus> statuses;
WioCellular.getSocketStatus(PDP_CONTEXT_ID, &statuses);
WioCellular.getSocketStatus(WioNetwork.config.pdpContextId, &statuses);

Serial.printf("Socket Statuses:\n");
for (const auto &status : statuses) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/cellular/transparent/transparent.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <Adafruit_TinyUSB.h>
#include <WioCellular.h>

static constexpr int POWER_ON_TIMEOUT = 20000; // [ms]
static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms]

void setup(void) {
Serial.begin(115200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <Adafruit_TinyUSB.h>
#include <WioCellular.h>

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) \
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit f05d320

Please sign in to comment.