Go Down

Topic: Serial Communication between Arduino and NodeMCU (Read 502 times) previous topic - next topic

nguyenpt

Hello guys.
I want to send data which 2 packets ( temperature and hudimity) from arduino  to ESP NodeMcu. I used Serial Communication but it didn't work. Can you help me?
Thank you somuch.

Here are my code:

Tx _ Arduino
 
Code: [Select]

#include <SoftwareSerial.h>
SoftwareSerial s(0,1);
char inData[24];
byte index;
int windVal;
int temp;
boolean started = false;
boolean ended = false;

void setup()
{
Serial.begin(9600);
s.begin(9600);
Serial.println("Temperature & Humidity");
}

void loop()
{
  while(Serial.available() > 0)
  {
  char aChar = Serial.read();
  if(aChar == '<')
  {
      started = true;
      index = 0;
      inData[index] = '\0';
  }
  else if(aChar == '>')
  {
      ended = true;
  }
  else if(started)
  {
      inData[index] = aChar;
      index++;
      inData[index] = ',';
  }
  }

  if(started && ended)
  {
  // Use the value
  if(inData[0] == 'T')
  {
     inData[0] = ' ';
     int windVal = atoi(inData);
                s.write(windVal);
           Serial.println(" ");
     Serial.print("Temp:");
           Serial.print(inData);
           Serial.print("C");
           Serial.println(" ");
  }
  else if(inData[0] == 'H')
     {
     inData[0] = ' ';
           int temp = atoi(inData);
           s.write(temp);
     Serial.println(" ");
           Serial.print("Humidity:");
           Serial.print(inData);
           Serial.print("%");
           Serial.println(" ");
     }

  started = false;
  ended = false;

  index = 0;
  inData[index] = '\0';
  }
}



Rx_NodeMCU
Code: [Select]

#include <SoftwareSerial.h>


// Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

SoftwareSerial s(3,1);


// WiFi parameters
#define WLAN_SSID       "Nangsuat-Technology"
#define WLAN_PASS       "comphone"

// Adafruit IO
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "nhatnguyenpt"
#define AIO_KEY         "e01a5a80956249ca886990be948e6dd7"  // Obtained from account info on io.adafruit.com



// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// Setup feeds for temperature & humidity
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temp");
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity");

