Skip to content

Commit f05d320

Browse files
committed
Update examples
1 parent 79693cd commit f05d320

File tree

10 files changed

+214
-441
lines changed

10 files changed

+214
-441
lines changed

examples/application/soracom-gps-tracker/soracom-gps-tracker.ino

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99
// http://librarymanager#ArduinoJson 7.0.4
1010

1111
#include <Adafruit_TinyUSB.h>
12-
#include <algorithm>
1312
#include <WioCellular.h>
1413
#include <ArduinoJson.h>
1514

15+
#define SEARCH_ACCESS_TECHNOLOGY (WioCellularNetwork::SearchAccessTechnology::LTEM)
16+
#define LTEM_BAND (WioCellularNetwork::NTTDOCOMO_LTEM_BAND)
1617
static const char APN[] = "soracom.io";
18+
1719
static const char HOST[] = "uni.soracom.io";
1820
static constexpr int PORT = 23080;
1921

20-
static constexpr int INTERVAL = 1000 * 60 * 5; // [ms]
21-
static constexpr int POWER_ON_TIMEOUT = 20000; // [ms]
22-
static constexpr int RECEIVE_TIMEOUT = 10000; // [ms]
22+
static constexpr int INTERVAL = 1000 * 60 * 5; // [ms]
23+
static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms]
24+
static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms]
2325

