I am working on a project where I want to use a magnetometer as a detection sensor to be used with the Boron 404X. The way it works is I have a magnetometer connected to the Boron 404X via I2C communication. I have successfully been able to transmit one magnetometer event reading (X, Y, and Z axes) over cellular using particle.publish() through a webhook to my phone as string text.
The main issue is that I would like to send multiple event readings (preferably as one JSON file) as one event to some server. As I understand it, particle.publish() uses UTF-8 characters, not JSON. Also, there is a data limit of 1024 bytes per event using this function, which is not enough for my purposes. I was wondering if there was some other way to communicate over cellular, allowing me to transmit a large JSON file.
I am not sure how large this JSON file would be, but here is the information I have gathered about the magnetometer data. The magnetometer collects data at 20Hz with a 16-bit output for the X, Y, and Z axes (48 bits total per reading). However, since each character is 1 byte for particle.publish(), and assuming 3 digits (characters) per axis all concatenated together, I would only be able to send 1024/(9x20) = 5.6 seconds of readings for one event. 5.6 seconds is not enough for my purposes, most likely 30 seconds of data per event would suffice.
In summary, I want to take a set magnetometer readings over a span of 30 seconds and store them as a JSON file (whether in SIM or internal memory, it doesn't matter). Then, I would like to transmit that JSON file to a server using cellular on the Boron 404X. It does not matter how long it would take to transmit through cellular, since I know that cellular data rates are low. I just need this operation to be doable. I do not know how to convert these readings to JSON nor do I know how to send a JSON file over cellular.
I have linked the magnetometer and the magnetometer's datasheet that I am using, as well as code that I used to transmit this data to my phone using the Pushover webhook. The code will first see if the magnetometer is connected to the Boron. If it is, it will collect data from the magnetometer, store that data into a variable as a string, and then send that data to my phone. Any help on this is appreciated.
Magnetometer: https://cdn-learn.adafruit.com/downloads/pdf/adafruit-lis2mdl-triple-axis-magnetometer.pdf
Magnetometer Datasheet: https://www.st.com/resource/en/datasheet/lis2mdl.pdf
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_BusIO_Register.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_Sensor.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_LIS2MDL.h>
#include <string>
using namespace std;
/* Assign a unique ID to this sensor at the same time */
Adafruit_LIS2MDL lis2mdl = Adafruit_LIS2MDL(12345);
#define LIS2MDL_CLK 13
#define LIS2MDL_MISO 12
#define LIS2MDL_MOSI 11
#define LIS2MDL_CS 10
String str1,str2;
unsigned long pwrCheckTimeStart; //to check readings every 20sec
void setup() {
// Enable auto-gain
lis2mdl.enableAutoRange(true);
// Initialize the sensor
if (lis2mdl.begin()) { // I2C mode
str1 = "Mag There?";
str2 = "Yes";
sendData();
}
else {
str1 = "Mag There?";
str2 = "No";
sendData();
while (1) delay(10);
}
pwrCheckTimeStart = millis();
}
void loop() {
// Loop every 20 sec
if(millis()-pwrCheckTimeStart>20000) {
pwrCheckTimeStart = millis();
// Get new sensor event
sensors_event_t event;
lis2mdl.getEvent(&event);
// How the magnetometer reading appears on my phone
str1 = "MagReadings";
str2 = "X: " + String(event.magnetic.x) + " Y: " + String(event.magnetic.y) + " Z: " + String(event.magnetic.z);
sendData();
}
//********************
}
void sendData(){
unsigned long startConnectTime = millis();
char pushMessage[50], pushName[50];
str1.toCharArray(pushName, str1.length() + 1);
str2.toCharArray(pushMessage, str2.length() + 1);
Serial.println(str1);
Serial.println(str2);
String pushoverPacket = "[{\"key\":\"title\", \"value\":\"";
pushoverPacket.concat(str1);
pushoverPacket.concat("\"},");
pushoverPacket.concat("{\"key\":\"message\", \"value\":\"");
pushoverPacket.concat(str2);
pushoverPacket.concat("\"}]");
Particle.publish("magread", pushoverPacket, PRIVATE);//then send to push safer so we get the notifications on our mobile devices
Serial.print(millis() - startConnectTime);
Serial.println("ms to connect");
}