@@ -42,12 +42,14 @@ HWCDC USBSerial;
4242constexpr auto WIFI_CONNECT_TIMEOUT_SEC = MIN_TO_SEC (2 );
4343/* * Maximum number of seconds to try making new WiFi connection. */
4444constexpr auto WIFI_NEW_CONNECT_TIMEOUT_SEC = 20 ;
45- /* * Specifies how frequently to upload data points to the server. */
46- constexpr auto UPLOAD_INTERVAL_SEC = MIN_TO_SEC (5 );
45+ /* * Minimum number of packets required before upload. */
46+ constexpr auto UPLOAD_PACKET_MIN = 5 ;
47+ /* * Specifies how frequently to create a packet w/ min/mean/max stats. */
48+ constexpr auto PACKET_INTERVAL_SEC = MIN_TO_SEC (1 );
4749/* * Specifies how frequently to check for OTA updates from our server. */
4850constexpr auto OTA_INTERVAL_SEC = HR_TO_SEC (24 );
4951/* * Maximum number of data packets to retain when WiFi is unavailable. */
50- constexpr auto MAX_SAVED_PACKETS = DAY_TO_SEC (14 ) / UPLOAD_INTERVAL_SEC ;
52+ constexpr auto MAX_SAVED_PACKETS = DAY_TO_SEC (14 ) / PACKET_INTERVAL_SEC ;
5153
5254/* * Storage instance to manage stored credentials. */
5355static Storage Creds;
@@ -63,6 +65,8 @@ static bool firstSend;
6365 */
6466void printReadingToConsole (double reading);
6567
68+ void otaUpdateCheck (API &);
69+
6670/* *
6771 * Callback for AccessPoint that verifies credentials and attempts registration.
6872 * @param ssid The name of the network to connect to
@@ -167,77 +171,74 @@ void setup()
167171
168172void loop ()
169173{
170- #ifndef UPLOAD_DISABLED
171- static int wakeupCount = 0 ;
174+ auto lastWake = xTaskGetTickCount ();
172175
173- vTaskDelay (pdMS_TO_TICKS (SEC_TO_MS (UPLOAD_INTERVAL_SEC )));
176+ auto readyCount = packets.size ();
177+ if (readyCount == MAX_SAVED_PACKETS ) {
178+ SERIAL .println (" Discarded a packet!" );
179+ packets.pop_back ();
180+ }
174181
182+ // Timestamp current packet, then immediately create new packet for ongoing
183+ // samples to feed into.
175184 packets.front ().timestamp = Timestamp ();
176185 packets.emplace_front ();
177186
178- if (WiFi.status () != WL_CONNECTED ) {
179- SERIAL .println (" Attempting WiFi reconnect..." );
180- WiFi.reconnect ();
181- delay (5000 );
182- }
183-
184- if (WiFi.status () == WL_CONNECTED ) {
185- API api (buildDeviceId (), Creds.get (Storage::Entry::Token));
187+ #ifndef UPLOAD_DISABLED
188+ static int otaCountdown = 0 ; // zero to force check on first upload
186189
187- if (firstSend) {
188- const bool success = api.sendMeasurementWithDiagnostics (
189- packets.back (), NOISEMETER_VERSION , String (millis () / 1000 ));
190+ otaCountdown -= PACKET_INTERVAL_SEC ;
190191
191- if (success) {
192- packets.pop_back ();
193- firstSend = false ;
194- }
192+ if (readyCount >= UPLOAD_PACKET_MIN ) {
193+ if (WiFi.status () != WL_CONNECTED ) {
194+ SERIAL .println (" Attempting WiFi reconnect..." );
195+ WiFi.reconnect ();
196+ delay (5000 );
195197 }
196198
197- const auto size = packets.size ();
198- if (size > 1 ) {
199- if (size == 2 && api.sendMeasurement (packets.back ())) {
200- packets.pop_back ();
201- } else if (size > 2 && api.sendMeasurements (packets, ++packets.cbegin ())) {
202- packets.erase (++packets.cbegin (), packets.cend ());
203- } else {
204- SERIAL .print (size - 1 );
205- SERIAL .println (" packets still need to be sent!" );
199+ if (WiFi.status () == WL_CONNECTED ) {
200+ API api (buildDeviceId (), Creds.get (Storage::Entry::Token));
201+
202+ if (firstSend) {
203+ const bool success = api.sendMeasurementWithDiagnostics (
204+ packets.back (), NOISEMETER_VERSION , String (millis () / 1000 ));
206205
207- if (size >= MAX_SAVED_PACKETS ) {
208- SERIAL .println (" Discarded a packet!" );
206+ if (success) {
209207 packets.pop_back ();
208+ readyCount--;
209+ firstSend = false ;
210210 }
211211 }
212- }
212+
213+ if (readyCount == 1 ) {
214+ if (api.sendMeasurement (packets.back ())) {
215+ packets.pop_back ();
216+ readyCount = 0 ;
217+ }
218+ } else {
219+ if (api.sendMeasurements (packets, ++packets.cbegin ())) {
220+ packets.erase (++packets.cbegin (), packets.cend ());
221+ readyCount = 0 ;
222+ }
223+ }
224+
225+ if (readyCount > 0 ) {
226+ SERIAL .print (readyCount);
227+ SERIAL .println (" packets still need to be sent!" );
228+ }
213229
214230#if defined(BOARD_REV4) || defined(BOARD_REV123)
215- // We have WiFi: also check for software updates
216- if (++wakeupCount >= OTA_INTERVAL_SEC / UPLOAD_INTERVAL_SEC ) {
217- wakeupCount = 0 ;
218- SERIAL .println (" Checking for updates..." );
219-
220- const auto ota = api.getLatestSoftware ();
221- if (ota) {
222- if (ota->version .compareTo (NOISEMETER_VERSION ) > 0
223- #if defined(BOARD_REV123)
224- && ota->version .startsWith (" 0.3." )
225- #endif // defined(BOARD_REV123)
226- ) {
227- SERIAL .print (ota->version );
228- SERIAL .println (" available!" );
229-
230- if (downloadOTAUpdate (ota->url , api.rootCertificate ())) {
231- SERIAL .println (" Download success! Restarting..." );
232- delay (1000 );
233- ESP .restart ();
234- } else { SERIAL .println (" Update download failed." ); }
235- } /* else { SERIAL.println("No new updates."); }*/
236- } else { SERIAL .println (" Failed to reach update server!" ); }
237- }
231+ // We have WiFi: also check for software updates
232+ if (otaCountdown <= 0 ) {
233+ otaCountdown = OTA_INTERVAL_SEC ;
234+ otaUpdateCheck (api);
235+ }
238236#endif // defined(BOARD_REV4) || defined(BOARD_REV123)
237+ }
239238 }
240239#endif // !UPLOAD_DISABLED
240+
241+ vTaskDelayUntil (&lastWake, pdMS_TO_TICKS (SEC_TO_MS (PACKET_INTERVAL_SEC )));
241242}
242243
243244void measurementHandler (void *)
@@ -268,6 +269,29 @@ void printReadingToConsole(double reading)
268269 SERIAL .println (output);
269270}
270271
272+ void otaUpdateCheck (API & api)
273+ {
274+ SERIAL .println (" Checking for updates..." );
275+
276+ const auto ota = api.getLatestSoftware ();
277+ if (ota) {
278+ if (ota->version .compareTo (NOISEMETER_VERSION ) > 0
279+ #if defined(BOARD_REV123)
280+ && ota->version .startsWith (" 0.3." )
281+ #endif // defined(BOARD_REV123)
282+ ) {
283+ SERIAL .print (ota->version );
284+ SERIAL .println (" available!" );
285+
286+ if (downloadOTAUpdate (ota->url , api.rootCertificate ())) {
287+ SERIAL .println (" Download success! Restarting..." );
288+ delay (1000 );
289+ ESP .restart ();
290+ } else { SERIAL .println (" Update download failed." ); }
291+ } /* else { SERIAL.println("No new updates."); }*/
292+ } else { SERIAL .println (" Failed to reach update server!" ); }
293+ }
294+
271295std::optional<const char *> saveNetworkCreds (String ssid, String psk, String email)
272296{
273297 // Confirm that the given credentials will fit in the allocated EEPROM space.
0 commit comments