2426
#define ABORT_IF_FAILED(result) \
2527
do { \
@@ -28,9 +30,7 @@ static constexpr int RECEIVE_TIMEOUT = 10000; // [ms]
2830

2931
static uint32_t MeasureTime = -INTERVAL;
3032
static String LatestGpsData;
31-
static String LatestRegStatus;
3233

33-
static constexpr int PDP_CONTEXT_ID = 1;
3434
static constexpr int SOCKET_ID = 0;
3535

3636
static JsonDocument JsonDoc;
@@ -52,7 +52,10 @@ void setup(void) {
5252
WioCellular.begin();
5353
ABORT_IF_FAILED(WioCellular.powerOn(POWER_ON_TIMEOUT));
5454

55-
setupCellular();
55+
WioNetwork.config.searchAccessTechnology = SEARCH_ACCESS_TECHNOLOGY;
56+
WioNetwork.config.ltemBand = LTEM_BAND;
57+
WioNetwork.config.apn = APN;
58+
WioNetwork.begin();
5659

5760
WioCellular.enableGrovePower();
5861
GpsBegin();
@@ -67,58 +70,29 @@ void loop(void) {
6770
}
6871

6972
if (millis() - MeasureTime >= INTERVAL) {
70-
digitalWrite(LED_BUILTIN, HIGH);
73+
if (WioNetwork.canCommunicate()) {
74+
digitalWrite(LED_BUILTIN, HIGH);
7175

72-
JsonDoc.clear();
73-
if (measure(JsonDoc)) {
74-
std::string jsonStr;
75-
serializeJson(JsonDoc, jsonStr);
76+
JsonDoc.clear();
77+
if (measure(JsonDoc)) {
78+
send(JsonDoc);
79+
}
7680

77-
send(reinterpret_cast<const uint8_t*>(jsonStr.data()), jsonStr.size());
81+
digitalWrite(LED_BUILTIN, LOW);
7882
}
7983

80-
digitalWrite(LED_BUILTIN, LOW);
81-
8284
MeasureTime = millis();
8385
}
8486

85-
WioCellular.doWork(10);
86-
}
87-
88-
static void setupCellular(void) {
89-
Serial.println("### Setup cellular");
90-
91-
WioCellular.registerUrcHandler([](const std::string& response) -> bool {
92-
if (response.compare(0, 8, "+CEREG: ") == 0) {
93-
LatestRegStatus = response.substr(8).c_str();
94-
return true;
95-
}
96-
return false;
97-
});
98-
ABORT_IF_FAILED(WioCellular.setEpsNetworkRegistrationStatusUrc(2));
99-
100-
std::vector<WioCellularModule::PdpContext> pdpContexts;
101-
ABORT_IF_FAILED(WioCellular.getPdpContext(&pdpContexts));
102-
103-
if (std::find_if(pdpContexts.begin(), pdpContexts.end(), [](const WioCellularModule::PdpContext& pdpContext) {
104-
return pdpContext.apn == APN;
105-
})
106-
== pdpContexts.end()) {
107-
ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(0));
108-
ABORT_IF_FAILED(WioCellular.setPdpContext({ PDP_CONTEXT_ID, "IP", APN, "0.0.0.0", 0, 0, 0 }));
109-
ABORT_IF_FAILED(WioCellular.setPhoneFunctionality(1));
110-
}
111-
112-
Serial.println("### Completed");
87+
Serial.flush();
88+
WioCellular.doWork(10); // Spin
11389
}
11490

11591
static bool measure(JsonDocument& doc) {
11692
Serial.println("### Measuring");
11793

11894
doc["uptime"] = millis() / 1000;
11995

120-
doc["regStatus"] = LatestRegStatus.c_str();
121-
12296
int index[5];
12397
index[0] = LatestGpsData.indexOf(',');
12498
index[1] = index[0] >= 0 ? LatestGpsData.indexOf(',', index[0] + 1) : -1;
@@ -147,37 +121,43 @@ static bool measure(JsonDocument& doc) {
147121
return true;
148122
}
149123

150-
static bool send(const void* data, size_t size) {
151-
bool result = true;
152-
124+
static bool send(const JsonDocument& doc) {
153125
Serial.println("### Sending");
154126

155-
if (result) {
156-
Serial.print("Connecting ");
157-
Serial.print(HOST);
158-
Serial.print(":");
159-
Serial.println(PORT);
160-
if (WioCellular.openSocket(PDP_CONTEXT_ID, SOCKET_ID, "TCP", HOST, PORT, 0) != WioCellularResult::Ok) {
161-
Serial.println("ERROR: Failed to open socket");
162-
result = false;
163-
}
127+
int socketId;
128+
if (WioCellular.getSocketUnusedConnectId(WioNetwork.config.pdpContextId, &socketId) != WioCellularResult::Ok) {
129+
Serial.println("ERROR: Failed to get unused connect id");
130+
return false;
164131
}
165132

133+
Serial.print("Connecting ");
134+
Serial.print(HOST);
135+
Serial.print(":");
136+
Serial.println(PORT);
137+
if (WioCellular.openSocket(WioNetwork.config.pdpContextId, socketId, "TCP", HOST, PORT, 0) != WioCellularResult::Ok) {
138+
Serial.println("ERROR: Failed to open socket");
139+
return false;
140+
}
141+
142+
bool result = true;
143+
166144
if (result) {
167145
Serial.print("Sending ");
168-
printData(Serial, data, size);
146+
std::string str;
147+
serializeJson(doc, str);
148+
printData(Serial, str.data(), str.size());
169149
Serial.println();
170-
if (WioCellular.sendSocket(SOCKET_ID, data, size) != WioCellularResult::Ok) {
150+
if (WioCellular.sendSocket(socketId, str.data(), str.size()) != WioCellularResult::Ok) {
171151
Serial.println("ERROR: Failed to send socket");
172152
result = false;
173153
}
174154
}
175155

176-
static uint8_t recvData[1500];
156+
static uint8_t recvData[WioCellular.RECEIVE_SOCKET_SIZE_MAX];
177157
size_t recvSize;
178158
if (result) {
179159
Serial.println("Receiving");
180-
if (WioCellular.receiveSocket(SOCKET_ID, recvData, sizeof(recvData), &recvSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) {
160+
if (WioCellular.receiveSocket(socketId, recvData, sizeof(recvData), &recvSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) {
181161
Serial.println("ERROR: Failed to receive socket");
182162
result = false;
183163
} else {
@@ -186,7 +166,7 @@ static bool send(const void* data, size_t size) {
186166
}
187167
}
188168

189-
if (WioCellular.closeSocket(SOCKET_ID) != WioCellularResult::Ok) {
169+
if (WioCellular.closeSocket(socketId) != WioCellularResult::Ok) {
190170
Serial.println("ERROR: Failed to close socket");
191171
result = false;
192172
}

examples/cellular/shell/shell.ino

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
// https://github.com/matsujirushi/ntshell 0.3.1
1010

1111
#include <Adafruit_TinyUSB.h>
12-
#include <algorithm>
1312
#include <nrfx_power.h>
1413
#include <ntshell.h> // Natural Tiny Shell
1514
#include <util/ntopt.h> // Natural Tiny Shell
1615
#include <WioCellular.h>
1716

17+
#define SEARCH_ACCESS_TECHNOLOGY (WioCellularNetwork::SearchAccessTechnology::LTEM)
18+
#define LTEM_BAND (WioCellularNetwork::NTTDOCOMO_LTEM_BAND)
1819
static const char APN[] = "soracom.io";
1920

20-
static constexpr int POWER_ON_TIMEOUT = 20000; // [ms]
21+
static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms]
22+
static constexpr int RECEIVE_TIMEOUT = 1000 * 10; // [ms]
2123

22-
static constexpr int PDP_CONTEXT_ID = 1;
2324
static constexpr int SOCKET_ID = 0;
2425

2526
static ntshell_t Shell;
@@ -96,32 +97,14 @@ void setup(void) {
9697
Serial.printf("... %lu[ms]\n", millis() - start);
9798
}
9899

99-
Serial.printf("Set URC settings\n");
100-
WioCellular.setEpsNetworkRegistrationStatusUrc(2);
101-
102-
Serial.printf("Check PDP context\n");
103-
std::vector<WioCellularModule::PdpContext> pdpContexts;
104-
WioCellularResult result;
105-
if ((result = WioCellular.getPdpContext(&pdpContexts)) != WioCellularResult::Ok) {
106-
Serial.printf("ERROR: %d\n", static_cast<int>(result));
107-
abort();
108-
}
109-
110-
if (std::find_if(pdpContexts.begin(), pdpContexts.end(), [](const WioCellularModule::PdpContext &pdpContext) {
111-
return pdpContext.apn == APN;
112-
})
113-
== pdpContexts.end()) {
114-
Serial.printf("Set PDP context\n");
115-
WioCellular.setPhoneFunctionality(0);
116-
if ((result = WioCellular.setPdpContext({ PDP_CONTEXT_ID, "IP", APN, "0.0.0.0", 0, 0, 0 })) != WioCellularResult::Ok) {
117-
Serial.printf("ERROR: %d\n", static_cast<int>(result));
118-
abort();
119-
}
120-
WioCellular.setPhoneFunctionality(1);
121-
}
100+
WioNetwork.config.searchAccessTechnology = SEARCH_ACCESS_TECHNOLOGY;
101+
WioNetwork.config.ltemBand = LTEM_BAND;
102+
WioNetwork.config.apn = APN;
103+
WioNetwork.begin();
122104
}
123105

124106
void loop(void) {
107+
Serial.flush();
125108
WioCellular.doWork(10); // Spin
126109
ntshell_execute_nb(&Shell);
127110
}
@@ -262,7 +245,7 @@ static int CommandPdpContext(int argc, char **argv) {
262245

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

267250
Serial.printf("Socket Statuses:\n");
268251
for (const auto &status : statuses) {
@@ -283,7 +266,7 @@ static int CommandSocketOpen(int argc, char **argv) {
283266
return 1;
284267
}
285268

286-
WioCellular.openSocket(PDP_CONTEXT_ID, SOCKET_ID, argv[1], argv[2], atoi(argv[3]), 0);
269+
WioCellular.openSocket(WioNetwork.config.pdpContextId, SOCKET_ID, argv[1], argv[2], atoi(argv[3]), 0);
287270

288271
return 0;
289272
}
@@ -326,7 +309,7 @@ static int CommandSocketSendReceive(int argc, char **argv) {
326309

327310
static char data[1500];
328311
size_t dataSize;
329-
if (WioCellular.receiveSocket(SOCKET_ID, data, sizeof(data), &dataSize, 5000) != WioCellularResult::Ok) {
312+
if (WioCellular.receiveSocket(SOCKET_ID, data, sizeof(data), &dataSize, RECEIVE_TIMEOUT) != WioCellularResult::Ok) {
330313
Serial.printf("RECEIVE ERROR\n");
331314
return 0;
332315
}

examples/cellular/transparent/transparent.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <Adafruit_TinyUSB.h>
88
#include <WioCellular.h>
99

10-
static constexpr int POWER_ON_TIMEOUT = 20000; // [ms]
10+
static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms]
1111

1212
void setup(void) {
1313
Serial.begin(115200);

examples/soracom/soracom-connectivity-diagnostics/soracom-connectivity-diagnostics.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <Adafruit_TinyUSB.h>
1616
#include <WioCellular.h>
1717

18-
static constexpr int POWER_ON_TIMEOUT = 20000; // [ms]
18+
static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms]
1919

2020
#define CONSOLE Serial
2121
#define ABORT_IF_FAILED(result) \
@@ -134,6 +134,7 @@ static void pingToSoracomNetwork(void) {
134134
ABORT_IF_FAILED(executeCommand("AT+QPING=1,\"pong.soracom.io\",3,3", 300000));
135135
const auto start = millis();
136136
while (pingResponse.size() < 3 + 1) {
137+
Serial.flush();
137138
WioCellular.doWork(timeout - (millis() - start));
138139
if (millis() - start >= timeout) break;
139140
}

0 commit comments

Comments
 (0)