Serial communication help

Hello,

I am trying to do some serial communication but honestly I only know one way, and I am not sure if it is the best way. My code does everything as expected but at the end it checks to see the command sent from ESP is equal to certain commands and does things accordingly. my issue is my command does equal exactly what I expected but it exits the if else statement as if it does not equal. I am sure it is something silly.

The manageLED() function and espListen() function where the issue happens. and handlePump1() and handlePump2() are the ESP side are the functions sending the Serial information. everything works as expected for the most part.

First of all Let me post the results. The if/else statement will A-turn pump on B-turn pump off or C- print "this is what I received". What I want to receive is 1ON, 1OFF. and that is exactly what I get but it prints this is what I received and prints 1ON.

It would seem that I am just having an issue with Serial communication as I am having the arduino send the ESP data and I am not getting any result for that.

1ON
This is what I received: 1ON

1ON
This is what I received: 1ON

1OFF
This is what I received: 1OFF

I am not posting my main large project as it is alot of code to go through. these are the codes that I am getting the issues that are described here. but I am saying this because you will notice the ESP code expects things that are non existent in the arduino code.

is there anyone that can assist me in learning how to get the two things to communicate arduino r3 and esp-01. thank you!

#include <SimpleDHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>


ESP8266WebServer server(80);

#define pinDHT11 2
SimpleDHT11 dht11;

byte f = 0;
byte temperature = 0;
byte humidity = 0;
char pump1On[] = "1ON";
char pump1Off[] = "1OFF";
char pump2On[] = "2ON";
char pump2Off[] = "2OFF";
String pumpIO[2] = {"OFF", "OFF"};
int values[] = {0, 0, 0, 0};
int currentValue = 0;
int moisture[] = {0, 0};
int lastWaterMoisture[] = {0, 0};




void setup() {

  Serial.begin(115200);
  WiFi.begin("***********", "********"); // connect to Access Point

  while (WiFi.status() != WL_CONNECTED) { //While Wifi is not connected, Print ..
    delay(500);
    Serial.print("..");
  }

  Serial.print("IP Address - ");
  Serial.println(WiFi.localIP());// Print local IP

  server.on("/", rootPage);
  server.on("/home", rootPage);

  server.begin();
  delay(200);
  Serial.println("Server is listening");
}

void loop() {
  server.handleClient(); //handle HTML
  readTempHum(); //Read dht11 sensor
  receiveMoisture(); //receive moisture values from Arduino
  
}


void receiveMoisture(){//receive the 4 values, moistures, and last moistures
  if(Serial.available()){
    while(Serial.available){
    int incomingValue = Serial.read();
    
    values[currentValue] = incomingValue;

    currentValue++;
    if(currentValue > 3){
      currentValue = 0;
    }
    }
  }
  moisture[0] = values[0];//assign values to dashboard values
  moisture[1] = values[1];//same
  lastWaterMoisture[0] = values[2];//ditto
  lastWaterMoisture[1] = values[3];//you get it 
}

void readTempHum(){ //read temp from dht11
    dht11.read(pinDHT11, &temperature, &humidity, NULL);
  delay(2020);
  f = C2F(temperature);
  //Serial.println(f);//want to see the temp?
  //Serial.println(humidity);//want to see the Humidity?
}


void rootPage() { //sends arguments depending on what the person presses on dashboard
  if (server.hasArg("Pump1")) {
    handlePump1();
    // pumpValue = server.arg("Pump1");
  } else if (server.hasArg("Pump2")) {
    handlePump2();
    // pumpValue2 = server.arg("Pump2");
  } else {
    server.send ( 200, "text/html", getPage());
  }
}


void handlePump1() { //check pump1 on or off
  if (server.arg("Pump1") == "1") {
    Serial.println(pump1On);
    pumpIO[0] = "ON";
    server.send ( 200, "text/html", getPage() );
  } else if (server.arg("Pump1") == "0") {
    Serial.println(pump1Off);
    pumpIO[0] = "OFF";
    server.send ( 200, "text/html", getPage() );
  } else {
    Serial.println("Error");
  }
}

void handlePump2() { //check pump2 on or off
  if (server.arg("Pump2") == "1") {
    Serial.println(pump2On);
    pumpIO[1] = "ON";
    server.send ( 200, "text/html", getPage() );
  } else if (server.arg("Pump2") == "0" ) {
    Serial.println(pump2Off);
    pumpIO[1] = "OFF";
    server.send ( 200, "text/html", getPage() );
  } else {

  }
}



int C2F(int celcius) //
{
 return (celcius * 9 +2)/5+32;  
}


