I'm trying to read data from an IMU a send through mqtt. I'm reading data and putting inside a JSON object, in order to serialize it and send via ethernet. In order to debug I print the JSON object serialized on the Serial monitor, but the string correspondig to JSON does not appear on the monitor, while all others print() work fine. I'm using a custom board with an ATMega328 and programming it with ESP8266. I tried to write a sample program that print just a JSON object, and it work, but when I try to print it in the complete code it doesn't work. I also tried different solution, but nothing seems to work. Could someone help me? Thank you. I put my code below.
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <SparkFunLSM9DS1.h>
#include <Arduino_JSON.h>
LSM9DS1 imu;
// Enter a MAC address for your controller below.
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
const char *server = "192.168.0.109";
const int port = 1883;
float gx, gy, gz, ax, ay, az, mx, my, mz;
#define PRINT_CALCULATED
//#define PRINT_RAW
#define PRINT_SPEED 500 // 500 ms between prints
static unsigned long lastPrint = 0; // Keep track of print time
#define DECLINATION -8.58 // Declination (degrees) in Boulder, CO.
//Function definitions
void printGyro();
void printAccel();
void printMag();
void setup()
{
Ethernet.init(10); // SCSn pin
Serial.begin(128000);
Wire.begin();
if (imu.begin((uint8_t)0x6a, (uint8_t)0x1c, Wire) == false) // with no arguments, this uses default addresses (AG:0x6B, M:0x1E) and i2c port (Wire).
{
Serial.println("Failed to communicate with LSM9DS1.");
Serial.println("Double-check wiring.");
Serial.println("Default settings in this sketch will " \
"work for an out of the box LSM9DS1 " \
"Breakout, but may need to be modified " \
"if the board jumpers are.");
while (1);
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0){
Serial.println("Failed to configure Ethernet using DHCP");
if (Ethernet.hardwareStatus() == EthernetNoHardware){
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
}
else if (Ethernet.linkStatus() == LinkOFF){
Serial.println("Ethernet cable is not connected.");
}
// no point in carrying on, so do nothing forevermore:
while (true){
delay(1);
}
}
// print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
mqttClient.setServer(server, port);
if (mqttClient.connect("atmega")){
Serial.println("Connection has been established, well done");
}
else{
Serial.println("Looks like the server connection failed...");
}
}
void loop()
{
switch (Ethernet.maintain()){
case 1:
//renewed fail
Serial.println("Error: renewed fail");
break;
case 2:
//renewed success
Serial.println("Renewed success");
//print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
break;
case 3:
//rebind fail
Serial.println("Error: rebind fail");
break;
case 4:
//rebind success
Serial.println("Rebind success");
//print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
break;
default:
//nothing happened
break;
}
// Update the sensor values whenever new data is available
if ( imu.gyroAvailable() )
{
// To read from the gyroscope, first call the
// readGyro() function. When it exits, it'll update the
// gx, gy, and gz variables with the most current data.
imu.readGyro();
}
if ( imu.accelAvailable() )
{
// To read from the accelerometer, first call the
// readAccel() function. When it exits, it'll update the
// ax, ay, and az variables with the most current data.
imu.readAccel();
}
if ( imu.magAvailable() )
{
// To read from the magnetometer, first call the
// readMag() function. When it exits, it'll update the
// mx, my, and mz variables with the most current data.
imu.readMag();
}
if ((lastPrint + PRINT_SPEED) < millis())
{
printGyro(); // Print "G: gx, gy, gz"
printAccel(); // Print "A: ax, ay, az"
printMag(); // Print "M: mx, my, mz"
//===================================================
JSONVar doc;
doc["gx"] = String(gx);
doc["gy"] = String(gy);
doc["gz"] = String(gz);
String tmp = JSON.stringify(doc);
Serial.print("Value: ");
Serial.println(tmp); //This line doesn't print
//===================================================
/*char ax_str[6], ay_str[6], az_str[6];
char mx_str[6], my_str[6], mz_str[6];
dtostrf(ax, 4, 3, ax_str);
dtostrf(ay, 4, 3, ay_str);
dtostrf(az, 4, 3, az_str);
dtostrf(mx, 4, 3, mx_str);
dtostrf(my, 4, 3, my_str);
dtostrf(mz, 4, 3, mz_str);*/
//sprintf(packet, "{\"gx\":%s,\"gy\":%s,\"gz\":%s,\"ax\":%s,\"ay\":%s,\"az\":%s,\"mx\":%s,\"my\":%s,\"mz\":%s}", gx_str, gy_str, gz_str, ax_str, ay_str, az_str, mx_str, my_str, mz_str);
if (mqttClient.publish("imu", "packet.c_str()")){
//Serial.println("Publish succeded!");
}
else{
Serial.println("Publish failed!");
}
lastPrint = millis(); // Update lastPrint time
}
//delay(2000);
}
void printGyro()
{
// Now we can use the gx, gy, and gz variables as we please.
// Either print them as raw ADC values, or calculated in DPS.
//Serial.print("G: ");
#ifdef PRINT_CALCULATED
// If you want to print calculated values, you can use the
// calcGyro helper function to convert a raw ADC value to
// DPS. Give the function the value that you want to convert.
gx = imu.calcGyro(imu.gx);
gy = imu.calcGyro(imu.gy);
gz = imu.calcGyro(imu.gz);
#elif defined PRINT_RAW
Serial.print(imu.gx);
Serial.print(", ");
Serial.print(imu.gy);
Serial.print(", ");
Serial.println(imu.gz);
#endif
}
void printAccel()
{
// Now we can use the ax, ay, and az variables as we please.
// Either print them as raw ADC values, or calculated in g's.
//Serial.print("A: ");
#ifdef PRINT_CALCULATED
// If you want to print calculated values, you can use the
// calcAccel helper function to convert a raw ADC value to
// g's. Give the function the value that you want to convert.
ax = imu.calcGyro(imu.ax);
ay = imu.calcGyro(imu.ay);
az = imu.calcGyro(imu.az);
/*Serial.print(imu.calcAccel(imu.ax), 2);
Serial.print(", ");
Serial.print(imu.calcAccel(imu.ay), 2);
Serial.print(", ");
Serial.print(imu.calcAccel(imu.az), 2);
Serial.println(" g");*/
#elif defined PRINT_RAW
Serial.print(imu.ax);
Serial.print(", ");
Serial.print(imu.ay);
Serial.print(", ");
Serial.println(imu.az);
#endif
}
void printMag()
{
// Now we can use the mx, my, and mz variables as we please.
// Either print them as raw ADC values, or calculated in Gauss.
//Serial.print("M: ");
#ifdef PRINT_CALCULATED
// If you want to print calculated values, you can use the
// calcMag helper function to convert a raw ADC value to
// Gauss. Give the function the value that you want to convert.
mx = imu.calcGyro(imu.mx);
my = imu.calcGyro(imu.my);
mz = imu.calcGyro(imu.mz);
/*Serial.print(imu.calcMag(imu.mx), 2);
Serial.print(", ");
Serial.print(imu.calcMag(imu.my), 2);
Serial.print(", ");
Serial.print(imu.calcMag(imu.mz), 2);
Serial.println(" gauss");*/
#elif defined PRINT_RAW
Serial.print(imu.mx);
Serial.print(", ");
Serial.print(imu.my);
Serial.print(", ");
Serial.println(imu.mz);
#endif
}