0

I want to send one variable my Arduino UNO reads from a sensor to the NodeMCU, so it can send it as a json to a MQTT server.

I've tried multiple code implementations that I've seen on the web, but when I watch the console on the baud rate I set for the SoftwareSerial, it only gives me random strings.

Code on the uno: https://pastebin.com/fZHtEdjV

#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial toNode(2,3); // (Rx, Tx)

int sensorPin = A0;
int sensorValue;

void setup() {
 Serial.begin(9600);
 toNode.begin(115200);

 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(8, OUTPUT);
}

void loop() {

 sensorValue = analogRead(sensorPin); 
 Serial.println("Analog Value : ");
 Serial.println(sensorValue);

 if (sensorValue<300) {
 digitalWrite(10, HIGH);
 digitalWrite(9, LOW);
 digitalWrite(8, LOW);
 parseJson(sensorValue);
 }
 else if(sensorValue>300 && sensorValue<450){
 digitalWrite(9, HIGH);
 digitalWrite(8, LOW);
 digitalWrite(10, LOW);
 parseJson(sensorValue);
 }
 else if(sensorValue < 300)
 {
 digitalWrite(8, HIGH);
 digitalWrite(9, LOW);
 digitalWrite(10, LOW);
 parseJson(sensorValue);
 }

 delay(1000); 
}

void parseJson(int criticidade) {
   String njs;
   njs = String(criticidade);
   toNode.println(njs);
}

Code on the NodeMCU: https://pastebin.com/SFNC5JfG

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial fromUno(D5,D6); // (Rx, Tx)

char* ssid = "B";
char* password =  "A";
const char* mqttServer = "Z";
const int mqttPort = 1;
const char* mqttUser = "Y";
const char* mqttPassword = "X;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {

  Serial.begin(9600);
  fromUno.begin(115200);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println("Connected to the WiFi network");

  client.setServer(mqttServer, mqttPort);

  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESP32Client", mqttUser, mqttPassword )) {

      Serial.println("connected");

    } else {

      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);

    }
  }
}

void loop() {
   char json[100];
   json[0] = fromUno.read();
   String json2 = "{\"planta\":{\"umidade\":200,\"criticidade\":1}}";

  Serial.println("Sending message to MQTT topic..");

  if (client.publish("test", json2) == true) {
    Serial.println("Success sending message");
  } else {
    Serial.println("Error sending message");
  }

  client.loop();
  Serial.println("-------------");

  delay(10000);
}

I expected that when reading the serial port from the NodeMCU, it would set the integer value I'm sending from the UNO to a variable.

2
  • Oh, you aren't going to be able use to 115200 with softserial. slow that baud rate down to 9600 or less and try again. Commented Jun 4, 2019 at 15:05
  • And since you are using println it should send a text representation of the integer Commented Jun 4, 2019 at 16:36

1 Answer 1

1

SoftwareSerial doesn't support 115200 baud rate. You may be able to get 57600 but you are better off trying 9600 or less.

Sign up to request clarification or add additional context in comments.

2 Comments

So if I try using 9600 will it send the integer on it's original value?
Your data corruption issue is because the bitbanging process doesn't keep the output in the correct values for the input to understand it. (you would see a similar issue if you use the wrong data rate.) The bits are being skewed since the processor can't do its normal job withing the time of the data read. (the issue could be on the sender, receiver or both since you are using softwareserial on both ends)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.