Blynk + esp8266 v3 ใช้กับ arduino mega + motor drive

ผมไม่เข้าใจเลยคับคือ กดใช้ joy stick ใน blynk เเละ blynk สามารถคุมรอบของมอเตอร์ได้เเละส่งให้ esp8266 v3 เเล้วส่งค่าให้ arduino mega ทำให้ motor drive เเละมอเตอทำงาน
นี้ของโค้ด esp8266

`#define BLYNK_PRINT Serial`

#include <SimpleTimer.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>

int pinValue1;
int pinValue2;
int pinValue3;
int pinValue4;
String v2arduino;

//SoftwareSerial esp(5, 4); /** rx,tx(GPIO 5 (D1),GPIO 4 (D2))*/


char auth[] = "hAHI2oi9eShQrBQ6NbpGBm_SAEeWQij2";
char ssid[] = "Mennoy";
char pass[] = "25442544";
char server[] = "oasiskit.com";

SimpleTimer timer;
String myString;
char rdata;
int firstVal, secondVal, thirdVal;

void setup()  {

  Serial.begin(9600);
  //esp.begin(115200);
  Blynk.begin(auth, ssid, pass, "oasiskit.com", 8080);
}

void loop()
{

  if (Serial.available() == 0 ) {
    Blynk.run();
    timer.run(); // Initiates BlynkTimer
    toarduino();
  }
  if (Serial.available() > 0 )
  {
    rdata = Serial.read();
    myString = myString + rdata;
    // Serial.print(rdata);
    if ( rdata == '\n')
    {
      // new code
      String l = getValue(myString, ',', 0);
      String m = getValue(myString, ',', 1);
      String n = getValue(myString, ',', 2);


      firstVal = l.toInt();
      secondVal = m.toInt();
      thirdVal = n.toInt();

      myString = "";
      // end new code
    }
  }
}
String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = { 0, -1 };
  int maxIndex = data.length() - 1;

  for (int i = 0; i <= maxIndex && found <= index; i++) {
    if (data.charAt(i) == separator || i == maxIndex) {
      found++;
      strIndex[0] = strIndex[1] + 1;
      strIndex[1] = (i == maxIndex) ? i + 1 : i;
    }
  }
  return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

// in Blynk app writes values to the Virtual Pin V3
BLYNK_WRITE(V10)
{
  pinValue1 = param.asInt(); // assigning incoming value from pin V10 to a variable


}


// in Blynk app writes values to the Virtual Pin V4
BLYNK_WRITE(V11)
{
  pinValue2 = param.asInt(); // assigning incoming value from pin V10 to a variable

}

// in Blynk app writes values to the Virtual Pin V5, this is for the slider
BLYNK_WRITE(V12)
{
  pinValue3 = param.asInt(); // assigning incoming value from pin V10 to a variable

}

void toarduino()
{
  v2arduino = v2arduino + pinValue1 + "," + pinValue2 + "," + pinValue3;
  Serial.println(v2arduino);
  delay(100);
  v2arduino = "";
}

เเละนี้โค้ดของ arduino คับ

#include <SoftwareSerial.h>

/**
   RX = 2
   TX = 3
*/
SoftwareSerial nodemcu(2,3);

long int data;
int firstVal, secondVal, thirdVal;

String myString; // complete message from arduino, which consistors of snesors data
char rdata; // received charactors
String cdata; // complete data


int ena = 5;
int enb = 6;

int in1 = 8;
int in2 = 9;
int in3 = 10;
int in4 = 11;

void setup()  {

  Serial.begin(9600);
  nodemcu.begin(9600);

  pinMode(ena, OUTPUT);
  pinMode(enb, OUTPUT);

  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  analogWrite(ena, 0);
  analogWrite(enb, 0);

}
void loop() {
  if (nodemcu.available() == 0 )
  {

    delay(100); // 100 milli seconds

  }
  if ( nodemcu.available() > 0 )
  {

    rdata = nodemcu.read();
    myString = myString + rdata;
    Serial.print(rdata);
    if ( rdata == '\n')
    {
      Serial.println(myString);
      // new code
      String l = getValue(myString, ',', 0);
      String m = getValue(myString, ',', 1);
      String n = getValue(myString, ',', 2);


      firstVal = l.toInt(); // for left and right
      secondVal = m.toInt(); // forward and reverse
      thirdVal = n.toInt(); // speed

      myString = "";
      if ( (firstVal == 2) && (secondVal == 2) ) // for joystic normal position
      {
        analogWrite(ena, 0);
        analogWrite(enb, 0);
        digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);
        digitalWrite(in3, LOW);
        digitalWrite(in3, LOW);
        

      } else

        if ( (firstVal == 2) && (secondVal > 3) ) // Forward direction
        {
          analogWrite(ena, thirdVal);
          analogWrite(enb, thirdVal);
          digitalWrite(in1, HIGH);
          digitalWrite(in2, LOW);
          digitalWrite(in3, LOW);
          digitalWrite(in4, HIGH);
          
        } else

          if ( (firstVal == 2) && (secondVal < 1) ) // reverse direction
          {
            analogWrite(ena, thirdVal);
            analogWrite(enb, thirdVal);
            digitalWrite(in1, LOW);
            digitalWrite(in2, HIGH);
            digitalWrite(in3, HIGH);
            digitalWrite(in4, LOW);
            

          } else

            if ( (firstVal > 3) && (secondVal == 2) ) // Right
            {
              analogWrite(ena, thirdVal);
              analogWrite(enb, thirdVal);
              digitalWrite(in1, HIGH);
              digitalWrite(in2, LOW);
              digitalWrite(in3, LOW);
              digitalWrite(in4, LOW);
              
            } else


              if ( (firstVal < 1) && (secondVal == 2) ) // left
              {
                analogWrite(ena, thirdVal);
                analogWrite(enb, thirdVal);
                digitalWrite(in1, LOW);
                digitalWrite(in2, LOW);
                digitalWrite(in3, LOW);
                digitalWrite(in4, HIGH);
               
              }
    }
  }
}


String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = { 0, -1 };
  int maxIndex = data.length() - 1;

  for (int i = 0; i <= maxIndex && found <= index; i++) {
    if (data.charAt(i) == separator || i == maxIndex) {
      found++;
      strIndex[0] = strIndex[1] + 1;
      strIndex[1] = (i == maxIndex) ? i + 1 : i;
    }
  }
  return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}`Preformatted text`

image