Why blynk restful api is not working with digital pin but it works fine with virtual pin

I am just trying to turn ON/OFF the builtin led in the NodeMCU using blynk restful API. I am able to make it turn ON/OFF using the virtual pin V2 as in below url.

http://blynk-cloud.com/1-Hfb2pP__c--vtev9yE7oGOnBTGicvJ/update/V2?value=1

However I am not able to control the digital pin D4 directly like
http://blynk-cloud.com/1-Hfb2pP__c--vtev9yE7oGOnBTGicvJ/update/D4?value=1

In the BLYNK Mobile APP, both the buttons for V2 and D4 works fine, i.e. both the buttons can turn ON/OFF the builtin LED. But in RESTful API D4 doesnot work. Please let me know if there is something wrong in the request URL of D4.

Below is the code:


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "1-Hfb2pP__c--vtev9yE7oGOnBTGicvJ";
char ssid[] = "Mobile111";
char pass[] = "";

void setup()
{
  Serial.begin(9600);
  pinMode(D4,OUTPUT);
  Blynk.begin(auth, ssid, pass);
}

BLYNK_WRITE(V2) // V2 is the number of Virtual Pin  
{
  int pinValue = param.asInt();
  Serial.println(pinValue);
  digitalWrite(D4, pinValue); //As direct control of D4 is not working. I am controlling D4 through V2
}

void loop()
{
  Blynk.run();
}


What happens if you change your API call to D2 instead of D4 ?

Pete.

I didnt try D2 because I want to control bulitin led which is D4. What I observed is with the D4 api is that it is working in the cloud but it seems not reaching the device or unable to parse. What I mean is if I perform GET call on D4 as http://blynk-cloud.com/1-Hfb2pP__c–vtev9yE7oGOnBTGicvJ/get/D4 then it returns [“1”], this means data is updating in the server for the field D4. I am assuming the problem is due to parsing as the return value is in TEXT format, I may need to convert it into INTEGER in NodeMCU, but I am not sure. I tried passing value as 255 instead of 1, but still it didnt work.

The reason I asked about D2 instead of D4 is the confusion that can occur with NodeMCU devices between the pin identifiers that are silkscreen printed in the boards, and that actual GPIO numbers that relate to those pins.
I think the type of board that you choose in your project can also have a influence on how the API references the digital pins.

My advice would be to use a multimeter and connect one side to GND and check the state of each pin on your NodeMCU to see if they are HIGH (3.3v) or LOW (0v). Then toggle the state of a digital pin using the API and see what changes. You may need to set the pin HIGH then LOW in the API to see which pin is changing.

You should also be aware that the builtin LED is active LOW, which means that it turns on when you set the pin to 0, not to 1.

Don’t get too hung-up on the text value that you’re getting back, this is normal. Forget the 255 thing as well, this is for virtual pins.

Pete.

You are right, it is working using D2 instead of D4 in the api. So then just to confirm, what I understand is the PIN number used in the API is the actual numbering as in ESP8266 GPIO numbering. Whereas, PIN number use in the code and Blynk App are correspond to what is printed on PCB of NodeMCU.

Please let me know if my understanding is correct or not.

As I said before, I think it depends on the board type you’ve chosen in the app.
Experiment as I suggested and find out what works for you.

Pete.

I believe this will depend on if you have selected the proper board when compiling your code in the Arduino IDE.

IMO it is always best to use the actual GPIO pin number, instead of the “D” pin number. This will ensure code compatibility between different devices.

void setup()
{
  Serial.begin(9600);
  pinMode(2,OUTPUT);
  Blynk.begin(auth, ssid, pass);
}

BLYNK_WRITE(V2) // V2 is the number of Virtual Pin  
{
  int pinValue = param.asInt();
  Serial.println(pinValue);
  digitalWrite(2, pinValue); //As direct control of D4 (GPIO 2) is not working. I am controlling D4 (GPIO 2) through V2
}