Hi!
I have a UART/hardware serial code I want to use with my Arudino Yun Rev 2. Does it have any serial ports I can use?
I have a code meant to use with Arduino Mega. Is there anyone here who can help me "translate" this code to the Yun? Or even NodeMCU if possible? If it helps, I don't even need the part that prints to serial monitor, I can write directly to some cloud service.
Here is the code meant for the Arduino Mega:
//This code was written to be easy to understand.
//Modify this code as you see fit.
//This code will output data to the Arduino serial monitor.
//Type commands into the Arduino serial monitor to control the EZO-Co2 sensor.
//This code was written in the Arduino 1.8.9 IDE
//An Arduino MEGA was used to test this code.
//This code was last tested 6/2019
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring = ""; //a string to hold the data from the Atlas Scientific product
boolean input_string_complete = false; //have we received all the data from the PC
boolean sensor_string_complete = false; //have we received all the data from the Atlas Scientific product
int Co2; //used to hold a integer number that is the Co2
void setup() { //set up the hardware
Serial.begin(9600); //set baud rate for the hardware serial port_0 to 9600
Serial3.begin(9600); //set baud rate for software serial port_3 to 9600
inputstring.reserve(10); //set aside some bytes for receiving data from the PC
sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
}
void serialEvent() { //if the hardware serial port_0 receives a char
inputstring = Serial.readStringUntil(13); //read the string until we see a <CR>
input_string_complete = true; //set the flag used to tell if we have received a completed string from the PC
}
void serialEvent3() { //if the hardware serial port_3 receives a char
sensorstring = Serial3.readStringUntil(13); //read the string until we see a <CR>
sensor_string_complete = true; //set the flag used to tell if we have received a completed string from the PC
}
void loop() { //here we go...
if (input_string_complete == true) { //if a string from the PC has been received in its entirety
Serial3.print(inputstring); //send that string to the Atlas Scientific product
Serial3.print('\r'); //add a <CR> to the end of the string
inputstring = ""; //clear the string
input_string_complete = false; //reset the flag used to tell if we have received a completed string from the PC
}
if (sensor_string_complete == true) { //if a string from the Atlas Scientific product has been received in its entirety
Serial.println(sensorstring); //send that string to the PC's serial monitor
/* //uncomment this section to see how to convert the reading from a string to an int
Co2 = sensorstring.toInt(); //convert the string to a integer number so it can be evaluated by the Arduino
if (Co2 >= 400) { //if the Co2 is greater than or equal to 400 ppm
Serial.println("high"); //print "high" this is demonstrating that the Arduino is evaluating the Co2 as a number and not as a string
}
if (Co2 <= 399) { //if the Co2 is less than or equal to 399 ppm
Serial.println("low"); //print "low" this is demonstrating that the Arduino is evaluating the Co2 as a number and not as a string
}
*/
}
sensorstring = ""; //clear the string:
sensor_string_complete = false; //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
}