Matheus Andrade
Matheus Andrade

Reputation: 27

Send an integer value from arduino uno to nodemcu

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.

Upvotes: 0

Views: 1281

Answers (1)

Matthew Whited
Matthew Whited

Reputation: 22443

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

Upvotes: 1

Related Questions