Hi Everyone, ive been trying to make NodeMCU able to parse data from arduino uno via softwareserial.
A little bit background i am using nodemcu to automatically upload a data to IoT platform such as ThinkSpeak. Using Arduino as the main microcontroller for i2c for a sensor and displaying a 20x4 lcd.
Ive manage to build the code but the strange thing (sort of) happen. the program ive been building succesfully sending data (arduino side). that looks like this (using comma as separator for a multiple variables)
1.2,1.3,1.4,-1.0,
the string actually runs trough the serial and on the receive side (nodeMCU) relaying received serial.read as serial.print at interval of 1000ms. However, the data seems doesnt to be parsed, only if i input it manually on the serial monitor (for the nodemcu) the data actually parsed and updated the iot platform.
Anything did i do wrong? thanks
Arduino Side (loop)
#include
void loop(void)
{
if (millis() > timer + INTERVAL)
{
timer = millis(); {
rawADCvalue = ads.readADC_Differential_0_1();
volts = (rawADCvalue * scalefactor) / 1000.0;
shuntvoltage_A = ina219_A.getShuntVoltage_mV();
busvoltage_A = ina219_A.getBusVoltage_V();
current_mA_A = ina219_A.getCurrent_mA();
power_mW_A = ina219_A.getPower_mW();
loadvoltage_A = busvoltage_A + (shuntvoltage_A / 1000);
shuntvoltage_B = ina219_B.getShuntVoltage_mV();
busvoltage_B = ina219_B.getBusVoltage_V();
current_mA_B = ina219_B.getCurrent_mA();
power_mW_B = ina219_B.getPower_mW();
loadvoltage_B = busvoltage_B + (shuntvoltage_B / 1000);
Serial.print(loadvoltage_A, 4); Serial.print(","); Serial.print(current_mA_A, 4); Serial.print(","); Serial.print(volts, 4); Serial.print(","); Serial.print(loadvoltage_B, 4); Serial.print(","); Serial.print(V0, 4); Serial.print(",");Serial.print(Rmeasured,4); Serial.print(","); Serial.println("");
mySerial.print(loadvoltage_A, 4); mySerial.print(","); mySerial.print(current_mA_A, 4); mySerial.print(","); mySerial.print(volts, 4); mySerial.print(","); mySerial.print(loadvoltage_B, 4); mySerial.print(","); mySerial.print(V0, 4); mySerial.print(","); mySerial.print(Rmeasured,4);mySerial.println("");
// mySerial.write(loadvoltage_A); mySerial.write(","); mySerial.write(current_mA_A); mySerial.write(","); mySerial.write(volts); mySerial.write(","); mySerial.write(loadvoltage_B); mySerial.write(","); mySerial.write(current_mA_B); mySerial.write(","); mySerial.write('\n');
// mySerial.write("\n");
// sprintf(combined, "%d, %d, %d, %d, %d ", loadvoltage_A, current_mA_A, volts, loadvoltage_B, current_mA_B);
// Serial.println(combined);
lcd.setCursor(0, 0); lcd.print(" -ResistivityMeter- ");
lcd.setCursor(0, 1); lcd.print("Vnow="); lcd.setCursor(5, 1); lcd.print(volts, 3);
lcd.setCursor(0, 2); lcd.print("Inow="); lcd.setCursor(5, 2); lcd.print(current_mA_A, 3);
lcd.setCursor(0, 3); lcd.print("V0 =");
lcd.setCursor(13, 2); lcd.print("R Total");
}
}
int button1 = digitalRead(13);
int button2 = digitalRead(12);
if (button1 == HIGH) {
} else {
V0 = volts;
lcd.setCursor(5, 3); lcd.print(V0, 3);
}
if (button2 == HIGH) {
} else {
V1 = loadvoltage_A;
if (current_mA_A == 0.0) {
lcd.setCursor(13, 3); lcd.print(" Try ");
} else {
Rmeasured = 2 * 3.14 * 0.038 * (V1 - V0 / current_mA_A);
lcd.setCursor(13, 3); lcd.print(Rmeasured, 3);
}
}
}
NodeMCU Side (loop)
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
if (millis() > timer + INTERVAL)
{
timer = millis();
while (Serial.available() > 0) {
delay(10);
char c = Serial.read();
data += c;
}
if (data.length() > 0) {
Serial.println(data);
Index1 = data.indexOf(',');
Index2 = data.indexOf(',', Index1 + 1);
Index3 = data.indexOf(',', Index2 + 1);
Index4 = data.indexOf(',', Index3 + 1);
Index5 = data.indexOf(',', Index4 + 1);
Index6 = data.indexOf(',', Index5 + 1);
secondValue = data.substring(Index1 + 1, Index2);
thirdValue = data.substring(Index2 + 1, Index3);
fourthValue = data.substring(Index3 + 1, Index4);
fifthValue = data.substring(Index4 + 1, Index5);
sixthValue = data.substring(Index5 + 1, Index6);
seventhValue = data.substring(Index6 + 1, Index7);
data_1 = secondValue.toFloat();
data_2 = thirdValue.toFloat();
data_3 = fourthValue.toFloat();
data_4 = fifthValue.toFloat();
data_5 = sixthValue.toFloat();
data_6 = seventhValue.toFloat();
ThingSpeak.setField(1, data_1);
ThingSpeak.setField(2, data_2);
ThingSpeak.setField(3, data_3);
ThingSpeak.setField(4, data_4);
ThingSpeak.setField(5, data_5);
ThingSpeak.setField(6, data_6);
// Serial.print(" Data 1 ");Serial.print(data_1);
// Serial.print(" Data 2 ");Serial.print(data_2);
// Serial.print(" Data 3 ");Serial.print(data_3);
// Serial.print(" Data 4 ");Serial.print(data_4);
// Serial.print(" Data 5 ");Serial.print(data_5);
// Serial.print(" Data 6 ");Serial.print(data_6);
// Serial.println(" ");
// set the status
ThingSpeak.setStatus(myStatus);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200) {
Serial.println("Channel update successful.");
}
else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
data = "";
}
}
}
Thank you so much for the help..