0

i have 2 integers i want to send them from Mega to nodeMcu i get them to talk to each other by this one on Mega :-

#include <DHT.h>      

#define DHTPIN 2      // Selecting the pin at which we have connected DHT11
#define DHTTYPE DHT11 // Selecting the type of DHT sensors

DHT dht ( DHTPIN, DHTTYPE ) ;

        void setup() {
      // initialize both serial ports:
      Serial.begin(9600);
      Serial1.begin(9600);
      dht.begin ();

    }

    void loop() {
      //==========// DHT part
   humidity = dht.readHumidity ( ) ;  // Declaring h a variable and storing the humidity in it.
   temp = dht.readTemperature ( ) ; // Declaring t a variable and storing the temperature in it.
  // Checking if the output is correct. If these are NaN, then there is something in it.
   if ( isnan ( humidity ) || isnan ( temp ) ) {
     Serial.println ( " Sensor not working " ) ;
   }
   else
    Serial.println(humidity);

//========// Send to wifi
 if (Serial1.available()) {

    itoa(humidity,str1,10);
    Serial1.write(str1,2);
    itoa(temp,str2,10);
    Serial1.write(str1,2);
    }

and this on on nodeMcu :-

#include <SoftwareSerial.h>

SoftwareSerial swSer(14, 12, false, 256);
char str1[10],str2[10];
int i,j;

void setup() {
  Serial.begin(9600);
  swSer.begin(9600);

  Serial.println("\nSoftware serial test started");

}

void loop() {
  while (swSer.available() > 0) {

    Serial.println("Humidity is");
    for (i=0; i<3; i++){
    str1[i]=(swSer.read());
    }
    Serial.println (str1);
    Serial.println("Temp is");
    for (j=0; j<3; j++){
    str2[j]=(swSer.read());
    }
    Serial.println (str2);


  }


}

But i get the result on nodeMcu not in right order (sometimes humidity printed in place of temperature and verse vise and sometimes i get no.2

can some one help me please to figure what is wrong ?

4
  • Please, read what Majenko written about serial reading. You code needed improvement in that area. hackingmajenkoblog.wordpress.com/2016/02/01/…. Really, read it. – user31481 Feb 11 '18 at 8:34
  • Why do you in Mega sketch test available if you want to write to Serial1? Why do you use itoa and write, if you can use print()? why do you use SoftwareSerial in esp8266? Why do you use read if you can use parseInt()? – Juraj Feb 11 '18 at 11:41
  • @user31481 this article helped so much thank you – Mai Ehab Feb 27 '18 at 22:19
  • @Juraj why do you use SoftwareSerial in esp8266? because i want to read from digital pin using parseInt() and print() is a good suggestion thanks – Mai Ehab Feb 28 '18 at 3:17
-2

I'd suggest some kind of character before each int so that the receiver knows which one it is getting.

2
  • Or a newline after the 2nd, with a space or comma between. – Chris Stratton Feb 11 '18 at 3:02
  • 2
    You have to elaborate. You are guiding a guy lost in the jungle. Waving your hand in a general direction is not good. Show how to do it. Post some code. – user31481 Feb 11 '18 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.