String getPage() { //HTML Page to for plant watering system dashboard
  String page = "<html><head><meta charset='utf-8'>";
  page += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
  page += "<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css' integrity='sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm' crossorigin='anonymous'>";
  page += "<script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js' integrity='sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl' crossorigin='anonymous'></script>";
  page += "<title>Plant</title></head><body>";
  page += "<h1 class='text-uppercase text-center'><strong>Plant ESP Dashboard</strong><form action='/'></h1>";
  page += "<div class='table-responsive'>";
  page += "    <table class='table'>";
  page += "        <thead>";
  page += "            <tr class='text-center'>";
  page += "                <th class='text-center'>Temperature</th>";
  page += "                <th class='text-center'>Humidity</th>";
  page += "            </tr>";
  page += "        </thead>";
  page += "        <tbody>";
  page += "            <tr class='text-center'>";
  page += "                <td>";
  page +=                 f;
  page += "                </td>";
  page += "                <td>";
  page +=                 humidity;
  page += "            </td>";
  page += "            </tr>";
  page += "            <tr></tr>";
  page += "        </tbody>";
  page += "    </table>";
  page += "</div>";
  page += "       <div class='col-md-4'><h4 class ='text-center'>Pump 1 ";
  page += "        <span class='badge badge-primary'>";
  page +=           pumpIO[0];
  page += "        </span></h4></div>";
  page += "<div class='btn-toolbar'>";
  page += "    <div class='btn-group btn-group-lg mx-auto' role='group'>";
  page += "        <button class='btn btn-success' type='submit' name='Pump1' value='1'><form action='/' method='POST'>On</button>";
  page += "        <button class='btn btn-primary' type='submit' name='Pump1' value='0'><form action='/' method='POST'>Off</button>";
  page += "    </div></div><div class='col-md-4'><h4 class ='text-center'>Pump 2 ";
  page += "        <span class='badge badge-success'>";
  page +=           pumpIO[1];
  page += "        </span></h4></div>";
  page += "<div class='btn-toolbar'>";
  page += "    <div class='btn-group btn-group-lg mx-auto' role='group'>";
  page += "        <button class='btn btn-success' type='submit' name='Pump2' value='1'><form action='/' method='POST'>On</button>";
  page += "        <button class='btn btn-primary' type='submit' name='Pump2' value='0'><form action='/' method='POST'>Off</button>";
  page += "     </div>";
  page += "</div>";
  page += "<p> </p>";
  page += "<div class='table-responsive'>";
  page += "    <table class='table'>";
  page += "        <thead>";
  page += "            <tr>";
  page += "                <th class='text-center'>Plant 1</th>";
  page += "                <th class='text-center'>Plant 2</th>";
  page += "            </tr>";
  page += "        </thead>";
  page += "        <tbody>";
  page += "            <tr class='text-center'>";
  page += "                <td>";
  page +=                 moisture[0];
  page += "                </td>";
  page += "                <td>";
  page +=                  moisture[1];
  page += "                </td>";
  page += "            </tr>";
  page += "            <tr class='text-center'>";
  page += "                <td>";
  page +=                  lastWaterMoisture[0];
  page += "                </td>";
  page += "                <td>";
  page +=                  lastWaterMoisture[1];
  page += "              </td>";
  page += "            </tr>";
  page += "        </tbody>";
  page += "    </table>";
  page += "</div>";
  page += "</body>";
  page += "</html>";
  return page;
}
#include<SoftwareSerial.h>

SoftwareSerial esp (2, 3);

#define led 13
String text = "";
void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(115200);
  esp.begin(115200);
  while (!Serial) {
    ;
  }
  Serial.println("Device: Arduino");
}

void loop() {
 espListen();
}


void espListen() {
  if (esp.available()) {
    while (esp.available()) {
      char c = esp.read(); // receive byte as a character
      text += c;
      Serial.print(c);         // print the character
    }
    manageLED(text);
  }
  text = "";
}


void manageLED(String cmd) {
  delay(100);
  if (cmd == "1ON") {
    Serial.println("Turning LED on");
    digitalWrite(led, HIGH);
  } else if (cmd == "1OFF") {
    Serial.println("Turning LED off");
    digitalWrite(led, LOW);
  } else {
    Serial.println("This is what I received: " + cmd);
  }
}

Have you tried printing what you receive ?

When you do, print “>” before it and “<” after it so that you can see if there are any unprintable characters

This is not sufficient to be sure that a complete message has been received

if (esp.available()) {
    while (esp.available()) {

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

If you can reduce your commands to a single character you will make the Arduino code much simpler. For example 'N' for on and 'F' for off.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Robin2 - This is perfect. however, after going through all of those examples and trying to implement. It would seem that I am unsure on how to check what is being received is equal to something else. it would seem I am missing something. in this case 1ON does not equal 1ON. why not? these examples do not fix my issue but do give me insight on different ways to go about sending the information. I am getting the information to the arduino, but this expression is not happening even thought what is received is exactly what I want. The same result with the examples that you gave me.

The information sent does not have to be 1ON, I have even changed it to 1, 2, 3, 4. I understand that it is a string/char and not an int unless I change the println.

how do I get this to work? I am accomplishing the serial communication. arduino is receiving exactly what I am sending most of the time. But when asking if 1ON == 1ON it says no and error prints 1ON like it was something that it didn't expect. when I changed to number( 1, 2, 3, 4) it did the same thing.

thank you for the help and the information on different ways to accomplish serial communication

MechNTech: it would seem I am missing something. in this case 1ON does not equal 1ON. why not?

You have to post the latest version of your program if you want help with it. The devil is in the detail.

When using Cstrings you must use strcmp() to compare values rather than ==

You can compare individual chars with ==

...R

Robin2: Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

All the information was in here and I do appreciate the help. My issue happened to be I was not using the cstring.h library. strncmp() was just what I was looking for.

I appreciate your time and effort!