How to show spesific string data from NodeMCU serial monitor to value display widget

Hey, right now I’m working on a project that gets data from accelerometer sensor to arduino uno and sends it to NodeMCU. After that, I want to separate the output at NodeMCU serial monitor to some value display widget on Blynk. But after googling it I can’t find how to to that
Here’s my UNO code

#include <MPU6050.h>
#include <Wire.h>

SoftwareSerial Serial1(10,11);

MPU6050 mpu;
String str;

byte degree[8] = {
  0b00000,
  0b00110,
  0b01111,
  0b00110,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};
 
void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial.println("Initialize MPU6050");
  
  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    Serial.println("NO MPU6050 SENSOR CONNECTED");
    delay(500);
  }
  
  //grometer callibration
  mpu.calibrateGyro();
  mpu.setThreshold(3);
}

void loop() {

  Vector rawGyro = mpu.readRawGyro();
  //to read gyrometer
  Serial.println("Gyrometer");
  Serial.print("X : ");
  Serial.print(rawGyro.XAxis);
  Serial.print("Y : ");
  Serial.print(rawGyro.YAxis);
  Serial.print("Z : ");
  Serial.println(rawGyro.ZAxis);
 
  //to read accelerometer
  Vector rawAccel = mpu.readRawAccel();
  Serial.println("Accelerometer");
  Serial.print("X : ");
  Serial.print(rawAccel.XAxis);
  Serial.print("Y : ");
  Serial.print(rawAccel.YAxis);
  Serial.print("Z : ");
  Serial.println(rawAccel.ZAxis);
 
  float celc = mpu.readTemperature();
  Serial.println("temperature ");
  Serial.print(celc);
  Serial.println(" *C");
  Serial.print(char(176));
  str = String('G')+String('x')+String(rawGyro.XAxis)+String(' ')+String('G')+String('y')+String(rawGyro.YAxis)+String(' ')+String('G')+String('z')+String(rawGyro.ZAxis)+String(' ')+String('T')+String(celc)+String(' ')+String('A')+String('x')+String(rawAccel.XAxis)+String(' ')+String('A')+String('y')+String(rawAccel.YAxis)+String(' ')+String('A')+String('z')+String(rawAccel.ZAxis);
  Serial1.println(str);


  delay(5000);
}

NodeMCU code:

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() { // run over and over
  if (Serial.available()) {
    Serial.write(Serial.read());
  }
}


so on COM7, for example I want show the Gx on its own Blynk value display widget, Gy also, and so on

PLEASE HELP ME!!!

@jenggwz please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

hey i’m sorry, I’m new to this forum
i’ve already edit the post

So presumably it’s the NodeMCU you want to use to connect to Blynk and transfer the data?

Your first task is to parse the data into individual variables that can be used to write the data to Blynk. Do not do this in the void loop as you’ve done in the Arduino code, Blynk doesn’t like this.
Then look at the Sketch Builder examples for the NodeMCU to see how to connect to Blynk. As you are already using Serial for your incoming data then you should avoid using it for debug prints. Instead, you could use Serial1 and an FTDI.

You’ll use a BlynkTimer to send the data to the Blynk server using Blynk.virtualWrite commands.

Pete.

can you show me how its done?
I just so confuse

yes I want to show data from NodeMCU that I get from Arduino UNO
the problem is, the data on NodeMCU is a line of string
so I don’t know how to take just a specific string from NodeMCU serial monitor (example: Gx, or Gy, etc) separately then show it on Blynk value display widget

Okay, so I assume that this inst your code then?

This isn’t a Blynk specific task, and this forum is not aimed at teaching basic C++ programming skills.
If you google “how to parse a string in C++” you’ll find lots of resources that you can use.

Once you’ve split the data into individual variables then as I said earlier…

If you struggle with that bit of the process then show us what you’d done up to that point and we’ll help you with the Blynk side of things.

Pete.

Thank you, I understand now
but when I parse the string like Gx-11.00, can the program know to keep the -11.00 to variable?

Sorry, I don’t understand the question.

Pete.

so, as you can see on my COM7
that data is come from the arduino sensor that send to NodeMCU in string
so I just want to take Gx-11.00, but I don’t understand how to ignore the ‘Gx’ string and just take the float
After that, the float will show on blynk display value widget
Thank you

Slicing the incoming string up into sections, including discarding any unwanted characters, is known as parsing. You have the ability to extract any data from that incoming string, provided there is a

However, you have control over both the Arduino code that creates the string, and the code that parses this data. This means that you can choose to eliminate the unwanted characters, or choose a proper delimiter that will make parsing the incoming string much more straightforward.

Pete.

Hey Pete thank you, I’ve split the data into variable
thanks for helping meee