/*************************** Sketch Code ************************************/
int windVal,temp;
void setup() {
  // Init sensor
 // dht.begin();

  Serial.begin(9600);
  s.begin(9600);
  Serial.println(F("Adafruit IO Example"));
 
  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  delay(10);
  Serial.print(F("Connecting to "));
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();

  Serial.println(F("WiFi connected"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());

  // connect to adafruit io
  connect();

}

void loop() {

  // ping adafruit io a few times to make sure we remain connected
  if(! mqtt.ping(3)) {
    // reconnect to adafruit io
    if(! mqtt.connected())
      connect();
  }

  if (Serial.available()) {
    Serial.write(Serial.read());
  }

  windVal = Serial.read(); //Read the serial data and store it
  temp = Serial.read();
  delay(1000);

 
  // Publish data
  if (! temperature.publish(windVal))
    Serial.println(F("Failed to publish temperature"));
  else
    Serial.println(F("Temperature published!"));

  if (! humidity.publish(temp))
    Serial.println(F("Failed to publish humidity"));
  else
    Serial.println(F("Humidity published!"));

  // Repeat every 10 seconds
   Serial.print("Humidity: ");
   Serial.print(windVal);
   Serial.print(",");
// Serial.print(" %\t");

   Serial.print("Temperature: ");
   Serial.println(temp);
// Serial.print(" *C ");
  delay(10000);

}


// connect to adafruit io via MQTT
void connect() {

  Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

  Serial.println(F("Adafruit IO Connected!"));

}



Here is my screenshoot Serial Monitor

pert


Nick_Pyner

On every Arduino I know, the hardware serial is on pins 0,1. This means you are using software serial on the hardware serial pins, which is a fatal mistake. You probably don't need software serial at all but, for the moment, move it to another pair of vacant pins.

I think you are doing the same thing on NodeMCU, and destined to get the same result, so the same applies.

If you insist on using software serial on NodeMCU, which is probably as bad an idea as it is using anywhere else, I understand you can call it on pins 13,15 i.e. connect to pins D7,D8 on the board.

GolamMostafa

#3
Mar 23, 2019, 04:41 am Last Edit: Mar 23, 2019, 10:43 am by GolamMostafa
@OP

There are many many logical/physical problems in your setup and codes. Therefore, I have preferred to prepare a tutorial for you rather than adjusting.correcting your system. Please, follow the steps; be familiar with the basics of hardware/software UART communications and get a working project.  

1.  Make the following setup among UNO, NodeMCU 1.0 (ESP-12), and DHT11 (Temp-Humidity Sensor).

Figure-1:

2.  Hardware UART Port of UN0 (RX, TX = 0, 1) is permanently engaged with the Serial Monitor and IDE; so, leave it there. Use software UART Port (SUART Port: 2, 3 = SRX, STX) to communicate with NodeMCU. The SUART Port is created by including the following lines in the sketch.
Code: [Select]
#include<SoftwareSrial.h>
SoftwareSerial SUART( 2, 3); //SRX  = DPin-2; STX = DPin-3
SUART.begin(115200);   //to match with NodeMCU which prefers higher Bd to work


3.  Similarly, Hardware UART Port of NodeMCU (RX, TX = 3, 1) is permanently engaged with the Serial Monitor and IDE; so, leave it there. Use software UART Port (SUART Port : 4/D2, 5/D1 = SRX, STX) to communicate with NodeMCU. The SUART Port is created by including the following lines in the sketch.
Code: [Select]
#include<SoftwareSrial.h>
SoftwareSerial SUART( 4, 5); //SRX  = DPin-D2; STX = DPin-D1
SUART.begin(115200);   //NodeMCU prefers higher Bd to work


4.  Upload the following sketches in the UNO and NodeMCU. (Read the comments that are given aside of the codes to under the reasons for including the code in the sketch).

Codes for UNO
Code: [Select]
#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX=Dpin-2; STX-DPin-3
//-------------------------
#include <SimpleDHT.h>
int pinDHT11 = 4;  //Humidity DATA line at A0-pin of UNO
SimpleDHT11 dht11;  //object is created from class
byte temperature = 0;
byte humidity = 0;
//--------------------------------------------

void setup()
{
  Serial.begin(115200); //enable Serial Monitor
  SUART.begin(115200); //enable SUART Port

}

void loop()
{
  //---acquire Temp and Humidity signal and save in variables
  dht11.read(pinDHT11, &temperature, &humidity, NULL);
  
  //---show temp and humidity as integers on Serial moniotr
  Serial.print("Temperature = "); Serial.print((int)temperature); Serial.print(" degC");
  Serial.print("  ");  //space
  Serial.print("Humidity = "); Serial.print((int)humidity); Serial.println(" %H");
  
  //----Send Temp and Humidity signal to NodeMCU via SUART port----
  SUART.print('<');                   //<(start mark)
  SUART.print((int)temperature,DEC);
  SUART.print(',');                   //,(data seperator)
  SUART.print((int)humidity, DEC);
  SUART.print('>');                   //>(end mark)
  SUART.println();

  delay(1000);
}

Screenshot of the Serial Monitor of UNO in response to above codes


Codes for NodeMCU
The following codes written for NodeMCU just as a test routine. Bring necessary change in this sketch so that you can see the Temperature and Humidity on the Serial Monitor of NodeMCU in the same format as they are appearing on the Serial Monitor of UNO.
Code: [Select]
#include<SoftwareSerial.h>
SoftwareSerial SUART(4, 5); //SRX=Dpin-D2; STX-DPin-D1
//-------------------------
#include <SimpleDHT.h>
int temperature;
int humidity;
//--------------------------------------------

void setup()
{
  Serial.begin(115200); //enable Serial Monitor
  SUART.begin(115200); //enable SUART Port

}

void loop()
{
  byte n = SUART.available(); //n != 0 means a character has arrived
  if (n != 0)
  {
    char x = SUART.read();  //read character
    Serial.print(x);        //show character on Serial Monitor
  }
}

Screenshot of the Serial Monitor of NodeMCU in response to above codes


Screenshot of the Serial Monitor of NodeMCU after processing of received string

Robin2

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up