Loading files
// network.h — connectivity layer: WiFi association, HTTP(S) JSON requests,
// measurement upload + retry-cache drain, remote config, OTA and the
// device command queue.
#pragma once
#include <Arduino.h>
#include <ArduinoJson.h>
#include "config.h" // for WIFI_CONNECT_TIMEOUT_MS (default arg below)
// ---- URL helpers ----------------------------------------------------------
String apiUrl(const String& path);
String absoluteUrl(String maybeRelativeUrl);
// ---- WiFi -----------------------------------------------------------------
bool connectWifi(unsigned long timeoutMs = WIFI_CONNECT_TIMEOUT_MS);
bool connectNetwork();
// ---- HTTP -----------------------------------------------------------------
bool httpGetJson(const String& url, JsonDocument& doc);
bool httpPostJson(const String& url, const String& json, String* response = nullptr);
bool httpPatchJson(const String& url, const String& json, String* response = nullptr);
// ---- Upload ---------------------------------------------------------------
// claimConfirmed (optional out-param) is set to true only when the server
// reports the device as claimed, so the caller can defer latching the local
// "claim registered" flag until the claim actually exists server-side.
bool uploadLine(const String& line, bool* claimConfirmed = nullptr);
bool uploadCachedLines();
// ---- Config / OTA / commands ---------------------------------------------
void fetchRemoteConfig();
// Push the device's local scale calibration up to the backend with a
// PATCH /config, so a tare/span done offline on the provisioning portal is
// reflected server-side (the server otherwise only ever holds its defaults for a
// portal-calibrated device). Hives 1–2 go in the legacy scale1/2 offset+factor
// fields; hives 3–18 go in the hive_scales[] array — the same two shapes
// fetchRemoteConfig() bridges in reverse. The PATCH bumps the server
// config_version; the returned version is recorded as last-applied so the
// following fetchRemoteConfig() sees "unchanged" and does not bridge these same
// values straight back. Called from the upload cycle only when a report is
// pending (scaleCalibrationReportPending()); the pending flag is cleared on a
// successful report and kept for a retry otherwise.
void reportScaleCalibration();
// Download and flash a HiveHub self-update image. `expectedSize`/`expectedCrc32`
// come from the backend's OTA check response: the size substitutes for a missing
// Content-Length header (a proxy/CDN may deliver the image chunked) and the CRC
// verifies the received bytes before the OTA partition is committed. Either may
// be 0 when the backend predates them; the corresponding check is then skipped.
bool performFirmwareUpdate(const String& firmwareUrl, int expectedSize = 0,
uint32_t expectedCrc32 = 0);
// (The old BeeCounter OTA-over-the-wire relay was removed with the wired
// BeeCounter path; a future BeeCounter OTA will use BLE/GATT and is not
// implemented yet.)
// Stream a HiveInside firmware image from `firmwareUrl` to the paired HiveInside
// sensor at `mac` over BLE GATT. The image is never fully buffered; bytes are
// relayed straight from the HTTPS download into the device's OTA service, which
// verifies `expectedCrc32` end-to-end before swapping its OTA slot.
//
// When `outMsg` is non-null it receives a human-readable result/cause (e.g.
// "HiveInside OTA completed", "HiveInside not found in scan", "firmware download
// failed (HTTP 404)") so the caller can report the real outcome to the backend
// instead of a bare boolean.
bool updateHiveInside(const String& mac, const String& firmwareUrl,
uint32_t expectedCrc32 = 0, String* outMsg = nullptr);
void checkForOtaUpdate();
void postCommandResult(int commandId, bool success, const String& message);
void checkCommands();
7452d848cThis file is pinned to the selected release. Download the original to open it in your design tool.