Serial Communication ESP and Arduino

Hello i've made 2 other post about this already but i still don't have the solution.

I am trying to send data from my arduino to the nodemcu with the use of Serial Communication.
If the Nodemcu receives the data it will put a message on a site.

Arduino void loop:

void loop()
{
unsigned int Pos;
int data=50;
int data1=60;
if(s.available()>0)

//
pixy.line.getAllFeatures(); //get line features
if (pixy.line.barcodes) // detected road sign
{
int code = pixy.line.barcodes[0].m_code;
Serial.println(code);
switch(code)

{
s.write(data);
s.write(data1);

Dont worry about the barcodes thats the pixy2 camera its part of my project.

Heres the Nodemcu void loop:
void loop() {

s.write("s");
if (s.available()>0)

if (!client.connected()) {
reconnect();
}
client.loop();
data=s.read();
if (data <= 20) {

unsigned long now = millis();
++value;
snprintf (msg, MSG_BUFFER_SIZE, "HIGHERT #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("Senne123", msg);
}

if (!client.connected()) {
reconnect();
}
client.loop();
data1=s.read();
if (data1 <= 60) {

unsigned long now = millis();
++value;
snprintf (msg, MSG_BUFFER_SIZE, "LOWERT #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("Senne123", msg);

}
}
It keeps on putting both messages on the site, HIGHERT and LOWERT. Is there any other way that i can read from the serial monitor ?

I've been working on this for 2 months already and still didn't find the solution. Every bit of help will be appreciated.

  if (s.available() > 0)
    if (!client.connected())
    {
      reconnect();
    }
  client.loop();
  data = s.read();

It is all very well checking whether data is available on s, whatever that is, but whether or not there is any data you go ahead and read from s

Is that deliberate ?

Hello UKHeliBob thanx for the reply, the data that the nodemcu has te read is the data that i am sending from the arduino. I've decleared it with an int, i just placed int data =50 and data1=60.
I still don't get the issue

if (s.available()>0)

There is no opening '{' brace so you check the condition but do nothing with the result.

Thanx for the reply Deva, but this if controls if the nodemcu send a character. If it dit it will send the data and data1 on the serial print. so it isn't a huge mistake and doesn't fix the problem.

Have a look at my tutorial on Serial Coms. Arduino to Arduino/PC
Handles delays on either side and includes a check sum.

Drmpf, i just had a little look im kinda new to coding. could you maybe send your discord and talk about it on there ?

For anybody that wants the help here is the full code:
ESP program
ESPLaserAAAAA.ino (4.2 KB)
Arduino Uno:
Master.ino (4.8 KB)

if (s.available()>0)

if (!client.connected()) {
reconnect();
}
client.loop();
data=s.read();

You do s.read() but there may be nothing available. You post is formatted in a way that is hard to read due to the new forum, it is not you fault, but still hard to read.

Can't you download the programs that i send ?

  • How can there not be anything available

Yeah i downloaded what you posted now.. it is still hard to read, but i've managed something
from master.ino

  if (s.available() > 0)  // this is a condition that is tested
    pixy.line.getAllFeatures();   // this is the line that gets executed if that condition is true
  if (pixy.line.barcodes) // this line gets executed regardless of the condition

from ESPlaser

if (s.available() > 0)  // this is the condition that is tested

    if (!client.connected()) {  // this line gets executed if the condition is true
      reconnect();   // and this one if the second condition is true
    }
  client.loop();   // this line 
  data = s.read();  // and this line get executed regardless of the condition

So even if there s.available() is false

data = s.read();

but there may be nothing to read.
Use ctrl-T to format your code (in the IDE) and the indentation should show you what what is conditionally executed. Clean up your code a bit, remove all the commented out stuff, remove white lines etc. rename it (please do, it will make it easier for me to open it) and attach it (them) to your next post.

Thank you a lot deva, i will do it tomorow for sure. Im new to programming and its my final school project for the year. Apreciate it a lot my guy!

Do you understand that you are reading even when data may not be available ?

I hope so, not only that in the other sketch

  if (s.available() > 0)

is never followed by a s.read(), so now the condition is always true (instead of just when the s.write("w"); is transmitted.
Anyway i think with correct conditional execution of code,
and the understanding that s.available() returns the number of bytes in the Serial (read) buffer. And that s.read() reads the next available byte in the buffer, and moves the pointer on to the next byte (and reduces what is available() by 1 )
available() is the difference between the pointer to the head of the buffer and the tail of the buffer. (rolling over with the size of the (ring) buffer).

Yes i get that, but that shouldn't be an issue right ? If there is nothing to read it won't show up on the serial monitor so the nodemcu won't do anything with it.

If there is nothing available when you do a read() then it will return -1
What is the point of checking whether something is available if you are going to try and read it anyway ?

oh alright i get it know thanks a lot!

Here the code written a bit cleaner
ESP:
ESPLaserA.ino (2.7 KB)
Arduino:
Master.ino (4.5 KB)

Yeah that does look a bit better. So i figure what you mean is actually this : (the first part of loop)

 s.write("s");
  if (s.available() > 0) {
    if (!client.connected()) {
      reconnect();
    }
    client.loop();
    data = s.read();
    if (data <= 20) {

      unsigned long now = millis();
      ++value;
      snprintf (msg, MSG_BUFFER_SIZE, "HIGHERT #%ld", value);
      Serial.print("Publish message: ");
      Serial.println(msg);
      client.publish("Senne123", msg);
    }
  }

so you check if there is anything in the buffer, and if there is, you check to see if you are connected, do the client.loop() (whatever that is) and do the 's.read()' knowing that there is something to read.

The master.ino code does not compile for now.

Actually to be honest, to do Serial communication the way that you are trying to, is not going to be very reliable. When the nodeMCU starts up it will most likely put out some gibberish, which may be interpreted by the UNO as data, and vice versa. There is a good chance that data & data1 will get swapped over, but maybe this is something to worry about later.

Well thats the problem when i say in my master code to only send data1 it still reads things in the nodemcu program. When i put data=100 that is more then the <20 it still does the command