Categories
Development

Input data from the Blynk app to hardware

We will check how to read input data from the user. The code used is very simple … basically Hello World in Arduino … yes, blinking the LED.

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "auth_data";
char ssid[] = "ssid_wifi";
char pass[] = "password_wifi";

int led_pin = 2;
int delay_time = 1000;

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

void loop() {
  Blynk.run();
  BLYNK_LOG("on");
  digitalWrite(led_pin, HIGH);
  delay(delay_time);
  BLYNK_LOG("off");      
  digitalWrite(led_pin, LOW);
  delay(delay_time);

}

We just added some logging, but you can really ignore that if you want.

Anyway, let’s create an app now. We will be using the text input widget.

In settings (just click on the widget) you need to select virtual pin for the output. We are using V0, but you can really use whichever you want (just change the code accordingly).

Let’s just add reading data from the app

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "2uTzRbOYroqcm5-I_835c5PfiS6SW0h0";
char ssid[] = "corega5";
char pass[] = "milky12345";

int led_pin = 2;
int delay_time = 1000;

BLYNK_WRITE(V0) //writing to V0 from input test
{
  delay_time = param.asInt(); 
}

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

void loop() {
  Blynk.run();
  BLYNK_LOG("on");
  digitalWrite(led_pin, HIGH);
  delay(delay_time);
  BLYNK_LOG("off");      
  digitalWrite(led_pin, LOW);
  delay(delay_time);

}

As you can see we just added BLYNK_WRITE and are changing the variable for the delay.

That’s it. Really simple.

Leave a Reply

Your email address will not be published. Required fields are marked *