Hye guys, I face some issue when I was trying to pass some values from node mcu to Arduino Uno. I want my node mcu to send either 0 or 1 to my Arduino uno. The idea is like this: Whenever I turn on the button in the Arduino IoT cloud dashboard, I want my nodemcu to send "1" to my aruduino uno, else if the button is turn off, it will send an "0". The program that I have wrote is working fine. But however, Arduino Uno will receive some unknown symbol as in the figure below.
As you can see the unknown symbol here is ⸮P.
Anyone have any idea what have just happened?
Please post your code.
#include "arduino_secrets.h"
/*
Sketch generated by the Arduino IoT Cloud Thing "The_magic_platform_things"
https://create.arduino.cc/cloud/things/743bc533-d0b9-48b9-8464-c03ec104e251
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
CloudLight lED;
CloudElectricPotential gSR_Sensor;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include <SoftwareSerial.h>
SoftwareSerial NodeMcu_SoftSerial(D1, D2); //RX, TX
#include "thingProperties.h"
//Declare Global Variable
char c;
String dataIn;
int8_t indexOfA, indexOfB, indexOfC, indexOfD;
String data1, data2, data3, data4;
int LED ;
int Music ;
int Speaker ;
///////////////////////////////////////////////////////
void setup() {
Serial.begin(57600); // Open serial communications (Arduino-PC)
NodeMcu_SoftSerial.begin(9600); //Open Serial Communication (Arduino-NodeMCU)
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
///////////////////////////////Receive Data from Arduino/////////////////////////////////////
while(NodeMcu_SoftSerial.available()>0) //read data serial
{
c =NodeMcu_SoftSerial.read();
if(c=='\n'){break;}
else {dataIn+=c;}
}
if(c=='\n')
{
Parse_the_Data();
//Print the received data in serial monitor
Serial.println("Human_resistance = " + data1);
Serial.println("muscle = " + data2);
Serial.println("heart = " + data3);
Serial.println("Stress_score = " + data4);
Serial.println("=========================================");
gSR_Sensor = data1.toInt(); //Pass data to IoT Platform
eMG_Sensor = data2.toInt(); //Pass data to IoT Platform
pulse_rate_sensor = data3.toInt();//Pass data to IoT Platform
stress_score = data4.toInt(); //Pass data to IoT Platform
/////////////////////////////////////Send data to Arduino Uno////////////////////////////////////
onLEDChange(); NodeMcu_SoftSerial.print("A");
onWaterAtomizerChange(); NodeMcu_SoftSerial.print("B");
//NodeMcu_SoftSerial.print(Speaker); NodeMcu_SoftSerial.print("C");
NodeMcu_SoftSerial.print("\n");
//Reset variable
c=0;
dataIn="";
}
}
void Parse_the_Data()
{
indexOfA = dataIn.indexOf("A");
indexOfB = dataIn.indexOf("B");
indexOfC = dataIn.indexOf("C");
indexOfD = dataIn.indexOf("D");
data1 = dataIn.substring (0, indexOfA);
data2 = dataIn.substring (indexOfA+1, indexOfB);
data3 = dataIn.substring (indexOfB+1, indexOfC);
data4 = dataIn.substring (indexOfC+1, indexOfD);
}
/*
Since LED is READ_WRITE variable, onLEDChange() is
executed every time a new value is received from IoT Cloud.
*/
//////////////////////////////////////////////////////////////////////////////////////////
void onLEDChange() {
if(lED==1)
{
NodeMcu_SoftSerial.print("1");
}
else
{
NodeMcu_SoftSerial.print("0");
}
}
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
void onWaterAtomizerChange() {
// Add your code here to act upon WaterAtomizer change
if(water_Atomizer==1)
{
NodeMcu_SoftSerial.print("1");
}
else
{
NodeMcu_SoftSerial.print("0");
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////Messenger widget///////////////////////////////////
void onMessageChange() {
if(message=="Hello")
{
message = "Hi, I am your Doctor";
}
else if(message=="What can you do?")
{
message = "I can help you to monitor and control stress.";
}
else if(message=="Can I have the report?")
{
message = "Skin resistance = " + data1 + ";" + "EMG reading = "+ data2 + ";" + "BPM = " + data3 + ";" + "Stress score = " + data4;
}
else if(message=="Is it normal?")
{
message = "A stress score of 33 or lower is considered low; a stress level of 33 to 66 is considered medium. Else, you are having high stress.";
}
else if(message=="How to relieve stress?")
{
message = "we recommend using music, aromatherapy, and blue light therapy to help you relax. Take a deep breath and calm down. Everything is good.";
}
}
////////////////////////////////////////////////////////////////////////////////////////
Hi @red_car this is the code at the nodemcu part. It responsible of sending data from arduino iot cloud back to the arduino uno.
are you trying to print integers?
No promises....
Try if ( c=='\n' || c=='\r') ...
You can also filter the ASCII to remove any values you do not need.
ASCII Table (cmu.edu)
Yes, @mickhowe . When I turn on a button in the Arduino IoT cloud, the Node MCU will send either 0 or 1 to the Arduino Uno to activate the LED and Water atomizer that is connected to it. For now, the Arduino Uno gets to receive 0 and 1. But sometimes some unknown symbol that is not needed.
Hi @mrburnette . Thank you. Can u show me a sample code to eliminate the symbol $ in my program?
if ( c=='\n' || c=='\r' || c=='$' ) {break;} // filter LF, CR, $ ...
Above you are filtering by exclusion. Filtering can also be done using category of ASCII:
see: https://docs.arduino.cc/built-in-examples/strings/CharacterAnalysis#code
You can extend this into any ASCII value by just using the char value:
https://www.arduino.cc/reference/en/language/variables/data-types/char/
if ( c==10 || c==13 || c==36 ) {break;} // exclude LF, CR, $
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.