Integration between Arduino MKR GSM 1400 and Hologram Cloud

[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);
}

Also, the LiPo wiring for the Arduino MKR GSM 1400 is BACKWARDS with respect to the most common lipo wiring and connectors, the connectors only fit one way. I did not realize this, and I simply plugged the lipo in, the only way it would fit, because it is notched on one side, and messed up both the lipo and the board. I had to solder a backwards connector so that + went to +, and - went to -.

Also Also, (jeeze), I removed the ArduinoJson library, and built the string myself with a char array. Works. If anyone has these problems, hopefully this thread will help!

Takeaways:

  • Verify the connector orientation with your battery and board. All of the connectors I have ever seen (whether loose purchases from amazon, or connectors on an actual battery itself) are wired one way, and the arduino board female connector is wired the OPPOSITE way.

  • Don’t use arduinojson library if you are having problems sending data that was formatted with this library.

1 Like