Hey, I’m a newbie to this
I have a project that send sensor data from arduino to NodeMCU, I have split the data into its own variable
I want to show the data to Blynk value display widget

Here’s my Arduino UNO code:

#include <Wire.h>
#include <HX711.h>
#include <SoftwareSerial.h>
#include <MPU6050.h>

#define DOUT A0
#define CLK A1

HX711 scale;
SoftwareSerial Serial1(10, 11);

float calibration_factor = 444.00;
int GRAM;
String str;

const int MPU = 0x68; // MPU6050 I2C address
float AccX, AccY, AccZ;
float GyroX, GyroY, GyroZ;
float accAngleX, accAngleY, gyroAngleX, gyroAngleY, gyroAngleZ;
float roll, pitch, yaw;
float AccErrorX, AccErrorY, GyroErrorX, GyroErrorY, GyroErrorZ;
float elapsedTime, currentTime, previousTime;
int c = 0;
void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  Wire.begin();                      // Initialize comunication
  Wire.beginTransmission(MPU);       // Start communication with MPU6050 // MPU=0x68
  Wire.write(0x6B);                  // Talk to the register 6B
  Wire.write(0x00);                  // Make reset - place a 0 into the 6B register
  Wire.endTransmission(true);        //end the transmission
  /*
  // Configure Accelerometer Sensitivity - Full Scale Range (default +/- 2g)
  Wire.beginTransmission(MPU);
  Wire.write(0x1C);                  //Talk to the ACCEL_CONFIG register (1C hex)
  Wire.write(0x10);                  //Set the register bits as 00010000 (+/- 8g full scale range)
  Wire.endTransmission(true);
  // Configure Gyro Sensitivity - Full Scale Range (default +/- 250deg/s)
  Wire.beginTransmission(MPU);
  Wire.write(0x1B);                   // Talk to the GYRO_CONFIG register (1B hex)
  Wire.write(0x10);                   // Set the register bits as 00010000 (1000deg/s full scale)
  Wire.endTransmission(true);
  delay(20);
  */
  // Call this function if you need to get the IMU error values for your module
//  calculate_IMU_error();
  scale.begin(DOUT, CLK);
  scale.set_scale();
  scale.tare();
  delay(20);
}
void loop() {
  // === Read acceleromter data === //
  Wire.beginTransmission(MPU);
  Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
  //For a range of +-2g, we need to divide the raw values by 16384, according to the datasheet
  AccX = (Wire.read() << 8 | Wire.read()) / 16384.0; // X-axis value
  AccY = (Wire.read() << 8 | Wire.read()) / 16384.0; // Y-axis value
  AccZ = (Wire.read() << 8 | Wire.read()) / 16384.0; // Z-axis value
  // Calculating Roll and Pitch from the accelerometer data
  accAngleX = (atan(AccY / sqrt(pow(AccX, 2) + pow(AccZ, 2))) * 180 / PI) - 1.81; // AccErrorX ~(0.58) See the calculate_IMU_error()custom function for more details
  accAngleY = (atan(-1 * AccX / sqrt(pow(AccY, 2) + pow(AccZ, 2))) * 180 / PI) - 0.35; // AccErrorY ~(-1.58)
  // === Read gyroscope data === //
  previousTime = currentTime;        // Previous time is stored before the actual time read
  currentTime = millis();            // Current time actual time read
  elapsedTime = (currentTime - previousTime) / 1000; // Divide by 1000 to get seconds
  Wire.beginTransmission(MPU);
  Wire.write(0x43); // Gyro data first register address 0x43
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, 6, true); // Read 4 registers total, each axis value is stored in 2 registers
  GyroX = (Wire.read() << 8 | Wire.read()) / 131.0; // For a 250deg/s range we have to divide first the raw value by 131.0, according to the datasheet
  GyroY = (Wire.read() << 8 | Wire.read()) / 131.0;
  GyroZ = (Wire.read() << 8 | Wire.read()) / 131.0;
  // Correct the outputs with the calculated error values
  GyroX = GyroX + 1.63;  // GyroErrorX ~(-0.56)
  GyroY = GyroY - 0.26; // GyroErrorY ~(2)
  GyroZ = GyroZ - 0.80; // GyroErrorZ ~ (-0.8)
  // Currently the raw values are in degrees per seconds, deg/s, so we need to multiply by sendonds (s) to get the angle in degrees
  gyroAngleX = gyroAngleX + GyroX * elapsedTime; // deg/s * s = deg
  gyroAngleY = gyroAngleY + GyroY * elapsedTime;
  yaw =  yaw + GyroZ * elapsedTime;
  // Complementary filter - combine acceleromter and gyro angle values
  roll = 0.96 * gyroAngleX + 0.04 * accAngleX;
  pitch = 0.96 * gyroAngleY + 0.04 * accAngleY;

  scale.set_scale(calibration_factor);
  GRAM = scale.get_units(), 4;
