I'm using mkr gsm 1400 and sim to connect to the internet,
I want to send data from a sensor with a timestamp to the kaa cloud .
I found example of code sending data to the cloud using esp8266(wifi) and it worked.
I'm not sure how to change the code so it can be used with the mrk gsm 1400 (MKRGSM library).
here's the code with esp8266
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
const char* ssid = ""; // WiFi name
const char* password = ""; // WiFi password
const char* mqtt_server = "mqtt.cloud.kaaiot.com";
const String TOKEN = ""; // Endpoint token - you get (or specify) it during device provisioning
const String APP_VERSION = ""; // Application version - you specify it during device provisioning
const unsigned long fiveSeconds = 1 * 5 * 1000UL;
static unsigned long lastPublish = 0 - fiveSeconds;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
setup_wifi();
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - lastPublish >= fiveSeconds) {
lastPublish += fiveSeconds;
DynamicJsonDocument telemetry(1023);
telemetry.createNestedObject();
telemetry[0]["temperature"] = random(18, 23);
telemetry[0]["humidity"] = random(40, 60);
String topic = "kp1/" + APP_VERSION + "/dcx/" + TOKEN + "/json";
client.publish(topic.c_str(), telemetry.as<String>().c_str());
Serial.println("Published on topic: " + topic);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.printf("\nHandling command message on topic: %s\n", topic);
DynamicJsonDocument doc(1023);
deserializeJson(doc, payload, length);
JsonVariant json_var = doc.as<JsonVariant>();
DynamicJsonDocument commandResponse(1023);
for (int i = 0; i < json_var.size(); i++) {
unsigned int command_id = json_var[i]["id"].as<unsigned int>();
commandResponse.createNestedObject();
commandResponse[i]["id"] = command_id;
commandResponse[i]["statusCode"] = 200;
commandResponse[i]["payload"] = "done";
}
String responseTopic = "kp1/" + APP_VERSION + "/cex/" + TOKEN + "/result/SWITCH";
client.publish(responseTopic.c_str(), commandResponse.as<String>().c_str());
Serial.println("Published response to SWITCH command on topic: " + responseTopic);
}
void setup_wifi() {
if (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.println();
Serial.printf("Connecting to [%s]", ssid);
WiFi.begin(ssid, password);
connectWiFi();
}
}
void connectWiFi() {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
char *client_id = "client-id-123ab";
if (client.connect(client_id)) {
Serial.println("Connected to WiFi");
// ... and resubscribe
subscribeToCommand();
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void subscribeToCommand() {
String topic = "kp1/" + APP_VERSION + "/cex/" + TOKEN + "/command/SWITCH/status";
client.subscribe(topic.c_str());
Serial.println("Subscribed on topic: " + topic);
}
Any ideas?