-->
Page 2 of 2

Re: How to serial communication between node MCU and arduino

PostPosted: Wed May 09, 2018 5:44 am
by engrmuhammadshoaib
Arduino UNO/ Nano Code:
/*
* bismillah hir rahman nir raheem
* UNO/Nano = Pin 7 & Pin 8
* Note: Uno and ESP8266 cross connection
*/
#include <SoftwareSerial.h>
SoftwareSerial ArduinoUno(7,8);
String f;
void setup(){
Serial.begin(9600);
ArduinoUno.begin(115200);
}
void loop(){
float i = (random(100) + 1);
float j = (random(100) + 1);

f = String('H')+String(i)+String('T')+String(j);
ArduinoUno.println(f);
delay(2000);
}

NodeMCU ESP8266 Code:

/*
* bismillah hir rahman nir raheem
* ESP8266 = Pin D7 & Pin D8
* Note: Uno and ESP8266 cross connection
*/
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>

SoftwareSerial NodeMCU(D7,D8);

void setup(){
Serial.begin(9600);
NodeMCU.begin(115200);
pinMode(D7,INPUT);
pinMode(D8,OUTPUT);
}

void loop(){
String content = "";
char character;

while(NodeMCU.available()) {
character = NodeMCU.read();
content.concat(character);
}
if (content != "") {
Serial.println(content);
}
Serial.println("Temperature");
Serial.println(content.substring(1, 6));
Serial.println("Humidity");
Serial.println(content.substring(7, 12));
delay(2000);
}