//  Serial.println(GRAM);
//  
  // Print the values on the serial monitor
  Serial.print(roll);
  Serial.print("/");
  Serial.print(pitch);
  Serial.print("/");
  Serial.println(yaw);
  Serial.print("/");
  Serial.println(GRAM);

  Serial.print(char(300));
  str = String(roll)+String('/')+String(pitch)+String('/')+String(yaw)+String('/')+String(GRAM);
  Serial1.println(str);

  delay(1000);
}

/////////////////////////////////////////
void calculate_IMU_error() {
  // We can call this funtion in the setup section to calculate the accelerometer and gyro data error. From here we will get the error values used in the above equations printed on the Serial Monitor.
  // Note that we should place the IMU flat in order to get the proper values, so that we then can the correct values
  // Read accelerometer values 200 times
  while (c < 200) {
    Wire.beginTransmission(MPU);
    Wire.write(0x3B);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU, 6, true);
    AccX = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
    AccY = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
    AccZ = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
    // Sum all readings
    AccErrorX = AccErrorX + ((atan((AccY) / sqrt(pow((AccX), 2) + pow((AccZ), 2))) * 180 / PI));
    AccErrorY = AccErrorY + ((atan(-1 * (AccX) / sqrt(pow((AccY), 2) + pow((AccZ), 2))) * 180 / PI));
    c++;
  }
  //Divide the sum by 200 to get the error value
  AccErrorX = AccErrorX / 200;
  AccErrorY = AccErrorY / 200;
  c = 0;
  // Read gyro values 200 times
  while (c < 200) {
    Wire.beginTransmission(MPU);
    Wire.write(0x43);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU, 6, true);
    GyroX = Wire.read() << 8 | Wire.read();
    GyroY = Wire.read() << 8 | Wire.read();
    GyroZ = Wire.read() << 8 | Wire.read();
    // Sum all readings
    GyroErrorX = GyroErrorX + (GyroX / 131.0);
    GyroErrorY = GyroErrorY + (GyroY / 131.0);
    GyroErrorZ = GyroErrorZ + (GyroZ / 131.0);
    c++;
  }
  //Divide the sum by 200 to get the error value
  GyroErrorX = GyroErrorX / 200;
  GyroErrorY = GyroErrorY / 200;
  GyroErrorZ = GyroErrorZ / 200;
  // Print the error values on the Serial Monitor
  Serial.print("AccErrorX: ");
  Serial.println(AccErrorX);
  Serial.print("AccErrorY: ");
  Serial.println(AccErrorY);
  Serial.print("GyroErrorX: ");
  Serial.println(GyroErrorX);
  Serial.print("GyroErrorY: ");
  Serial.println(GyroErrorY);
  Serial.print("GyroErrorZ: ");
  Serial.println(GyroErrorZ);
}

my NodeMCU code:

#include <Blynk.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;

char auth[] = "R_ZOJXHC_q6znQI1FO5De16qGiUSeYKY";
char ssid[] = "blablabla";
char pass[] = "blablabla";

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() { // run over and over
  recvWithEndMarker();
  showNewData();
  Blynk.run();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
   
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        String roll = getValue(receivedChars, '/', 0);
        String pitch = getValue(receivedChars, '/', 1);
        String yaw = getValue(receivedChars, '/', 2);
        String weight = getValue(receivedChars, '/', 3);
        Serial.print("Roll: ");
        Serial.println(roll.toFloat());
        BLYNK_WRITE(roll);
        Serial.print("Pitch: ");
        Serial.println(pitch.toFloat());
        Serial.print("Yaw: ");
        Serial.println(yaw.toFloat());
        Serial.print("Weight: ");
        Serial.println(weight.toFloat());
        Serial.println("****************************************************");
        newData = false;
    }
}

BLYNK_WRITE(V5){
  Serial.println();
}

String getValue(String data, char separator, int index){
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;
 
  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }
 
  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

PLEASE HELP ME
Thank you :slight_smile:

I’ve merged your two topics, as they are on the same subject, and the answer to your question is in this topic anyway.

BLYNK_WRITE is not the method used to send data to the Blynk server. As I’ve already explained, Blynk.virtualWrite is the correct method…

I also explained that you need to use this using a timer. You cannot have Blynk.virtualWrite commands called directly from your void loop, otherwise you’ll flood the server and your data will be rejected.
You should read this for a complete explanation, and examples of how to create a BlynkTimer…

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

Pete.

Okey Sir
I will try it, THANK YOU