Skip to content

Commit b81a2a5

Browse files
committed
feat(library): DatumIoT v1.4.0
- Add setMqttPort() to configure non-default MQTT port - Implement connectWiFi() (was declared but missing impl) - Add sendLog(level, message) for structured log publishing - Add rotateKey() for API key rotation trigger - Remove phantom private _handleCommand()/_extractJsonVal() declarations
1 parent 2acc071 commit b81a2a5

3 files changed

Lines changed: 72 additions & 3 deletions

File tree

libraries/DatumIoT/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Datum IoT
2-
version=1.3.0
2+
version=1.4.0
33
author=Datum IoT Platform
44
maintainer=Datum IoT Platform
55
sentence=Official Arduino library for connecting devices to the Datum IoT Server.

libraries/DatumIoT/src/DatumIoT.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ void DatumIoT::setServer(String url) {
5050
_mqtt.setServer(_mqttHost.c_str(), _mqttPort);
5151
}
5252

53+
void DatumIoT::setMqttPort(int port) {
54+
_mqttPort = port;
55+
if (_mqttHost.length() > 0)
56+
_mqtt.setServer(_mqttHost.c_str(), _mqttPort);
57+
}
58+
59+
void DatumIoT::connectWiFi(String ssid, String pass) {
60+
#ifdef ESP8266
61+
WiFi.begin(ssid.c_str(), pass.c_str());
62+
#else
63+
WiFi.begin(ssid.c_str(), pass.c_str());
64+
#endif
65+
DATUM_LOG("Connecting WiFi to %s...", ssid.c_str());
66+
int attempts = 0;
67+
while (WiFi.status() != WL_CONNECTED && attempts < 40) {
68+
delay(500);
69+
attempts++;
70+
}
71+
if (WiFi.status() == WL_CONNECTED)
72+
DATUM_LOG("WiFi connected! IP: %s", WiFi.localIP().toString().c_str());
73+
else
74+
DATUM_LOG("WiFi connect failed after %d attempts", attempts);
75+
}
76+
5377
bool DatumIoT::connect() {
5478
if (_mqtt.connected())
5579
return true;
@@ -192,6 +216,46 @@ bool DatumIoT::sendTelemetryJson(JsonObject json) {
192216
return _mqtt.publish(topic.c_str(), payload.c_str());
193217
}
194218

219+
bool DatumIoT::sendLog(String msg) {
220+
if (!_mqtt.connected()) return false;
221+
String topic = "dev/" + _deviceId + "/logs";
222+
StaticJsonDocument<256> doc;
223+
doc["msg"] = msg;
224+
doc["ts"] = millis() / 1000;
225+
String payload;
226+
serializeJson(doc, payload);
227+
return _mqtt.publish(topic.c_str(), payload.c_str());
228+
}
229+
230+
bool DatumIoT::rotateKey() {
231+
HTTPClient http;
232+
String url = _serverUrl + "/dev/" + _deviceId + "/rotate-key";
233+
#ifdef ESP8266
234+
WiFiClient client;
235+
http.begin(client, url);
236+
#else
237+
http.begin(url);
238+
#endif
239+
http.addHeader("Authorization", "Bearer " + _apiKey);
240+
http.addHeader("Content-Type", "application/json");
241+
int code = http.POST("{}");
242+
if (code == 200 || code == 201) {
243+
StaticJsonDocument<256> resp;
244+
if (deserializeJson(resp, http.getString()) == DeserializationError::Ok) {
245+
const char *newKey = resp["api_key"];
246+
if (newKey && strlen(newKey) > 0) {
247+
_apiKey = String(newKey);
248+
DATUM_LOG("Key rotated successfully");
249+
http.end();
250+
return true;
251+
}
252+
}
253+
}
254+
DATUM_LOG("Key rotation failed: %d", code);
255+
http.end();
256+
return false;
257+
}
258+
195259
// Callbacks
196260
void DatumIoT::onCommand(CommandCallback cb) { _cmdCallback = cb; }
197261

libraries/DatumIoT/src/DatumIoT.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class DatumIoT {
4141

4242
// Connection
4343
void setServer(String url);
44+
void setMqttPort(int port); // Override default MQTT port (1883)
4445
void connectWiFi(String ssid, String pass); // Helper blocking connect
4546
bool connect(); // Find MQTT/active connection
4647
void loop();
@@ -54,6 +55,12 @@ class DatumIoT {
5455
// Commands
5556
void onCommand(CommandCallback cb);
5657

58+
// Logging
59+
bool sendLog(String msg); // Publish to dev/{id}/logs via MQTT
60+
61+
// Key management
62+
bool rotateKey(); // POST /dev/{id}/rotate-key — updates internal _apiKey
63+
5764
// Utils
5865
String getDeviceId() const { return _deviceId; }
5966
String getApiKey() const { return _apiKey; }
@@ -74,8 +81,6 @@ class DatumIoT {
7481
bool _mqttConnect();
7582
void _mqttCallback(char *topic, byte *payload, unsigned int length);
7683
void _ackCommand(String cmdId);
77-
void _handleCommand(String payload);
78-
String _extractJsonVal(String json, String key);
7984
};
8085

8186
#endif

0 commit comments

Comments
 (0)