#include <PubSubClient.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
char host[] = "mqtt.akenza.io";
char clientid[] = "Arduino";
char username[] = "0783ddd64683f579";
char password[] = "bd604gmgit0x7kilc8puok3g2rxsldl2";
char outTopic[] = "/up/bd604gmgit0x7kilc8puok3g2rxsldl2/id/99E77F4ECC728656";
//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print((char)payload[i]);
str[i] = 0; // Null termination
StaticJsonDocument <256> doc;
deserializeJson(doc,payload);
// deserializeJson(doc,str); can use string instead of payload
const char* sensor = doc["sensor"];
float latitude = doc["data"][0];
float longitude = doc["data"][1];
Serial.println("latitude =");
Serial.println(latitude,2);
PubSubClient client(host, 1883, callback, wifiClient);
//Initialize serial and wait for port to open:
; // wait for serial port to connect. Needed for native USB port only
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
Serial.println("You're connected to the network");
if (client.connect(host, username, password)) {
Serial.print("Connected to ");
boolean r = client.subscribe(outTopic);
Serial.print("Subscribed to ");
Serial.println(outTopic);
Serial.println("Connection failed ");
Serial.println(client.state());
StaticJsonDocument<256> doc;
JsonArray data = doc.createNestedArray("data");
int b =serializeJson(doc, out);
Serial.print("publishing bytes = ");
boolean rc = client.publish(outTopic, out);
// The above line prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}