9
9
// http://librarymanager#ArduinoJson 7.0.4
10
10
11
11
#include < Adafruit_TinyUSB.h>
12
- #include < algorithm>
13
12
#include < WioCellular.h>
14
13
#include < ArduinoJson.h>
15
14
15
+ #define SEARCH_ACCESS_TECHNOLOGY (WioCellularNetwork::SearchAccessTechnology::LTEM)
16
+ #define LTEM_BAND (WioCellularNetwork::NTTDOCOMO_LTEM_BAND)
16
17
static const char APN[] = " soracom.io" ;
18
+
17
19
static const char HOST[] = " uni.soracom.io" ;
18
20
static constexpr int PORT = 23080 ;
19
21
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]
23
25
24
26
#define ABORT_IF_FAILED (result ) \
25
27
do { \
@@ -28,9 +30,7 @@ static constexpr int RECEIVE_TIMEOUT = 10000; // [ms]
28
30
29
31
static uint32_t MeasureTime = -INTERVAL;
30
32
static String LatestGpsData;
31
- static String LatestRegStatus;
32
33
33
- static constexpr int PDP_CONTEXT_ID = 1 ;
34
34
static constexpr int SOCKET_ID = 0 ;
35
35
36
36
static JsonDocument JsonDoc;
@@ -52,7 +52,10 @@ void setup(void) {
52
52
WioCellular.begin ();
53
53
ABORT_IF_FAILED (WioCellular.powerOn (POWER_ON_TIMEOUT));
54
54
55
- setupCellular ();
55
+ WioNetwork.config .searchAccessTechnology = SEARCH_ACCESS_TECHNOLOGY;
56
+ WioNetwork.config .ltemBand = LTEM_BAND;
57
+ WioNetwork.config .apn = APN;
58
+ WioNetwork.begin ();
56
59
57
60
WioCellular.enableGrovePower ();
58
61
GpsBegin ();
@@ -67,58 +70,29 @@ void loop(void) {
67
70
}
68
71
69
72
if (millis () - MeasureTime >= INTERVAL) {
70
- digitalWrite (LED_BUILTIN, HIGH);
73
+ if (WioNetwork.canCommunicate ()) {
74
+ digitalWrite (LED_BUILTIN, HIGH);
71
75
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
+ }
76
80
77
- send ( reinterpret_cast < const uint8_t *>(jsonStr. data ()), jsonStr. size () );
81
+ digitalWrite (LED_BUILTIN, LOW );
78
82
}
79
83
80
- digitalWrite (LED_BUILTIN, LOW);
81
-
82
84
MeasureTime = millis ();
83
85
}
84
86
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
113
89
}
114
90
115
91
static bool measure (JsonDocument& doc) {
116
92
Serial.println (" ### Measuring" );
117
93
118
94
doc[" uptime" ] = millis () / 1000 ;
119
95
120
- doc[" regStatus" ] = LatestRegStatus.c_str ();
121
-
122
96
int index[5 ];
123
97
index[0 ] = LatestGpsData.indexOf (' ,' );
124
98
index[1 ] = index[0 ] >= 0 ? LatestGpsData.indexOf (' ,' , index[0 ] + 1 ) : -1 ;
@@ -147,37 +121,43 @@ static bool measure(JsonDocument& doc) {
147
121
return true ;
148
122
}
149
123
150
- static bool send (const void * data, size_t size) {
151
- bool result = true ;
152
-
124
+ static bool send (const JsonDocument& doc) {
153
125
Serial.println (" ### Sending" );
154
126
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 ;
164
131
}
165
132
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
+
166
144
if (result) {
167
145
Serial.print (" Sending " );
168
- printData (Serial, data, size);
146
+ std::string str;
147
+ serializeJson (doc, str);
148
+ printData (Serial, str.data (), str.size ());
169
149
Serial.println ();
170
- if (WioCellular.sendSocket (SOCKET_ID, data, size) != WioCellularResult::Ok) {
150
+ if (WioCellular.sendSocket (socketId, str. data (), str. size () ) != WioCellularResult::Ok) {
171
151
Serial.println (" ERROR: Failed to send socket" );
172
152
result = false ;
173
153
}
174
154
}
175
155
176
- static uint8_t recvData[1500 ];
156
+ static uint8_t recvData[WioCellular. RECEIVE_SOCKET_SIZE_MAX ];
177
157
size_t recvSize;
178
158
if (result) {
179
159
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) {
181
161
Serial.println (" ERROR: Failed to receive socket" );
182
162
result = false ;
183
163
} else {
@@ -186,7 +166,7 @@ static bool send(const void* data, size_t size) {
186
166
}
187
167
}
188
168
189
- if (WioCellular.closeSocket (SOCKET_ID ) != WioCellularResult::Ok) {
169
+ if (WioCellular.closeSocket (socketId ) != WioCellularResult::Ok) {
190
170
Serial.println (" ERROR: Failed to close socket" );
191
171
result = false ;
192
172
}
0 commit comments