Hello Community,
I am new to the Arduino world, and I am well started into a project using a Nano and data inputs from 2 serial devices:
- ESP8266 fetching a JSON string on the net, wired on RX0.
- HMC5883 getting magnetic field used for a compass, wired on SDA/SCL.
I won't detail my project in full length as most information will not be relevant for my question. I'm basically using the parsed data to move motors and display information on LCD.
I have developed and run my full program successfully on a MEGA 2560 board on the same ports, and thus felt confident in my attempt to copy/paste the sketch onto my Nano. But to no avail.
Data from HMC compass seem to work OK, but the JSON string from ESP is polluted and unreadable. I am suspecting both data inputs are getting mixed up in one unreadable gibberish.
HMC is throwing out data at something around 20HZ, since the information needs to be updated in "real time", while I set the ESP to send data every 3 seconds.
I have tried to cool things down with two techniques:
- Interrupt on RX0
- Serial.Event for RX0 data read
These require to slice the data into single char, and concat the chars until the end of the line is reached. The functions do work as the data is parsed only when '\n' is read, but it doesn't solve my problem which is that weird characters are inserted inside the data before reaching '\n'.
My question is: is it possible to make the board focus on the reading of one serial only? Data from compass is only needed in set up loop, is it possible to disable it in main loop? How come the sketch is working like a charm on MEGA and not on Nano?
Here's a simplified sketch that roughly explains the issue:
#include <Wire.h>
#include <ArduinoJson.h>
#include <DFRobot_QMC5883.h>
String payload="";
DFRobot_QMC5883 compass;
void setup() {
//*********************************compass initialization****************************
Serial.begin(115200);
while (!compass.begin()) {
Serial.println("Could not find a valid 5883 sensor, check wiring!");
delay(500);
}
Serial.println("Initialize HMC5883");
// compass.setRange(HMC5883L_RANGE_1_3GA);
// compass.setMeasurementMode(HMC5883L_CONTINOUS);
compass.setDataRate(HMC5883L_DATARATE_15HZ);
// compass.setSamples(HMC5883L_SAMPLES_8);
delay(1000);
}
void loop() {
while (Serial.available() > 0) {
payload = Serial.readString();
Serial.print(payload);
char json[500];
payload.toCharArray(json, 500);
StaticJsonDocument<200> doc;
DeserializationError err = deserializeJson(doc, json);
if (err) {
Serial.print("ERROR: ");
Serial.println(err.c_str());
return;
}
const char *jsonlongitude = doc["iss_position"]["longitude"]; // "64.6418"
const char *jsonlatitude = doc["iss_position"]["latitude"]; // "-51.6235"
float longitude2 = atof(jsonlongitude);
float latitude2 = atof(jsonlatitude);
while (!Serial)
continue;
Serial.print("longitude: ");
Serial.println(longitude2, 4);
Serial.print("latitude: ");
Serial.println(latitude2, 4);
delay(1000);
//*********************************compass*******************************
float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / PI);
compass.setDeclinationAngle(declinationAngle);
Vector mag = compass.readRaw();
compass.getHeadingDegrees();
Serial.print("X:");
Serial.print(mag.XAxis);
Serial.print(" Y:");
Serial.print(mag.YAxis);
Serial.print(" Z:");
Serial.println(mag.ZAxis);
Serial.print("Degress = ");
Serial.println(mag.HeadingDegress);
delay(1000);
}
Thanks for your help!
Etienne