float in serial monitor

Hello,

I use serial communication to connect NodeMCU and Arduino UNO, i use software serial for it i get these type of output

instead of getting 2 values, the order is changed as shown in pic, i am using parseFloat() here, i want to save the float values into a variable with array, how do i do it so that those two values are alone saved in the array?? sometimes 2 values come, sometimes 3 values come,

the first is humidity and the other is temperature.

P.S baud rate is 9600 b/w nodemcu to arduino

parse code

void loop() {
  // put your main code here, to run repeatedly:
  while (NodeSerial.available() > 0)
  { 
     delay(1000);
    float val = NodeSerial.parseFloat();
    //Data.concat(val);
    if (NodeSerial.read() == '\n');
    //delay(1000);
  }
  /

Thanks.

You have to post all your code. The serial console output in your picture does not appear to match the displayed code fragment.

nodemcu receiving code

#include <ESP8266WiFi.h>
#include <WiFiClient.h>  
#include <ThingSpeak.h>  
#include <SoftwareSerial.h>

SoftwareSerial NodeSerial(D2, D3);  // RX | TX

const char* ssid = "Jerald";  
const char* password = "9443110000";  
WiFiClient client;  
unsigned long myChannelNumber =  441486;  
const char * myWriteAPIKey = "5QEPJGGHNVKRGPR6";  
uint8_t temperature, humidity; 
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  NodeSerial.begin(9600);  
  delay(10);                                                        
  // Connect to WiFi network  
  Serial.println();  
  Serial.println();  
  Serial.print("Connecting to ");  
  Serial.println(ssid);  
  WiFi.begin(ssid, password);  
  while (WiFi.status() != WL_CONNECTED)  
  {  
   delay(500);  
   Serial.print(".");  
  }  
  Serial.println("");  
  Serial.println("WiFi connected");  
  // Print the IP address  
  Serial.println(WiFi.localIP());  
  ThingSpeak.begin(client); 
}

void loop() {
  // put your main code here, to run repeatedly:
  static boolean data_state = false;
  float val1[3]; //char character;
  while (NodeSerial.available() > 0)
  { 
     delay(1000);
    float val = NodeSerial.parseFloat();
    //Data.concat(val);
    if (NodeSerial.read() == '\n');
    Serial.println(val);
    }
  // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different  
  // pieces of information in a channel. Here, we write to field 1.  
    if( data_state )  
    {  
      ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey);  
      data_state = false;  
    }  
    else  
    {  
      ThingSpeak.writeField(myChannelNumber, 2, humidity, myWriteAPIKey);  
      data_state = true;  
  } 
 // } /
  //delay(30000); // ThingSpeak will only accept updates every 15 seconds.  
  /*NodeSerial.print(10);
  NodeSerial.print("\n");
  delay(100);*/
}

arduino send code

#include <SoftwareSerial.h>
#include <dht.h>

#define dht_apin 7
dht DHT;


float temp;
float hum;

SoftwareSerial ArduinoSerial(3, 2);  // RX | TX

void setup() {
  // put your setup code here, to run once:
  pinMode(3, INPUT);
  pinMode(2, OUTPUT);
  Serial.begin(115200);
  ArduinoSerial.begin(9600);
  delay(500);//Delay to let system boot
  Serial.println("DHT11 Humidity & temperature Sensor\n\n");
  delay(1000);//Wait before accessing Sensor
}

void loop() {
  // put your main code here, to run repeatedly:
    //delay(2000);   //sensor boot-up delay
    //Read data and store it to variables hum and temp
    DHT.read11(dht_apin);
    hum = DHT.humidity;
    temp = DHT.temperature;
    ArduinoSerial.print("Current humidity = ");
    ArduinoSerial.print(hum);
    ArduinoSerial.print("%  ");
    Serial.print(DHT.humidity);
    ArduinoSerial.print("temperature = ");
    ArduinoSerial.print(temp);
    ArduinoSerial.println("C  ");
    Serial.print(DHT.temperature);    
    ArduinoSerial.print("\n");
}
   ArduinoSerial.print("Current humidity = ");
    ArduinoSerial.print(hum);
    ArduinoSerial.print("%  ");

It is a waste of time printing the text as you do not use it on the receiving side but it would be a good idea to format the data better so that receiving and parsing it was easier.

Have a look at the techniques used in Serial input basics - updated

Maybe your send part is sending data quicker than the receive part can process it. Do you have to send data every loop iteration ?

  float val1[3]; //char character;

That comment is complete rubbish.

  while (NodeSerial.available() > 0)
  {
     delay(1000);
    float val = NodeSerial.parseFloat();

Hey, there is data to read. Lets do nothing for a while.

Why?

    if (NodeSerial.read() == '\n');

There may not be anything to read, but, let's see if it's a \n.

Why?

You never use val, so I can't really see the point in having sent it.

You have no delays in the sending loop and " delay(1000);" in the receiving loop. I suspect your inout buffer is overflowing. Try putting the delay in the sending and NOT in the receiving. The "parseFloat()" will wait for the float to arrive.

johnwasser:
You have no delays in the sending loop and " delay(1000);" in the receiving loop. I suspect your inout buffer is overflowing. Try putting the delay in the sending and NOT in the receiving. The "parseFloat()" will wait for the float to arrive.

thanks much, its working now :slight_smile: thanks all guys all your help is implemented

all are working except the below error i want the parsed data to enter the array, but this error happens

char array1[2] = {'0','0'};

void loop() {
  while (NodeSerial.available() > 0)
  {
    delay(1000);  // receive delay working as expected
    float val = NodeSerial.parseInt();
    Serial.println(val);  
    for(j = 0; j < 2; j++)    //// here i get error 'j' was not used in this scope
    {
        array1[j] = val;
        Serial.println(array1);
    }  
  }

this above issue must be solved.

Delta_G:
Did you mean:

for(int j

omg, yes thanks, another issue is, according my loop code 1st loop gets temperature 2nd loop gets humidity and recurs i need to store temp and humidity in 2 different array(array1[0],array1[1]), how's that possible

current code

void loop() {
   while (NodeSerial.available() > 0)
  {
    delay(1000);
    float val = NodeSerial.parseInt();
    Serial.println(val);  
    for (int i=0; i < 2 ; i++)
    {
        array1[i] = val;
    }
    Serial.println(array1[0]);
    Serial.println(array1[1]);
  }

how's that possible

Here's a hint. It does NOT involve a for loop.

Suppose that I bop you on the top of the head, and then in the nose, and repeat.

How would you know whether you were going to be bopped on the head or in the nose next?

You'd count blows, wouldn't you?