BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Relay Not working WITH BlynkTimer

Hello Community I have a project where i take reading of battery from my device and turn it on or off according to reading but i don’t know why my relay is not turning on also when i press Blynk widget button relay still not working.


#include <ESP8266WiFi.h>
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>


BlynkTimer timer;
BlynkTimer timer1;

// WiFi Parameters
const char* ssid = "-----------------------";
const char* password = "-------------"; 
char auth[] = "-----------------------------------------------";


int relayPin = 2;

int sirenPin = 0;
void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Blynk.begin(auth, ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    Serial.println("Connecting...");
  }
  timer.setInterval(5000, battery);
}

void battery()

{


  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;  //Object of class HTTPClient
    http.begin("http://192.168.15.4/cgi-bin/lget.cgi?cmd=get_battery_exist&tmpdb=gui_for_web_battery_status,dm_battery_percent");
    int httpCode = http.GET();
    //Check the returning code
    if (httpCode > 0) {
      // Parsing
      const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(8) + 370;
      DynamicJsonBuffer jsonBuffer(bufferSize);
      JsonObject& root = jsonBuffer.parseObject(http.getString());
      // Parameters

      const char* dm_battery_percent = root["dm_battery_percent"]; // Getting Battery reading from device
      const char* gui_for_web_battery_status = root["gui_for_web_battery_status"]; // getting charge discharge status

      // Output to serial monitor

      String batt = dm_battery_percent;
      // greeting now contains "heo"
      Serial.println(batt);


      http.end();

      if (batt.toInt() <= 58)
      {
        digitalWrite(relayPin, LOW);

      }
      else if (batt.toInt() >= 56)
      {
        digitalWrite(relayPin, HIGH);

      }
    }

  }
}


void Sirenoff()
{
  digitalWrite(sirenPin, HIGH);
}

BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
  int pinData = param.asInt();

  if (pinData == 1)
  {
    digitalWrite(sirenPin, LOW);
    timer1.setTimeout(5000L, Sirenoff);  
  }
}


void loop()
{

  Blynk.run();
  timer.run();
  timer1.run();
  

}





You don’t have any pinMode declaration your void setup.
You also haven’t provided any information about they type of board you’re using and how you’ve connected-up your relay.
If you’re using a NodeMCU then do you realise that GPIO2 and the pin labelled D2 are different?

Pete.

@PeteKnight Thank you so much …its working after

 pinMode(relayPin, OUTPUT);
  pinMode(sirenPin, OUTPUT);
1 Like