[SOLVED] (removed ArduinoJson library, built strings myself).
Still trying to figure this out. Only the first message sent, which is sent in the setup function, with a message of “connected!”, is actually received by hologram. In the loop, I’m following the same exact message prototype, and the message in the loop is not received. Any help is greatly appreciated!
Here is my sketch:
#include <MKRGSM.h>
#include <ArduinoJson.h>
const char PINNUMBER[] = " "; //what am I suppose to put here?
// APN data
const char GPRS_APN[] = "hologram";
const char GPRS_LOGIN[] = " ";
const char GPRS_PASSWORD[] = " ";
String HOLOGRAM_DEVICE_KEY = "my_key_goes_here";
String HOLOGRAM_MESSAGE = "connected!";
String HOLOGRAM_TOPIC = "VIBRATION";
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess(true);
// Hologram's Embedded API (https://hologram.io/docs/reference/cloud/embedded/) URL and port
char server[] = "cloudsocket.hologram.io";
int port = 9999;
bool flag = false;
bool active = false;
int gsmBeginStatus;
int gprsAttachStatus;
int gsmReadyStatus;
void network_connect()
{
gsmAccess.shutdown();
flag = false;
Serial.println("Begin gsm Access");
gsmBeginStatus = gsmAccess.begin();
Serial.println(gsmBeginStatus);
if ((gsmBeginStatus == GSM_READY))
{
Serial.println("gsm access success!");
flag = true;
}
else
{
Serial.println("gsm failed to begin.");
}
if(flag)
{
Serial.println("attaching gprs...");
delay(100);
gprsAttachStatus = gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD);
if(gprsAttachStatus == GPRS_READY)
{
active = true;
Serial.println("gprs attached!");
}
else
{
Serial.println("gprs failure.");
}
}
else
{
Serial.println("Not active");
delay(1000);
}
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {};
Serial.println("Starting Arduino web client.");
while (!active)
{
network_connect();
}
Serial.println("setup complete.");
Serial.println("connecting to server...");
if(client.connect(server, port))
{
//this message is successfully received, in both my email via route and on the hologram dashboard
client.println("{\"k\":\"" + HOLOGRAM_DEVICE_KEY +"\",\"d\":\""+ HOLOGRAM_MESSAGE+ "\",\"t\":\""+HOLOGRAM_TOPIC+"\"}");
client.stop();
Serial.println("connected!");
}
else
{
Serial.println("connection failed.");
}
}
String res = "x: 11,512.62,508.98,505,506,507,507,519,517,517,516";
void loop()
{
Serial.println("creating document.");
for(int j = 0; j < 3; j++)
{
Serial.println(res.length());
char buf[64];
res.toCharArray(buf, 64);
StaticJsonDocument<96> doc;
doc["d"] = res;
Serial.println("serializing.");
String json_obj;
serializeJson(doc, json_obj);
Serial.println(json_obj);
Serial.println();
Serial.println("memory usage: ");
Serial.println(doc.capacity());
Serial.println(doc.memoryUsage());
Serial.println("sending to client...");
if (client.connect(server, port))
{
//this message is NEVER received.
client.println("{\"k\":\"" + HOLOGRAM_DEVICE_KEY +"\",\"d\":\""+ json_obj+ "\",\"t\":\""+HOLOGRAM_TOPIC+"\"}");
// //client.stop();
Serial.println("Sent!!");
}
else
{
Serial.println("client not connected.");
// network_connect();
}
}
delay(10000);
}