Problem with send&receive data

This is my first time using Blynk and I want to control the direction and speed of BLDC motor.
here is my code, I’ve uploaded the code onto ESP8266 NODEMCU and in Blynk app it shows the device is online but I can’t send or receive any data.
Please help me correct my code - I haven’t so much knowledge about programming so if the code seems funny is because of that - most of the code is commented because I wanted to test send&receive data first, Also Frequency set is for ARDUINO UNO not ESP8266.

#define BLYNK_PRINT Serial

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

char auth[] = "**********";

char ssid[] = "**********";;
char pass[] = "**********";;

#define PIN_UPTIME V0
/////////////////////////////////////////////////
 
//const int EN = 12;
// 
//const int IN1 = 13;
//const int IN2 = 14;
//const int IN3 = 15;

const int BATTERY = A0;
 
//// SPWM (Sine Wave)
////const int pwmSin[] = {127, 138, 149, 160, 170, 181, 191, 200, 209, 217, 224, 231, 237, 242, 246, 250, 252, 254, 254, 254, 252, 250, 246, 242, 237, 231, 224, 217, 209, 200, 191, 181, 170, 160, 149, 138, 127, 116, 105, 94, 84, 73, 64, 54, 45, 37, 30, 23, 17, 12, 8, 4, 2, 0, 0, 0, 2, 4, 8, 12, 17, 23, 30, 37, 45, 54, 64, 73, 84, 94, 105, 116 };
// 
// 
///// SVPWM (Space Vector Wave)
////const int pwmSin[] = {128, 147, 166, 185, 203, 221, 238, 243, 248, 251, 253, 255, 255, 255, 253, 251, 248, 243, 238, 243, 248, 251, 253, 255, 255, 255, 253, 251, 248, 243, 238, 221, 203, 185, 166, 147, 128, 109, 90, 71, 53, 35, 18, 13, 8, 5, 3, 1, 1, 1, 3, 5, 8, 13, 18, 13, 8, 5, 3, 1, 1, 1, 3, 5, 8, 13, 18, 35, 53, 71, 90, 109};
//const int pwmSin[] = {128, 132, 136, 140, 143, 147, 151, 155, 159, 162, 166, 170, 174, 178, 181, 185, 189, 192, 196, 200, 203, 207, 211, 214, 218, 221, 225, 228, 232, 235, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 249, 250, 250, 251, 252, 252, 253, 253, 253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 253, 252, 252, 251, 250, 250, 249, 248, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 249, 250, 250, 251, 252, 252, 253, 253, 253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 253, 252, 252, 251, 250, 250, 249, 248, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 235, 232, 228, 225, 221, 218, 214, 211, 207, 203, 200, 196, 192, 189, 185, 181, 178, 174, 170, 166, 162, 159, 155, 151, 147, 143, 140, 136, 132, 128, 124, 120, 116, 113, 109, 105, 101, 97, 94, 90, 86, 82, 78, 75, 71, 67, 64, 60, 56, 53, 49, 45, 42, 38, 35, 31, 28, 24, 21, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 24, 28, 31, 35, 38, 42, 45, 49, 53, 56, 60, 64, 67, 71, 75, 78, 82, 86, 90, 94, 97, 101, 105, 109, 113, 116, 120, 124};
// 
//int currentStepA;
//int currentStepB;
//int currentStepC;
//int sineArraySize;
//int increment = 0;
boolean direct = 1; // direction true=forward, false=backward

int Dly = 0;
int Dir = 0;

int Batt = 0;
 
//////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V1) 
{
  Dir = param.asInt();
  Serial.print("V1 value is: ");
  Serial.println(Dir);
}

BLYNK_WRITE(V2) 
{
  Dly = param.asInt();
  Serial.print("V2 Slider value is: ");
  Serial.println(Dly);
}

BLYNK_READ(PIN_UPTIME)
{
  Batt = analogRead(BATTERY);
  Batt = map(Batt, 0, 1023, 0, 100);
  Blynk.virtualWrite(PIN_UPTIME, Batt);
}
/////////////////////////////////////////////////
 
