Reading string from serial monitor and trigger using NodeMCU

i successfully connect arduino mega to nodeMcu to transfer data from DHT11 sensor to arduino and then to the nodeMCU. rightnow, nodemcu receives the data in serial monitor via RX pin.

My question:
How do i take the data from serial monitor by programming in the nodeMCU so that i can send it to IoT environments like Blynk and related?

example:" serial monitor on nodeMCU now reads :H70T21"

which means 70% humid and 21*c temp. i want to send that value to Blynk

Thanks

TechyLad55:
which means 70% humid and 21*c temp. i want to send that value to Blynk

Follow the tutorial of this link.

ok sorry, i know how to use blynk, what i dont know is how do i programme so that nodeMCU reads the string in the serial monitor and do something about it?

like if(serialmonitorSays H70){

humidity = 70;
}

how do i do that? is it possible?

Ok! You want to enter from the InputBox of the Serial Monitor of ESP this string: "H70T21" + a Newline character. Now, you desire to catch them by the ESP and isolate the values of humidity and temperature from the received string and then save them into the following two variables. If it is your requirement, then it can be done.
humidity = 70%
temperature = 21 degC

Hints:
Collect and save the incoming stream in a char type array until you find Newline character (\n). You can use byte n = Serial.readBytesUntil('\n', myData, 10); method.

Get characters 7, 0 using isdigit() function and then apply atoi() function to obtain the numerical value 70.

Similarly, for 21.

thanks! may i know how its done?

You could take a look at Robin2's serial basics tutorial

TechyLad55:
thanks! may i know how its done?

Are you familiar with the following functions?:

byte n = Serial.available();
char x = Serial.read();
byte n = Serial.readBytesUntil('\n', myData, m); 
bool flag = isDigit();
int x = atoi(myArray);

If not, get a quick familiarization (at least what they do) with them from Arduino Reference Manual and web. The correct use of these functions will solve your problem.

does the /n character mean that it records everything in a line as one data? if so, i should serial.println my datas to allow it to read, am i right?

No, the '\n' character is the newline.

ok like this, the picture attached below shows h81.0t29.0 being constantly sent to the serial monitor from nodeMCU, its from the serial monitor output.

Its not from the inputBox of serial monitor

its not from the serial monitor input, its from the rx pin that receives the data. now i want to take that and isolate it and store it in a variable

example: if(letter is h){

humidity = checkFloat()

therefore humidity is now 81

}
if(letter is t){

temp = checkFloat()

therefore temp is now 29

}

i see many tutorials on the net and most are regarding putting into the serial monitor input , and thats not my case.

hope maybe u can give me a sample coding and clarify, thanks a lot!

1. When you enter this string h81.0t29.0 in the InputBox of the Serial Monitor (Fig-1) with Line ending tab selected at Newline (or Carriage return) option and click on the Send button, the following frames (one after another) arrive at the RX-pin of the NodeMCU/ESP8266. Now, it is obvious that the InputBox of the Serial Monitor can be used in lieu of a remotely located UART device for program development, debugging, and troubleshooting. However, it is recommended to connect user UART device with a software UART Port (SUAR) of the ESP as the hardware UART Port (UART) remains engaged with IDE/Serial Monitor for sketch uploading/debugging.

Given string : h81.0t29.0 with implied Newline character (\n) to be added by Serial Monitor
Frames : 0x68, 0x38, 0x31, 0x2E, 0x30, 0x79, 0x32, 0x39, 0x2E, 0x30, 0x0A (or 0x0D) (consult Fig-2)
SerialMonitor.png
Figure-1: Serial Monitor of Arduino IDE


Figure-2: ASCII Table

2. Now, write code for your ESP to check that frames are coming from Serial Monitor; catch them one-by-one and present them one-by-one in the OutputBox of the Serial Monitor (Fig-3).
Coes:

void setup()
{
  Serial.begin(115200);
  Serial.print("  ");
}

void loop() 
{
  byte n = Serial.available();
  if (n !=0)
  {
    char x = Serial.read();
    Serial.print(x);
  }
}

Screen shot:
sm-2.png

3. Now, try to add codes with the sketch of Step-2 to save the received characters into a char type array (char myData[]) and the reception/storage will be finished once you recognize the Newline character. The following function is suitable for this job:

byte n = Serial.readBytesUntil('\n', myData, 10);

4. You have all the characters in an array in Step-3, which can be processed to retrieve your humidity and temperature data.
Hints: To retrieve humidity data

Place ASCII code of '0' (0x30) at location myData[0].
Place null character ('\0') at the place of the character t of myData[].
Use atof() function to get the float value of humidity in a float type variable.

Screen shot:
sm-3.png

SerialMonitor.png

sm-2.png

sm-3.png

Hi, thank you very much for the clear guide.

just one question though, why "Place ASCII code of '0' (0x30) at location myData[0]" ?

whats the use of placing ASCII code '0'? and why 0? what does it mean and whats (0 x 30) ??

sorry for the basic programming questions, but anyways thank you very much!

We took time to compose post for you; in return, we expect that you will also took time to read our posts and try yo get the answers yourself for the unknowns looking at the database of web.

Let us see what have been said in the following text codes:

GolamMostafa:

Place ASCII code of '0' (0x30) at location myData[0].

Place null character ('\0') at the place of the character t of myData[].
Use atof() function to get the float value of humidity in a float type variable.

What are there in array myData[] after receiving the string that has come from the InputBox?

char myData[] = {'h', '8', '1', '.', '0', 't', '2', '9', '0', '\n'};

We want to extract the humidity value 81.0 from the above array using atof() function.

Now, you tell me in the return post (anything will be read whatever you say) about the working principle of the atof(arg) function as to --
What value(s) does it expect as input for its argument field.
What value the function will return.

0x30 is a hexadecimal number (base 16).
It's the same value as 48 decimal.
It also happens to be the value of the ASCII code for the zero digit character.