void setup()
{
  Serial.begin(9600);
  Serial.println("Setting Up BLDC Driver");
  
//  setPwmFrequency(IN1); // Increase PWM frequency to 32 kHz  (make unaudible)
//  setPwmFrequency(IN2);
//  setPwmFrequency(IN3);
// 
//  pinMode(IN1, OUTPUT); 
//  pinMode(IN2, OUTPUT); 
//  pinMode(IN3, OUTPUT); 
//  
//  pinMode(EN, OUTPUT); 
// 
//  digitalWrite(EN, HIGH);
//  
//  sineArraySize = sizeof(pwmSin)/sizeof(int); // Find lookup table size
//  int phaseShift = sineArraySize / 3;         // Find phase shift and initial A, B C phase values
//  currentStepA = 0;
//  currentStepB = currentStepA + phaseShift;
//  currentStepC = currentStepB + phaseShift;
// 
//  sineArraySize--; // Convert from array Size to last PWM array number

  Blynk.begin(auth, ssid, pass);
}
 
//////////////////////////////////////////////////////////////////////////////
 
void loop()
{
  Blynk.run();

  direct = Dir;
  Serial.print("Direction = ");
  Serial.print(direct);
  Serial.println(" 1 = Forward, 0 = Backward");
  
//  analogWrite(IN1, pwmSin[currentStepA]);
//  analogWrite(IN2, pwmSin[currentStepB]);
//  analogWrite(IN3, pwmSin[currentStepC]);  
//  
//  if (direct==true) increment = 1;
//  else increment = -1;     
// 
//  currentStepA = currentStepA + increment;
//  currentStepB = currentStepB + increment;
//  currentStepC = currentStepC + increment;
// 
//  if(currentStepA > sineArraySize)  currentStepA = 0;
//  if(currentStepA < 0)  currentStepA = sineArraySize;
// 
//  if(currentStepB > sineArraySize)  currentStepB = 0;
//  if(currentStepB < 0)  currentStepB = sineArraySize;
// 
//  if(currentStepC > sineArraySize)  currentStepC = 0;
//  if(currentStepC < 0) currentStepC = sineArraySize; 
//  
//  // Control speed by this delay
  Serial.print("Delay = ");
  Serial.print(Dly);
  Serial.println(" mS");
  delay(Dly);
}
 
 
//void setPwmFrequency(int pin)
//{
//  if(pin == 5 || pin == 6 || pin == 9 || pin == 10)
//  {
//    if(pin == 5 || pin == 6)
//    {
//      TCCR0B = TCCR0B & 0b11111000 | 0x01;
//    } 
//    else
//    {
//      TCCR1B = TCCR1B & 0b11111000 | 0x01;
//    }
//  }
//  else if(pin == 3 || pin == 11) 
//  {
//    TCCR2B = TCCR2B & 0b11111000 | 0x01;
//  }
//}

“delay(Dly);” is 300mS max!

When you post code to this forum it needs to be posted in a certain way so that it displays correctly. This was explained in the text that you deleted before posting your query.
I’ve corrected it for you this time, but in future please format it like this:
Blynk%20-%20FTFC

Your (rather crappy) code does work - I’ve just tried it.
I get outpits like this in the serial monitor:

Direction = 0 1 = Forward, 0 = Backward
Delay = 475 mS
Direction = 0 1 = Forward, 0 = Backward
Delay = 475 mS
Direction = 0 1 = Forward, 0 = Backward
Delay = 475 mS
Direction = 0 1 = Forward, 0 = Backward

with the values changing when I change the switch and slider widgets.
The V0 widget displays a value which changes if I put my finger on the A0 pin. This widget needs to have a refresh interval (I used 1 second) rather than the default PUSH setting.

Having said all that, I doubt very much if you’ll get this to work the way you want, as all that code in the void loop will cause Blynk to disconnect.
You should read this:

Pete.

1 Like

Thank you for your help